summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_users_groups.py
blob: 7e5ecc7b9c8221a97d90a45ef6f560dec1b1690b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# vi: ts=4 expandtab
#
#    Copyright (C) 2012 Canonical Ltd.
#
#    Author: Ben Howard <ben.howard@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import grp
import pwd
import os
import traceback

from cloudinit import templater
from cloudinit import util
from cloudinit import ssh_util
from cloudinit.settings import PER_INSTANCE

frequency = PER_INSTANCE


def handle(name, cfg, cloud, log, _args):
    groups_cfg = None
    users_cfg = None
    user_zero = None

    if 'groups' in cfg:
        for group in cfg['groups']:
            if isinstance(group, dict):
                for name, values in group.iteritems():
                    if isinstance(values, list):
                        cloud.distro.create_group(name, values)
                    elif isinstance(values, str):
                        cloud.distro.create_group(name, values.split(','))
            else:
                cloud.distro.create_group(item, [])

    if 'users' in  cfg:
        user_zero = None

        for name, user_config in cfg['users'].iteritems():
            if not user_zero:
                user_zero = name

            # Handle the default user creation
            if name == "default" and user_config:
                log.info("Creating default user")

                # Create the default user if so defined
                try:
                    cloud.distro.add_default_user()

                    if user_zero == name:
                        user_zero = cloud.distro.get_default_user()

                except NotImplementedError as e:

                    if user_zero == name:
                        user_zero = None

                    log.warn("Distro has not implemented default user " \
                               "creation. No default user will be created")
            else:
                # Make options friendly for distro.create_user
                new_opts = {}
                if isinstance(user_config, dict):
                    for opt in user_config:
                        new_opts[opt.replace('-', '')] = user_config[opt]

                cloud.distro.create_user(name, **new_opts)