move default user info out of code and into config

Remove the need to have 'default_user' and 'default_user_groups' groups be
hard coded into the distro class, instead let that set of configuration be
located in the config file where it should be specified instead.
This commit is contained in:
Scott Moser 2012-10-23 12:13:41 +02:00
commit fa1f9bf766
6 changed files with 48 additions and 36 deletions

View File

@ -37,10 +37,7 @@ LOG = logging.getLogger(__name__)
class Distro(object):
__metaclass__ = abc.ABCMeta
default_user = None
default_user_groups = None
def __init__(self, name, cfg, paths):
self._paths = paths
@ -176,22 +173,10 @@ class Distro(object):
return False
def get_default_user(self):
if not self.default_user:
return None
user_cfg = {
'name': self.default_user,
'plain_text_passwd': self.default_user,
'home': "/home/%s" % (self.default_user),
'shell': "/bin/bash",
'lock_passwd': True,
'gecos': "%s" % (self.default_user.title()),
'sudo': "ALL=(ALL) NOPASSWD:ALL",
}
def_groups = self.default_user_groups
if not def_groups:
def_groups = []
user_cfg['groups'] = util.uniq_merge_sorted(def_groups)
return user_cfg
return self.get_option('default_user')
def get_default_user_groups(self):
return self.get_option('default_user_groups')
def create_user(self, name, **kwargs):
"""
@ -504,6 +489,7 @@ def _normalize_users(u_cfg, def_user_cfg=None):
# Pickup what the default 'real name' is
# and any groups that are provided by the
# default config
def_user_cfg = def_user_cfg.copy()
def_user = def_user_cfg.pop('name')
def_groups = def_user_cfg.pop('groups', [])
# Pickup any config + groups for that user name

View File

@ -28,4 +28,4 @@ LOG = logging.getLogger(__name__)
class Distro(rhel.Distro):
default_user = 'ec2-user'
pass

View File

@ -28,7 +28,4 @@ LOG = logging.getLogger(__name__)
class Distro(debian.Distro):
default_user = 'ubuntu'
default_user_groups = ("adm,audio,cdrom,dialout,floppy,video,"
"plugdev,dip,netdev,sudo")
pass

View File

@ -1,7 +1,9 @@
# The top level settings are used as module
# and system configuration.
# Implement for Ubuntu only: create the default 'ubuntu' user
# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
- default
@ -71,6 +73,15 @@ cloud_final_modules:
system_info:
# This will affect which distro class gets used
distro: ubuntu
# Default user name + that default users groups (if added/used)
default_user:
name: Ubuntu
plain_text_passwd: 'ubuntu'
home: /home/ubuntu
shell: /bin/bash
lock_passwd: True
gecos: Ubuntu
groups: [adm, audio, cdrom, dialout, floppy, video, plugdev, dip, netdev]
# Other config here will be given to the distro class and/or path classes
paths:
cloud_dir: /var/lib/cloud/

View File

@ -96,3 +96,14 @@ users:
# foobar: ...
#
# users[0] (the first user in users) overrides the user directive.
#
# The 'default' user above references the distro's config:
# system_info:
# default_user:
# name: Ubuntu
# plain_text_passwd: 'ubuntu'
# home: /home/ubuntu
# shell: /bin/bash
# lock_passwd: True
# gecos: Ubuntu
# groups: [adm, audio, cdrom, dialout, floppy, video, plugdev, dip, netdev]

View File

@ -4,19 +4,26 @@ from cloudinit import distros
from cloudinit import helpers
from cloudinit import settings
bcfg = {
'name': 'bob',
'plain_text_passwd': 'ubuntu',
'home': "/home/ubuntu",
'shell': "/bin/bash",
'lock_passwd': True,
'gecos': "Ubuntu",
'groups': ["foo"]
}
class TestUGNormalize(MockerTestCase):
def _make_distro(self, dtype, def_user=None, def_groups=None):
def _make_distro(self, dtype, def_user=None):
cfg = dict(settings.CFG_BUILTIN)
cfg['system_info']['distro'] = dtype
paths = helpers.Paths(cfg['system_info']['paths'])
distro_cls = distros.fetch(dtype)
distro = distro_cls(dtype, cfg['system_info'], paths)
if def_user:
distro.default_user = def_user
if def_groups:
distro.default_user_groups = def_groups
cfg['system_info']['default_user'] = def_user.copy()
distro = distro_cls(dtype, cfg['system_info'], paths)
return distro
def _norm(self, cfg, distro):
@ -71,7 +78,7 @@ class TestUGNormalize(MockerTestCase):
self.assertEquals({}, users)
def test_users_simple_dict(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'users': {
'default': True,
@ -95,7 +102,7 @@ class TestUGNormalize(MockerTestCase):
self.assertIn('bob', users)
def test_users_simple_dict_no(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'users': {
'default': False,
@ -137,7 +144,7 @@ class TestUGNormalize(MockerTestCase):
self.assertEquals({'default': False}, users['bob'])
def test_users_old_user(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'user': 'zetta',
'users': 'default'
@ -185,7 +192,7 @@ class TestUGNormalize(MockerTestCase):
self.assertEquals({}, groups)
def test_users_dict_default_additional(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'users': [
{'name': 'default', 'blah': True}
@ -201,7 +208,7 @@ class TestUGNormalize(MockerTestCase):
users['bob']['default'])
def test_users_dict_extract(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'users': [
'default',
@ -228,7 +235,7 @@ class TestUGNormalize(MockerTestCase):
self.assertEquals(config, expected_config)
def test_users_dict_default(self):
distro = self._make_distro('ubuntu', 'bob')
distro = self._make_distro('ubuntu', bcfg)
ug_cfg = {
'users': [
'default',