Beispiel #1
0
    def take_action(self, parsed_args):
        try:
            hostname = parsed_args.hostname.strip()

            hostnames = [hostname]
            if hostname == 'all':
                hostnames = _get_all_hostnames()
                # if there are no hosts, don't bother doing anything
                if not hostnames:
                    return

            destroy_type = 'kill'
            if parsed_args.stop:
                destroy_type = 'stop'
            include_data = False
            if parsed_args.includedata:
                include_data = True
            remove_images = False
            if parsed_args.removeimages:
                remove_images = True

            if include_data and not self._is_ok_to_delete_data():
                LOG.info('Aborting destroy')
                return

            verbose_level = self.app.options.verbose_level

            job = CLIENT.host_destroy(hostnames, destroy_type,
                                      verbose_level, include_data,
                                      remove_images)
            status = job.wait()
            if verbose_level > 2:
                LOG.info('\n\n' + 80 * '=')
                LOG.info(u._('DEBUG command output:\n{out}')
                         .format(out=job.get_console_output()))
            if status != 0:
                raise CommandError(u._('Job failed:\n{msg}')
                                   .format(msg=job.get_error_message()))
            elif verbose_level > 1:
                # log any ansible warnings
                msg = job.get_error_message()
                if msg:
                    LOG.warn(msg)

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #2
0
    def take_action(self, parsed_args):
        try:
            property_name = parsed_args.propertyname.strip()
            property_value = parsed_args.propertyvalue.strip()
            property_dict = {}
            property_dict[property_name] = property_value

            if parsed_args.hosts:
                if parsed_args.groups:
                    raise CommandError(
                        u._('Invalid to use both hosts and groups arguments '
                            'together.'))

                host_names = _get_names(parsed_args.hosts)

                CLIENT.property_set(property_dict, 'host', host_names)

            elif parsed_args.groups:
                group_names = _get_names(parsed_args.groups)

                CLIENT.property_set(property_dict, 'group', group_names)
            else:
                CLIENT.property_set(property_dict, 'global')

        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #3
0
    def take_action(self, parsed_args):
        try:
            hostname = None
            if parsed_args.hostname:
                hostname = parsed_args.hostname.strip()

            hosts = []
            if hostname:
                hosts = CLIENT.host_get([hostname])
            else:
                hosts = CLIENT.host_get_all()

            data = []
            if hosts:
                for host in hosts:
                    data.append((host.name, host.get_groups()))
            else:
                data.append(('', ''))

            data = convert_lists_to_string(data, parsed_args)
            return ((u._('Host'), u._('Groups')), sorted(data))

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #4
0
    def take_action(self, parsed_args):
        try:
            if parsed_args.all:
                self.is_all_flag = True
            if parsed_args.long:
                self.is_long_flag = True

            if parsed_args.hosts:
                if parsed_args.groups:
                    raise CommandError(
                        u._('Invalid to use both hosts and groups arguments '
                            'together.'))

                self.is_global = False
                self.list_type = u._('Host')
                host_names = _get_names(parsed_args.hosts)

                property_list = CLIENT.property_get('host', host_names)

            elif parsed_args.groups:
                self.is_global = False
                self.list_type = u._('Group')
                group_names = _get_names(parsed_args.groups)
                property_list = CLIENT.property_get('group', group_names)

            else:
                property_list = CLIENT.property_get('global')

            data = self._get_list_data(property_list)
            header = self._get_list_header()
            return (header, data)

        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #5
0
    def take_action(self, parsed_args):
        try:
            hostname = parsed_args.hostname.strip()
            CLIENT.host_add([hostname])

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #6
0
    def take_action(self, parsed_args):
        try:
            groupname = parsed_args.groupname.strip()

            CLIENT.group_remove([groupname])
        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #7
0
    def take_action(self, parsed_args):
        try:
            hostname = parsed_args.hostname.strip()
            hostnames = [hostname]
            if hostname == 'all':
                hostnames = _get_all_hostnames()

            if parsed_args.predeploy:
                # run pre-deploy checks
                verbose_level = self.app.options.verbose_level
                job = CLIENT.host_precheck(hostnames, verbose_level)
                status = job.wait()
                if verbose_level > 2:
                    LOG.info('\n\n' + 80 * '=')
                    LOG.info(u._('DEBUG command output:\n{out}')
                             .format(out=job.get_console_output()))
                if status != 0:
                    raise CommandError(u._('Job failed:\n{msg}')
                                       .format(msg=job.get_error_message()))
                elif verbose_level > 1:
                    # log any ansible warnings
                    msg = job.get_error_message()
                    if msg:
                        LOG.warn(msg)
            else:
                # just do an ssh check
                summary = CLIENT.host_ssh_check(hostnames)
                all_ok = True
                for hostname, info in summary.items():
                    status = u._('success')
                    msg = ''
                    if not info['success']:
                        status = u._('failed-')
                        msg = info['msg']
                        all_ok = False
                    LOG.info(u._('Host {host}: {sts} {msg}')
                             .format(host=hostname, sts=status, msg=msg))

                if not all_ok:
                    raise CommandError(u._('Host check failed.'))
        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #8
