~openstack-charmers/charms/precise/ceilometer/old-1410

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2014-08-13 14:14:12 UTC
  • mfrom: (50.1.1 ceilometer)
  • Revision ID: liam.young@canonical.com-20140813141412-1aka30eps05ypyzj
[gnuoy, rs=jamespage] Sync charm-helpers from lp:charm-helpers

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 shutil
 
16
from contextlib import contextmanager
15
17
 
16
18
from collections import OrderedDict
17
19
 
52
54
def service_running(service):
53
55
    """Determine whether a system service is running"""
54
56
    try:
55
 
        output = subprocess.check_output(['service', service, 'status'])
 
57
        output = subprocess.check_output(['service', service, 'status'], stderr=subprocess.STDOUT)
56
58
    except subprocess.CalledProcessError:
57
59
        return False
58
60
    else:
62
64
            return False
63
65
 
64
66
 
 
67
def service_available(service_name):
 
68
    """Determine whether a system service is available"""
 
69
    try:
 
70
        subprocess.check_output(['service', service_name, 'status'], stderr=subprocess.STDOUT)
 
71
    except subprocess.CalledProcessError:
 
72
        return False
 
73
    else:
 
74
        return True
 
75
 
 
76
 
65
77
def adduser(username, password=None, shell='/bin/bash', system_user=False):
66
78
    """Add a user to the system"""
67
79
    try:
329
341
        pkgcache = apt_pkg.Cache()
330
342
    pkg = pkgcache[package]
331
343
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)
 
344
 
 
345
 
 
346
@contextmanager
 
347
def chdir(d):
 
348
    cur = os.getcwd()
 
349
    try:
 
350
        yield os.chdir(d)
 
351
    finally:
 
352
        os.chdir(cur)
 
353
 
 
354
 
 
355
def chownr(path, owner, group):
 
356
    uid = pwd.getpwnam(owner).pw_uid
 
357
    gid = grp.getgrnam(group).gr_gid
 
358
 
 
359
    for root, dirs, files in os.walk(path):
 
360
        for name in dirs + files:
 
361
            full = os.path.join(root, name)
 
362
            broken_symlink = os.path.lexists(full) and not os.path.exists(full)
 
363
            if not broken_symlink:
 
364
                os.chown(full, uid, gid)