diff --git a/kolla_kubernetes/cmd/shell.py b/kolla_kubernetes/app.py similarity index 89% rename from kolla_kubernetes/cmd/shell.py rename to kolla_kubernetes/app.py index 555c87de8..548b5502c 100755 --- a/kolla_kubernetes/cmd/shell.py +++ b/kolla_kubernetes/app.py @@ -29,14 +29,14 @@ logging.getLogger("requests").setLevel(logging.INFO) logging.getLogger("stevedore.extension").setLevel(logging.INFO) -class KollaKubernetesShell(app.App): +class KollaKubernetesApp(app.App): _singleton = None @staticmethod def Get(): - if KollaKubernetesShell._singleton is None: - KollaKubernetesShell._singleton = KollaKubernetesShell() - return KollaKubernetesShell._singleton + if KollaKubernetesApp._singleton is None: + KollaKubernetesApp._singleton = KollaKubernetesApp() + return KollaKubernetesApp._singleton def __init__(self): super(self.__class__, self).__init__( @@ -80,10 +80,10 @@ class KollaKubernetesShell(app.App): command.Command.get_parser(). That argparse namepace is then handed directly to the command.Command.take_action() method of each subcommand. Subcommands may access global options by - calling KollaKubernetesShell.Get().get_parsed_options(). + calling KollaKubernetesApp.Get().get_parsed_options(). """ - parser = super(KollaKubernetesShell, self).build_option_parser( + parser = super(KollaKubernetesApp, self).build_option_parser( description, version) @@ -102,7 +102,7 @@ class KollaKubernetesShell(app.App): def main(argv=sys.argv[1:]): - kks = KollaKubernetesShell().Get() + kks = KollaKubernetesApp().Get() return kks.run(argv) if __name__ == '__main__': diff --git a/kolla_kubernetes/cmd/__init__.py b/kolla_kubernetes/cmd/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/kolla_kubernetes/cli/__init__.py b/kolla_kubernetes/commands/__init__.py similarity index 100% rename from kolla_kubernetes/cli/__init__.py rename to kolla_kubernetes/commands/__init__.py diff --git a/kolla_kubernetes/common/type_utils.py b/kolla_kubernetes/commands/base_command.py similarity index 65% rename from kolla_kubernetes/common/type_utils.py rename to kolla_kubernetes/commands/base_command.py index d4cbef9c9..9b700822a 100644 --- a/kolla_kubernetes/common/type_utils.py +++ b/kolla_kubernetes/commands/base_command.py @@ -10,10 +10,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +from cliff import command -def str_to_bool(text): - if not text: - return False - if text.lower() in ['true', 'yes']: - return True - return False +from kolla_kubernetes.app import KollaKubernetesApp + + +class KollaKubernetesBaseCommand(command.Command): + + def get_global_args(self): + """Provides a method to access global parsed options""" + return KollaKubernetesApp.Get().get_parsed_options() diff --git a/kolla_kubernetes/cli/service.py b/kolla_kubernetes/commands/cmd_resource.py similarity index 81% rename from kolla_kubernetes/cli/service.py rename to kolla_kubernetes/commands/cmd_resource.py index 5b8ffc69f..1c9a183b1 100644 --- a/kolla_kubernetes/cli/service.py +++ b/kolla_kubernetes/commands/cmd_resource.py @@ -13,70 +13,21 @@ from __future__ import print_function import sys -from cliff import command -from oslo_config import cfg from oslo_log import log -from kolla_kubernetes.cmd.shell import KollaKubernetesShell -from kolla_kubernetes.common.utils import FileUtils -from kolla_kubernetes.common.utils import JinjaUtils -from kolla_kubernetes.common.utils import YamlUtils +from kolla_kubernetes.commands.base_command import KollaKubernetesBaseCommand from kolla_kubernetes.service_resources import KollaKubernetesResources from kolla_kubernetes.service_resources import Service +from kolla_kubernetes.utils import FileUtils +from kolla_kubernetes.utils import JinjaUtils +from kolla_kubernetes.utils import YamlUtils -CONF = cfg.CONF LOG = log.getLogger(__name__) KKR = KollaKubernetesResources.Get() -class _BaseCommand(command.Command): - - def get_global_args(self): - """Provides a method to access global parsed options""" - return KollaKubernetesShell.Get().get_parsed_options() - - -class _ServiceCommand(_BaseCommand): - - _action = None # must be set in derived classes - - def get_parser(self, prog_name): - parser = super(_ServiceCommand, self).get_parser(prog_name) - parser.add_argument('service') - return parser - - def take_action(self, parsed_args): - assert self._action is not None, ( - "code error: derived classes must set _action") - - service = KKR.getServiceByName(parsed_args.service) - if (self._action == 'bootstrap'): - service.do_apply('create', Service.LEGACY_BOOTSTRAP_RESOURCES) - elif (self._action == 'run'): - service.do_apply('create', Service.LEGACY_RUN_RESOURCES) - elif (self._action == 'kill'): - service.do_apply('delete', Service.VALID_RESOURCE_TYPES) - else: - raise Exception("Code Error") - - -class Bootstrap(_ServiceCommand): - """Roll out configurations and bootstrap a service.""" - _action = 'bootstrap' - - -class Run(_ServiceCommand): - """Run a service.""" - _action = 'run' - - -class Kill(_ServiceCommand): - """Kill a service.""" - _action = 'kill' - - -class Resource(_BaseCommand): +class Resource(KollaKubernetesBaseCommand): """Create or delete kolla-kubernetes resources""" def get_parser(self, prog_name): @@ -185,7 +136,7 @@ class ResourceTemplate(Resource): FileUtils.read_string_from_file(args.template_file))) -class ResourceMap(_BaseCommand): +class ResourceMap(KollaKubernetesBaseCommand): """List available kolla-kubernetes resources to be created or deleted""" # If the operator has any question on what Services have what resources, diff --git a/kolla_kubernetes/commands/cmd_service.py b/kolla_kubernetes/commands/cmd_service.py new file mode 100644 index 000000000..7074c0c19 --- /dev/null +++ b/kolla_kubernetes/commands/cmd_service.py @@ -0,0 +1,62 @@ +# 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. + +from __future__ import print_function + +from oslo_log import log + +from kolla_kubernetes.commands.base_command import KollaKubernetesBaseCommand +from kolla_kubernetes.service_resources import KollaKubernetesResources +from kolla_kubernetes.service_resources import Service + +LOG = log.getLogger(__name__) + +KKR = KollaKubernetesResources.Get() + + +class _ServiceCommand(KollaKubernetesBaseCommand): + + _action = None # must be set in derived classes + + def get_parser(self, prog_name): + parser = super(_ServiceCommand, self).get_parser(prog_name) + parser.add_argument('service') + return parser + + def take_action(self, parsed_args): + assert self._action is not None, ( + "code error: derived classes must set _action") + + service = KKR.getServiceByName(parsed_args.service) + if (self._action == 'bootstrap'): + service.do_apply('create', Service.LEGACY_BOOTSTRAP_RESOURCES) + elif (self._action == 'run'): + service.do_apply('create', Service.LEGACY_RUN_RESOURCES) + elif (self._action == 'kill'): + service.do_apply('delete', Service.VALID_RESOURCE_TYPES) + else: + raise Exception("Code Error") + + +class Bootstrap(_ServiceCommand): + """Roll out configurations and bootstrap a service.""" + _action = 'bootstrap' + + +class Run(_ServiceCommand): + """Run a service.""" + _action = 'run' + + +class Kill(_ServiceCommand): + """Kill a service.""" + _action = 'kill' diff --git a/kolla_kubernetes/common/__init__.py b/kolla_kubernetes/common/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/kolla_kubernetes/common/pathfinder.py b/kolla_kubernetes/pathfinder.py similarity index 97% rename from kolla_kubernetes/common/pathfinder.py rename to kolla_kubernetes/pathfinder.py index af8c0b13a..22b828d9d 100644 --- a/kolla_kubernetes/common/pathfinder.py +++ b/kolla_kubernetes/pathfinder.py @@ -38,8 +38,8 @@ class PathFinder(object): # Editable installs (aka. Development: pip install --editable .) # use this root path to locate ../kolla # For editable, resolves to /path/to/git/repo/kolla-kubernetes - return os.path.dirname(os.path.dirname( - os.path.dirname(os.path.abspath(__file__)))) + return os.path.dirname( + os.path.dirname(os.path.abspath(__file__))) @staticmethod def find_kolla_dir(): diff --git a/kolla_kubernetes/service_resources.py b/kolla_kubernetes/service_resources.py index b0cb8ca0d..7e7eb00f1 100644 --- a/kolla_kubernetes/service_resources.py +++ b/kolla_kubernetes/service_resources.py @@ -18,11 +18,11 @@ import time from oslo_config import cfg from oslo_log import log as logging -from kolla_kubernetes.common.pathfinder import PathFinder -from kolla_kubernetes.common.utils import ExecUtils -from kolla_kubernetes.common.utils import JinjaUtils -from kolla_kubernetes.common.utils import StringUtils -from kolla_kubernetes.common.utils import YamlUtils +from kolla_kubernetes.pathfinder import PathFinder +from kolla_kubernetes.utils import ExecUtils +from kolla_kubernetes.utils import JinjaUtils +from kolla_kubernetes.utils import StringUtils +from kolla_kubernetes.utils import YamlUtils CONF = cfg.CONF LOG = logging.getLogger() diff --git a/kolla_kubernetes/tests/common/__init__.py b/kolla_kubernetes/tests/common/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/kolla_kubernetes/tests/common/test_type_utils.py b/kolla_kubernetes/tests/common/test_type_utils.py deleted file mode 100644 index 90a01718d..000000000 --- a/kolla_kubernetes/tests/common/test_type_utils.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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. - -from kolla_kubernetes.common import type_utils -from kolla_kubernetes.tests import base - - -class StrToBoolTest(base.BaseTestCase): - - scenarios = [ - ('none', dict(text=None, expect=False)), - ('empty', dict(text='', expect=False)), - ('junk', dict(text='unlikely', expect=False)), - ('no', dict(text='no', expect=False)), - ('yes', dict(text='yes', expect=True)), - ('0', dict(text='0', expect=False)), - ('1', dict(text='1', expect=False)), - ('True', dict(text='True', expect=True)), - ('False', dict(text='False', expect=False)), - ('true', dict(text='true', expect=True)), - ('false', dict(text='false', expect=False)), - ('shouty', dict(text='TRUE', expect=True)), - ] - - def test_str_to_bool(self): - self.assertEqual(self.expect, type_utils.str_to_bool(self.text)) diff --git a/kolla_kubernetes/tests/common/test_pathfinder.py b/kolla_kubernetes/tests/test_pathfinder.py similarity index 94% rename from kolla_kubernetes/tests/common/test_pathfinder.py rename to kolla_kubernetes/tests/test_pathfinder.py index 51cd5c5c4..e84c69afd 100644 --- a/kolla_kubernetes/tests/common/test_pathfinder.py +++ b/kolla_kubernetes/tests/test_pathfinder.py @@ -10,7 +10,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from kolla_kubernetes.common.pathfinder import PathFinder +from kolla_kubernetes.pathfinder import PathFinder from kolla_kubernetes.tests import base diff --git a/kolla_kubernetes/tests/common/test_utils.py b/kolla_kubernetes/tests/test_utils.py similarity index 83% rename from kolla_kubernetes/tests/common/test_utils.py rename to kolla_kubernetes/tests/test_utils.py index fd195ee26..b15cf1e46 100644 --- a/kolla_kubernetes/tests/common/test_utils.py +++ b/kolla_kubernetes/tests/test_utils.py @@ -15,11 +15,12 @@ import os import tempfile import time -from kolla_kubernetes.common.utils import FileUtils -from kolla_kubernetes.common.utils import JinjaUtils -from kolla_kubernetes.common.utils import StringUtils -from kolla_kubernetes.common.utils import YamlUtils from kolla_kubernetes.tests import base +from kolla_kubernetes.utils import FileUtils +from kolla_kubernetes.utils import JinjaUtils +from kolla_kubernetes.utils import StringUtils +from kolla_kubernetes.utils import TypeUtils +from kolla_kubernetes.utils import YamlUtils test_yaml = ''' @@ -146,6 +147,27 @@ class TestStringUtils(UtilsTestCase): self.assertEqual(StringUtils.pad_str("aa", 2, ""), "aaaa") +class TestTypeUtils(base.BaseTestCase): + + scenarios = [ + ('none', dict(text=None, expect=False)), + ('empty', dict(text='', expect=False)), + ('junk', dict(text='unlikely', expect=False)), + ('no', dict(text='no', expect=False)), + ('yes', dict(text='yes', expect=True)), + ('0', dict(text='0', expect=False)), + ('1', dict(text='1', expect=False)), + ('True', dict(text='True', expect=True)), + ('False', dict(text='False', expect=False)), + ('true', dict(text='true', expect=True)), + ('false', dict(text='false', expect=False)), + ('shouty', dict(text='TRUE', expect=True)), + ] + + def test_str_to_bool(self): + self.assertEqual(self.expect, TypeUtils.str_to_bool(self.text)) + + class TestYamlUtils(UtilsTestCase): def test_write_and_read_string(self): diff --git a/kolla_kubernetes/common/utils.py b/kolla_kubernetes/utils.py similarity index 96% rename from kolla_kubernetes/common/utils.py rename to kolla_kubernetes/utils.py index 08c8803bd..22a93e9df 100644 --- a/kolla_kubernetes/common/utils.py +++ b/kolla_kubernetes/utils.py @@ -21,8 +21,6 @@ import yaml from oslo_log import log as logging -from kolla_kubernetes.common import type_utils - LOG = logging.getLogger() @@ -123,7 +121,7 @@ class JinjaUtils(object): loader=jinja2.DictLoader({name: template_str})) # Do not print type for bools "!!bool" on output - j2env.filters['bool'] = type_utils.str_to_bool + j2env.filters['bool'] = TypeUtils.str_to_bool # Add a "raise" keyword for raising exceptions from within jinja def jinja_raise(message): @@ -163,6 +161,17 @@ class StringUtils(object): return re.sub("^", (pad * num), s, 0, re.MULTILINE) +class TypeUtils(object): + + @staticmethod + def str_to_bool(text): + if not text: + return False + if text.lower() in ['true', 'yes']: + return True + return False + + class YamlUtils(object): @staticmethod diff --git a/setup.cfg b/setup.cfg index c212665db..a26c22c18 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,15 +28,15 @@ data_files = [entry_points] console_scripts = - kolla-kubernetes = kolla_kubernetes.cmd.shell:main + kolla-kubernetes = kolla_kubernetes.app:main kolla_kubernetes.cli = - bootstrap = kolla_kubernetes.cli.service:Bootstrap - run = kolla_kubernetes.cli.service:Run - kill = kolla_kubernetes.cli.service:Kill - resource = kolla_kubernetes.cli.service:Resource - resource-template = kolla_kubernetes.cli.service:ResourceTemplate - resource-map = kolla_kubernetes.cli.service:ResourceMap + bootstrap = kolla_kubernetes.commands.cmd_service:Bootstrap + run = kolla_kubernetes.commands.cmd_service:Run + kill = kolla_kubernetes.commands.cmd_service:Kill + resource = kolla_kubernetes.commands.cmd_resource:Resource + resource-template = kolla_kubernetes.commands.cmd_resource:ResourceTemplate + resource-map = kolla_kubernetes.commands.cmd_resource:ResourceMap [pbr] warnerrors = true diff --git a/tools/kolla_kubernetes.py b/tools/kolla_kubernetes.py index 39dd06475..738073644 120000 --- a/tools/kolla_kubernetes.py +++ b/tools/kolla_kubernetes.py @@ -1 +1 @@ -../kolla_kubernetes/cmd/shell.py \ No newline at end of file +../kolla_kubernetes/app.py \ No newline at end of file