diff options
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/config/cc_ssh_import_id.py | 56 | ||||
-rw-r--r-- | cloudinit/distros/ubuntu.py | 18 |
2 files changed, 41 insertions, 33 deletions
diff --git a/cloudinit/config/cc_ssh_import_id.py b/cloudinit/config/cc_ssh_import_id.py index cd97b99b..1625c8e0 100644 --- a/cloudinit/config/cc_ssh_import_id.py +++ b/cloudinit/config/cc_ssh_import_id.py @@ -19,42 +19,68 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from cloudinit import util +import pwd # The ssh-import-id only seems to exist on ubuntu (for now) # https://launchpad.net/ssh-import-id distros = ['ubuntu'] -def handle(name, cfg, cloud, log, args): +def handle(_name, cfg, cloud, log, args): + + # import for "user: XXXXX" if len(args) != 0: user = args[0] ids = [] if len(args) > 1: ids = args[1:] - else: - user = cloud.distro.get_default_user() - if 'users' in cfg: - user_zero = cfg['users'].keys()[0] + import_ssh_ids(ids, user, log) + return + + # import for cloudinit created users + elist = [] + for user in cfg['users'].keys(): + if user == "default": + user = cloud.distro.get_default_user() + if not user: + continue + import_ids = util.get_cfg_option_list(cfg, "ssh_import_id", []) + else: + if not isinstance(cfg['users'][user], dict): + log.debug("cfg['users'][%s] not a dict, skipping ssh_import", + user) + import_ids = util.get_cfg_option_list(cfg['users'][user], + "ssh_import_id", []) - if user_zero != "default": - user = user_zero + if not len(import_ids): + continue - ids = util.get_cfg_option_list(cfg, "ssh_import_id", []) + try: + import_ssh_ids(ids, user, log) + except Exception as exc: + util.logexc(exc, "ssh-import-id failed for: %s %s" % (user, ids)) + elist.append(exc) - if len(ids) == 0: - log.debug("Skipping module named %s, no ids found to import", name) - return + if len(elist): + raise elist[0] - if not user: - log.debug("Skipping module named %s, no user found to import", name) + +def import_ssh_ids(ids, user, log): + if not (user and ids): + log.debug("empty user(%s) or ids(%s). not importing", user, ids) return + try: + _check = pwd.getpwnam(user) + except KeyError as exc: + raise exc + cmd = ["sudo", "-Hu", user, "ssh-import-id"] + ids log.debug("Importing ssh ids for user %s.", user) try: util.subp(cmd, capture=False) - except util.ProcessExecutionError as e: + except util.ProcessExecutionError as exc: util.logexc(log, "Failed to run command to import %s ssh ids", user) - raise e + raise exc diff --git a/cloudinit/distros/ubuntu.py b/cloudinit/distros/ubuntu.py index 4b3f8572..cb93f971 100644 --- a/cloudinit/distros/ubuntu.py +++ b/cloudinit/distros/ubuntu.py @@ -32,21 +32,3 @@ class Distro(debian.Distro): distro_name = 'ubuntu' default_user = 'ubuntu' - - def create_user(self, name, **kargs): - - if not super(Distro, self).create_user(name, **kargs): - return False - - if 'sshimportid' in kargs: - cmd = ["sudo", "-Hu", name, "ssh-import-id"] + kargs['sshimportid'] - LOG.debug("Importing ssh ids for user %s, post user creation." - % name) - - try: - util.subp(cmd, capture=True) - except util.ProcessExecutionError as e: - util.logexc(LOG, "Failed to import %s ssh ids", name) - raise e - - return True |