~james-page/charm-helpers/amulet-switch-to-cs

« back to all changes in this revision

Viewing changes to charmhelpers/core/host.py

  • Committer: Matthew Bruzek
  • Date: 2016-06-02 16:00:32 UTC
  • mfrom: (579.1.5 charm-helpers)
  • Revision ID: matthew.bruzek@canonical.com-20160602160032-ddea8ewi8aj2fazk
[jamesbeedy] Added uid and gid specification functionality for adduser and addgroup.
[mbruzek] Added a newline to fix lint.

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: