summaryrefslogtreecommitdiffstats
path: root/build/lib/Bcfg2/Client/Tools/Upstart.py
diff options
context:
space:
mode:
authorFabian Affolter <fabian@bernewireless.net>2010-12-08 23:27:15 +0100
committerFabian Affolter <fabian@bernewireless.net>2010-12-08 23:27:15 +0100
commite833a7a76b231cd346f09c9a422ecb855d1cc6b4 (patch)
treee2762fc31e1162a6859dbd6cd7b5c28208296d9d /build/lib/Bcfg2/Client/Tools/Upstart.py
parentbaf5854f414c2a35887e3fbbcfd094e12e7c6ee7 (diff)
downloadbcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.tar.gz
bcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.tar.bz2
bcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.zip
Merge with upstream
Diffstat (limited to 'build/lib/Bcfg2/Client/Tools/Upstart.py')
-rw-r--r--build/lib/Bcfg2/Client/Tools/Upstart.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/build/lib/Bcfg2/Client/Tools/Upstart.py b/build/lib/Bcfg2/Client/Tools/Upstart.py
new file mode 100644
index 000000000..113f28d23
--- /dev/null
+++ b/build/lib/Bcfg2/Client/Tools/Upstart.py
@@ -0,0 +1,78 @@
+"""Upstart support for Bcfg2."""
+__revision__ = '$Revision$'
+
+import glob
+import re
+
+import Bcfg2.Client.Tools
+import Bcfg2.Client.XML
+
+
+class Upstart(Bcfg2.Client.Tools.SvcTool):
+ """Upstart service support for Bcfg2."""
+ name = 'Upstart'
+ __execs__ = ['/lib/init/upstart-job',
+ '/sbin/initctl',
+ '/usr/sbin/service']
+ __handles__ = [('Service', 'upstart')]
+ __req__ = {'Service': ['name', 'status']}
+ svcre = re.compile("/etc/init/(?P<name>.*).conf")
+
+ def get_svc_command(self, service, action):
+ return "/usr/sbin/service %s %s" % (service.get('name'), action)
+
+ def VerifyService(self, entry, _):
+ """Verify Service status for entry
+
+ Verifying whether or not the service is enabled can be done
+ at the file level with upstart using the contents of
+ /etc/init/servicename.conf. All we need to do is make sure
+ the service is running when it should be.
+ """
+ try:
+ output = self.cmd.run('/usr/sbin/service %s status' % \
+ entry.get('name'))[1][0]
+ except IndexError:
+ self.logger.error("Service %s not an Upstart service" % \
+ entry.get('name'))
+ return False
+ try:
+ running = output.split(' ')[1].split('/')[1].startswith('running')
+ if running:
+ entry.set('current_status', 'on')
+ if entry.get('status') == 'off':
+ status = False
+ else:
+ status = True
+ else:
+ entry.set('current_status', 'off')
+ if entry.get('status') == 'on':
+ status = False
+ else:
+ status = True
+ except IndexError:
+ # service does not exist
+ entry.set('current_status', 'off')
+ status = False
+
+ return status
+
+ def InstallService(self, entry):
+ """Install Service for entry."""
+ if entry.get('status') == 'on':
+ pstatus = self.cmd.run(self.get_svc_command(entry, 'start'))[0]
+ elif entry.get('status') == 'off':
+ pstatus = self.cmd.run(self.get_svc_command(entry, 'stop'))[0]
+ # pstatus is true if command failed
+ return not pstatus
+
+ def FindExtra(self):
+ """Locate extra Upstart services."""
+ specified = [entry.get('name') for entry in self.getSupportedEntries()]
+ extra = []
+ for name in [self.svcre.match(fname).group('name') for fname in
+ glob.glob("/etc/init/*.conf") \
+ if self.svcre.match(fname).group('name') not in specified]:
+ extra.append(name)
+ return [Bcfg2.Client.XML.Element('Service', type='upstart', name=name) \
+ for name in extra]