예제 #1
0
 def check_host(self, hostname, result_only=False):
     command_string = '/usr/bin/sudo -u %s ansible ' % get_admin_user()
     gen_file_path = self.create_json_gen_file()
     err_msg = None
     output = None
     try:
         inventory_string = '-i ' + gen_file_path
         ping_string = ' %s %s' % (hostname, '-m ping')
         cmd = (command_string + inventory_string + ping_string)
         err_msg, output = utils.run_cmd(cmd, False)
     except Exception as e:
         raise e
     finally:
         if gen_file_path:
             os.remove(gen_file_path)
     if err_msg:
         if result_only:
             return False
         else:
             raise exceptions.CommandError(
                 'Host (%s) check failed : %s %s' %
                 (hostname, err_msg, output))
     else:
         if not result_only:
             self.log.info('Host (%s) check succeeded' % hostname)
     return True
예제 #2
0
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)
예제 #3
0
 def check_host(self, hostname, result_only=False):
     command_string = '/usr/bin/sudo -u %s ansible ' % get_admin_user()
     gen_file_path = self.create_json_gen_file()
     err_msg = None
     output = None
     try:
         inventory_string = '-i ' + gen_file_path
         ping_string = ' %s %s' % (hostname, '-m ping')
         cmd = (command_string + inventory_string + ping_string)
         err_msg, output = utils.run_cmd(cmd, False)
     except Exception as e:
         raise e
     finally:
         if gen_file_path:
             os.remove(gen_file_path)
     if err_msg:
         if result_only:
             return False
         else:
             raise exceptions.CommandError(
                 'Host (%s) check failed : %s %s'
                 % (hostname, err_msg, output))
     else:
         if not result_only:
             self.log.info('Host (%s) check succeeded' % hostname)
     return True
예제 #4
0
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)
예제 #5
0
def run_ansible_cmd(cmd, host):
    # sudo -u kolla ansible ol7-c4 -i inv_path -a "cmd"
    out = None
    user = get_admin_user()
    inventory = Inventory.load()
    inv_path = inventory.create_json_gen_file()

    acmd = ('/usr/bin/sudo -u %s ansible %s -i %s -a "%s"'
            % (user, host, inv_path, cmd))

    try:
        (out, err) = subprocess.Popen(acmd, shell=True,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE).communicate()
    except Exception as e:
        print('%s\nCannot communicate with host: %s, skipping' % (e, host))
    finally:
        os.remove(inv_path)

    if not out:
        print('Host %s is not accessible: %s, skipping' % (host, err))
    elif '>>' not in out:
        print('Ansible command: %s' % acmd)
        print('Host: %s. \nInvalid ansible return data: [%s]. skipping'
              % (host, out))
        out = None
    return out
예제 #6
0
def _get_cmd_prefix():
    editor_path = os.path.join(utils.get_kollacli_home(), 'tools',
                               PWD_EDITOR_FILENAME)
    pwd_file_path = os.path.join(utils.get_kolla_etc(), PWDS_FILENAME)
    user = utils.get_admin_user()
    prefix = '/usr/bin/sudo -u %s %s -p %s ' % (user, editor_path,
                                                pwd_file_path)
    return prefix
예제 #7
0
 def set_remote(self, remote_flag):
     self.set_var(ANSIBLE_BECOME, 'yes')
     if remote_flag:
         # set the ssh info for all the servers in the group
         self.set_var(ANSIBLE_SSH_USER, utils.get_admin_user())
         self.clear_var(ANSIBLE_CONNECTION)
     else:
         # remove ssh info, add local connection type
         self.set_var(ANSIBLE_CONNECTION, 'local')
         self.clear_var(ANSIBLE_SSH_USER)
예제 #8
0
 def set_remote(self, remote_flag):
     self.set_var(ANSIBLE_BECOME, 'yes')
     if remote_flag:
         # set the ssh info for all the servers in the group
         self.set_var(ANSIBLE_SSH_USER, utils.get_admin_user())
         self.clear_var(ANSIBLE_CONNECTION)
     else:
         # remove ssh info, add local connection type
         self.set_var(ANSIBLE_CONNECTION, 'local')
         self.clear_var(ANSIBLE_SSH_USER)
예제 #9
0
def _post_setup_checks(net_addr, log):
    try:
        ssh_client = ssh_connect(net_addr, get_admin_user(), '')

    except Exception as e:
        raise CommandError("remote login failed : %s" % e)

    try:
        # a basic test
        ssh_client.exec_command('ls')

    except Exception as e:
        raise CommandError("remote command 'ls' failed : %s" % e)

    finally:
        _close_ssh_client(ssh_client)
예제 #10
0
def _post_setup_checks(net_addr, log):
    try:
        ssh_client = ssh_connect(net_addr, get_admin_user(), '')

    except Exception as e:
        raise CommandError("remote login failed : %s" % e)

    try:
        # a basic test
        ssh_client.exec_command('ls')

    except Exception as e:
        raise CommandError("remote command 'ls' failed : %s" % e)

    finally:
        _close_ssh_client(ssh_client)
