~billy-olsen/charms/trusty/ceph-radosgw/lp1500635

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2014-10-06 22:16:05 UTC
  • mfrom: (26.1.5 ceph-radosgw)
  • Revision ID: james.page@ubuntu.com-20141006221605-z9wo0l6kj02s4onu
[coreycb,r=james-page] Add amulet tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
    """Determine whether a system service is available"""
69
69
    try:
70
70
        subprocess.check_output(['service', service_name, 'status'], stderr=subprocess.STDOUT)
71
 
    except subprocess.CalledProcessError:
72
 
        return False
 
71
    except subprocess.CalledProcessError as e:
 
72
        return 'unrecognized service' not in e.output
73
73
    else:
74
74
        return True
75
75
 
209
209
    return system_mounts
210
210
 
211
211
 
212
 
def file_hash(path):
213
 
    """Generate a md5 hash of the contents of 'path' or None if not found """
 
212
def file_hash(path, hash_type='md5'):
 
213
    """
 
214
    Generate a hash checksum of the contents of 'path' or None if not found.
 
215
 
 
216
    :param str hash_type: Any hash alrgorithm supported by :mod:`hashlib`,
 
217
                          such as md5, sha1, sha256, sha512, etc.
 
218
    """
214
219
    if os.path.exists(path):
215
 
        h = hashlib.md5()
 
220
        h = getattr(hashlib, hash_type)()
216
221
        with open(path, 'r') as source:
217
222
            h.update(source.read())  # IGNORE:E1101 - it does have update
218
223
        return h.hexdigest()
220
225
        return None
221
226
 
222
227
 
 
228
def check_hash(path, checksum, hash_type='md5'):
 
229
    """
 
230
    Validate a file using a cryptographic checksum.
 
231
 
 
232
    :param str checksum: Value of the checksum used to validate the file.
 
233
    :param str hash_type: Hash algorithm used to generate `checksum`.
 
234
        Can be any hash alrgorithm supported by :mod:`hashlib`,
 
235
        such as md5, sha1, sha256, sha512, etc.
 
236
    :raises ChecksumError: If the file fails the checksum
 
237
 
 
238
    """
 
239
    actual_checksum = file_hash(path, hash_type)
 
240
    if checksum != actual_checksum:
 
241
        raise ChecksumError("'%s' != '%s'" % (checksum, actual_checksum))
 
242
 
 
243
 
 
244
class ChecksumError(ValueError):
 
245
    pass
 
246
 
 
247
 
223
248
def restart_on_change(restart_map, stopstart=False):
224
249
    """Restart services based on configuration files changing
225
250