WIP: Added initial support for stop containers

The kolla side of this may be pushed upstream so depending on
changes this code may need to change a bit as well.  A test
needs to be added for this as well.

Change-Id: I52173ce0775ebb94b99faadb8ad18a7eaac648aa
Jira-Issue: OSTACKDEV-142
This commit is contained in:
Borne Mace 2016-10-17 09:35:05 -07:00
parent 0c56e5792d
commit bc42e6b13e
4 changed files with 93 additions and 1 deletions

View File

@ -138,3 +138,27 @@ class AsyncApi(object):
ansible_job = actions.precheck(hostnames, verbose_level)
return Job(ansible_job)
def async_host_stop(self, 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)
ansible_job = actions.stop_hosts(hostnames, verbose_level)
return Job(ansible_job)

View File

@ -57,7 +57,7 @@ class HostDestroy(Command):
"""Destroy.
Stops and removes all kolla related docker containers on either the
specified host or if no host is specified, on all hosts.
specified host or all hosts if the hostname all is used.
"""
def get_parser(self, prog_name):
@ -309,6 +309,50 @@ class HostSetup(Command):
return hosts_info
class HostStop(Command):
"""Stop.
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='<hostname | all>',
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.async_host_stop(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)
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()

View File

@ -95,6 +95,29 @@ def precheck(hostnames, verbose_level=1):
return job
def stop_hosts(hostnames=[], verbose_level=1):
'''stop containers on a set of hosts.
The containers on the specified hosts will be stopped
or killed if the stop takes over 20 seconds.
'''
playbook = AnsiblePlaybook()
playbook_name = 'stop.yml'
LOG.info(u._LI('Please be patient as this may take a while.'))
kolla_home = get_kolla_home()
playbook.playbook_path = os.path.join(kolla_home,
'ansible/' + playbook_name)
# 'hosts' is defined as 'all' in the playbook yml code, but inventory
# filtering will subset that down to the hosts in playbook.hosts.
playbook.hosts = hostnames
if verbose_level <= 1:
playbook.print_output = False
playbook.verbose_level = verbose_level
job = playbook.run()
return job
def upgrade(verbose_level=1, servicenames=[]):
playbook = AnsiblePlaybook()
kolla_home = get_kolla_home()

View File

@ -45,6 +45,7 @@ kolla.cli =
host_list = kollacli.commands.host:HostList
host_remove = kollacli.commands.host:HostRemove
host_setup = kollacli.commands.host:HostSetup
host_stop = kollacli.commands.host:HostStop
password_clear = kollacli.commands.password:PasswordClear
password_init = kollacli.commands.password:PasswordInit
password_list = kollacli.commands.password:PasswordList