reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / plugins / vsys_sysctl.py
1 """ vsys_sysctl
2
3     Touches a slice with lxcsu if it has vsys_sysctl attributes
4 """
5
6 import logger
7 import os
8
9 def start():
10     logger.log("vsys_sysctl: plugin starting up...")
11
12 def GetSlivers(data, config=None, plc=None):
13     """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys."""
14
15     if 'slivers' not in data:
16         logger.log_missing_data("vsys.GetSlivers", 'slivers')
17         return
18
19     slices = []
20
21     for sliver in data['slivers']:
22         slicename = sliver["name"]
23         for attribute in sliver['attributes']:
24             if attribute['tagname'].startswith('vsys_sysctl.'):
25                 dir = "/vservers/%s/vsys_sysctl" % slicename
26                 if not os.path.exists(dir):
27                     try:
28                         logger.log("vsys_sysctl: create dir %s" % dir)
29                         os.mkdir(dir)
30                     except:
31                         logger.log("vsys_sysctl: failed to create dir %s" % dir)
32
33                 (junk, key) = attribute['tagname'].split(".", 1)
34                 value = str(attribute['value'])
35
36                 fn = os.path.join(dir, key)
37                 if not test_value(fn, value):
38                     # All we need to do to make vsys_sysctl work is to lxcsu
39                     # into the slice and do anything.
40                     result = os.system("lxcsu -r %s :" % slicename)
41                     if result != 0:
42                         logger.log("vsys_sysctl: failed to lxcsu into %s" % slicename)
43                         continue
44
45                     # Store the key name and value inside of /vsys_sysctl in the
46                     # slice. This lets us know that we've done the sysctl.
47                     try:
48                         logger.log("vsys_sysctl: create file %s value %s" % (fn, value))
49                         with open(fn, "w") as f:
50                             f.write(value+"\n")
51                     except:
52                         logger.log("vsys_sysctl: failed to create file %s" % fn)
53
54 def test_value(fn, value):
55     try:
56         slice_value = file(fn, "r").readline().strip()
57     except:
58         slice_value = None
59
60     return (value == slice_value)
61