Merge lp:~adam-collard/charm-helpers/pause-service-helper into lp:charm-helpers

Proposed by Adam Collard
Status: Merged
Merged at revision: 406
Proposed branch: lp:~adam-collard/charm-helpers/pause-service-helper
Merge into: lp:charm-helpers
Diff against target: 109 lines (+64/-6)
3 files modified
VERSION (+1/-1)
charmhelpers/core/host.py (+31/-5)
tests/core/test_host.py (+32/-0)
To merge this branch: bzr merge lp:~adam-collard/charm-helpers/pause-service-helper
Reviewer Review Type Date Requested Status
Chris Glass (community) Approve
Alberto Donato (community) Approve
David Britton Pending
Review via email: mp+264866@code.launchpad.net

Description of the change

This branch adds service_pause and service_resume helpers to charmhelpers.core.host

The intention of these helpers is to go a bit further than service_stop and service_start by also adding an Upstart override to prevent the services starting at boot.

systemd support will land in a follow up branch, it should be noted that existing helpers for the services do not support systemd.

To post a comment you must log in.
Revision history for this message
Alberto Donato (ack) wrote :

+1 with an inline comment.

review: Approve
406. By Adam Collard

Default init_dir to None for better forwards compatability (ack's review)

Revision history for this message
Chris Glass (tribaal) wrote :

Looks great! Thanks for your contribution.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'VERSION'
2--- VERSION 2015-05-20 14:31:33 +0000
3+++ VERSION 2015-07-15 17:26:31 +0000
4@@ -1,1 +1,1 @@
5-0.3.2
6+0.3.3
7
8=== modified file 'charmhelpers/core/host.py'
9--- charmhelpers/core/host.py 2015-06-01 17:08:32 +0000
10+++ charmhelpers/core/host.py 2015-07-15 17:26:31 +0000
11@@ -63,6 +63,36 @@
12 return service_result
13
14
15+def service_pause(service_name, init_dir=None):
16+ """Pause a system service.
17+
18+ Stop it, and prevent it from starting again at boot."""
19+ if init_dir is None:
20+ init_dir = "/etc/init"
21+ stopped = service_stop(service_name)
22+ # XXX: Support systemd too
23+ override_path = os.path.join(
24+ init_dir, '{}.conf.override'.format(service_name))
25+ with open(override_path, 'w') as fh:
26+ fh.write("manual\n")
27+ return stopped
28+
29+
30+def service_resume(service_name, init_dir=None):
31+ """Resume a system service.
32+
33+ Reenable starting again at boot. Start the service"""
34+ # XXX: Support systemd too
35+ if init_dir is None:
36+ init_dir = "/etc/init"
37+ override_path = os.path.join(
38+ init_dir, '{}.conf.override'.format(service_name))
39+ if os.path.exists(override_path):
40+ os.unlink(override_path)
41+ started = service_start(service_name)
42+ return started
43+
44+
45 def service(action, service_name):
46 """Control a system service"""
47 cmd = ['service', service_name, action]
48@@ -140,11 +170,7 @@
49
50 def add_user_to_group(username, group):
51 """Add a user to a group"""
52- cmd = [
53- 'gpasswd', '-a',
54- username,
55- group
56- ]
57+ cmd = ['gpasswd', '-a', username, group]
58 log("Adding user {} to group {}".format(username, group))
59 subprocess.check_call(cmd)
60
61
62=== modified file 'tests/core/test_host.py'
63--- tests/core/test_host.py 2015-07-14 18:12:04 +0000
64+++ tests/core/test_host.py 2015-07-15 17:26:31 +0000
65@@ -1,5 +1,9 @@
66+import os.path
67 from collections import OrderedDict
68 import subprocess
69+from tempfile import mkdtemp
70+from shutil import rmtree
71+
72 import apt_pkg
73
74 from mock import patch, call
75@@ -99,6 +103,34 @@
76 service.assert_called_with('restart', service_name)
77
78 @patch.object(host, 'service')
79+ def test_pauses_a_service(self, service):
80+ service_name = 'foo-service'
81+ service.side_effect = [True]
82+ tempdir = mkdtemp(prefix="test_pauses_a_service")
83+ self.addCleanup(rmtree, tempdir)
84+ self.assertTrue(host.service_pause(service_name, init_dir=tempdir))
85+
86+ service.assert_called_with('stop', service_name)
87+ override_path = os.path.join(
88+ tempdir, "{}.conf.override".format(service_name))
89+ with open(override_path, "r") as fh:
90+ override_contents = fh.read()
91+ self.assertEqual("manual\n", override_contents)
92+
93+ @patch.object(host, 'service')
94+ def test_resumes_a_service(self, service):
95+ service_name = 'foo-service'
96+ service.side_effect = [True]
97+ tempdir = mkdtemp(prefix="test_resumes_a_service")
98+ self.addCleanup(rmtree, tempdir)
99+ self.assertTrue(host.service_resume(service_name, init_dir=tempdir))
100+
101+ service.assert_called_with('start', service_name)
102+ override_path = os.path.join(
103+ tempdir, "{}.conf.override".format(service_name))
104+ self.assertFalse(os.path.exists(override_path))
105+
106+ @patch.object(host, 'service')
107 def test_reloads_a_service(self, service):
108 service_name = 'foo-service'
109 service.side_effect = [True]

Subscribers

People subscribed via source and target branches