~corey.bryant/charms/trusty/glance/amulet-git-fixups

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2013-09-20 15:52:45 UTC
  • mto: (29.2.187 glance)
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.page@canonical.com-20130920155245-udz175q6z2dw6f87
Renamed tests->unit_tests, updated as appropriate, re-synced charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#  Nick Moffitt <nick.moffitt@canonical.com>
6
6
#  Matthew Wedgwood <matthew.wedgwood@canonical.com>
7
7
 
8
 
import apt_pkg
9
8
import os
10
9
import pwd
11
10
import grp
20
19
 
21
20
 
22
21
def service_start(service_name):
23
 
    service('start', service_name)
 
22
    return service('start', service_name)
24
23
 
25
24
 
26
25
def service_stop(service_name):
27
 
    service('stop', service_name)
 
26
    return service('stop', service_name)
28
27
 
29
28
 
30
29
def service_restart(service_name):
31
 
    service('restart', service_name)
 
30
    return service('restart', service_name)
32
31
 
33
32
 
34
33
def service_reload(service_name, restart_on_failure=False):
35
 
    if not service('reload', service_name) and restart_on_failure:
36
 
        service('restart', service_name)
 
34
    service_result = service('reload', service_name)
 
35
    if not service_result and restart_on_failure:
 
36
        service_result = service('restart', service_name)
 
37
    return service_result
37
38
 
38
39
 
39
40
def service(action, service_name):
136
137
        target.write(content)
137
138
 
138
139
 
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
140
def mount(device, mountpoint, options=None, persist=False):
183
141
    '''Mount a filesystem'''
184
142
    cmd_args = ['mount']