0
    def take_action(self, parsed_args):
        try:
            if not parsed_args.hostname and not parsed_args.file:
                raise CommandError(
                    u._('Host name or hosts info file path is required.'))
            if parsed_args.hostname and parsed_args.file:
                raise CommandError(
                    u._('Host name and hosts info file path '
                        'cannot both be present.'))

            if parsed_args.file:
                # multi-host setup via xml file
                # The xml file's content is like a dict of format:
                # hostname1:
                #     uname: user1
                #     password: <password1>
                # hostname2:
                #     uname: user2
                #     password: <password2>
                hosts_data = self._get_yml_data(parsed_args.file.strip())
                CLIENT.host_setup(hosts_data)
            else:
                # single host setup
                hostname = parsed_args.hostname.strip()
                summary = CLIENT.host_ssh_check([hostname])
                if summary[hostname]['success']:
                    LOG.info(
                        u._LI('Skipping setup of host ({host}) as '
                              'ssh check is ok.').format(host=hostname))
                    return 0

                if parsed_args.insecure:
                    password = parsed_args.insecure.strip()
                else:
                    password = getpass.getpass(
                        u._('{name} password for {host}: ')
                        .format(name=get_setup_user(), host=hostname))
                CLIENT.host_setup({hostname: {'password': password}})

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #9
0
    def take_action(self, parsed_args):
        try:
            groupname = parsed_args.groupname.strip()
            servicename = parsed_args.servicename.strip()

            group = CLIENT.group_get([groupname])[0]
            group.remove_service(servicename)

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #10
0
    def take_action(self, parsed_args):
        try:
            hostname = parsed_args.hostname.strip()
            hostnames = [hostname]
            if hostname == 'all':
                hostnames = _get_all_hostnames()
            CLIENT.host_remove(hostnames)

        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #11
0
 def take_action(self, parsed_args):
     try:
         data = [('',)]
         groups = CLIENT.group_get_all()
         if groups:
             data = []
             for group in groups:
                 data.append((group.get_name(),))
         data = convert_lists_to_string(data, parsed_args)
         return ((u._('Group'), ), sorted(data))
     except ClientException as e:
         raise CommandError(str(e))
     except Exception as e:
         raise Exception(traceback.format_exc())
Beispiel #12
0
    def take_action(self, parsed_args):
        try:
            legal_types = ['inventory']
            import_type = parsed_args.import_type
            if not import_type or import_type not in legal_types:
                raise CommandError(
                    u._('Import type must be {type}.').format(
                        type=legal_types))

            file_path = None
            if parsed_args.file_path:
                file_path = parsed_args.file_path.strip()
            CLIENT.config_import_inventory(file_path)
        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #13
0
def handers_action_result(job, status, verbose_level):
    if verbose_level > 2:
        LOG.info('\n\n' + 80 * '=')
        LOG.info(
            u._('DEBUG command output:\n{out}').format(
                out=job.get_console_output()))
    if status == 0:
        if verbose_level > 1:
            # log any ansible warnings
            msg = job.get_error_message()
            if msg:
                LOG.warn(msg)
        LOG.info(u._('Success'))
    else:
        raise CommandError(
            u._('Job failed:\n{msg}').format(msg=job.get_error_message()))
Beispiel #14
0
    def take_action(self, parsed_args):
        try:
            data = [('', '')]
            services = CLIENT.service_get_all()
            if services:
                data = []
                for service in services:
                    groupnames = sorted(service.get_groups())
                    data.append((service.name, groupnames))

            data = convert_lists_to_string(data, parsed_args)
            return (u._('Service'), u._('Groups')), sorted(data)
        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #15
0
    def take_action(self, parsed_args):
        try:
            verbose_level = self.app.options.verbose_level
            job = CLIENT.reconfigure(verbose_level)
            status = job.wait()
            if verbose_level > 2:
                LOG.info('\n\n' + 80 * '=')
                LOG.info(u._('DEBUG command output:\n{out}')
                         .format(out=job.get_console_output()))
            if status == 0:
                LOG.info(u._('Success'))
            else:
                raise CommandError(u._('Job failed:\n{msg}')
                                   .format(msg=job.get_error_message()))

        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #16
0
    def take_action(self, parsed_args):
        try:
            password_name = parsed_args.passwordname.strip()
            if parsed_args.insecure is not False:
                # --insecure flag is present
                password = ''  # nosec
                if parsed_args.insecure:
                    password = parsed_args.insecure.strip()
            else:
                password = getpass.getpass(u._('Password: '******'Retype Password: '******'Passwords do not match'))

            CLIENT.password_set(password_name, password)

        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #17
