From 62631d30aae55a42b77d326af75d5e476d4baf36 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 21 Sep 2012 14:15:09 -0700 Subject: 1. Cleanup the user creation so that the distro class is responsible only for creating users and groups and normalizing a input configuration into a normalized format that splits up the user list, the group list and the default user listsand let the add user/group config module handle calling those methods to add its own users/groups and the default user (if any). 2. Also add in tests for this normalization process to ensure that it is pretty bug free and works with the different types of formats that users/groups/defaults + options can take. --- doc/examples/cloud-config-user-groups.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'doc/examples') diff --git a/doc/examples/cloud-config-user-groups.txt b/doc/examples/cloud-config-user-groups.txt index 1da0d717..073fbd8f 100644 --- a/doc/examples/cloud-config-user-groups.txt +++ b/doc/examples/cloud-config-user-groups.txt @@ -81,14 +81,16 @@ users: # directive. # system: Create the user as a system user. This means no home directory. # -# Default user creation: Ubuntu Only -# Unless you define users, you will get a Ubuntu user on Ubuntu systems with the +# Default user creation: +# +# Unless you define users, you will get a 'ubuntu' user on buntu systems with the # legacy permission (no password sudo, locked user, etc). If however, you want # to have the ubuntu user in addition to other users, you need to instruct # cloud-init that you also want the default user. To do this use the following # syntax: # users: -# default: True +# - default +# - bob # foobar: ... # # users[0] (the first user in users) overrides the user directive. -- cgit v1.2.3 From 009faa0546ffbcadbbcaa9692d6842890e6f2e10 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 21 Sep 2012 15:12:11 -0700 Subject: Fix some docs + pylint warnings + log on default created in the module. --- cloudinit/config/cc_users_groups.py | 5 ++--- cloudinit/distros/__init__.py | 3 ++- doc/examples/cloud-config-user-groups.txt | 10 ++++++---- .../test_distros/test_user_data_normalize.py | 22 +++++++++++----------- 4 files changed, 21 insertions(+), 19 deletions(-) (limited to 'doc/examples') diff --git a/cloudinit/config/cc_users_groups.py b/cloudinit/config/cc_users_groups.py index 273c5068..a6ce49ac 100644 --- a/cloudinit/config/cc_users_groups.py +++ b/cloudinit/config/cc_users_groups.py @@ -24,7 +24,6 @@ frequency = PER_INSTANCE def handle(name, cfg, cloud, log, _args): - distro = cloud.distro ((users, default_user), groups) = distro.normalize_users_groups(cfg) for (name, members) in groups.items(): @@ -34,7 +33,6 @@ def handle(name, cfg, cloud, log, _args): user = default_user['name'] config = default_user['config'] def_base_config = { - 'name': user, 'plain_text_passwd': user, 'home': "/home/%s" % user, 'shell': "/bin/bash", @@ -43,7 +41,8 @@ def handle(name, cfg, cloud, log, _args): 'sudo': "ALL=(ALL) NOPASSWD:ALL", } u_config = util.mergemanydict([def_base_config, config]) - distro.create_user(**u_config) + distro.create_user(user, **u_config) + log.info("Added default '%s' user with passwordless sudo", user) for (user, config) in users.items(): distro.create_user(user, **config) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 361d2c05..3de5be36 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -400,7 +400,8 @@ class Distro(object): } else: LOG.warn(("Distro has not provided a default user " - "creation. No default user will be normalized.")) + "for creation. No default user will be " + "normalized.")) users.pop('default', None) except NotImplementedError: LOG.warn(("Distro has not implemented default user " diff --git a/doc/examples/cloud-config-user-groups.txt b/doc/examples/cloud-config-user-groups.txt index 073fbd8f..1a46c540 100644 --- a/doc/examples/cloud-config-user-groups.txt +++ b/doc/examples/cloud-config-user-groups.txt @@ -1,11 +1,11 @@ -# add groups to the system +# Add groups to the system # The following example adds the ubuntu group with members foo and bar and # the group cloud-users. groups: - ubuntu: [foo,bar] - cloud-users -# add users to the system. Users are added after groups are added. +# Add users to the system. Users are added after groups are added. users: - default - name: foobar @@ -81,16 +81,18 @@ users: # directive. # system: Create the user as a system user. This means no home directory. # + # Default user creation: # -# Unless you define users, you will get a 'ubuntu' user on buntu systems with the +# Unless you define users, you will get a 'ubuntu' user on ubuntu systems with the # legacy permission (no password sudo, locked user, etc). If however, you want -# to have the ubuntu user in addition to other users, you need to instruct +# to have the 'ubuntu' user in addition to other users, you need to instruct # cloud-init that you also want the default user. To do this use the following # syntax: # users: # - default # - bob +# - .... # foobar: ... # # users[0] (the first user in users) overrides the user directive. diff --git a/tests/unittests/test_distros/test_user_data_normalize.py b/tests/unittests/test_distros/test_user_data_normalize.py index caf479cd..d636bb84 100644 --- a/tests/unittests/test_distros/test_user_data_normalize.py +++ b/tests/unittests/test_distros/test_user_data_normalize.py @@ -78,21 +78,21 @@ class TestUGNormalize(MockerTestCase): 'default': True, } } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((_users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertEquals('bob', def_user['name']) ug_cfg = { 'users': { 'default': 'yes', } } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((_users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertEquals('bob', def_user['name']) ug_cfg = { 'users': { 'default': '1', } } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((_users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertEquals('bob', def_user['name']) def test_users_simple_dict_no(self): @@ -102,14 +102,14 @@ class TestUGNormalize(MockerTestCase): 'default': False, } } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((_users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertEquals({}, def_user) ug_cfg = { 'users': { 'default': 'no', } } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((_users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertEquals({}, def_user) def test_users_simple_csv(self): @@ -117,7 +117,7 @@ class TestUGNormalize(MockerTestCase): ug_cfg = { 'users': 'joe,bob', } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, _def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('joe', users) self.assertIn('bob', users) self.assertEquals({}, users['joe']) @@ -131,7 +131,7 @@ class TestUGNormalize(MockerTestCase): 'bob' ], } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, _def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('joe', users) self.assertIn('bob', users) self.assertEquals({}, users['joe']) @@ -144,7 +144,7 @@ class TestUGNormalize(MockerTestCase): {'name': 'default', 'blah': True} ], } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('bob', def_user['name']) self.assertEquals(",".join(distro.get_default_user_groups()), def_user['config']['groups']) @@ -159,7 +159,7 @@ class TestUGNormalize(MockerTestCase): 'default', ], } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('bob', def_user['name']) self.assertEquals(",".join(distro.get_default_user_groups()), def_user['config']['groups']) @@ -174,7 +174,7 @@ class TestUGNormalize(MockerTestCase): {'name': 'bob'}, ], } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, _def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('joe', users) self.assertIn('bob', users) self.assertEquals({'tr_me': True}, users['joe']) @@ -188,7 +188,7 @@ class TestUGNormalize(MockerTestCase): {'name': 'bob'}, ], } - ((users, def_user), groups) = distro.normalize_users_groups(ug_cfg) + ((users, _def_user), _groups) = distro.normalize_users_groups(ug_cfg) self.assertIn('joe', users) self.assertIn('bob', users) self.assertEquals({}, users['joe']) -- cgit v1.2.3