21
21
def service_start(service_name):
22
"""Start a system service"""
22
23
return service('start', service_name)
25
26
def service_stop(service_name):
27
"""Stop a system service"""
26
28
return service('stop', service_name)
29
31
def service_restart(service_name):
32
"""Restart a system service"""
30
33
return service('restart', service_name)
33
36
def service_reload(service_name, restart_on_failure=False):
37
"""Reload a system service, optionally falling back to restart if reload fails"""
34
38
service_result = service('reload', service_name)
35
39
if not service_result and restart_on_failure:
36
40
service_result = service('restart', service_name)
40
44
def service(action, service_name):
45
"""Control a system service"""
41
46
cmd = ['service', service_name, action]
42
47
return subprocess.call(cmd) == 0
45
50
def service_running(service):
51
"""Determine whether a system service is running"""
47
53
output = subprocess.check_output(['service', service, 'status'])
48
54
except subprocess.CalledProcessError:
57
63
def adduser(username, password=None, shell='/bin/bash', system_user=False):
64
"""Add a user to the system"""
60
66
user_info = pwd.getpwnam(username)
61
67
log('user {0} already exists!'.format(username))
140
146
def mount(device, mountpoint, options=None, persist=False):
141
'''Mount a filesystem'''
147
"""Mount a filesystem at a particular mountpoint"""
142
148
cmd_args = ['mount']
143
149
if options is not None:
144
150
cmd_args.extend(['-o', options])
157
163
def umount(mountpoint, persist=False):
158
'''Unmount a filesystem'''
164
"""Unmount a filesystem"""
159
165
cmd_args = ['umount', mountpoint]
161
167
subprocess.check_output(cmd_args)
172
'''List of all mounted volumes as [[mountpoint,device],[...]]'''
178
"""Get a list of all mounted volumes as [[mountpoint,device],[...]]"""
173
179
with open('/proc/mounts') as f:
174
180
# [['/mount/point','/dev/path'],[...]]
175
181
system_mounts = [m[1::-1] for m in [l.strip().split()
191
def restart_on_change(restart_map):
192
''' Restart services based on configuration files changing
197
def restart_on_change(restart_map, stopstart=False):
198
"""Restart services based on configuration files changing
194
200
This function is used a decorator, for example
213
219
for path in restart_map:
214
220
if checksums[path] != file_hash(path):
215
221
restarts += restart_map[path]
216
for service_name in list(OrderedDict.fromkeys(restarts)):
217
service('restart', service_name)
222
services_list = list(OrderedDict.fromkeys(restarts))
224
for service_name in services_list:
225
service('restart', service_name)
227
for action in ['stop', 'start']:
228
for service_name in services_list:
229
service(action, service_name)
222
234
def lsb_release():
223
'''Return /etc/lsb-release in a dict'''
235
"""Return /etc/lsb-release in a dict"""
225
237
with open('/etc/lsb-release', 'r') as lsb:
240
252
random.choice(alphanumeric_chars) for _ in range(length)]
241
253
return(''.join(random_chars))
256
def list_nics(nic_type):
257
'''Return a list of nics of given type(s)'''
258
if isinstance(nic_type, basestring):
259
int_types = [nic_type]
263
for int_type in int_types:
264
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
265
ip_output = subprocess.check_output(cmd).split('\n')
266
ip_output = (line for line in ip_output if line)
267
for line in ip_output:
268
if line.split()[1].startswith(int_type):
269
interfaces.append(line.split()[1].replace(":", ""))
273
def set_nic_mtu(nic, mtu):
274
'''Set MTU on a network interface'''
275
cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
276
subprocess.check_call(cmd)
279
def get_nic_mtu(nic):
280
cmd = ['ip', 'addr', 'show', nic]
281
ip_output = subprocess.check_output(cmd).split('\n')
283
for line in ip_output:
286
mtu = words[words.index("mtu") + 1]
290
def get_nic_hwaddr(nic):
291
cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
292
ip_output = subprocess.check_output(cmd)
294
words = ip_output.split()
295
if 'link/ether' in words:
296
hwaddr = words[words.index('link/ether') + 1]