0
 def take_action(self, parsed_args):
     try:
         mode = parsed_args.mode.strip()
         remote_flag = True
         if mode == 'local':
             remote_flag = False
             LOG.info(
                 u._('Please note that local mode is not supported '
                     'and should never be used in production '
                     'environments.'))
         elif mode != 'remote':
             raise CommandError(
                 u._('Invalid deploy mode. Mode must be '
                     'either "local" or "remote".'))
         CLIENT.set_deploy_mode(remote_flag)
     except CommandError as e:
         raise e
     except Exception:
         raise Exception(traceback.format_exc())
Beispiel #18
0
    def take_action(self, parsed_args):
        services = None
        try:
            if parsed_args.services:
                service_list = parsed_args.services.strip()
                services = service_list.split(',')
            verbose_level = self.app.options.verbose_level
            job = CLIENT.upgrade(verbose_level, services)
            status = job.wait()
            if verbose_level > 2:
                LOG.info('\n\n' + 80 * '=')
                LOG.info(
                    u._('DEBUG command output:\n{out}').format(
                        out=job.get_console_output()))
            if status == 0:
                LOG.info(u._('Success'))
            else:
                raise CommandError(
                    u._('Job failed:\n{msg}').format(
                        msg=job.get_error_message()))

        except Exception:
            raise Exception(traceback.format_exc())
Beispiel #19
0
    def __init__(self):
        super(KollaCli, self).__init__(
            description=u._('Command-Line Client for OpenStack Kolla'),
            version=VERSION,
            command_manager=CommandManager('kolla.cli'),
        )

        inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH)
        if not self._is_inventory_present(inventory_path):
            err_string = u._(
                'Required file ({inventory}) does not exist.\n'
                'Please re-install the kollacli to '
                'recreate the file.').format(inventory=inventory_path)
            raise CommandError(err_string)

        # set up logging and test that user running shell is part
        # of kolla group
        ClientApi()

        # paramiko log is very chatty, tune it down
        logging.getLogger('paramiko').setLevel(logging.WARNING)

        self.dump_stack_trace = False
Beispiel #20
0
    def take_action(self, parsed_args):
        try:
            hostname = parsed_args.hostname.strip()

            hostnames = [hostname]
            if hostname == 'all':
                hostnames = _get_all_hostnames()
                # if there are no hosts, don't bother doing anything
                if not hostnames:
                    return

            destroy_type = 'kill'
            if parsed_args.stop:
                destroy_type = 'stop'
            include_data = False
            if parsed_args.includedata:
                include_data = True
            remove_images = False
            if parsed_args.removeimages:
                remove_images = True

            if include_data and not self._is_ok_to_delete_data():
                LOG.info('Aborting destroy')
                return

            verbose_level = self.app.options.verbose_level

            job = CLIENT.host_destroy(hostnames, destroy_type,
                                      verbose_level, include_data,
                                      remove_images)
            status = job.wait()
            handers_action_result(job, status, verbose_level)
        except ClientException as e:
            raise CommandError(str(e))
        except Exception as e:
            raise Exception(traceback.format_exc())
Beispiel #21
0
    def take_action(self, parsed_args):
        hosts = None
        serial_flag = False
        verbose_level = self.app.options.verbose_level
        timeout_target = 0
        try:
            if parsed_args.hosts:
                host_list = parsed_args.hosts.strip()
                hosts = host_list.split(',')
            if parsed_args.serial:
                serial_flag = True
            if parsed_args.timeout:
                try:
                    timeout = float(parsed_args.timeout[0])
                except Exception:
                    raise CommandError(u._('Timeout value is not a number.'))
                timeout_target = time.time() + (60 * timeout)

            # if we are doing a targeted host deploy make sure we are doing it
            # to only compute nodes
            if hosts:
                invalid_host_list = []
                compute_group = CLIENT.group_get(['compute'])[0]
                compute_hosts = compute_group.get_hosts()
                for host in hosts:
                    if host not in compute_hosts:
                        invalid_host_list.append(host)
                if len(invalid_host_list) > 0:
                    raise CommandError(
                        u._('Invalid hosts for host targeted deploy. '
                            'Hosts must be in the compute group only.'
                            'Invalid hosts: {hosts}').format(
                                hosts=invalid_host_list))

            job = CLIENT.deploy(hosts, serial_flag, verbose_level)

            # wait for job to complete
            status = None
            while status is None:
                if timeout_target and time.time() > timeout_target:
                    job.kill()
                    raise CommandError(u._('Job timed out and was killed.'))
                time.sleep(1)
                status = job.get_status()

            # job is done
            if verbose_level > 2:
                LOG.info('\n\n' + 80 * '=')
                LOG.info(
                    u._('DEBUG command output:\n{out}').format(
                        out=job.get_console_output()))
            if status == 0:
                if verbose_level > 1:
                    # log any ansible warnings
                    msg = job.get_error_message()
                    if msg:
                        LOG.warn(msg)
                LOG.info(u._('Success'))
            else:
                raise CommandError(
                    u._('Job failed:\n{msg}').format(
                        msg=job.get_error_message()))

        except Exception:
            raise Exception(traceback.format_exc())