~brad-marshall/charms/trusty/ntp/remove-ntp-status-check

« back to all changes in this revision

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

  • Committer: Matt Bruzek
  • Date: 2014-09-11 20:46:39 UTC
  • mfrom: (16.1.1 ntp)
  • Revision ID: matthew.bruzek@canonical.com-20140911204639-hi63o5fwwbr0umwv
[rcj] Add missing jinja templating module.

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:
320
332
 
321
333
    '''
322
334
    import apt_pkg
 
335
    from charmhelpers.fetch import apt_cache
323
336
    if not pkgcache:
324
 
        apt_pkg.init()
325
 
        # Force Apt to build its cache in memory. That way we avoid race
326
 
        # conditions with other applications building the cache in the same
327
 
        # place.
328
 
        apt_pkg.config.set("Dir::Cache::pkgcache", "")
329
 
        pkgcache = apt_pkg.Cache()
 
337
        pkgcache = apt_cache()
330
338
    pkg = pkgcache[package]
331
339
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)
 
340
 
 
341
 
 
342
@contextmanager
 
343
def chdir(d):
 
344
    cur = os.getcwd()
 
345
    try:
 
346
        yield os.chdir(d)
 
347
    finally:
 
348
        os.chdir(cur)
 
349
 
 
350
 
 
351
def chownr(path, owner, group):
 
352
    uid = pwd.getpwnam(owner).pw_uid
 
353
    gid = grp.getgrnam(group).gr_gid
 
354
 
 
355
    for root, dirs, files in os.walk(path):
 
356
        for name in dirs + files:
 
357
            full = os.path.join(root, name)
 
358
            broken_symlink = os.path.lexists(full) and not os.path.exists(full)
 
359
            if not broken_symlink:
 
360
                os.chown(full, uid, gid)