~openstack-charmers-next/charms/wily/nova-cloud-controller/trunk

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2016-06-17 10:52:35 UTC
  • Revision ID: james.page@ubuntu.com-20160617105235-vs2clgt11766yyjv
Switch to using charm-store for amulet tests

All OpenStack charms are now directly published to the charm store
on landing; switch Amulet helper to resolve charms using the
charm store rather than bzr branches, removing the lag between
charm changes landing and being available for other charms to
use for testing.

This is also important for new layered charms where the charm must
be build and published prior to being consumable.

Change-Id: Ia4cbe14575851c0f54cbb5845ffd6b8669fb7fa2

Show diffs side-by-side

added added

removed removed

Lines of Context:
176
176
 
177
177
 
178
178
def adduser(username, password=None, shell='/bin/bash', system_user=False,
179
 
            primary_group=None, secondary_groups=None):
 
179
            primary_group=None, secondary_groups=None, uid=None):
180
180
    """Add a user to the system.
181
181
 
182
182
    Will log but otherwise succeed if the user already exists.
187
187
    :param bool system_user: Whether to create a login or system user
188
188
    :param str primary_group: Primary group for user; defaults to username
189
189
    :param list secondary_groups: Optional list of additional groups
 
190
    :param int uid: UID for user being created
190
191
 
191
192
    :returns: The password database entry struct, as returned by `pwd.getpwnam`
192
193
    """
193
194
    try:
194
195
        user_info = pwd.getpwnam(username)
195
196
        log('user {0} already exists!'.format(username))
 
197
        if uid:
 
198
            user_info = pwd.getpwuid(int(uid))
 
199
            log('user with uid {0} already exists!'.format(uid))
196
200
    except KeyError:
197
201
        log('creating user {0}'.format(username))
198
202
        cmd = ['useradd']
 
203
        if uid:
 
204
            cmd.extend(['--uid', str(uid)])
199
205
        if system_user or password is None:
200
206
            cmd.append('--system')
201
207
        else:
230
236
    return user_exists
231
237
 
232
238
 
233
 
def add_group(group_name, system_group=False):
234
 
    """Add a group to the system"""
 
239
def uid_exists(uid):
 
240
    """Check if a uid exists"""
 
241
    try:
 
242
        pwd.getpwuid(uid)
 
243
        uid_exists = True
 
244
    except KeyError:
 
245
        uid_exists = False
 
246
    return uid_exists
 
247
 
 
248
 
 
249
def group_exists(groupname):
 
250
    """Check if a group exists"""
 
251
    try:
 
252
        grp.getgrnam(groupname)
 
253
        group_exists = True
 
254
    except KeyError:
 
255
        group_exists = False
 
256
    return group_exists
 
257
 
 
258
 
 
259
def gid_exists(gid):
 
260
    """Check if a gid exists"""
 
261
    try:
 
262
        grp.getgrgid(gid)
 
263
        gid_exists = True
 
264
    except KeyError:
 
265
        gid_exists = False
 
266
    return gid_exists
 
267
 
 
268
 
 
269
def add_group(group_name, system_group=False, gid=None):
 
270
    """Add a group to the system
 
271
 
 
272
    Will log but otherwise succeed if the group already exists.
 
273
 
 
274
    :param str group_name: group to create
 
275
    :param bool system_group: Create system group
 
276
    :param int gid: GID for user being created
 
277
 
 
278
    :returns: The password database entry struct, as returned by `grp.getgrnam`
 
279
    """
235
280
    try:
236
281
        group_info = grp.getgrnam(group_name)
237
282
        log('group {0} already exists!'.format(group_name))
 
283
        if gid:
 
284
            group_info = grp.getgrgid(gid)
 
285
            log('group with gid {0} already exists!'.format(gid))
238
286
    except KeyError:
239
287
        log('creating group {0}'.format(group_name))
240
288
        cmd = ['addgroup']
 
289
        if gid:
 
290
            cmd.extend(['--gid', str(gid)])
241
291
        if system_group:
242
292
            cmd.append('--system')
243
293
        else: