1f174533228a674795cf96cf79d561025383c000
[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 OMF 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 # given that we're now responsible for fetching this one, I have to
45 # decide on an actual path - not jsut a name to search for in PATH
46 omf_rc_trigger_script="/usr/bin/plc_trigger_omf_rc"
47
48 # hopefully temporary: when trigger script is missing, fetch it at the url here
49 omf_rc_trigger_url="http://git.mytestbed.net/?p=omf.git;a=blob_plain;f=omf_rc/bin/plc_trigger_omf_rc;hb=HEAD"
50 def fetch_trigger_script_if_missing (slicename):
51     full_path="/vservers/%s/%s"%(slicename,omf_rc_trigger_script)
52     if not os.path.isfile (full_path):
53         retcod=subprocess.call (['curl','--silent','-o',full_path,omf_rc_trigger_url])
54         if retcod!=0:
55             logger.log("Could not fetch %s"%omf_rc_trigger_url)
56         else:
57             subprocess.call(['chmod','+x',full_path])
58             logger.log("omf_resctl: fetched %s"%(full_path))
59             logger.log("omf_resctl: from %s"%(omf_rc_trigger_url))
60
61 def GetSlivers(data, conf = None, plc = None):
62     logger.log("omf_resctl.GetSlivers")
63     if 'accounts' not in data:
64         logger.log_missing_data("omf_resctl.GetSlivers",'accounts')
65         return
66
67     try:
68         xmpp_server=data['xmpp']['server']
69         if not xmpp_server: 
70             # we have the key but no value, just as bad
71             raise Exception
72     except:
73         # disabled feature - bailing out
74         logger.log("omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting")
75         return
76
77     hostname = data['hostname']
78
79     def is_omf_friendly (sliver):
80         for chunk in sliver['attributes']:
81             if chunk['tagname']=='omf_control': return True
82
83     for sliver in data['slivers']:
84         # skip non OMF-friendly slices
85         if not is_omf_friendly (sliver): continue
86         slicename=sliver['name']
87         yaml_template = config_ple_template
88         yaml_contents = yaml_template\
89             .replace('_xmpp_server_',xmpp_server)\
90             .replace('_slicename_',slicename)\
91             .replace('_hostname_',hostname)
92         yaml_full_path="/vservers/%s/%s"%(slicename,yaml_slice_path)
93         yaml_full_dir=os.path.dirname(yaml_full_path)
94         if not os.path.isdir(yaml_full_dir):
95             try: os.makedirs(yaml_full_dir)
96             except OSError: pass
97
98         config_changes=tools.replace_file_with_string(yaml_full_path,yaml_contents)
99         logger.log("yaml_contents length=%d, config_changes=%r"%(len(yaml_contents),config_changes))
100         # would make sense to also check for changes to authorized_keys 
101         # would require saving a copy of that some place for comparison
102         # xxx todo
103         keys_changes = False
104         if config_changes or keys_changes:
105             # instead of restarting the service we call a companion script
106             try:
107                 fetch_trigger_script_if_missing (slicename)
108                 # the trigger script actually needs to be run in the slice context of course
109                 slice_command = [ "sudo", "-i",  omf_rc_trigger_script ]
110                 to_run = tools.command_in_slice (slicename, slice_command)
111                 logger.log("command_in_slice: %s"%to_run)
112                 sp=subprocess.Popen(to_run, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
113                 (out,err)=sp.communicate()
114                 retcod=sp.returncode
115                 # we don't wait for that, try to display a retcod for info purpose only
116                 # might be None if that config script lasts or hangs whatever
117                 logger.log("omf_resctl: %s: called OMF rc control script (imm. retcod=%r)"%(slicename,retcod))
118                 logger.log("omf_resctl: got stdout\n%s"%out)
119                 logger.log("omf_resctl: got stderr\n%s"%err)
120             except:
121                 import traceback
122                 traceback.print_exc()
123                 logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\
124                                    omf_rc_trigger_script, name=slicename)
125         else:
126             logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)