72
72
stopped = service_stop(service_name)
73
73
upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
74
74
sysv_file = os.path.join(initd_dir, service_name)
75
if os.path.exists(upstart_file):
76
service('disable', service_name)
77
elif os.path.exists(upstart_file):
76
78
override_path = os.path.join(
77
79
init_dir, '{}.override'.format(service_name))
78
80
with open(override_path, 'w') as fh:
80
82
elif os.path.exists(sysv_file):
81
83
subprocess.check_call(["update-rc.d", service_name, "disable"])
83
# XXX: Support SystemD too
85
"Unable to detect {0} as either Upstart {1} or SysV {2}".format(
86
"Unable to detect {0} as SystemD, Upstart {1} or"
86
88
service_name, upstart_file, sysv_file))
94
96
Reenable starting again at boot. Start the service"""
95
97
upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
96
98
sysv_file = os.path.join(initd_dir, service_name)
97
if os.path.exists(upstart_file):
100
service('enable', service_name)
101
elif os.path.exists(upstart_file):
98
102
override_path = os.path.join(
99
103
init_dir, '{}.override'.format(service_name))
100
104
if os.path.exists(override_path):
102
106
elif os.path.exists(sysv_file):
103
107
subprocess.check_call(["update-rc.d", service_name, "enable"])
105
# XXX: Support SystemD too
106
109
raise ValueError(
107
"Unable to detect {0} as either Upstart {1} or SysV {2}".format(
110
"Unable to detect {0} as SystemD, Upstart {1} or"
108
112
service_name, upstart_file, sysv_file))
110
114
started = service_running(service_name)
116
120
def service(action, service_name):
117
121
"""Control a system service"""
118
cmd = ['service', service_name, action]
122
if init_is_systemd():
123
cmd = ['systemctl', action, service_name]
125
cmd = ['service', service_name, action]
119
126
return subprocess.call(cmd) == 0
122
def service_running(service):
129
def service_running(service_name):
123
130
"""Determine whether a system service is running"""
125
output = subprocess.check_output(
126
['service', service, 'status'],
127
stderr=subprocess.STDOUT).decode('UTF-8')
128
except subprocess.CalledProcessError:
131
if init_is_systemd():
132
return service('is-active', service_name)
131
if ("start/running" in output or "is running" in output):
135
output = subprocess.check_output(
136
['service', service_name, 'status'],
137
stderr=subprocess.STDOUT).decode('UTF-8')
138
except subprocess.CalledProcessError:
141
if ("start/running" in output or "is running" in output):
137
147
def service_available(service_name):
159
SYSTEMD_SYSTEM = '/run/systemd/system'
162
def init_is_systemd():
163
return os.path.isdir(SYSTEMD_SYSTEM)
149
166
def adduser(username, password=None, shell='/bin/bash', system_user=False,
150
167
primary_group=None, secondary_groups=None):