~gnuoy/charms/trusty/nova-compute/resurrect-peer-relation

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2014-07-28 11:36:16 UTC
  • mfrom: (68.1.3 nova-compute)
  • Revision ID: james.page@ubuntu.com-20140728113616-spl81505m0q840sy
[corey.bryant,r=james-page] Add amulet tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import string
13
13
import subprocess
14
14
import hashlib
15
 
import apt_pkg
16
15
 
17
16
from collections import OrderedDict
18
17
 
19
18
from hookenv import log
 
19
from fstab import Fstab
20
20
 
21
21
 
22
22
def service_start(service_name):
35
35
 
36
36
 
37
37
def service_reload(service_name, restart_on_failure=False):
38
 
    """Reload a system service, optionally falling back to restart if reload fails"""
 
38
    """Reload a system service, optionally falling back to restart if
 
39
    reload fails"""
39
40
    service_result = service('reload', service_name)
40
41
    if not service_result and restart_on_failure:
41
42
        service_result = service('restart', service_name)
144
145
        target.write(content)
145
146
 
146
147
 
147
 
def mount(device, mountpoint, options=None, persist=False):
 
148
def fstab_remove(mp):
 
149
    """Remove the given mountpoint entry from /etc/fstab
 
150
    """
 
151
    return Fstab.remove_by_mountpoint(mp)
 
152
 
 
153
 
 
154
def fstab_add(dev, mp, fs, options=None):
 
155
    """Adds the given device entry to the /etc/fstab file
 
156
    """
 
157
    return Fstab.add(dev, mp, fs, options=options)
 
158
 
 
159
 
 
160
def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"):
148
161
    """Mount a filesystem at a particular mountpoint"""
149
162
    cmd_args = ['mount']
150
163
    if options is not None:
155
168
    except subprocess.CalledProcessError, e:
156
169
        log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
157
170
        return False
 
171
 
158
172
    if persist:
159
 
        # TODO: update fstab
160
 
        pass
 
173
        return fstab_add(device, mountpoint, filesystem, options=options)
161
174
    return True
162
175
 
163
176
 
169
182
    except subprocess.CalledProcessError, e:
170
183
        log('Error unmounting {}\n{}'.format(mountpoint, e.output))
171
184
        return False
 
185
 
172
186
    if persist:
173
 
        # TODO: update fstab
174
 
        pass
 
187
        return fstab_remove(mountpoint)
175
188
    return True
176
189
 
177
190
 
198
211
def restart_on_change(restart_map, stopstart=False):
199
212
    """Restart services based on configuration files changing
200
213
 
201
 
    This function is used a decorator, for example
 
214
    This function is used a decorator, for example::
202
215
 
203
216
        @restart_on_change({
204
217
            '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
205
218
            })
206
219
        def ceph_client_changed():
207
 
            ...
 
220
            pass  # your code here
208
221
 
209
222
    In this example, the cinder-api and cinder-volume services
210
223
    would be restarted if /etc/ceph/ceph.conf is changed by the
300
313
 
301
314
def cmp_pkgrevno(package, revno, pkgcache=None):
302
315
    '''Compare supplied revno with the revno of the installed package
303
 
       1 => Installed revno is greater than supplied arg
304
 
       0 => Installed revno is the same as supplied arg
305
 
      -1 => Installed revno is less than supplied arg
 
316
 
 
317
    *  1 => Installed revno is greater than supplied arg
 
318
    *  0 => Installed revno is the same as supplied arg
 
319
    * -1 => Installed revno is less than supplied arg
 
320
 
306
321
    '''
 
322
    import apt_pkg
307
323
    if not pkgcache:
308
324
        apt_pkg.init()
 
325
        # Force Apt to build its cache in memory. That way we avoid race
 
326
        # conditions with other applications building the cache in the same
 
327
        # place.
 
328
        apt_pkg.config.set("Dir::Cache::pkgcache", "")
309
329
        pkgcache = apt_pkg.Cache()
310
330
    pkg = pkgcache[package]
311
331
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)