예제 #11
0
    def run(self):
        globals_string = None
        password_string = None
        inventory_path = None
        cmd = ''
        try:
            flag = ''
            # verbose levels: 1=not verbose, 2=more verbose
            if self.verbose_level > 1:
                flag = '-vvv'

            admin_user = get_admin_user()
            command_string = ('/usr/bin/sudo -u %s ansible-playbook %s'
                              % (admin_user, flag))
            inventory = Inventory.load()
            inventory_filter = {}
            if self.hosts:
                for hostname in self.hosts:
                    host = inventory.get_host(hostname)
                    if not host:
                        raise CommandError(
                            'Host (%s) not found. ' % hostname)
                inventory_filter['deploy_hosts'] = self.hosts
            elif self.groups:
                for groupname in self.groups:
                    group = inventory.get_group(groupname)
                    if not group:
                        raise CommandError(
                            'Group (%s) not found. ' % groupname)
                inventory_filter['deploy_groups'] = self.groups

            inventory_path = inventory.create_json_gen_file(inventory_filter)
            inventory_string = '-i ' + inventory_path
            cmd = (command_string + ' ' + inventory_string)

            if self.include_globals:
                globals_string = self._get_globals_path()
                cmd = (cmd + ' ' + globals_string)

            if self.include_passwords:
                password_string = self._get_password_path()
                cmd = (cmd + ' ' + password_string)

            cmd = (cmd + ' ' + self.playbook_path)

            if self.extra_vars or self.serial:
                extra_vars = ''
                if self.extra_vars:
                    extra_vars = self.extra_vars
                    if self.serial:
                        extra_vars += ' '
                if self.serial:
                    extra_vars += 'serial_var=1'

                cmd = (cmd + ' --extra-vars \"' +
                       extra_vars + '\"')

            if self.services:
                service_string = ''
                first = True
                for service in self.services:
                    valid_service = inventory.get_service(service)
                    if not valid_service:
                        raise CommandError(
                            'Service (%s) not found. ' % service)
                    if not first:
                        service_string = service_string + ','
                    else:
                        first = False
                    service_string = service_string + service
                cmd = (cmd + ' --tags ' + service_string)

            if self.flush_cache:
                cmd = (cmd + ' --flush-cache')

            if self.verbose_level > 1:
                # log the ansible command
                self.log.debug('cmd:' + cmd)

                if self.verbose_level > 2:
                    # log the inventory
                    dbg_gen = inventory_path
                    (inv, _) = \
                        subprocess.Popen(dbg_gen.split(' '),
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE).communicate()
                    self.log.debug(inv)

            err_msg, output = run_cmd(cmd, self.print_output)
            if err_msg:
                if not self.print_output:
                    # since the user didn't see the output, include it in
                    # the error message
                    err_msg = '%s %s' % (err_msg, output)
                raise CommandError(err_msg)

            self.log.info('Success')
        except CommandError as e:
            raise e
        except Exception:
            raise Exception(traceback.format_exc())
        finally:
            if inventory_path:
                os.remove(inventory_path)
예제 #12
0
    def run(self):
        globals_string = None
        password_string = None
        inventory_path = None
        cmd = ''
        try:
            flag = ''
            # verbose levels: 1=not verbose, 2=more verbose
            if self.verbose_level > 1:
                flag = '-vvv'

            admin_user = get_admin_user()
            command_string = ('/usr/bin/sudo -u %s ansible-playbook %s' %
                              (admin_user, flag))
            inventory = Inventory.load()
            inventory_filter = {}
            if self.hosts:
                for hostname in self.hosts:
                    host = inventory.get_host(hostname)
                    if not host:
                        raise CommandError('Host (%s) not found. ' % hostname)
                inventory_filter['deploy_hosts'] = self.hosts
            elif self.groups:
                for groupname in self.groups:
                    group = inventory.get_group(groupname)
                    if not group:
                        raise CommandError('Group (%s) not found. ' %
                                           groupname)
                inventory_filter['deploy_groups'] = self.groups

            inventory_path = inventory.create_json_gen_file(inventory_filter)
            inventory_string = '-i ' + inventory_path
            cmd = (command_string + ' ' + inventory_string)

            if self.include_globals:
                globals_string = self._get_globals_path()
                cmd = (cmd + ' ' + globals_string)

            if self.include_passwords:
                password_string = self._get_password_path()
                cmd = (cmd + ' ' + password_string)

            cmd = (cmd + ' ' + self.playbook_path)

            if self.extra_vars or self.serial:
                extra_vars = ''
                if self.extra_vars:
                    extra_vars = self.extra_vars
                    if self.serial:
                        extra_vars += ' '
                if self.serial:
                    extra_vars += 'serial_var=1'

                cmd = (cmd + ' --extra-vars \"' + extra_vars + '\"')

            if self.services:
                service_string = ''
                first = True
                for service in self.services:
                    valid_service = inventory.get_service(service)
                    if not valid_service:
                        raise CommandError('Service (%s) not found. ' %
                                           service)
                    if not first:
                        service_string = service_string + ','
                    else:
                        first = False
                    service_string = service_string + service
                cmd = (cmd + ' --tags ' + service_string)

            if self.flush_cache:
                cmd = (cmd + ' --flush-cache')

            if self.verbose_level > 1:
                # log the ansible command
                self.log.debug('cmd:' + cmd)

                if self.verbose_level > 2:
                    # log the inventory
                    dbg_gen = inventory_path
                    (inv, _) = \
                        subprocess.Popen(dbg_gen.split(' '),
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE).communicate()
                    self.log.debug(inv)

            err_msg, output = run_cmd(cmd, self.print_output)
            if err_msg:
                if not self.print_output:
                    # since the user didn't see the output, include it in
                    # the error message
                    err_msg = '%s %s' % (err_msg, output)
                raise CommandError(err_msg)

            self.log.info('Success')
        except CommandError as e:
            raise e
        except Exception:
            raise Exception(traceback.format_exc())
        finally:
            if inventory_path:
                os.remove(inventory_path)
예제 #13
0
def _get_cmd_prefix():
    editor_path = os.path.join(utils.get_kollacli_home(), "tools", PWD_EDITOR_FILENAME)
    pwd_file_path = os.path.join(utils.get_kolla_etc(), PWDS_FILENAME)
    user = utils.get_admin_user()
    prefix = "/usr/bin/sudo -u %s %s -p %s " % (user, editor_path, pwd_file_path)
    return prefix