-
Notifications
You must be signed in to change notification settings - Fork 23
/
svc_systemd.py
113 lines (90 loc) · 3.13 KB
/
svc_systemd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from pipes import quote
from bundlewrap.exceptions import BundleError
from bundlewrap.items import Item
from bundlewrap.utils.text import mark_for_translation as _
def svc_start(node, svcname):
return node.run("systemctl start -- {}".format(quote(svcname)))
def svc_running(node, svcname):
result = node.run(
"systemctl status -- {}".format(quote(svcname)),
may_fail=True,
)
return result.return_code == 0
def svc_stop(node, svcname):
return node.run("systemctl stop -- {}".format(quote(svcname)))
def svc_enable(node, svcname):
return node.run("systemctl enable -- {}".format(quote(svcname)))
def svc_enabled(node, svcname):
result = node.run(
"systemctl is-enabled -- {}".format(quote(svcname)),
may_fail=True,
)
return result.return_code == 0
def svc_disable(node, svcname):
return node.run("systemctl disable -- {}".format(quote(svcname)))
class SvcSystemd(Item):
"""
A service managed by systemd.
"""
BUNDLE_ATTRIBUTE_NAME = "svc_systemd"
# bw 3.0: Both should default to True.
ITEM_ATTRIBUTES = {
'enabled': None,
'running': True,
}
ITEM_TYPE_NAME = "svc_systemd"
def __repr__(self):
return "<SvcSystemd name:{} enabled:{} running:{}>".format(
self.name,
self.attributes['enabled'],
self.attributes['running'],
)
def cdict(self):
cdict = {}
for option, value in self.attributes.items():
if value is not None:
cdict[option] = value
return cdict
def fix(self, status):
if 'enabled' in status.keys_to_fix:
if self.attributes['enabled']:
svc_enable(self.node, self.name)
else:
svc_disable(self.node, self.name)
if 'running' in status.keys_to_fix:
if self.attributes['running']:
svc_start(self.node, self.name)
else:
svc_stop(self.node, self.name)
def get_canned_actions(self):
return {
'reload': {
'command': "systemctl reload -- {}".format(self.name),
'needs': [self.id],
},
'restart': {
'command': "systemctl restart -- {}".format(self.name),
'needs': [self.id],
},
}
def sdict(self):
return {
'enabled': svc_enabled(self.node, self.name),
'running': svc_running(self.node, self.name),
}
@classmethod
def validate_attributes(cls, bundle, item_id, attributes):
for attribute in ('enabled', 'running'):
if (
not isinstance(attributes.get(attribute, True), bool) and
not attributes.get(attribute, None) is None
):
raise BundleError(_(
"expected boolean or None for '{attribute}' on {item} in bundle '{bundle}'"
).format(
attribute=attribute,
bundle=bundle.name,
item=item_id,
))