[Part2]: Optimise and simplify action code

1. complete destroy_hosts action
2. complete stop_hosts action
3. complete prechecks action

Change-Id: I419da55d68525f4caf3d33e8780694d26b33c97b
Depend-On: I0505bb0f8b3b19a0d1880e7b88b55246bcebc3ec
Co-Authored-By: gujin <gu.jin@99cloud.net>
This commit is contained in:
cao.yuan 2019-07-22 12:57:13 +08:00
parent c12fb0755e
commit 81a8a22d9a
2 changed files with 53 additions and 65 deletions

View File

@ -16,7 +16,7 @@ import kolla_cli.i18n as u
from kolla_cli.api.exceptions import InvalidArgument
from kolla_cli.api.job import Job
from kolla_cli.common.ansible import actions
from kolla_cli.common.ansible.actions import KollaAction
from kolla_cli.common.inventory import Inventory
from kolla_cli.common.utils import check_arg
from kolla_cli.common.utils import safe_decode
@ -183,9 +183,10 @@ class HostApi(object):
inventory = Inventory.load()
inventory.validate_hostnames(hostnames)
ansible_job = actions.destroy_hosts(hostnames, destroy_type,
verbose_level, include_data,
remove_images)
action = KollaAction(verbose_level=verbose_level,
playbook_name='destroy.yml')
ansible_job = action.destroy_hosts(hostnames, destroy_type,
include_data, remove_images)
return Job(ansible_job)
@staticmethod
@ -209,7 +210,9 @@ class HostApi(object):
inventory = Inventory.load()
inventory.validate_hostnames(hostnames)
ansible_job = actions.precheck(hostnames, verbose_level)
action = KollaAction(verbose_level=verbose_level,
playbook_name='site.yml')
ansible_job = action.precheck(hostnames)
return Job(ansible_job)
@staticmethod
@ -233,7 +236,9 @@ class HostApi(object):
inventory = Inventory.load()
inventory.validate_hostnames(hostnames)
ansible_job = actions.stop_hosts(hostnames, verbose_level)
action = KollaAction(verbose_level=verbose_level,
playbook_name='site.yml')
ansible_job = action.stop_hosts(hostnames)
return Job(ansible_job)

View File

@ -59,33 +59,52 @@ class KollaAction(object):
job = self.playbook.run()
return job
def destroy_hosts(self, hostnames, destroy_type,
include_data=False, remove_images=False):
'''destroy containers on a set of hosts.
def destroy_hosts(hostnames, destroy_type,
verbose_level=1, include_data=False,
remove_images=False):
'''destroy containers on a set of hosts.
The containers on the specified hosts will be stopped
or killed.
'''
The containers on the specified hosts will be stopped
or killed.
'''
playbook = AnsiblePlaybook()
playbook_name = 'destroy.yml'
LOG.info(u._LI('Please be patient as this may take a while.'))
# 'hosts' is defined as 'all' in the playbook yml code, but inventory
# filtering will subset that down to the hosts in playbook.hosts.
self.playbook.hosts = hostnames
if remove_images:
self.playbook.extra_vars = 'destroy_include_images=yes'
if self.playbook.verbose_level <= 1:
self.playbook.print_output = False
job = self.playbook.run()
return job
LOG.info(u._LI('Please be patient as this may take a while.'))
kolla_home = get_kolla_ansible_home()
playbook.playbook_path = os.path.join(kolla_home,
'ansible/' + playbook_name)
def stop_hosts(self, hostnames=[]):
'''stop containers on a set of hosts.
# '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 remove_images:
playbook.extra_vars = 'destroy_include_images=yes'
if verbose_level <= 1:
playbook.print_output = False
playbook.verbose_level = verbose_level
job = playbook.run()
return job
The containers on the specified hosts will be stopped
or killed if the stop takes over 20 seconds.
'''
LOG.info(u._LI('Please be patient as this may take a while.'))
# 'hosts' is defined as 'all' in the playbook yml code, but inventory
# filtering will subset that down to the hosts in playbook.hosts.
self.playbook.hosts = hostnames
self.playbook.extra_vars = 'kolla_action=stop'
if self.playbook.verbose_level <= 1:
self.playbook.print_output = False
job = self.playbook.run()
return job
def precheck(self, hostnames):
'''run check playbooks on a set of hosts'''
# define 'hosts' to be all, but inventory filtering will subset
# that down to the hosts in playbook.hosts.
self.playbook.hosts = hostnames
self.playbook.extra_vars = 'kolla_action=precheck hosts=all'
self.playbook.print_output = True
job = self.playbook.run()
return job
def deploy(hostnames=[],
@ -106,24 +125,6 @@ def deploy(hostnames=[],
return job
def precheck(hostnames, verbose_level=1):
'''run check playbooks on a set of hosts'''
kolla_home = get_kolla_ansible_home()
playbook = AnsiblePlaybook()
playbook.playbook_path = os.path.join(kolla_home,
'ansible/site.yml')
# define 'hosts' to be all, but inventory filtering will subset
# that down to the hosts in playbook.hosts.
playbook.extra_vars = 'kolla_action=precheck hosts=all'
playbook.hosts = hostnames
playbook.print_output = True
playbook.verbose_level = verbose_level
job = playbook.run()
return job
def pull(verbose_level=1):
'''run pull action against all hosts'''
playbook = AnsiblePlaybook()
@ -137,24 +138,6 @@ def pull(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()
kolla_home = get_kolla_ansible_home()
playbook.playbook_path = os.path.join(kolla_home,
'ansible/site.yml')
playbook.extra_vars = 'kolla_action=stop'
playbook.hosts = hostnames
playbook.verbose_level = verbose_level
job = playbook.run()
return job
def reconfigure(verbose_level=1):
playbook = AnsiblePlaybook()
kolla_home = get_kolla_ansible_home()