~james-page/charms/trusty/neutron-openvswitch/optmize-headers

« back to all changes in this revision

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

  • Committer: Corey Bryant
  • Date: 2016-01-04 21:29:08 UTC
  • Revision ID: corey.bryant@canonical.com-20160104212908-0pcanpyjy7pcycbq
[corey.bryant,r=trivial] Sync charm-helpers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
    """Pause a system service.
68
68
 
69
69
    Stop it, and prevent it from starting again at boot."""
70
 
    stopped = service_stop(service_name)
 
70
    stopped = True
 
71
    if service_running(service_name):
 
72
        stopped = service_stop(service_name)
71
73
    upstart_file = os.path.join(init_dir, "{}.conf".format(service_name))
72
74
    sysv_file = os.path.join(initd_dir, service_name)
73
75
    if os.path.exists(upstart_file):
105
107
            "Unable to detect {0} as either Upstart {1} or SysV {2}".format(
106
108
                service_name, upstart_file, sysv_file))
107
109
 
108
 
    started = service_start(service_name)
 
110
    started = service_running(service_name)
 
111
    if not started:
 
112
        started = service_start(service_name)
109
113
    return started
110
114
 
111
115
 
142
146
        return True
143
147
 
144
148
 
145
 
def adduser(username, password=None, shell='/bin/bash', system_user=False):
146
 
    """Add a user to the system"""
 
149
def adduser(username, password=None, shell='/bin/bash', system_user=False,
 
150
            primary_group=None, secondary_groups=None):
 
151
    """
 
152
    Add a user to the system.
 
153
 
 
154
    Will log but otherwise succeed if the user already exists.
 
155
 
 
156
    :param str username: Username to create
 
157
    :param str password: Password for user; if ``None``, create a system user
 
158
    :param str shell: The default shell for the user
 
159
    :param bool system_user: Whether to create a login or system user
 
160
    :param str primary_group: Primary group for user; defaults to their username
 
161
    :param list secondary_groups: Optional list of additional groups
 
162
 
 
163
    :returns: The password database entry struct, as returned by `pwd.getpwnam`
 
164
    """
147
165
    try:
148
166
        user_info = pwd.getpwnam(username)
149
167
        log('user {0} already exists!'.format(username))
158
176
                '--shell', shell,
159
177
                '--password', password,
160
178
            ])
 
179
        if not primary_group:
 
180
            try:
 
181
                grp.getgrnam(username)
 
182
                primary_group = username  # avoid "group exists" error
 
183
            except KeyError:
 
184
                pass
 
185
        if primary_group:
 
186
            cmd.extend(['-g', primary_group])
 
187
        if secondary_groups:
 
188
            cmd.extend(['-G', ','.join(secondary_groups)])
161
189
        cmd.append(username)
162
190
        subprocess.check_call(cmd)
163
191
        user_info = pwd.getpwnam(username)
566
594
        os.chdir(cur)
567
595
 
568
596
 
569
 
def chownr(path, owner, group, follow_links=True):
 
597
def chownr(path, owner, group, follow_links=True, chowntopdir=False):
 
598
    """
 
599
    Recursively change user and group ownership of files and directories
 
600
    in given path. Doesn't chown path itself by default, only its children.
 
601
 
 
602
    :param bool follow_links: Also Chown links if True
 
603
    :param bool chowntopdir: Also chown path itself if True
 
604
    """
570
605
    uid = pwd.getpwnam(owner).pw_uid
571
606
    gid = grp.getgrnam(group).gr_gid
572
607
    if follow_links:
574
609
    else:
575
610
        chown = os.lchown
576
611
 
 
612
    if chowntopdir:
 
613
        broken_symlink = os.path.lexists(path) and not os.path.exists(path)
 
614
        if not broken_symlink:
 
615
            chown(path, uid, gid)
577
616
    for root, dirs, files in os.walk(path):
578
617
        for name in dirs + files:
579
618
            full = os.path.join(root, name)
584
623
 
585
624
def lchownr(path, owner, group):
586
625
    chownr(path, owner, group, follow_links=False)
 
626
 
 
627
 
 
628
def get_total_ram():
 
629
    '''The total amount of system RAM in bytes.
 
630
 
 
631
    This is what is reported by the OS, and may be overcommitted when
 
632
    there are multiple containers hosted on the same machine.
 
633
    '''
 
634
    with open('/proc/meminfo', 'r') as f:
 
635
        for line in f.readlines():
 
636
            if line:
 
637
                key, value, unit = line.split()
 
638
                if key == 'MemTotal:':
 
639
                    assert unit == 'kB', 'Unknown unit'
 
640
                    return int(value) * 1024  # Classic, not KiB.
 
641
        raise NotImplementedError()