restructuring of directories to separate cli from common code

- add new commands directory under kollacli for cli commands
- split misc commands into support and deploy
- clean up imports
- move sshutils and utils under common directory
This commit is contained in:
Steve Noyes 2015-11-24 15:04:24 -05:00
parent 4dbbc62f3e
commit dae53cabc8
21 changed files with 190 additions and 163 deletions

View File

101
kollacli/commands/deploy.py Normal file
View File

@ -0,0 +1,101 @@
# Copyright(c) 2015, Oracle and/or its affiliates. All Rights Reserved.
#
# 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 logging
import traceback
import kollacli.i18n as u
from kollacli.common.ansible.actions import deploy
from kollacli.common.inventory import Inventory
from kollacli.common.utils import convert_to_unicode
from kollacli.exceptions import CommandError
from cliff.command import Command
LOG = logging.getLogger(__name__)
class Deploy(Command):
"""Deploy"""
def get_parser(self, prog_name):
parser = super(Deploy, self).get_parser(prog_name)
parser.add_argument('--hosts', nargs='?',
metavar='<host_list>',
help=u._('Deployment host list'))
parser.add_argument('--groups', nargs='?',
metavar='<group_list>',
help=u._('Deployment group list'))
parser.add_argument('--services', nargs='?',
metavar='<service_list>',
help=u._('Deployment service list'))
parser.add_argument('--serial', action='store_true',
help=u._('Deploy serially'))
return parser
def take_action(self, parsed_args):
hosts = None
groups = None
services = None
serial_flag = False
verbose_level = self.app.options.verbose_level
try:
if parsed_args.hosts:
host_list = parsed_args.hosts.strip()
hosts = convert_to_unicode(host_list).split(',')
if parsed_args.groups:
group_list = parsed_args.groups.strip()
groups = convert_to_unicode(group_list).split(',')
if parsed_args.services:
service_list = parsed_args.services.strip()
services = convert_to_unicode(service_list).split(',')
if parsed_args.serial:
serial_flag = True
deploy(hosts, groups, services, serial_flag,
verbose_level)
except Exception:
raise Exception(traceback.format_exc())
class Setdeploy(Command):
"""Set deploy mode
Set deploy mode to either local or remote. Local indicates
that the openstack deployment will be to the local host.
Remote means that the deployment is on remote hosts.
"""
def get_parser(self, prog_name):
parser = super(Setdeploy, self).get_parser(prog_name)
parser.add_argument('mode', metavar='<mode>',
help=u._('mode=<local, remote>'))
return parser
def take_action(self, parsed_args):
try:
mode = parsed_args.mode.strip()
remote_flag = False
if mode == 'remote':
remote_flag = True
elif mode != 'local':
raise CommandError(
u._('Invalid deploy mode. Mode must be '
'either "local" or "remote".'))
inventory = Inventory.load()
inventory.set_deploy_mode(remote_flag)
Inventory.save(inventory)
except CommandError as e:
raise e
except Exception:
raise Exception(traceback.format_exc())

View File

