~gandelman-a/charms/precise/glance/is_relation_made_fix

« back to all changes in this revision

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

  • Committer: Adam Gandelman
  • Date: 2013-11-06 04:29:05 UTC
  • mfrom: (37.2.6 glance-ceilometer)
  • Revision ID: adamg@canonical.com-20131106042905-kts4w1l4js2ocv30
[jamespage] Add support for rabbitmq use for ceilometer notifications

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
 
21
21
def service_start(service_name):
 
22
    """Start a system service"""
22
23
    return service('start', service_name)
23
24
 
24
25
 
25
26
def service_stop(service_name):
 
27
    """Stop a system service"""
26
28
    return service('stop', service_name)
27
29
 
28
30
 
29
31
def service_restart(service_name):
 
32
    """Restart a system service"""
30
33
    return service('restart', service_name)
31
34
 
32
35
 
33
36
def service_reload(service_name, restart_on_failure=False):
 
37
    """Reload a system service, optionally falling back to restart if reload fails"""
34
38
    service_result = service('reload', service_name)
35
39
    if not service_result and restart_on_failure:
36
40
        service_result = service('restart', service_name)
38
42
 
39
43
 
40
44
def service(action, service_name):
 
45
    """Control a system service"""
41
46
    cmd = ['service', service_name, action]
42
47
    return subprocess.call(cmd) == 0
43
48
 
44
49
 
45
50
def service_running(service):
 
51
    """Determine whether a system service is running"""
46
52
    try:
47
53
        output = subprocess.check_output(['service', service, 'status'])
48
54
    except subprocess.CalledProcessError:
55
61
 
56
62
 
57
63
def adduser(username, password=None, shell='/bin/bash', system_user=False):
58
 
    """Add a user"""
 
64
    """Add a user to the system"""
59
65
    try:
60
66
        user_info = pwd.getpwnam(username)
61
67
        log('user {0} already exists!'.format(username))
138
144
 
139
145
 
140
146
def mount(device, mountpoint, options=None, persist=False):
141
 
    '''Mount a filesystem'''
 
147
    """Mount a filesystem at a particular mountpoint"""
142
148
    cmd_args = ['mount']
143
149
    if options is not None:
144
150
        cmd_args.extend(['-o', options])
155
161
 
156
162
 
157
163
def umount(mountpoint, persist=False):
158
 
    '''Unmount a filesystem'''
 
164
    """Unmount a filesystem"""
159
165
    cmd_args = ['umount', mountpoint]
160
166
    try:
161
167
        subprocess.check_output(cmd_args)
169
175
 
170
176
 
171
177
def mounts():
172
 
    '''List of all mounted volumes as [[mountpoint,device],[...]]'''
 
178
    """Get a list of all mounted volumes as [[mountpoint,device],[...]]"""
173
179
    with open('/proc/mounts') as f:
174
180
        # [['/mount/point','/dev/path'],[...]]
175
181
        system_mounts = [m[1::-1] for m in [l.strip().split()
178
184
 
179
185
 
180
186
def file_hash(path):
181
 
    ''' Generate a md5 hash of the contents of 'path' or None if not found '''
 
187
    """Generate a md5 hash of the contents of 'path' or None if not found """
182
188
    if os.path.exists(path):
183
189
        h = hashlib.md5()
184
190
        with open(path, 'r') as source:
189
195
 
190
196
 
191
197
def restart_on_change(restart_map):
192
 
    ''' Restart services based on configuration files changing
 
198
    """Restart services based on configuration files changing
193
199
 
194
200
    This function is used a decorator, for example
195
201
 
202
208
    In this example, the cinder-api and cinder-volume services
203
209
    would be restarted if /etc/ceph/ceph.conf is changed by the
204
210
    ceph_client_changed function.
205
 
    '''
 
211
    """
206
212
    def wrap(f):
207
213
        def wrapped_f(*args):
208
214
            checksums = {}
220
226
 
221
227
 
222
228
def lsb_release():
223
 
    '''Return /etc/lsb-release in a dict'''
 
229
    """Return /etc/lsb-release in a dict"""
224
230
    d = {}
225
231
    with open('/etc/lsb-release', 'r') as lsb:
226
232
        for l in lsb:
230
236
 
231
237
 
232
238
def pwgen(length=None):
233
 
    '''Generate a random pasword.'''
 
239
    """Generate a random pasword."""
234
240
    if length is None:
235
241
        length = random.choice(range(35, 45))
236
242
    alphanumeric_chars = [