85824e189df7de5e94bff96e32e46ebea022db15
[nodemanager.git] / plugins / omf_resctl.py
1 #
2 # NodeManager plugin - first step of handling omf_controlled slices
3 #
4
5 """
6 Overwrites the 'resctl' tag of slivers controlled by OMF so slivermanager.py does the right thing
7 """
8
9 import os, os.path
10 import glob
11 import subprocess
12
13 import tools
14 import logger
15
16 priority = 50
17
18 def start():
19     pass
20
21 ### the new template for v6
22 # hard-wire this for now
23 # once the variables are expanded, this is expected to go into
24 config_ple_template="""---
25 # Example:
26 # _slicename_ = nicta_ruby
27 # _hostname_ = planetlab1.research.nicta.com.au
28 # _xmpp_server_ = xmpp.planet-lab.eu
29  
30 :uid: _slicename_@_hostname_
31 :uri: xmpp://_slicename_-_hostname_-<%= "#{Process.pid}" %>:_slicename_-_hostname_-<%= "#{Process.pid}" %>@_xmpp_server_
32 :environment: production
33 :debug: false
34  
35 :auth:
36   :root_cert_dir: /home/_slicename_/root_certs
37   :entity_cert: /home/_slicename_/entity.crt
38   :entity_key: /home/_slicename_/.ssh/id_rsa
39 """
40
41 # the path where the config is expected from within the sliver
42 yaml_slice_path="/etc/omf_rc/config.yml"
43 # the path for the script that we call when a change occurs
44 omf_rc_trigger_script="plc_trigger_omf_rc"
45
46 def GetSlivers(data, conf = None, plc = None):
47     logger.log("omf_resctl.GetSlivers")
48     if 'accounts' not in data:
49         logger.log_missing_data("omf_resctl.GetSlivers",'accounts')
50         return
51
52     try:
53         xmpp_server=data['xmpp']['server']
54         if not xmpp_server: 
55             # we have the key but no value, just as bad
56             raise Exception
57     except:
58         # disabled feature - bailing out
59         logger.log("omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting")
60         return
61
62     hostname = data['hostname']
63
64     def is_omf_friendly (sliver):
65         for chunk in sliver['attributes']:
66             if chunk['tagname']=='omf_control': return True
67
68     for sliver in data['slivers']:
69         # skip non OMF-friendly slices
70         if not is_omf_friendly (sliver): continue
71         slicename=sliver['name']
72         yaml_template = config_ple_template
73         yaml_contents = yaml_template\
74             .replace('_xmpp_server_',xmpp_server)\
75             .replace('_slicename_',slicename)\
76             .replace('_hostname_',hostname)
77         yaml_full_path="/vservers/%s/%s"%(slicename,yaml_slice_path)
78         yaml_full_dir=os.path.dirname(yaml_full_path)
79         if not os.path.isdir(yaml_full_dir):
80             try: os.makedirs(yaml_full_dir)
81             except OSError: pass
82
83         config_changes=tools.replace_file_with_string(yaml_full_path,yaml_contents)
84         logger.log("yaml_contents length=%d, config_changes=%r"%(len(yaml_contents),config_changes))
85         # would make sense to also check for changes to authorized_keys 
86         # would require saving a copy of that some place for comparison
87         # xxx todo
88         keys_changes = False
89         if config_changes or keys_changes:
90             # instead of restarting the service we call a companion script
91             try:
92                 # the trigger script actually needs to be run in the slice context of course
93                 # xxx we might need to use
94                 # slice_command=['bash','-l','-c',omf_rc_trigger_script] 
95                 slice_command = [omf_rc_trigger_script]
96                 to_run = tools.command_in_slice (slicename, slice_command)
97                 sp=subprocess.Popen(to_run, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
98                 (out,err)=sp.communicate()
99                 retcod=sp.returncode
100                 # we don't wait for that, try to display a retcod for info purpose only
101                 # might be None if that config script lasts or hangs whatever
102                 logger.log("omf_resctl: %s: called OMF rc control script (imm. retcod=%r)"%(slicename,retcod))
103                 logger.log("omf_resctl: got stdout\n%s"%out)
104                 logger.log("omf_resctl: got stderr\n%s"%err)
105             except:
106                 import traceback
107                 traceback.print_exc()
108                 logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\
109                                    omf_rc_trigger_script, name=slicename)
110         else:
111             logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)