Merge "Renamed/Moved files, Split some files, Renamed few classes"

This commit is contained in:
Jenkins 2016-07-19 00:45:12 +00:00 committed by Gerrit Code Review
commit 69f033591d
16 changed files with 138 additions and 126 deletions

View File

@ -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__':

View File

@ -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()

View File

@ -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,

View File

@ -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'

View File

@ -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():

View File

@ -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()

View File

@ -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))

View File

@ -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

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -1 +1 @@
../kolla_kubernetes/cmd/shell.py
../kolla_kubernetes/app.py