reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[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=0o755):
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         with open(vinit_source) as f:
59             code = f.read()
60         if tools.replace_file_with_string(vinit_script, code, chmod=0o755):
61             logger.log("Initscript: %s: installed generic vinit rc script" % self.name)
62         # create symlink for runlevel 3
63         if not os.path.islink(enable_link):
64             try:
65                 logger.log("Initscript: %s: creating runlevel3 symlink %s" % (self.name, enable_link))
66                 os.symlink(enable_target, enable_link)
67             except:
68                 logger.log_exc("Initscript failed to create runlevel3 symlink %s" % enable_link, name=self.name)
69
70     # very similar but with systemd unit files - we target 'multi-user' in this context
71     def install_and_enable_vinit_for_systemd(self):
72         """
73         suitable for systemd-based VMs
74         """
75
76         ##########
77         ########## initscripts : current status - march 2015
78         ##########
79         #
80         # the initscripts business worked smoothly up to f18 inclusive
81         # with f20 and the apparition of machinectl, things started to
82         # behave really weird
83         #
84         # so starting with f20, after having tried pretty hard to get this right,
85         # but to no success obviously, and in order to stay on the safe side
86         # of the force, I am turning off the initscript machinery completely
87         # that is to say: the vinit.service does not get installed at all
88         # 
89         if os.path.isfile('/usr/bin/machinectl'):
90             logger.log("WARNING: initscripts are not supported anymore in nodes that have machinectl")
91             return
92
93         vinit_source = "/usr/share/NodeManager/sliver-systemd/vinit.service"
94         vinit_unit_file = "/vservers/%s/usr/lib/systemd/system/vinit.service" % self.name
95         enable_link = "/vservers/%s/etc/systemd/system/multi-user.target.wants/vinit.service" % self.name
96         enable_target = "/usr/lib/systemd/system/vinit.service"
97         # install in sliver
98         with open(vinit_source) as f:
99             code = f.read()
100         if tools.replace_file_with_string(vinit_unit_file, code, chmod=0o755):
101             logger.log("Initscript: %s: installed vinit.service unit file" % self.name)
102         # create symlink for enabling this unit
103         if not os.path.islink(enable_link):
104             try:
105                 logger.log("Initscript: %s: creating enabling symlink %s" % (self.name, enable_link))
106                 os.symlink(enable_target, enable_link)
107             except:
108                 logger.log_exc("Initscript failed to create enabling symlink %s" % enable_link, name=name)