moved kollacli stop to kollacli host destroy

This commit is contained in:
Borne Mace 2015-09-18 14:13:45 -07:00
parent 54526d29b5
commit b8ecb5bdfc
4 changed files with 89 additions and 85 deletions

View File

@ -1,7 +1,7 @@
---
- hosts: '{{ hosts }}'
tasks:
- name: stop kolla containers
- name: destroy kolla containers
shell: '{{ item }}'
ignore_errors: yes
with_items:

View File

@ -172,78 +172,3 @@ class Setdeploy(Command):
raise e
except Exception:
raise Exception(traceback.format_exc())
class Stop(Command):
"""Stop
Stops and removes all containers on either the specified host
or if no host is specified, on all hosts.
"""
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(Stop, self).get_parser(prog_name)
parser.add_argument('hostname', nargs='?', metavar='[hostname]',
help='hostname')
return parser
def take_action(self, parsed_args):
try:
hostname = 'all'
if parsed_args.hostname:
hostname = parsed_args.hostname.strip()
hostname = convert_to_unicode(hostname)
inventory = Inventory.load()
if hostname != 'all':
host = inventory.get_host(hostname)
if not host:
raise CommandError(
'ERROR: Host (%s) not found. ' % hostname +
'Please add it with "host add"')
flag = ''
# verbose levels: 1=not verbose, 2=more verbose
if self.app.options.verbose_level > 1:
flag = '-vvv'
kollacli_home = get_kollacli_home()
admin_user = get_admin_user()
command_string = ('sudo -u %s ansible-playbook %s '
% (admin_user, flag))
inventory_string = '-i ' + os.path.join(kollacli_home,
'tools',
'json_generator.py ')
playbook_string = ' ' + os.path.join(kollacli_home,
'ansible/stop.yml')
envvar_string = ' --extra-vars "hosts="' + hostname + '"'
cmd = command_string + inventory_string
cmd = cmd + playbook_string + envvar_string
print_output = False
if self.app.options.verbose_level > 1:
# log the ansible command
self.log.debug('cmd:' + cmd)
print_output = True
if self.app.options.verbose_level > 2:
# log the inventory
dbg_gen = os.path.join(kollacli_home, 'tools',
'json_generator.py ')
(inv, _) = \
subprocess.Popen(dbg_gen.split(' '),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
self.log.debug(inv)
err_flag, _ = run_cmd(cmd, print_output)
if err_flag:
raise Exception('stop failed')
self.log.info('stop succeeded')
except CommandError as e:
raise e
except Exception:
raise Exception(traceback.format_exc())

View File

@ -14,12 +14,17 @@
import argparse
import getpass
import logging
import os
import traceback
import utils
from kollacli.ansible.inventory import Inventory
from kollacli.exceptions import CommandError
from kollacli.utils import convert_to_unicode
from kollacli.utils import get_kollacli_home
from kollacli.utils import get_admin_user
from kollacli.utils import get_setup_user
from kollacli.utils import run_cmd
from cliff.command import Command
from cliff.lister import Lister
@ -54,6 +59,80 @@ class HostAdd(Command):
except Exception as e:
raise Exception(traceback.format_exc())
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.
"""
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(HostDestroy, self).get_parser(prog_name)
parser.add_argument('hostname', nargs='?', metavar='[hostname]',
help='host name or ip address')
return parser
def take_action(self, parsed_args):
try:
hostname = 'all'
if parsed_args.hostname:
hostname = parsed_args.hostname.strip()
hostname = convert_to_unicode(hostname)
inventory = Inventory.load()
if hostname != 'all':
host = inventory.get_host(hostname)
if not host:
raise CommandError(
'ERROR: Host (%s) not found. ' % hostname +
'Please add it with "host add"')
flag = ''
# verbose levels: 1=not verbose, 2=more verbose
if self.app.options.verbose_level > 1:
flag = '-vvv'
kollacli_home = get_kollacli_home()
admin_user = get_admin_user()
command_string = ('sudo -u %s ansible-playbook %s '
% (admin_user, flag))
inventory_string = '-i ' + os.path.join(kollacli_home,
'tools',
'json_generator.py ')
playbook_string = ' ' + os.path.join(kollacli_home,
'ansible/host_destroy.yml')
envvar_string = ' --extra-vars "hosts="' + hostname + '"'
cmd = command_string + inventory_string
cmd = cmd + playbook_string + envvar_string
print_output = False
if self.app.options.verbose_level > 1:
# log the ansible command
self.log.debug('cmd:' + cmd)
print_output = True
if self.app.options.verbose_level > 2:
# log the inventory
dbg_gen = os.path.join(kollacli_home, 'tools',
'json_generator.py ')
(inv, _) = \
subprocess.Popen(dbg_gen.split(' '),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
self.log.debug(inv)
err_flag, _ = run_cmd(cmd, print_output)
if err_flag:
raise Exception('destroy failed')
self.log.info('destroy succeeded')
except CommandError as e:
raise e
except Exception:
raise Exception(traceback.format_exc())
class HostRemove(Command):
"""Remove host from openstack-kolla"""

View File

@ -34,29 +34,29 @@ kolla.cli =
deploy = kollacli.common:Deploy
dump = kollacli.common:Dump
group_add = kollacli.group:GroupAdd
group_remove = kollacli.group:GroupRemove
group_addhost = kollacli.group:GroupAddhost
group_removehost = kollacli.group:GroupRemovehost
group_listhosts = kollacli.group:GroupListhosts
group_addservice = kollacli.group:GroupAddservice
group_removeservice = kollacli.group:GroupRemoveservice
group_listhosts = kollacli.group:GroupListhosts
group_listservices = kollacli.group:GroupListservices
group_remove = kollacli.group:GroupRemove
group_removehost = kollacli.group:GroupRemovehost
group_removeservice = kollacli.group:GroupRemoveservice
host_add = kollacli.host:HostAdd
host_remove = kollacli.host:HostRemove
host_list = kollacli.host:HostList
host_check = kollacli.host:HostCheck
host_destroy = kollacli.host:HostDestroy
host_list = kollacli.host:HostList
host_remove = kollacli.host:HostRemove
host_setup = kollacli.host:HostSetup
list = kollacli.common:List
password_set = kollacli.password:PasswordSet
password_clear = kollacli.password:PasswordClear
password_list = kollacli.password:PasswordList
property_set = kollacli.property:PropertySet
password_set = kollacli.password:PasswordSet
property_clear = kollacli.property:PropertyClear
property_list = kollacli.property:PropertyList
property_set = kollacli.property:PropertySet
service_list = kollacli.service:ServiceList
service_listgroups = kollacli.service:ServiceListGroups
setdeploy = kollacli.common:Setdeploy
stop = kollacli.common:Stop
[extract_messages]
keywords = _ gettext ngettext l_ lazy_gettext