36
37
def service_reload(service_name, restart_on_failure=False):
37
"""Reload a system service, optionally falling back to restart if reload fails"""
38
"""Reload a system service, optionally falling back to restart if
38
40
service_result = service('reload', service_name)
39
41
if not service_result and restart_on_failure:
40
42
service_result = service('restart', service_name)
143
145
target.write(content)
146
def mount(device, mountpoint, options=None, persist=False):
148
def fstab_remove(mp):
149
"""Remove the given mountpoint entry from /etc/fstab
151
return Fstab.remove_by_mountpoint(mp)
154
def fstab_add(dev, mp, fs, options=None):
155
"""Adds the given device entry to the /etc/fstab file
157
return Fstab.add(dev, mp, fs, options=options)
160
def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"):
147
161
"""Mount a filesystem at a particular mountpoint"""
148
162
cmd_args = ['mount']
149
163
if options is not None:
154
168
except subprocess.CalledProcessError, e:
155
169
log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
173
return fstab_add(device, mountpoint, filesystem, options=options)
197
def restart_on_change(restart_map):
211
def restart_on_change(restart_map, stopstart=False):
198
212
"""Restart services based on configuration files changing
200
214
This function is used a decorator, for example
219
233
for path in restart_map:
220
234
if checksums[path] != file_hash(path):
221
235
restarts += restart_map[path]
222
for service_name in list(OrderedDict.fromkeys(restarts)):
223
service('restart', service_name)
236
services_list = list(OrderedDict.fromkeys(restarts))
238
for service_name in services_list:
239
service('restart', service_name)
241
for action in ['stop', 'start']:
242
for service_name in services_list:
243
service(action, service_name)
246
266
random.choice(alphanumeric_chars) for _ in range(length)]
247
267
return(''.join(random_chars))
270
def list_nics(nic_type):
271
'''Return a list of nics of given type(s)'''
272
if isinstance(nic_type, basestring):
273
int_types = [nic_type]
277
for int_type in int_types:
278
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
279
ip_output = subprocess.check_output(cmd).split('\n')
280
ip_output = (line for line in ip_output if line)
281
for line in ip_output:
282
if line.split()[1].startswith(int_type):
283
interfaces.append(line.split()[1].replace(":", ""))
287
def set_nic_mtu(nic, mtu):
288
'''Set MTU on a network interface'''
289
cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
290
subprocess.check_call(cmd)
293
def get_nic_mtu(nic):
294
cmd = ['ip', 'addr', 'show', nic]
295
ip_output = subprocess.check_output(cmd).split('\n')
297
for line in ip_output:
300
mtu = words[words.index("mtu") + 1]
304
def get_nic_hwaddr(nic):
305
cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
306
ip_output = subprocess.check_output(cmd)
308
words = ip_output.split()
309
if 'link/ether' in words:
310
hwaddr = words[words.index('link/ether') + 1]
314
def cmp_pkgrevno(package, revno, pkgcache=None):
315
'''Compare supplied revno with the revno of the installed package
316
1 => Installed revno is greater than supplied arg
317
0 => Installed revno is the same as supplied arg
318
-1 => Installed revno is less than supplied arg
323
pkgcache = apt_pkg.Cache()
324
pkg = pkgcache[package]
325
return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)