From 1498b7eade7ef1667f2fab3a62b8f40de742864c Mon Sep 17 00:00:00 2001 From: zhulingjie Date: Fri, 9 Aug 2019 09:39:20 +0800 Subject: [PATCH] Move stop acton to kolla model from host Change-Id: Id8cb840651e4455064699a2a53943d364d9a7e82 --- kolla_cli/api/control_plane.py | 25 +++++++++++++++ kolla_cli/api/host.py | 26 --------------- kolla_cli/commands/host.py | 32 ------------------- kolla_cli/commands/kolla_action.py | 27 ++++++++++++++++ kolla_cli/common/ansible/actions.py | 2 +- kolla_cli/tests/functional/test_stop.py | 4 +-- kolla_cli/tests/unit/test_host_cmd.py | 23 -------------- kolla_cli/tests/unit/test_stop_cmd.py | 42 +++++++++++++++++++++++++ setup.cfg | 2 +- 9 files changed, 98 insertions(+), 85 deletions(-) create mode 100644 kolla_cli/tests/unit/test_stop_cmd.py diff --git a/kolla_cli/api/control_plane.py b/kolla_cli/api/control_plane.py index 77dc2e3..fa31adf 100644 --- a/kolla_cli/api/control_plane.py +++ b/kolla_cli/api/control_plane.py @@ -120,6 +120,31 @@ class ControlPlaneApi(object): ansible_job = action.pull(hostnames, servicenames) return Job(ansible_job) + @staticmethod + def stop(verbose_level=1, hostnames=[]): + # type: (int, List[str]) -> Job + """Stop Hosts. + + Stops all kolla related docker containers on the specified hosts. + + :param hostnames: host names + :type hostnames: list + :param verbose_level: the higher the number, the more verbose + :type verbose_level: integer + :return: Job object + :rtype: Job + """ + check_arg(hostnames, u._('Host names'), list, + empty_ok=True, none_ok=True) + check_arg(verbose_level, u._('Verbose level'), int) + + hostnames = safe_decode(hostnames) + + action = KollaAction(verbose_level=verbose_level, + playbook_name='site.yml') + ansible_job = action.stop(hostnames) + return Job(ansible_job) + @staticmethod def upgrade(verbose_level=1, servicenames=[]): # type: (int, List[str]) -> Job diff --git a/kolla_cli/api/host.py b/kolla_cli/api/host.py index 1b9e338..9b0b499 100644 --- a/kolla_cli/api/host.py +++ b/kolla_cli/api/host.py @@ -190,32 +190,6 @@ class HostApi(object): include_data, remove_images) return Job(ansible_job) - @staticmethod - def host_stop(hostnames, verbose_level=1): - # type: (List[str], int) -> Job - """Stop Hosts. - - Stops all kolla related docker containers on the specified hosts. - - :param hostnames: host names - :type hostnames: list - :param verbose_level: the higher the number, the more verbose - :type verbose_level: integer - :return: Job object - :rtype: Job - """ - check_arg(hostnames, u._('Host names'), list) - check_arg(verbose_level, u._('Verbose level'), int) - - hostnames = safe_decode(hostnames) - inventory = Inventory.load() - inventory.validate_hostnames(hostnames) - - action = KollaAction(verbose_level=verbose_level, - playbook_name='site.yml') - ansible_job = action.stop_hosts(hostnames) - return Job(ansible_job) - class Host(object): """Host""" diff --git a/kolla_cli/commands/host.py b/kolla_cli/commands/host.py index 4b859dc..7fea51c 100644 --- a/kolla_cli/commands/host.py +++ b/kolla_cli/commands/host.py @@ -294,38 +294,6 @@ class HostSetup(Command): return hosts_info -class HostStop(Command): - """Stop all kolla containers on host(s). - - Stops all kolla related docker containers on either the - specified host or all hosts if the hostname all is used. - """ - - def get_parser(self, prog_name): - parser = super(HostStop, self).get_parser(prog_name) - parser.add_argument('hostname', metavar='', - help=u._('Host name or ip address or "all"')) - return parser - - def take_action(self, parsed_args): - try: - hostname = parsed_args.hostname.strip() - - hostnames = [hostname] - if hostname == 'all': - hostnames = _get_all_hostnames() - - verbose_level = self.app.options.verbose_level - - job = CLIENT.host_stop(hostnames, verbose_level) - 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()) - - def _get_all_hostnames(): hostnames = [] hosts = CLIENT.host_get_all() diff --git a/kolla_cli/commands/kolla_action.py b/kolla_cli/commands/kolla_action.py index 4f0f993..546ad43 100644 --- a/kolla_cli/commands/kolla_action.py +++ b/kolla_cli/commands/kolla_action.py @@ -176,6 +176,33 @@ class Reconfigure(Command): raise Exception(traceback.format_exc()) +class Stop(Command): + """Stop all kolla containers on host(s). + + Stops all kolla related docker containers on either the + specified host or all hosts if the hostname all is used. + """ + def get_parser(self, prog_name): + parser = super(Stop, self).get_parser(prog_name) + parser.add_argument('--hosts', nargs='?', + metavar='', + help=u._('Stop host list')) + return parser + + def take_action(self, parsed_args): + try: + hosts = [] + verbose_level = self.app.options.verbose_level + if parsed_args.hosts: + host_list = parsed_args.hosts.strip() + hosts = host_list.split(',') + job = CLIENT.stop(verbose_level, hosts) + status = job.wait() + handers_action_result(job, status, verbose_level) + except Exception: + raise Exception(traceback.format_exc()) + + class Upgrade(Command): """Upgrades existing OpenStack Environment.""" def get_parser(self, prog_name): diff --git a/kolla_cli/common/ansible/actions.py b/kolla_cli/common/ansible/actions.py index dad7fe3..e1fd041 100644 --- a/kolla_cli/common/ansible/actions.py +++ b/kolla_cli/common/ansible/actions.py @@ -101,7 +101,7 @@ class KollaAction(object): job = self.playbook.run() return job - def stop_hosts(self, hostnames=[]): + def stop(self, hostnames=[]): '''stop containers on a set of hosts. The containers on the specified hosts will be stopped diff --git a/kolla_cli/tests/functional/test_stop.py b/kolla_cli/tests/functional/test_stop.py index 6e16c7b..a4aecdd 100644 --- a/kolla_cli/tests/functional/test_stop.py +++ b/kolla_cli/tests/functional/test_stop.py @@ -41,7 +41,7 @@ class TestFunctional(KollaCliTest): # stop services, initialize server self.log.info('Start stop #1') - job = CLIENT.host_stop(hostnames) + job = CLIENT.stop(1, hostnames) self._process_job(job, 'stop #1') self.log.info('updating various properties for the test') @@ -59,7 +59,7 @@ class TestFunctional(KollaCliTest): self._process_job(job, 'deploy') self.log.info('Start stop #2') - job = CLIENT.host_stop(hostnames) + job = CLIENT.stop(1, hostnames) self._process_job(job, 'stop #2') def _process_job(self, job, descr, expect_kill=False): diff --git a/kolla_cli/tests/unit/test_host_cmd.py b/kolla_cli/tests/unit/test_host_cmd.py index 226bb50..c573342 100644 --- a/kolla_cli/tests/unit/test_host_cmd.py +++ b/kolla_cli/tests/unit/test_host_cmd.py @@ -176,26 +176,3 @@ class TestUnit(KollaCliUnitTest): mock_setup.assert_called_once_with({hostname: {'password': password}}) mock_yml.assert_called_once_with(fake_path) mock_ssh_check.assert_not_called() - - @mock.patch('kolla_cli.common.ansible.job.AnsibleJob.get_status') - @mock.patch('kolla_cli.api.client.ClientApi.host_get_all') - @mock.patch('kolla_cli.api.client.ClientApi.host_stop') - @mock.patch('kolla_cli.shell.KollaCli._is_inventory_present', - return_value=True) - def test_host_stop(self, _, mock_stop, mock_get_all, - mock_get_status): - hostname = 'foo' - mock_get_all.return_value = [self.get_fake_host(hostname)] - mock_get_status.return_value = 0 - mock_stop.return_value = self.get_fake_job() - - # host stop hostname - ret = self.run_cli_command('host stop %s' % hostname) - self.assertEqual(ret, 0) - mock_stop.assert_called_once_with([hostname], 1) - - # host stop all - mock_stop.reset_mock() - ret = self.run_cli_command('host stop all') - self.assertEqual(ret, 0) - mock_stop.assert_called_once_with([hostname], 1) diff --git a/kolla_cli/tests/unit/test_stop_cmd.py b/kolla_cli/tests/unit/test_stop_cmd.py new file mode 100644 index 0000000..c93c88c --- /dev/null +++ b/kolla_cli/tests/unit/test_stop_cmd.py @@ -0,0 +1,42 @@ +# Copyright (c) 2018 OpenStack Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import mock + +from kolla_cli.tests.unit.common import KollaCliUnitTest + + +class TestUnit(KollaCliUnitTest): + @mock.patch('kolla_cli.api.control_plane.ControlPlaneApi.stop') + @mock.patch('kolla_cli.common.ansible.job.AnsibleJob.get_status') + @mock.patch('kolla_cli.shell.KollaCli._is_inventory_present', + return_value=True) + def test_stop(self, _, mock_get_status, mock_stop): + mock_get_status.return_value = 0 + mock_stop.return_value = self.get_fake_job() + ret = self.run_cli_command('action stop') + self.assertEqual(ret, 0) + mock_stop.assert_called_once_with(1, []) + + @mock.patch('kolla_cli.api.control_plane.ControlPlaneApi.stop') + @mock.patch('kolla_cli.common.ansible.job.AnsibleJob.get_status') + @mock.patch('kolla_cli.shell.KollaCli._is_inventory_present', + return_value=True) + def test_stop_with_hosts(self, _, mock_get_status, mock_stop): + mock_get_status.return_value = 0 + mock_stop.return_value = self.get_fake_job() + hostnames = ['host1', 'host2'] + ret = self.run_cli_command('action stop --hosts {hosts}'.format( + hosts=','.join(hostnames))) + self.assertEqual(ret, 0) + mock_stop.assert_called_once_with(1, hostnames) diff --git a/setup.cfg b/setup.cfg index c482765..90cbb3b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ kolla.cli = action_postdeploy = kolla_cli.commands.kolla_action:PostDeploy action_pull = kolla_cli.commands.kolla_action:Pull action_reconfigure = kolla_cli.commands.kolla_action:Reconfigure + action_stop = kolla_cli.commands.kolla_action:Stop action_upgrade = kolla_cli.commands.kolla_action:Upgrade certificate_init = kolla_cli.commands.kolla_action:CertificateInit config_reset = kolla_cli.commands.config:ConfigReset @@ -54,7 +55,6 @@ kolla.cli = host_list = kolla_cli.commands.host:HostList host_remove = kolla_cli.commands.host:HostRemove host_setup = kolla_cli.commands.host:HostSetup - host_stop = kolla_cli.commands.host:HostStop password_clear = kolla_cli.commands.password:PasswordClear password_init = kolla_cli.commands.password:PasswordInit password_list = kolla_cli.commands.password:PasswordList