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