another pass for prettifying - mostly format instead of %
[nodemanager.git] / initscript.py
1 import os, os.path
2 import tools
3
4 import logger
5
6 class Initscript:
7
8     def __init__(self, name):
9         self.name = name
10         self.initscript = ''
11
12     def configure(self, rec):
13         # install or remove the slice inistscript, as instructed by the initscript tag
14         new_initscript = rec['initscript']
15         if new_initscript == self.initscript:
16             return
17         logger.log("initscript.configure {}".format(self.name))
18         self.initscript = new_initscript
19         code = self.initscript
20         sliver_initscript = "/vservers/%s/etc/rc.d/init.d/vinit.slice" % self.name
21         if tools.replace_file_with_string(sliver_initscript, code, remove_if_empty=True, chmod=0755):
22             if code:
23                 logger.log("Initscript: %s: Installed new initscript in %s" % (self.name, sliver_initscript))
24                 if self.is_running():
25                     # Only need to rerun the initscript if the vserver is
26                     # already running. If the vserver isn't running, then the
27                     # initscript will automatically be started by
28                     # /etc/rc.d/vinit when the vserver is started.
29                     self.rerun_slice_vinit()
30             else:
31                 logger.log("Initscript: %s: Removed obsolete initscript %s" % (self.name, sliver_initscript))
32
33     def install_and_enable_vinit(self):
34         "prepare sliver rootfs init and systemd so the vinit service kicks in"
35         # the fact that systemd attempts to run old-style services 
36         # says we should do either one or the other and not both
37         # but actually if that was true we could just do it for init and be fine
38         # which is not what we've seen starting with f18
39         # so we try for a systemd system, and if it fails it means 
40         # one of the dir does not exist and so we are dealing with an init-based rootfs
41         try:
42             self.install_and_enable_vinit_for_systemd ()
43         except:
44             self.install_and_enable_vinit_for_init ()
45
46     # unconditionnally install and enable the generic vinit script
47     # mimicking chkconfig for enabling the generic vinit script
48     # this is hardwired for runlevel 3
49     def install_and_enable_vinit_for_init(self):
50         """
51         suitable for init-based VMs
52         """
53         vinit_source = "/usr/share/NodeManager/sliver-initscripts/vinit"
54         vinit_script = "/vservers/%s/etc/rc.d/init.d/vinit" % self.name
55         enable_link = "/vservers/%s/etc/rc.d/rc3.d/S99vinit" % self.name
56         enable_target = "../init.d/vinit"
57         # install in sliver
58         code = file(vinit_source).read()
59         if tools.replace_file_with_string(vinit_script, code, chmod=0755):
60             logger.log("Initscript: %s: installed generic vinit rc script" % self.name)
61         # create symlink for runlevel 3
62         if not os.path.islink(enable_link):
63             try:
64                 logger.log("Initscript: %s: creating runlevel3 symlink %s" % (self.name,enable_link))
65                 os.symlink(enable_target, enable_link)
66             except:
67                 logger.log_exc("Initscript failed to create runlevel3 symlink %s" % enable_link, name=self.name)
68
69     # very similar but with systemd unit files - we target 'multi-user' in this context
70     def install_and_enable_vinit_for_systemd(self):
71         """
72         suitable for systemd-based VMs
73         """
74
75         ##########
76         ########## initscripts : current status - march 2015
77         ##########
78         #
79         # the initscripts business worked smoothly up to f18 inclusive
80         # with f20 and the apparition of machinectl, things started to
81         # behave really weird
82         #
83         # so starting with f20, after having tried pretty hard to get this right,
84         # but to no success obviously, and in order to stay on the safe side
85         # of the force, I am turning off the initscript machinery completely
86         # that is to say: the vinit.service does not get installed at all
87         # 
88         if os.path.isfile('/usr/bin/machinectl'):
89             logger.log("WARNING: initscripts are not supported anymore in nodes that have machinectl")
90             return
91
92         vinit_source = "/usr/share/NodeManager/sliver-systemd/vinit.service"
93         vinit_unit_file = "/vservers/%s/usr/lib/systemd/system/vinit.service" % self.name
94         enable_link = "/vservers/%s/etc/systemd/system/multi-user.target.wants/vinit.service" % self.name
95         enable_target = "/usr/lib/systemd/system/vinit.service"
96         # install in sliver
97         code = file(vinit_source).read()
98         if tools.replace_file_with_string(vinit_unit_file, code, chmod=0755):
99             logger.log("Initscript: %s: installed vinit.service unit file" % self.name)
100         # create symlink for enabling this unit
101         if not os.path.islink(enable_link):
102             try:
103                 logger.log("Initscript: %s: creating enabling symlink %s" % (self.name, enable_link))
104                 os.symlink(enable_target, enable_link)
105             except:
106                 logger.log_exc("Initscript failed to create enabling symlink %s" % enable_link,name=name)