diff --git a/os_cloud_config/cmd/generate_keystone_pki.py b/os_cloud_config/cmd/generate_keystone_pki.py index 569b675..db7ac16 100644 --- a/os_cloud_config/cmd/generate_keystone_pki.py +++ b/os_cloud_config/cmd/generate_keystone_pki.py @@ -14,8 +14,8 @@ import argparse import textwrap +from os_cloud_config.cmd.utils import environment from os_cloud_config import keystone_pki -from os_cloud_config import utils def parse_args(): @@ -51,13 +51,13 @@ def parse_args(): 'heat metadata file injected into image). ' 'Different key/certs names and different ' 'parent node are used (default: false)') - utils._add_logging_arguments(parser) + environment._add_logging_arguments(parser) return parser.parse_args() def main(): args = parse_args() - utils._configure_logging(args) + environment._configure_logging(args) if args.heatenv: keystone_pki.generate_certs_into_json(args.heatenv, args.seed) diff --git a/os_cloud_config/cmd/init_keystone.py b/os_cloud_config/cmd/init_keystone.py index f43972f..c82877a 100644 --- a/os_cloud_config/cmd/init_keystone.py +++ b/os_cloud_config/cmd/init_keystone.py @@ -16,8 +16,8 @@ import argparse import textwrap +from os_cloud_config.cmd.utils import environment from os_cloud_config.keystone import initialize -from os_cloud_config import utils def parse_args(): @@ -63,13 +63,13 @@ def parse_args(): parser.add_argument('--poll-interval', dest='pollinterval', default=10, type=int, help="Seconds to wait between keystone checks") - utils._add_logging_arguments(parser) + environment._add_logging_arguments(parser) return parser.parse_args() def main(): args = parse_args() - utils._configure_logging(args) + environment._configure_logging(args) initialize(args.host, args.admin_token, args.admin_email, args.admin_password, args.region, args.ssl, args.public, diff --git a/os_cloud_config/cmd/register_nodes.py b/os_cloud_config/cmd/register_nodes.py index a42ba7c..8df8b7a 100644 --- a/os_cloud_config/cmd/register_nodes.py +++ b/os_cloud_config/cmd/register_nodes.py @@ -18,8 +18,8 @@ import json import logging import textwrap +from os_cloud_config.cmd.utils import environment from os_cloud_config import nodes -from os_cloud_config import utils def parse_args(): @@ -45,18 +45,18 @@ def parse_args(): parser.add_argument('-n', '--nodes', dest='nodes', required=True, help='A JSON file containing a list of nodes that ' 'are intended to be registered') - utils._add_logging_arguments(parser) + environment._add_logging_arguments(parser) return parser.parse_args() def main(): args = parse_args() - utils._configure_logging(args) + environment._configure_logging(args) try: with open(args.nodes, 'r') as node_file: nodes_list = json.load(node_file) - utils._ensure_environment() + environment._ensure() # TODO(StevenK): Filter out registered nodes. nodes.register_all_nodes(args.service_host, nodes_list) diff --git a/os_cloud_config/cmd/setup_endpoints.py b/os_cloud_config/cmd/setup_endpoints.py index fd331ee..b970e58 100644 --- a/os_cloud_config/cmd/setup_endpoints.py +++ b/os_cloud_config/cmd/setup_endpoints.py @@ -17,8 +17,8 @@ import simplejson import sys import textwrap +from os_cloud_config.cmd.utils import environment from os_cloud_config import keystone -from os_cloud_config import utils def parse_args(): @@ -53,13 +53,13 @@ def parse_args(): parser.add_argument('-r', '--region', dest='region', help='represents the geographic location of the ' 'service endpoint') - utils._add_logging_arguments(parser) + environment._add_logging_arguments(parser) return parser.parse_args() def main(stdout=None): args = parse_args() - utils._configure_logging(args) + environment._configure_logging(args) sys.stderr.write(args.services) if os.path.isfile(args.services): diff --git a/os_cloud_config/cmd/setup_neutron.py b/os_cloud_config/cmd/setup_neutron.py index 296248a..7813b6f 100644 --- a/os_cloud_config/cmd/setup_neutron.py +++ b/os_cloud_config/cmd/setup_neutron.py @@ -18,8 +18,8 @@ import json import logging import textwrap +from os_cloud_config.cmd.utils import environment from os_cloud_config import neutron -from os_cloud_config import utils def parse_args(): @@ -56,16 +56,16 @@ def parse_args(): parser.add_argument('-n', '--network-json', dest='json', help='JSON formatted description of the network(s) to ' 'create', required=True) - utils._add_logging_arguments(parser) + environment._add_logging_arguments(parser) return parser.parse_args() def main(): args = parse_args() - utils._configure_logging(args) + environment._configure_logging(args) try: - utils._ensure_environment() + environment._ensure() with open(args.json, 'r') as jsonfile: network_desc = json.load(jsonfile) neutron.initialize_neutron(network_desc) diff --git a/os_cloud_config/cmd/utils/__init__.py b/os_cloud_config/cmd/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/os_cloud_config/cmd/utils/environment.py b/os_cloud_config/cmd/utils/environment.py new file mode 100644 index 0000000..5168fc2 --- /dev/null +++ b/os_cloud_config/cmd/utils/environment.py @@ -0,0 +1,55 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# 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 logging.config +import os +import sys + +from os_cloud_config import exception + + +def _ensure(): + environ = ("OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL", "OS_TENANT_NAME") + missing = set(environ).difference(os.environ) + plural = "s are" + if missing: + if len(missing) == 1: + plural = " is" + message = ("%s environment variable%s required to be set." % ( + ", ".join(sorted(missing)), plural)) + raise exception.MissingEnvironment(message) + + +def _add_logging_arguments(parser): + group = parser.add_mutually_exclusive_group() + group.add_argument('--debug', action='store_true', + help='set logging level to DEBUG (default is INFO)') + group.add_argument('--log-config', + help='external logging configuration file') + + +def _configure_logging(args): + if args.log_config: + logging.config.fileConfig(args.log_config, + disable_existing_loggers=False) + else: + format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + date_format = '%Y-%m-%d %H:%M:%S' + log_level = logging.DEBUG if args.debug else logging.INFO + logging.basicConfig(datefmt=date_format, + format=format, + level=log_level, + stream=sys.stdout) diff --git a/os_cloud_config/cmd/utils/tests/__init__.py b/os_cloud_config/cmd/utils/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/os_cloud_config/utils/tests/test_utils.py b/os_cloud_config/cmd/utils/tests/test_environment.py similarity index 90% rename from os_cloud_config/utils/tests/test_utils.py rename to os_cloud_config/cmd/utils/tests/test_environment.py index 31aed45..df7d813 100644 --- a/os_cloud_config/utils/tests/test_utils.py +++ b/os_cloud_config/cmd/utils/tests/test_environment.py @@ -16,9 +16,9 @@ import mock import testtools +from os_cloud_config.cmd.utils import environment from os_cloud_config import exception from os_cloud_config.tests import base -from os_cloud_config import utils class UtilsTest(base.TestCase): @@ -29,7 +29,7 @@ class UtilsTest(base.TestCase): "environment variables are required to be set.") with testtools.ExpectedException(exception.MissingEnvironment, message): - utils._ensure_environment() + environment._ensure() @mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a', 'OS_TENANT_NAME': 'a'}) @@ -37,9 +37,9 @@ class UtilsTest(base.TestCase): message = "OS_USERNAME environment variable is required to be set." with testtools.ExpectedException(exception.MissingEnvironment, message): - utils._ensure_environment() + environment._ensure() @mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a', 'OS_TENANT_NAME': 'a', 'OS_USERNAME': 'a'}) def test_ensure_environment_missing_none(self): - self.assertIs(None, utils._ensure_environment()) + self.assertIs(None, environment._ensure()) diff --git a/os_cloud_config/utils/__init__.py b/os_cloud_config/utils/__init__.py index 8efeaae..e69de29 100644 --- a/os_cloud_config/utils/__init__.py +++ b/os_cloud_config/utils/__init__.py @@ -1,55 +0,0 @@ -# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. -# -# 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 logging.config -import os -import sys - -from os_cloud_config import exception - - -def _ensure_environment(): - environ = ("OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL", "OS_TENANT_NAME") - missing = set(environ).difference(os.environ) - plural = "s are" - if missing: - if len(missing) == 1: - plural = " is" - message = ("%s environment variable%s required to be set." % ( - ", ".join(sorted(missing)), plural)) - raise exception.MissingEnvironment(message) - - -def _add_logging_arguments(parser): - group = parser.add_mutually_exclusive_group() - group.add_argument('--debug', action='store_true', - help='set logging level to DEBUG (default is INFO)') - group.add_argument('--log-config', - help='external logging configuration file') - - -def _configure_logging(args): - if args.log_config: - logging.config.fileConfig(args.log_config, - disable_existing_loggers=False) - else: - format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - date_format = '%Y-%m-%d %H:%M:%S' - log_level = logging.DEBUG if args.debug else logging.INFO - logging.basicConfig(datefmt=date_format, - format=format, - level=log_level, - stream=sys.stdout)