reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / cgroups.py
1 # Simple wrapper arround cgroups so we don't have to worry the type of
2 # virtualization the sliver runs on (lxc, qemu/kvm, etc.) managed by libvirt
3 #
4 # Xavi Leon <xleon@ac.upc.edu>
5
6 import os, os.path
7 import pyinotify
8 import logger
9 from functools import reduce
10
11 # Base dir for libvirt
12 BASE_DIR = '/sys/fs/cgroup'
13
14 def get_cgroup_paths(subsystem="cpuset"):
15     subsystem_bases = [ 
16         # observed on f16-f18
17         os.path.join(BASE_DIR, subsystem, 'libvirt', 'lxc'),
18         # as observed on f20
19         os.path.join(BASE_DIR, subsystem ),
20         # f21
21         os.path.join(BASE_DIR, subsystem, 'machine.slice'),
22         # as observed on f16 libvirt 1.2.1
23         os.path.join(BASE_DIR, subsystem, 'machine'),
24         ]
25     # try several locations and return all the results
26     # get_cgroup_path will sort it out
27     
28     # just return all the subdirs in the listed bases 
29     return [ subdir
30                  # scan the bases
31                  for subsystem_base in subsystem_bases if os.path.isdir(subsystem_base)
32                      # in each base search the immediate sons that are also dirs
33                      for subdir in [ os.path.join(subsystem_base, f) for f in os.listdir(subsystem_base) ]
34                          if os.path.isdir(subdir) ]
35
36 def get_cgroup_path(name, subsystem="cpuset"):
37     """
38     Returns the base path for the cgroup with a specific name or None.
39     """
40     result = reduce(lambda a, b: b if name in os.path.basename(b) else a,
41                     get_cgroup_paths(subsystem), None)
42
43     if result is None:
44         name = name + ".libvirt-lxc"
45         result = reduce(lambda a, b: b if name in os.path.basename(b) else a,
46                         get_cgroup_paths(subsystem), None)
47
48     return result
49
50 def get_base_path():
51     return BASE_DIR
52
53 def get_cgroups():
54     """ Returns the list of cgroups active at this moment on the node """
55     return list(map(os.path.basename, get_cgroup_paths()))
56
57 def write(name, key, value, subsystem="cpuset"):
58     """ Writes a value to the file key with the cgroup with name """
59     base_path = get_cgroup_path(name, subsystem)
60     with open(os.path.join(base_path, key), 'w') as f:
61         print(value, file=f)
62     logger.verbose("cgroups.write: overwrote {}".format(base_path))
63
64 def append(name, key, value, subsystem="cpuset"):
65     """ Appends a value to the file key with the cgroup with name """
66     base_path = get_cgroup_path(name, subsystem)
67     with open(os.path.join(base_path, key), 'a') as f:
68         print(value, file=f)
69     logger.verbose("cgroups.append: appended {}".format(base_path))
70
71 if __name__ == '__main__':
72
73     # goes with the system tests
74     name='inri_sl1' 
75
76     subsystems = 'blkio cpu cpu,cpuacct cpuacct cpuset devices freezer memory net_cls perf_event systemd'.split()
77
78     for subsystem in subsystems:
79         print('get_cgroup_path({}, {}) =  {}'.\
80             format(name, subsystem, get_cgroup_path(name, subsystem)))
81
82 #        print 'get_cgroup_paths = {}'.format(get_cgroup_paths(subsystem))