def ssh_setup_host(net_addr, password, setup_user=None): log = logging.getLogger(__name__) admin_user = get_admin_user() if setup_user is None: setup_user = get_setup_user() public_key = ssh_get_public_key() ssh_client = None try: ssh_client = ssh_connect(net_addr, setup_user, password) # before modifying the host, check that it meets requirements # TODO(bmace) pre / post checks should be done with ansible # populate authorized keys file w/ public key key_dir = os.path.join(os.path.expanduser('~kolla'), '.ssh', 'authorized_keys') cmd = ('/usr/bin/sudo su - %s -c "echo \'%s\' >> %s"' % (admin_user, public_key, key_dir)) _exec_ssh_cmd(cmd, ssh_client, log) # TODO(bmace) verify ssh connection to the new account except Exception as e: raise e finally: _close_ssh_client(ssh_client)
def take_action(self, parsed_args): try: if not parsed_args.hostname and not parsed_args.file: raise CommandError('Hostname or hosts info file path ' + 'is required') if parsed_args.hostname and parsed_args.file: raise CommandError('Hostname and hosts info file path ' + 'cannot both be present') inventory = Inventory.load() if parsed_args.file: # multi-host setup via xml file hosts_data = self.get_yml_data(parsed_args.file.strip()) inventory.setup_hosts(hosts_data) else: # single host setup hostname = parsed_args.hostname.strip() hostname = utils.convert_to_unicode(hostname) if not inventory.get_host(hostname): _host_not_found(self.log, hostname) check_ok = inventory.check_host(hostname, True) if check_ok: self.log.info( 'Skipping setup of host (%s) as check is ok' % hostname) return True if parsed_args.insecure: password = parsed_args.insecure.strip() else: setup_user = get_setup_user() password = getpass.getpass('%s password for %s: ' % (setup_user, hostname)) password = utils.convert_to_unicode(password) inventory.setup_host(hostname, password) except CommandError as e: raise e except Exception as e: raise Exception(traceback.format_exc())