reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[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 omf_rc_trigger_log="/var/log/plc_trigger_omf_rc.log"
53
54 # hopefully temporary: when trigger script is missing, fetch it at the url here
55 omf_rc_trigger_url="http://git.mytestbed.net/?p=omf.git;a=blob_plain;f=omf_rc/bin/plc_trigger_omf_rc;hb=HEAD"
56 def fetch_trigger_script_if_missing (slicename):
57     full_path="/vservers/%s/%s"%(slicename, omf_rc_trigger_script)
58     if not os.path.isfile (full_path):
59         retcod=subprocess.call (['curl', '--silent', '-o', full_path, omf_rc_trigger_url])
60         if retcod!=0:
61             logger.log("Could not fetch %s"%omf_rc_trigger_url)
62         else:
63             subprocess.call(['chmod', '+x', full_path])
64             logger.log("omf_resctl: fetched %s"%(full_path))
65             logger.log("omf_resctl: from %s"%(omf_rc_trigger_url))
66
67 def GetSlivers(data, conf = None, plc = None):
68     logger.log("omf_resctl.GetSlivers")
69     if 'accounts' not in data:
70         logger.log_missing_data("omf_resctl.GetSlivers", 'accounts')
71         return
72
73     try:
74         xmpp_server=data['xmpp']['server']
75         if not xmpp_server: 
76             # we have the key but no value, just as bad
77             raise Exception
78     except:
79         # disabled feature - bailing out
80         logger.log("omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting")
81         return
82
83     hostname = data['hostname']
84
85     def is_omf_friendly (sliver):
86         for chunk in sliver['attributes']:
87             if chunk['tagname']=='omf_control': return True
88
89     for sliver in data['slivers']:
90         # skip non OMF-friendly slices
91         if not is_omf_friendly (sliver): continue
92         slicename=sliver['name']
93         expires=str(sliver['expires'])
94         yaml_template = config_ple_template
95         yaml_contents = yaml_template\
96             .replace('_xmpp_server_', xmpp_server)\
97             .replace('_slicename_', slicename)\
98             .replace('_hostname_', hostname)\
99             .replace('_expires_', expires)
100         yaml_full_path="/vservers/%s/%s"%(slicename, yaml_slice_path)
101         yaml_full_dir=os.path.dirname(yaml_full_path)
102         if not os.path.isdir(yaml_full_dir):
103             try: os.makedirs(yaml_full_dir)
104             except OSError: pass
105
106         config_changes=tools.replace_file_with_string(yaml_full_path, yaml_contents)
107         logger.log("yaml_contents length=%d, config_changes=%r"%(len(yaml_contents), config_changes))
108         # would make sense to also check for changes to authorized_keys 
109         # would require saving a copy of that some place for comparison
110         # xxx todo
111         keys_changes = False
112         if config_changes or keys_changes:
113             # instead of restarting the service we call a companion script
114             try:
115                 fetch_trigger_script_if_missing (slicename)
116                 # the trigger script actually needs to be run in the slice context of course
117                 # in addition there is a requirement to pretend we run as a login shell
118                 # hence sudo -i
119                 slice_command = [ "sudo", "-i",  omf_rc_trigger_script ]
120                 to_run = tools.command_in_slice (slicename, slice_command)
121                 log_filename = "/vservers/%s/%s"%(slicename, omf_rc_trigger_log)
122                 logger.log("omf_resctl: starting %s"%to_run)
123                 logger.log("redirected into %s"%log_filename)
124                 logger.log("*not* waiting for completion..")
125                 with open(log_filename, "a") as log_file:
126                     subprocess.Popen(to_run, stdout=log_file, stderr=subprocess.STDOUT)
127                 # a first version tried to 'communicate' on that subprocess instance
128                 # but that tended to create deadlocks in some cases
129                 # causing nodemanager to stall...
130                 # we're only losing the child's retcod, no big deal
131             except:
132                 import traceback
133                 traceback.print_exc()
134                 logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\
135                                    omf_rc_trigger_script, name=slicename)
136         else:
137             logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)