~matsubara/charms/trusty/mongodb/bug-1410847

« back to all changes in this revision

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

  • Committer: Charles Butler
  • Date: 2014-07-08 18:53:07 UTC
  • mto: This revision was merged to the branch mainline in revision 50.
  • Revision ID: chuck@dasroot.net-20140708185307-aakhfrsvr2wwoxof
Adds charmhelpers, and refactors to green light deployments

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
from collections import OrderedDict
17
17
 
18
18
from hookenv import log
 
19
from fstab import Fstab
19
20
 
20
21
 
21
22
def service_start(service_name):
34
35
 
35
36
 
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
 
39
    reload fails"""
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)
144
146
 
145
147
 
146
 
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"):
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))
156
170
        return False
 
171
 
157
172
    if persist:
158
 
        # TODO: update fstab
159
 
        pass
 
173
        return fstab_add(device, mountpoint, filesystem, options=options)
160
174
    return True
161
175
 
162
176
 
168
182
    except subprocess.CalledProcessError, e:
169
183
        log('Error unmounting {}\n{}'.format(mountpoint, e.output))
170
184
        return False
 
185
 
171
186
    if persist:
172
 
        # TODO: update fstab
173
 
        pass
 
187
        return fstab_remove(mountpoint)
174
188
    return True
175
189
 
176
190
 
197
211
def restart_on_change(restart_map, stopstart=False):
198
212
    """Restart services based on configuration files changing
199
213
 
200
 
    This function is used a decorator, for example
 
214
    This function is used a decorator, for example::
201
215
 
202
216
        @restart_on_change({
203
217
            '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
204
218
            })
205
219
        def ceph_client_changed():
206
 
            ...
 
220
            pass  # your code here
207
221
 
208
222
    In this example, the cinder-api and cinder-volume services
209
223
    would be restarted if /etc/ceph/ceph.conf is changed by the
295
309
    if 'link/ether' in words:
296
310
        hwaddr = words[words.index('link/ether') + 1]
297
311
    return hwaddr
 
312
 
 
313
 
 
314
def cmp_pkgrevno(package, revno, pkgcache=None):
 
315
    '''Compare supplied revno with the revno of the installed package
 
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
 
 
321
    '''
 
322
    import apt_pkg
 
323
    if not pkgcache:
 
324
        apt_pkg.init()
 
325
        pkgcache = apt_pkg.Cache()
 
326
    pkg = pkgcache[package]
 
327
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)