diff options
author | Scott Moser <smoser@ubuntu.com> | 2016-03-09 20:47:27 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2016-03-09 20:47:27 -0500 |
commit | 0865e5179f2a803f727e83b5e36681613e63fe8a (patch) | |
tree | 3c593556155c5b1f199e22a525bd94b6f66efe71 /cloudinit/distros | |
parent | 93e553d64baf6f7e9b135b86f822c4af8bd192d0 (diff) | |
parent | 075fdbfcc567de07fcf54566872d812c8573efb9 (diff) | |
download | vyos-cloud-init-0865e5179f2a803f727e83b5e36681613e63fe8a.tar.gz vyos-cloud-init-0865e5179f2a803f727e83b5e36681613e63fe8a.zip |
add default user to 'lxd' group and create groups when necessary.
This add 'lxd' to the list of groups that the default user is added to.
It also changes behavior to create any necessary groups that are listed
for the user rather than failing to add the user.
Theres also a fix for usage of logexc that I found along the way.
LP: #1539317
Diffstat (limited to 'cloudinit/distros')
-rw-r--r-- | cloudinit/distros/__init__.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index a73acae5..e8220985 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -319,6 +319,11 @@ class Distro(object): LOG.info("User %s already exists, skipping." % name) return + if 'create_groups' in kwargs: + create_groups = kwargs.pop('create_groups') + else: + create_groups = True + adduser_cmd = ['useradd', name] log_adduser_cmd = ['useradd', name] @@ -346,6 +351,19 @@ class Distro(object): redact_opts = ['passwd'] + groups = kwargs.get('groups') + if groups: + if isinstance(groups, (list, tuple)): + kwargs['groups'] = ",".join(groups) + else: + groups = groups.split(",") + + if create_groups: + for group in kwargs.get('groups').split(","): + if not util.is_group(group): + self.create_group(group) + LOG.debug("created group %s for user %s", name, group) + # Check the values and create the command for key, val in kwargs.items(): @@ -534,8 +552,10 @@ class Distro(object): util.logexc(LOG, "Failed to append sudoers file %s", sudo_file) raise e - def create_group(self, name, members): + def create_group(self, name, members=None): group_add_cmd = ['groupadd', name] + if not members: + members = [] # Check if group exists, and then add it doesn't if util.is_group(name): @@ -545,7 +565,7 @@ class Distro(object): util.subp(group_add_cmd) LOG.info("Created new group %s" % name) except Exception: - util.logexc("Failed to create group %s", name) + util.logexc(LOG, "Failed to create group %s", name) # Add members to the group, if so defined if len(members) > 0: |