Update host destroy and precheck api to take list of hosts

Having 'all' as a hostname is awkward in the api.
This changes the api so that a list of hostnames is
required. 'all' is still valid in the CLI, so no change
to the CLI behavior.

This also fixes bug 22918000 - a hostname
validity check is now done on the host destroy.

Jira-Issue: OPENSTACK-746
This commit is contained in:
Steve Noyes 2016-03-18 15:01:46 -04:00
parent 1cdd533d5b
commit f593325553
5 changed files with 45 additions and 37 deletions

View File

@ -1,5 +1,5 @@
---
- hosts: '{{ hosts }}'
- hosts: all
tasks:
- name: get running container ids
shell: docker ps | grep '{{ prefix }}' | awk '{print $1}'

View File

@ -1,5 +1,5 @@
---
- hosts: '{{ hosts }}'
- hosts: all
tasks:
- name: get running non-data container ids
shell: docker ps | grep '{{ prefix }}' | grep -v '\-data' | awk '{print $1}'

View File

@ -42,25 +42,23 @@ class AsyncApi(object):
ansible_job = actions.upgrade(verbose_level)
return Job(ansible_job)
def async_host_destroy(self, hostname, destroy_type, verbose_level=1,
def async_host_destroy(self, hostnames, destroy_type, verbose_level=1,
include_data=False):
"""Destroy Hosts.
Stops and removes all kolla related docker containers on either the
specified host or all hosts if hostname is "all".
Stops and removes all kolla related docker containers on the
specified hosts.
"""
ansible_job = actions.destroy_hosts(hostname, destroy_type,
ansible_job = actions.destroy_hosts(hostnames, destroy_type,
verbose_level, include_data)
return Job(ansible_job)
def async_host_precheck(self, hostname, verbose_level=1):
"""Check pre-deployment configuration of host(s).
def async_host_precheck(self, hostnames, verbose_level=1):
"""Check pre-deployment configuration of hosts.
Check if host is ready for a new deployment. This will fail if
the host is not configured correctly or if it has already been
deployed to.
If hostname is "all", then check all hosts.
any of the hosts are not configured correctly or if they have
already been deployed to.
"""
ansible_job = actions.precheck(hostname, verbose_level)
ansible_job = actions.precheck(hostnames, verbose_level)
return Job(ansible_job)

View File

@ -21,6 +21,7 @@ import yaml
import kollacli.i18n as u
from kollacli.api.client import ClientApi
# TODO(snoyes): remove inventory reference from CLI
from kollacli.common.inventory import Inventory
from kollacli.common.utils import convert_to_unicode
from kollacli.common.utils import get_setup_user
@ -89,6 +90,14 @@ class HostDestroy(Command):
hostname = parsed_args.hostname.strip()
hostname = convert_to_unicode(hostname)
inventory = Inventory.load()
hostnames = [hostname]
if hostname == 'all':
hostnames = inventory.get_hostnames()
else:
if not inventory.get_host(hostname):
_host_not_found(hostname)
destroy_type = 'kill'
if parsed_args.stop:
destroy_type = 'stop'
@ -98,7 +107,7 @@ class HostDestroy(Command):
verbose_level = self.app.options.verbose_level
job = CLIENT.async_host_destroy(hostname, destroy_type,
job = CLIENT.async_host_destroy(hostnames, destroy_type,
verbose_level, include_data)
status = job.wait()
if verbose_level > 2:
@ -195,15 +204,18 @@ class HostCheck(Command):
try:
hostname = parsed_args.hostname.strip()
hostname = convert_to_unicode(hostname)
if hostname != 'all':
inventory = Inventory.load()
inventory = Inventory.load()
hostnames = [hostname]
if hostname == 'all':
hostnames = inventory.get_hostnames()
else:
if not inventory.get_host(hostname):
_host_not_found(hostname)
if parsed_args.predeploy:
# run pre-deploy checks
verbose_level = self.app.options.verbose_level
job = CLIENT.async_host_precheck(hostname, verbose_level)
job = CLIENT.async_host_precheck(hostnames, verbose_level)
status = job.wait()
if verbose_level > 2:
LOG.info('\n\n' + 80 * '=')
@ -216,10 +228,6 @@ class HostCheck(Command):
else:
# run ssh checks
all_ok = True
hostnames = [hostname]
if hostname == 'all':
inventory = Inventory.load()
hostnames = inventory.get_hostnames()
summary = inventory.ssh_check_hosts(hostnames)
for hostname, info in summary.items():
status = 'success'

View File

@ -29,14 +29,13 @@ from kollacli.exceptions import CommandError
LOG = logging.getLogger(__name__)
def destroy_hosts(hostname, destroy_type, verbose_level=1, include_data=False):
'''destroy containers on a host (or all hosts).
def destroy_hosts(hostnames, destroy_type,
verbose_level=1, include_data=False):
'''destroy containers on a set of hosts.
If hostname == 'all', then containers on all hosts will be
stopped. Otherwise, the containers on the specified host
will be stopped.
The destroy type can either be 'stop' or 'kill'.
The containers on the specified hosts will be stopped
or killed. That will be determined by the destroy_type,
which can either be 'stop' or 'kill'.
'''
if destroy_type not in ['stop', 'kill']:
raise CommandError(
@ -58,9 +57,12 @@ def destroy_hosts(hostname, destroy_type, verbose_level=1, include_data=False):
playbook = AnsiblePlaybook()
playbook.playbook_path = os.path.join(kollacli_home,
'ansible/' + playbook_name)
playbook.extra_vars = 'hosts=' + hostname + \
' prefix=' + container_prefix + \
# 'hosts' is defined as 'all' in the playbook yml code, but inventory
# filtering will subset that down to the hosts in playbook.hosts.
playbook.extra_vars = 'prefix=' + container_prefix + \
' destroy_type=' + destroy_type
playbook.hosts = hostnames
if verbose_level <= 1:
playbook.print_output = False
playbook.verbose_level = verbose_level
@ -87,18 +89,18 @@ def deploy(hostnames=[], groupnames=[], servicenames=[],
return job
def precheck(hostname, verbose_level=1):
'''run check playbooks on a host (or all hosts).
If hostname == 'all', then checks will be run on all hosts,
otherwise the check will only be run on the specified host.
'''
def precheck(hostnames, verbose_level=1):
'''run check playbooks on a set of hosts'''
playbook_name = 'prechecks.yml'
kolla_home = get_kolla_home()
playbook = AnsiblePlaybook()
playbook.playbook_path = os.path.join(kolla_home,
'ansible/' + playbook_name)
playbook.extra_vars = 'hosts=' + hostname
# define 'hosts' to be all, but inventory filtering will subset
# that down to the hosts in playbook.hosts.
playbook.extra_vars = 'hosts=all'
playbook.hosts = hostnames
playbook.print_output = True
playbook.verbose_level = verbose_level
job = playbook.run()