~openstack-charmers-archive/charms/trusty/ceph/next

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2013-06-23 19:10:07 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: james.page@canonical.com-20130623191007-oez473wzbv30yg7y
Initial move to charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tools for working with the host system"""
 
2
# Copyright 2012 Canonical Ltd.
 
3
#
 
4
# Authors:
 
5
#  Nick Moffitt <nick.moffitt@canonical.com>
 
6
#  Matthew Wedgwood <matthew.wedgwood@canonical.com>
 
7
 
 
8
import apt_pkg
 
9
import os
 
10
import pwd
 
11
import grp
 
12
import subprocess
 
13
import hashlib
 
14
 
 
15
from hookenv import log, execution_environment
 
16
 
 
17
 
 
18
def service_start(service_name):
 
19
    service('start', service_name)
 
20
 
 
21
 
 
22
def service_stop(service_name):
 
23
    service('stop', service_name)
 
24
 
 
25
 
 
26
def service_restart(service_name):
 
27
    service('restart', service_name)
 
28
 
 
29
 
 
30
def service_reload(service_name, restart_on_failure=False):
 
31
    if not service('reload', service_name) and restart_on_failure:
 
32
        service('restart', service_name)
 
33
 
 
34
 
 
35
def service(action, service_name):
 
36
    cmd = ['service', service_name, action]
 
37
    return subprocess.call(cmd) == 0
 
38
 
 
39
 
 
40
def adduser(username, password=None, shell='/bin/bash', system_user=False):
 
41
    """Add a user"""
 
42
    try:
 
43
        user_info = pwd.getpwnam(username)
 
44
        log('user {0} already exists!'.format(username))
 
45
    except KeyError:
 
46
        log('creating user {0}'.format(username))
 
47
        cmd = ['useradd']
 
48
        if system_user or password is None:
 
49
           cmd.append('--system')
 
50
        else:
 
51
           cmd.extend([
 
52
               '--create-home',
 
53
               '--shell', shell,
 
54
               '--password', password,
 
55
           ])
 
56
        cmd.append(username)
 
57
        subprocess.check_call(cmd)
 
58
        user_info = pwd.getpwnam(username)
 
59
    return user_info
 
60
 
 
61
 
 
62
def add_user_to_group(username, group):
 
63
    """Add a user to a group"""
 
64
    cmd = [
 
65
        'gpasswd', '-a',
 
66
        username,
 
67
        group
 
68
    ]
 
69
    log("Adding user {} to group {}".format(username, group))
 
70
    subprocess.check_call(cmd)
 
71
 
 
72
 
 
73
def rsync(from_path, to_path, flags='-r', options=None):
 
74
    """Replicate the contents of a path"""
 
75
    context = execution_environment()
 
76
    options = options or ['--delete', '--executability']
 
77
    cmd = ['/usr/bin/rsync', flags]
 
78
    cmd.extend(options)
 
79
    cmd.append(from_path.format(**context))
 
80
    cmd.append(to_path.format(**context))
 
81
    log(" ".join(cmd))
 
82
    return subprocess.check_output(cmd).strip()
 
83
 
 
84
 
 
85
def symlink(source, destination):
 
86
    """Create a symbolic link"""
 
87
    context = execution_environment()
 
88
    log("Symlinking {} as {}".format(source, destination))
 
89
    cmd = [
 
90
        'ln',
 
91
        '-sf',
 
92
        source.format(**context),
 
93
        destination.format(**context)
 
94
    ]
 
95
    subprocess.check_call(cmd)
 
96
 
 
97
 
 
98
def mkdir(path, owner='root', group='root', perms=0555, force=False):
 
99
    """Create a directory"""
 
100
    context = execution_environment()
 
101
    log("Making dir {} {}:{} {:o}".format(path, owner, group,
 
102
                                          perms))
 
103
    uid = pwd.getpwnam(owner.format(**context)).pw_uid
 
104
    gid = grp.getgrnam(group.format(**context)).gr_gid
 
105
    realpath = os.path.abspath(path)
 
106
    if os.path.exists(realpath):
 
107
        if force and not os.path.isdir(realpath):
 
108
            log("Removing non-directory file {} prior to mkdir()".format(path))
 
109
            os.unlink(realpath)
 
110
    else:
 
111
        os.makedirs(realpath, perms)
 
112
    os.chown(realpath, uid, gid)
 
113
 
 
114
 
 
115
def write_file(path, fmtstr, owner='root', group='root', perms=0444, **kwargs):
 
116
    """Create or overwrite a file with the contents of a string"""
 
117
    context = execution_environment()
 
118
    context.update(kwargs)
 
119
    log("Writing file {} {}:{} {:o}".format(path, owner, group,
 
120
        perms))
 
121
    uid = pwd.getpwnam(owner.format(**context)).pw_uid
 
122
    gid = grp.getgrnam(group.format(**context)).gr_gid
 
123
    with open(path.format(**context), 'w') as target:
 
124
        os.fchown(target.fileno(), uid, gid)
 
125
        os.fchmod(target.fileno(), perms)
 
126
        target.write(fmtstr.format(**context))
 
127
 
 
128
 
 
129
def render_template_file(source, destination, **kwargs):
 
130
    """Create or overwrite a file using a template"""
 
131
    log("Rendering template {} for {}".format(source,
 
132
        destination))
 
133
    context = execution_environment()
 
134
    with open(source.format(**context), 'r') as template:
 
135
        write_file(destination.format(**context), template.read(),
 
136
                   **kwargs)
 
137
 
 
138
 
 
139
def filter_installed_packages(packages):
 
140
    """Returns a list of packages that require installation"""
 
141
    apt_pkg.init()
 
142
    cache = apt_pkg.Cache()
 
143
    _pkgs = []
 
144
    for package in packages:
 
145
        try:
 
146
            p = cache[package]
 
147
            p.current_ver or _pkgs.append(package)
 
148
        except KeyError:
 
149
            log('Package {} has no installation candidate.'.format(package),
 
150
                level='WARNING')
 
151
            _pkgs.append(package)
 
152
    return _pkgs
 
153
 
 
154
 
 
155
def apt_install(packages, options=None, fatal=False):
 
156
    """Install one or more packages"""
 
157
    options = options or []
 
158
    cmd = ['apt-get', '-y']
 
159
    cmd.extend(options)
 
160
    cmd.append('install')
 
161
    if isinstance(packages, basestring):
 
162
        cmd.append(packages)
 
163
    else:
 
164
        cmd.extend(packages)
 
165
    log("Installing {} with options: {}".format(packages,
 
166
                                                options))
 
167
    if fatal:
 
168
        subprocess.check_call(cmd)
 
169
    else:
 
170
        subprocess.call(cmd)
 
171
 
 
172
 
 
173
def apt_update(fatal=False):
 
174
    """Update local apt cache"""
 
175
    cmd = ['apt-get', 'update']
 
176
    if fatal:
 
177
        subprocess.check_call(cmd)
 
178
    else:
 
179
        subprocess.call(cmd)
 
180
 
 
181
 
 
182
def mount(device, mountpoint, options=None, persist=False):
 
183
    '''Mount a filesystem'''
 
184
    cmd_args = ['mount']
 
185
    if options is not None:
 
186
        cmd_args.extend(['-o', options])
 
187
    cmd_args.extend([device, mountpoint])
 
188
    try:
 
189
        subprocess.check_output(cmd_args)
 
190
    except subprocess.CalledProcessError, e:
 
191
        log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
 
192
        return False
 
193
    if persist:
 
194
        # TODO: update fstab
 
195
        pass
 
196
    return True
 
197
 
 
198
 
 
199
def umount(mountpoint, persist=False):
 
200
    '''Unmount a filesystem'''
 
201
    cmd_args = ['umount', mountpoint]
 
202
    try:
 
203
        subprocess.check_output(cmd_args)
 
204
    except subprocess.CalledProcessError, e:
 
205
        log('Error unmounting {}\n{}'.format(mountpoint, e.output))
 
206
        return False
 
207
    if persist:
 
208
        # TODO: update fstab
 
209
        pass
 
210
    return True
 
211
 
 
212
 
 
213
def mounts():
 
214
    '''List of all mounted volumes as [[mountpoint,device],[...]]'''
 
215
    with open('/proc/mounts') as f:
 
216
        # [['/mount/point','/dev/path'],[...]]
 
217
        system_mounts = [m[1::-1] for m in [l.strip().split()
 
218
                                            for l in f.readlines()]]
 
219
    return system_mounts
 
220
 
 
221
 
 
222
def file_hash(path):
 
223
    ''' Generate a md5 hash of the contents of 'path' or None if not found '''
 
224
    if os.path.exists(path):
 
225
        h = hashlib.md5()
 
226
        with open(path, 'r') as source:
 
227
            h.update(source.read())  # IGNORE:E1101 - it does have update
 
228
        return h.hexdigest()
 
229
    else:
 
230
        return None
 
231
 
 
232
 
 
233
def restart_on_change(restart_map):
 
234
    ''' Restart services based on configuration files changing
 
235
 
 
236
    This function is used a decorator, for example
 
237
 
 
238
        @restart_on_change({
 
239
            '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
 
240
            })
 
241
        def ceph_client_changed():
 
242
            ...
 
243
 
 
244
    In this example, the cinder-api and cinder-volume services
 
245
    would be restarted if /etc/ceph/ceph.conf is changed by the
 
246
    ceph_client_changed function.
 
247
    '''
 
248
    def wrap(f):
 
249
        def wrapped_f(*args):
 
250
            checksums = {}
 
251
            for path in restart_map:
 
252
                checksums[path] = file_hash(path)
 
253
            f(*args)
 
254
            restarts = []
 
255
            for path in restart_map:
 
256
                if checksums[path] != file_hash(path):
 
257
                    restarts += restart_map[path]
 
258
            for service_name in list(set(restarts)):
 
259
                service('restart', service_name)
 
260
        return wrapped_f
 
261
    return wrap