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