~sdn-charmers/charms/trusty/neutron-contrail/dpdk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host_factory/ubuntu.py

  • Committer: Robert Ayres
  • Date: 2016-10-10 20:55:32 UTC
  • Revision ID: robert.ayres@canonical.com-20161010205532-zes2f1g9e34e7arf
Sync charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import subprocess
 
2
 
 
3
 
 
4
def service_available(service_name):
 
5
    """Determine whether a system service is available"""
 
6
    try:
 
7
        subprocess.check_output(
 
8
            ['service', service_name, 'status'],
 
9
            stderr=subprocess.STDOUT).decode('UTF-8')
 
10
    except subprocess.CalledProcessError as e:
 
11
        return b'unrecognized service' not in e.output
 
12
    else:
 
13
        return True
 
14
 
 
15
 
 
16
def add_new_group(group_name, system_group=False, gid=None):
 
17
    cmd = ['addgroup']
 
18
    if gid:
 
19
        cmd.extend(['--gid', str(gid)])
 
20
    if system_group:
 
21
        cmd.append('--system')
 
22
    else:
 
23
        cmd.extend([
 
24
            '--group',
 
25
        ])
 
26
    cmd.append(group_name)
 
27
    subprocess.check_call(cmd)
 
28
 
 
29
 
 
30
def lsb_release():
 
31
    """Return /etc/lsb-release in a dict"""
 
32
    d = {}
 
33
    with open('/etc/lsb-release', 'r') as lsb:
 
34
        for l in lsb:
 
35
            k, v = l.split('=')
 
36
            d[k.strip()] = v.strip()
 
37
    return d
 
38
 
 
39
 
 
40
def cmp_pkgrevno(package, revno, pkgcache=None):
 
41
    """Compare supplied revno with the revno of the installed package.
 
42
 
 
43
    *  1 => Installed revno is greater than supplied arg
 
44
    *  0 => Installed revno is the same as supplied arg
 
45
    * -1 => Installed revno is less than supplied arg
 
46
 
 
47
    This function imports apt_cache function from charmhelpers.fetch if
 
48
    the pkgcache argument is None. Be sure to add charmhelpers.fetch if
 
49
    you call this function, or pass an apt_pkg.Cache() instance.
 
50
    """
 
51
    import apt_pkg
 
52
    if not pkgcache:
 
53
        from charmhelpers.fetch import apt_cache
 
54
        pkgcache = apt_cache()
 
55
    pkg = pkgcache[package]
 
56
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)