Moving code from __init__

Putting a function in the __init__ is considered a bad practice
in Python world. Moving it to CMD utils where it should belong.

Change-Id: I16279053202659c796bd6de2f4afdbf51b245a5a
This commit is contained in:
Ladislav Smola 2014-08-26 10:02:03 +02:00
parent 765e13b045
commit c56fe96eed
10 changed files with 76 additions and 76 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

View File

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

View File

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

View File

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