fix syntax error
[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     logger.log("omf_resctl: plugin starting up...")
20
21 def GetSlivers(data, conf = None, plc = None):
22     if 'accounts' not in data:
23         logger.log_missing_data("omf_resctl.GetSlivers",'accounts')
24         return
25
26     try:
27         xmpp_server=data['xmpp']['server']
28         if not xmpp_server: 
29             # we have the key but no value, just as bad
30             raise Exception
31     except:
32         # disabled feature - bailing out
33         # xxx might need to clean up more deeply..
34         logger.log("PLC config unsufficient (not enabled, or no server set), see the PLC_OMF category -- plugin exiting")
35         return
36
37     # as hrn is set only at AddNode-time, upgraded myplcs might still miss this
38     # clue: just overwrite the hostname of all nodes
39     # for node in GetNodes(): UpdateNode(node['node_id'],{'hostname':node['hostname']})
40     try:
41         node_hrn = data['hrn']
42         if not node_hrn: raise Exception
43     except:
44         logger.log("Failed to read hrn from GetSlivers, using 'default' - *please upgrade PLCAPI*")
45         node_hrn='default   # Failed to read hrn from GetSlivers, please upgrade PLCAPI'
46
47     for sliver in data['slivers']:
48         name=sliver['name']
49         sliver_pub_key_dir=os.path.join("/home", name, ".ssh/")
50         sliver_private_key=os.path.join(sliver_pub_key_dir, "id_rsa")
51         for chunk in sliver['attributes']:
52             if chunk['tagname']=='omf_control':
53                 # scan all versions of omf-resctl
54                 etc_path="/vservers/%s/etc/"%name
55                 pattern = etc_path + "omf-resctl-*/omf-resctl.yaml.in"
56                 templates = glob.glob (pattern)
57                 if not templates:
58                     logger.log("WARNING: omf_resctl plugin, no template found for slice %s using pattern %s"\
59                                    %(name,pattern))
60                     continue
61                 for template in templates:
62                     # remove the .in extension
63                     yaml=template[:-3]
64                     # figure service name as subdir under etc/
65                     service_name=os.path.split(template.replace(etc_path,''))[0]
66                     # read template and replace
67                     template_contents=file(template).read()
68                     yaml_contents=template_contents\
69                         .replace('@XMPP_SERVER@',xmpp_server)\
70                         .replace('@NODE_HRN@',node_hrn)\
71                         .replace('@SLICE_NAME@',name)\
72                         .replace('@SLIVER_PRIVATE_KEY@',sliver_private_key)\
73                         .replace('@SLIVER_PUB_KEY_DIR@',sliver_pub_key_dir)
74                     changes=tools.replace_file_with_string(yaml,yaml_contents)
75                     logger.log("yaml_contents length=%d, changes=%r"%(len(yaml_contents),changes))
76                     if changes:
77                         sp=subprocess.Popen(['vserver',name,'exec','service',service_name,'restart'],
78                                             stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
79                         (output,retcod)=sp.communicate()
80                         logger.log("omf_resctl: %s: restarted resource controller (retcod=%r)"%(name,retcod))
81                         logger.log("omf_resctl: got output\n%s"%output)
82                     else:
83                         logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % name)