~1chb1n/charms/trusty/glance/stable-flip-tests-helper-syncs

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: james.page at ubuntu
  • Date: 2015-08-10 16:34:51 UTC
  • Revision ID: james.page@ubuntu.com-20150810163451-jj475fptvmakbqv4
Tags: 15.07
[gnuoy] 15.07 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import os
25
25
import re
26
26
import pwd
 
27
import glob
27
28
import grp
28
29
import random
29
30
import string
62
63
    return service_result
63
64
 
64
65
 
 
66
def service_pause(service_name, init_dir=None):
 
67
    """Pause a system service.
 
68
 
 
69
    Stop it, and prevent it from starting again at boot."""
 
70
    if init_dir is None:
 
71
        init_dir = "/etc/init"
 
72
    stopped = service_stop(service_name)
 
73
    # XXX: Support systemd too
 
74
    override_path = os.path.join(
 
75
        init_dir, '{}.conf.override'.format(service_name))
 
76
    with open(override_path, 'w') as fh:
 
77
        fh.write("manual\n")
 
78
    return stopped
 
79
 
 
80
 
 
81
def service_resume(service_name, init_dir=None):
 
82
    """Resume a system service.
 
83
 
 
84
    Reenable starting again at boot. Start the service"""
 
85
    # XXX: Support systemd too
 
86
    if init_dir is None:
 
87
        init_dir = "/etc/init"
 
88
    override_path = os.path.join(
 
89
        init_dir, '{}.conf.override'.format(service_name))
 
90
    if os.path.exists(override_path):
 
91
        os.unlink(override_path)
 
92
    started = service_start(service_name)
 
93
    return started
 
94
 
 
95
 
65
96
def service(action, service_name):
66
97
    """Control a system service"""
67
98
    cmd = ['service', service_name, action]
90
121
            ['service', service_name, 'status'],
91
122
            stderr=subprocess.STDOUT).decode('UTF-8')
92
123
    except subprocess.CalledProcessError as e:
93
 
        return 'unrecognized service' not in e.output
 
124
        return b'unrecognized service' not in e.output
94
125
    else:
95
126
        return True
96
127
 
139
170
 
140
171
def add_user_to_group(username, group):
141
172
    """Add a user to a group"""
142
 
    cmd = [
143
 
        'gpasswd', '-a',
144
 
        username,
145
 
        group
146
 
    ]
 
173
    cmd = ['gpasswd', '-a', username, group]
147
174
    log("Adding user {} to group {}".format(username, group))
148
175
    subprocess.check_call(cmd)
149
176
 
269
296
        return None
270
297
 
271
298
 
 
299
def path_hash(path):
 
300
    """
 
301
    Generate a hash checksum of all files matching 'path'. Standard wildcards
 
302
    like '*' and '?' are supported, see documentation for the 'glob' module for
 
303
    more information.
 
304
 
 
305
    :return: dict: A { filename: hash } dictionary for all matched files.
 
306
                   Empty if none found.
 
307
    """
 
308
    return {
 
309
        filename: file_hash(filename)
 
310
        for filename in glob.iglob(path)
 
311
    }
 
312
 
 
313
 
272
314
def check_hash(path, checksum, hash_type='md5'):
273
315
    """
274
316
    Validate a file using a cryptographic checksum.
296
338
 
297
339
        @restart_on_change({
298
340
            '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
 
341
            '/etc/apache/sites-enabled/*': [ 'apache2' ]
299
342
            })
300
 
        def ceph_client_changed():
 
343
        def config_changed():
301
344
            pass  # your code here
302
345
 
303
346
    In this example, the cinder-api and cinder-volume services
304
347
    would be restarted if /etc/ceph/ceph.conf is changed by the
305
 
    ceph_client_changed function.
 
348
    ceph_client_changed function. The apache2 service would be
 
349
    restarted if any file matching the pattern got changed, created
 
350
    or removed. Standard wildcards are supported, see documentation
 
351
    for the 'glob' module for more information.
306
352
    """
307
353
    def wrap(f):
308
354
        def wrapped_f(*args, **kwargs):
309
 
            checksums = {}
310
 
            for path in restart_map:
311
 
                checksums[path] = file_hash(path)
 
355
            checksums = {path: path_hash(path) for path in restart_map}
312
356
            f(*args, **kwargs)
313
357
            restarts = []
314
358
            for path in restart_map:
315
 
                if checksums[path] != file_hash(path):
 
359
                if path_hash(path) != checksums[path]:
316
360
                    restarts += restart_map[path]
317
361
            services_list = list(OrderedDict.fromkeys(restarts))
318
362
            if not stopstart: