f6c2a8ef9d0ab08681109ec60f135645d713d1d6
[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="/some/path/to/the/change/script"
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         # xxx might need to clean up more deeply..
60         logger.log("PLC config unsufficient (not enabled, or no server set), see the PLC_OMF category -- plugin exiting")
61         return
62
63     hostname = data['hostname']
64
65     def is_omf_friendly (sliver):
66         for chunk in sliver['attributes']:
67             if chunk['tagname']=='omf_control': return True
68
69     for sliver in data['slivers']:
70         # skip non OMF-friendly slices
71         if not is_omf_friendly (sliver): continue
72         slicename=sliver['name']
73         yaml_template = config_ple_template
74         yaml_contents = yaml_template\
75             .replace('_xmpp_server_',xmpp_server)\
76             .replace('_slicename_',slicename)\
77             .replace('_hostname_',hostname)
78         yaml_full_path="/vservers/%s/%s"%(slicename,yaml_slice_path)
79         yaml_full_dir=os.path.dirname(yaml_full_path)
80         if not os.path.isdir(yaml_full_dir):
81             try: os.makedirs(yaml_full_dir)
82             except OSError: pass
83
84         changes=tools.replace_file_with_string(yaml_full_path,yaml_contents)
85         logger.log("yaml_contents length=%d, changes=%r"%(len(yaml_contents),changes))
86         if changes:
87             # instead of restarting the service we call a companion script
88             try:
89                 sp=subprocess.Popen([omf_rc_trigger_script],
90                                     stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
91                 (output,retcod)=sp.communicate()
92                 logger.log("omf_resctl: %s: called OMF rc control script (retcod=%r)"%(slicename,retcod))
93                 logger.log("omf_resctl: got output\n%s"%output)
94             except:
95                 logger.log("omf_resctl: WARNING: Could not call trigger script %s"%omf_rc_trigger_script)
96         else:
97             logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)