@ -16,8 +16,8 @@ import traceback
import kollacli.i18n as u
from kollacli.common.inventory import Inventory
from kollacli.common.utils import convert_to_unicode
from kollacli.exceptions import CommandError
from kollacli import utils
from cliff.command import Command
from cliff.lister import Lister
@ -34,7 +34,7 @@ class GroupAdd(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
inventory = Inventory.load()
inventory.add_group(groupname)
@ -57,7 +57,7 @@ class GroupRemove(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
inventory = Inventory.load()
inventory.remove_group(groupname)
Inventory.save(inventory)
@ -80,9 +80,9 @@ class GroupAddhost(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
hostname = parsed_args.hostname.strip()
hostname = utils.convert_to_unicode(hostname)
hostname = convert_to_unicode(hostname)
inventory = Inventory.load()
inventory.add_host(hostname, groupname)
Inventory.save(inventory)
@ -106,9 +106,9 @@ class GroupRemovehost(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
hostname = parsed_args.hostname.strip()
hostname = utils.convert_to_unicode(hostname)
hostname = convert_to_unicode(hostname)
inventory = Inventory.load()
inventory.remove_host(hostname, groupname)
@ -153,9 +153,9 @@ class GroupAddservice(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
servicename = parsed_args.servicename.strip()
servicename = utils.convert_to_unicode(servicename)
servicename = convert_to_unicode(servicename)
inventory = Inventory.load()
inventory.add_group_to_service(groupname, servicename)
@ -180,9 +180,9 @@ class GroupRemoveservice(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
servicename = parsed_args.servicename.strip()
servicename = utils.convert_to_unicode(servicename)
servicename = convert_to_unicode(servicename)
inventory = Inventory.load()
inventory.remove_group_from_service(groupname, servicename)

View File

@ -22,9 +22,9 @@ import kollacli.i18n as u
from kollacli.common.ansible.actions import destroy_hosts
from kollacli.common.inventory import Inventory
from kollacli.common.utils import convert_to_unicode
from kollacli.common.utils import get_setup_user
from kollacli.exceptions import CommandError
from kollacli.utils import convert_to_unicode
from kollacli.utils import get_setup_user
from cliff.command import Command
from cliff.lister import Lister

View File

@ -16,8 +16,8 @@ import traceback
import kollacli.i18n as u
from kollacli.common.inventory import Inventory
from kollacli.common.utils import convert_to_unicode
from kollacli.exceptions import CommandError
from kollacli import utils
from cliff.command import Command
from cliff.lister import Lister
@ -41,9 +41,9 @@ class ServiceAddGroup(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
servicename = parsed_args.servicename.strip()
servicename = utils.convert_to_unicode(servicename)
servicename = convert_to_unicode(servicename)
inventory = Inventory.load()
@ -70,9 +70,9 @@ class ServiceRemoveGroup(Command):
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
groupname = utils.convert_to_unicode(groupname)
groupname = convert_to_unicode(groupname)
servicename = parsed_args.servicename.strip()
servicename = utils.convert_to_unicode(servicename)
servicename = convert_to_unicode(servicename)
inventory = Inventory.load()

View File

@ -19,64 +19,18 @@ import traceback
import kollacli.i18n as u
from kollacli.common.ansible.actions import deploy
from kollacli.common.inventory import Inventory
from kollacli.exceptions import CommandError
from kollacli.utils import convert_to_unicode
from kollacli.utils import get_kolla_etc
from kollacli.utils import get_kolla_home
from kollacli.utils import get_kolla_log_dir
from kollacli.utils import get_kollacli_etc
from kollacli.utils import run_cmd
from kollacli.common.utils import get_kolla_etc
from kollacli.common.utils import get_kolla_home
from kollacli.common.utils import get_kolla_log_dir
from kollacli.common.utils import get_kollacli_etc
from kollacli.common.utils import run_cmd
from cliff.command import Command
LOG = logging.getLogger(__name__)
class Deploy(Command):
"""Deploy"""
def get_parser(self, prog_name):
parser = super(Deploy, self).get_parser(prog_name)
parser.add_argument('--hosts', nargs='?',
metavar='<host_list>',
help=u._('Deployment host list'))
parser.add_argument('--groups', nargs='?',
metavar='<group_list>',
help=u._('Deployment group list'))
parser.add_argument('--services', nargs='?',
metavar='<service_list>',
help=u._('Deployment service list'))
parser.add_argument('--serial', action='store_true',
help=u._('Deploy serially'))
return parser
def take_action(self, parsed_args):
hosts = None
groups = None
services = None
serial_flag = False
verbose_level = self.app.options.verbose_level
try:
if parsed_args.hosts:
host_list = parsed_args.hosts.strip()
hosts = convert_to_unicode(host_list).split(',')
if parsed_args.groups:
group_list = parsed_args.groups.strip()
groups = convert_to_unicode(group_list).split(',')
if parsed_args.services:
service_list = parsed_args.services.strip()
services = convert_to_unicode(service_list).split(',')
if parsed_args.serial:
serial_flag = True
deploy(hosts, groups, services, serial_flag,
verbose_level)
except Exception:
raise Exception(traceback.format_exc())
class Dump(Command):
"""Dumps configuration data for debugging
@ -165,35 +119,3 @@ class Dump(Command):
if inv_path:
os.remove(inv_path)
return
class Setdeploy(Command):
"""Set deploy mode
Set deploy mode to either local or remote. Local indicates
that the openstack deployment will be to the local host.
Remote means that the deployment is on remote hosts.
"""
def get_parser(self, prog_name):
parser = super(Setdeploy, self).get_parser(prog_name)
parser.add_argument('mode', metavar='<mode>',
help=u._('mode=<local, remote>'))
return parser
def take_action(self, parsed_args):
try:
mode = parsed_args.mode.strip()
remote_flag = False
if mode == 'remote':
remote_flag = True
elif mode != 'local':
raise CommandError(
u._('Invalid deploy mode. Mode must be '
'either "local" or "remote".'))
inventory = Inventory.load()
inventory.set_deploy_mode(remote_flag)
Inventory.save(inventory)
except CommandError as e:
raise e
except Exception:
raise Exception(traceback.format_exc())

View File

@ -19,10 +19,10 @@ import kollacli.i18n as u
from kollacli.common.ansible.playbook import AnsiblePlaybook
from kollacli.common import properties
from kollacli.common.properties import AnsibleProperties
from kollacli.common.utils import get_kolla_etc
from kollacli.common.utils import get_kolla_home
from kollacli.common.utils import get_kollacli_home
from kollacli.exceptions import CommandError
from kollacli.utils import get_kolla_etc
from kollacli.utils import get_kolla_home
from kollacli.utils import get_kollacli_home
LOG = logging.getLogger(__name__)

View File

@ -18,11 +18,11 @@ import traceback
import kollacli.i18n as u
from kollacli.common.utils import get_admin_user
from kollacli.common.utils import get_ansible_command
from kollacli.common.utils import get_kolla_etc
from kollacli.common.utils import run_cmd
from kollacli.exceptions import CommandError
from kollacli.utils import get_admin_user
from kollacli.utils import get_ansible_command
from kollacli.utils import get_kolla_etc
from kollacli.utils import run_cmd
from kollacli.common.inventory import Inventory

View File

@ -20,13 +20,15 @@ import traceback
import kollacli.i18n as u
from kollacli import exceptions
from kollacli import utils
from kollacli.common.sshutils import ssh_setup_host
from kollacli.common.utils import get_admin_user
from kollacli.common.utils import get_ansible_command
from kollacli.common.utils import get_kollacli_etc
from kollacli.common.utils import run_cmd
from kollacli.common.utils import sync_read_file
from kollacli.common.utils import sync_write_file
from kollacli.exceptions import CommandError
from kollacli.sshutils import ssh_setup_host
from kollacli.utils import get_admin_user
from kollacli.utils import get_ansible_command
ANSIBLE_SSH_USER = 'ansible_ssh_user'
ANSIBLE_CONNECTION = 'ansible_connection'
@ -158,7 +160,7 @@ class HostGroup(object):
self.set_var(ANSIBLE_BECOME, 'yes')
if remote_flag:
# set the ssh info for all the servers in the group
self.set_var(ANSIBLE_SSH_USER, utils.get_admin_user())
self.set_var(ANSIBLE_SSH_USER, get_admin_user())
self.clear_var(ANSIBLE_CONNECTION)
else:
# remove ssh info, add local connection type
@ -279,11 +281,11 @@ class Inventory(object):
@staticmethod
def load():
"""load the inventory from a pickle file"""
inventory_path = os.path.join(utils.get_kollacli_etc(), INVENTORY_PATH)
inventory_path = os.path.join(get_kollacli_etc(), INVENTORY_PATH)
data = ''
try:
if os.path.exists(inventory_path):
data = utils.sync_read_file(inventory_path)
data = sync_read_file(inventory_path)
if data.strip():
inventory = jsonpickle.decode(data)
@ -302,13 +304,13 @@ class Inventory(object):
@staticmethod
def save(inventory):
"""Save the inventory in a pickle file"""
inventory_path = os.path.join(utils.get_kollacli_etc(), INVENTORY_PATH)
inventory_path = os.path.join(get_kollacli_etc(), INVENTORY_PATH)
try:
# multiple trips thru json to render a readable inventory file
data = jsonpickle.encode(inventory)
data_str = json.loads(data)
pretty_data = json.dumps(data_str, indent=4)
utils.sync_write_file(inventory_path, pretty_data)
sync_write_file(inventory_path, pretty_data)
except Exception as e:
raise CommandError(
@ -454,7 +456,7 @@ class Inventory(object):
self.log.info(u._LI('Host ({host}) setup succeeded.')
.format(host=hostname))
except Exception as e:
raise exceptions.CommandError(
raise CommandError(
u._('Host ({host}) setup failed : {error}')
.format(host=hostname, error=str(e)))
return True
@ -469,7 +471,7 @@ class Inventory(object):
inventory_string = '-i ' + gen_file_path
ping_string = ' %s %s' % (hostname, '-m ping')
cmd = (command_string + inventory_string + ping_string)
err_msg, output = utils.run_cmd(cmd, False)
err_msg, output = run_cmd(cmd, False)
except Exception as e:
raise e
finally:
@ -479,7 +481,7 @@ class Inventory(object):
if result_only:
return False
else:
raise exceptions.CommandError(
raise CommandError(
u._('Host ({host}) check failed. : {error} {message}')
.format(host=hostname, error=err_msg, message=output))
else:

View File

@ -15,8 +15,8 @@ import os
import kollacli.i18n as u
from kollacli.common import utils
from kollacli.exceptions import CommandError
from kollacli import utils
PWDS_FILENAME = 'passwords.yml'
PWD_EDITOR_FILENAME = 'passwd_editor.py'

View File

@ -16,10 +16,10 @@ import os
import six
import yaml
from kollacli.utils import change_property
from kollacli.utils import get_kolla_etc
from kollacli.utils import get_kolla_home
from kollacli.utils import sync_read_file
from kollacli.common.utils import change_property
from kollacli.common.utils import get_kolla_etc
from kollacli.common.utils import get_kolla_home
from kollacli.common.utils import sync_read_file
ALLVARS_PATH = 'ansible/group_vars/all.yml'
GLOBALS_FILENAME = 'globals.yml'

View File

@ -16,11 +16,12 @@ import os.path
import paramiko
import traceback
from kollacli.common.utils import get_admin_user
from kollacli.common.utils import get_kollacli_etc
from kollacli.common.utils import get_setup_user
import kollacli.i18n as u
from kollacli.utils import get_admin_user
from kollacli.utils import get_kollacli_etc
from kollacli.utils import get_setup_user
MIN_DOCKER_VERSION = '1.8.1'

View File

@ -22,10 +22,10 @@ from cliff.commandmanager import CommandManager
import kollacli.i18n as u
from kollacli.common.inventory import INVENTORY_PATH
from kollacli.common.utils import get_kolla_log_dir
from kollacli.common.utils import get_kolla_log_file_size
from kollacli.common.utils import get_kollacli_etc
from kollacli.exceptions import CommandError
from kollacli.utils import get_kolla_log_dir
from kollacli.utils import get_kolla_log_file_size
from kollacli.utils import get_kollacli_etc
LOG = logging.getLogger(__name__)

View File

@ -31,31 +31,31 @@ console_scripts =
kollacli = kollacli.shell:main
kolla.cli =
deploy = kollacli.misc:Deploy
dump = kollacli.misc:Dump
group_add = kollacli.group:GroupAdd
group_addhost = kollacli.group:GroupAddhost
group_listhosts = kollacli.group:GroupListhosts
group_listservices = kollacli.group:GroupListservices
group_remove = kollacli.group:GroupRemove
group_removehost = kollacli.group:GroupRemovehost
host_add = kollacli.host:HostAdd
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
password_clear = kollacli.password:PasswordClear
password_list = kollacli.password:PasswordList
password_set = kollacli.password:PasswordSet
property_clear = kollacli.property:PropertyClear
property_list = kollacli.property:PropertyList
property_set = kollacli.property:PropertySet
service_addgroup = kollacli.service:ServiceAddGroup
service_list = kollacli.service:ServiceList
service_listgroups = kollacli.service:ServiceListGroups
service_removegroup = kollacli.service:ServiceRemoveGroup
setdeploy = kollacli.misc:Setdeploy
deploy = kollacli.commands.deploy:Deploy
dump = kollacli.commands.support:Dump
group_add = kollacli.commands.group:GroupAdd
group_addhost = kollacli.commands.group:GroupAddhost
group_listhosts = kollacli.commands.group:GroupListhosts
group_listservices = kollacli.commands.group:GroupListservices
group_remove = kollacli.commands.group:GroupRemove
group_removehost = kollacli.commands.group:GroupRemovehost
host_add = kollacli.commands.host:HostAdd
host_check = kollacli.commands.host:HostCheck
host_destroy = kollacli.commands.host:HostDestroy
host_list = kollacli.commands.host:HostList
host_remove = kollacli.commands.host:HostRemove
host_setup = kollacli.commands.host:HostSetup
password_clear = kollacli.commands.password:PasswordClear
password_list = kollacli.commands.password:PasswordList
password_set = kollacli.commands.password:PasswordSet
property_clear = kollacli.commands.property:PropertyClear
property_list = kollacli.commands.property:PropertyList
property_set = kollacli.commands.property:PropertySet
service_addgroup = kollacli.commands.service:ServiceAddGroup
service_list = kollacli.commands.service:ServiceList
service_listgroups = kollacli.commands.service:ServiceListGroups
service_removegroup = kollacli.commands.service:ServiceRemoveGroup
setdeploy = kollacli.commands.deploy:Setdeploy
[extract_messages]
keywords = _ gettext ngettext l_ lazy_gettext

View File

@ -23,7 +23,7 @@ import yaml
from oslo_utils.encodeutils import safe_decode
import kollacli.utils as utils
import kollacli.common.utils as utils
TEST_SUFFIX = 'test/'
VENV_PY_PATH = '.venv/bin/python'

View File

@ -16,7 +16,7 @@ from common import KollaCliTest
import os
import unittest
from kollacli.utils import get_kolla_etc
from kollacli.common.utils import get_kolla_etc
class TestFunctional(KollaCliTest):

View File

@ -17,7 +17,7 @@ from common import KollaCliTest
import os
import unittest
from kollacli.utils import get_kolla_etc
from kollacli.common.utils import get_kolla_etc
class TestFunctional(KollaCliTest):

View File

@ -15,13 +15,14 @@
import getopt
import sys
from kollacli import utils
from kollacli.common.utils import change_property
from kollacli.common.utils import sync_read_file
def _print_pwd_keys(path):
pwd_keys = ''
prefix = ''
pwd_data = utils.sync_read_file(path)
pwd_data = sync_read_file(path)
for line in pwd_data.split('\n'):
if line.startswith('#'):
# skip commented lines
@ -67,7 +68,7 @@ def main():
_print_pwd_keys(path)
else:
# edit a password
utils.change_property(path, pwd_key, pwd_value, clear_flag)
change_property(path, pwd_key, pwd_value, clear_flag)
if __name__ == '__main__':