diff options
Diffstat (limited to 'cloudinit/config/cc_ssh_import_id.py')
-rw-r--r-- | cloudinit/config/cc_ssh_import_id.py | 56 |
1 files changed, 41 insertions, 15 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 |