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:
parent
765e13b045
commit
c56fe96eed
@ -14,8 +14,8 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config import keystone_pki
|
from os_cloud_config import keystone_pki
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
@ -51,13 +51,13 @@ def parse_args():
|
|||||||
'heat metadata file injected into image). '
|
'heat metadata file injected into image). '
|
||||||
'Different key/certs names and different '
|
'Different key/certs names and different '
|
||||||
'parent node are used (default: false)')
|
'parent node are used (default: false)')
|
||||||
utils._add_logging_arguments(parser)
|
environment._add_logging_arguments(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
utils._configure_logging(args)
|
environment._configure_logging(args)
|
||||||
|
|
||||||
if args.heatenv:
|
if args.heatenv:
|
||||||
keystone_pki.generate_certs_into_json(args.heatenv, args.seed)
|
keystone_pki.generate_certs_into_json(args.heatenv, args.seed)
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config.keystone import initialize
|
from os_cloud_config.keystone import initialize
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
@ -63,13 +63,13 @@ def parse_args():
|
|||||||
parser.add_argument('--poll-interval', dest='pollinterval',
|
parser.add_argument('--poll-interval', dest='pollinterval',
|
||||||
default=10, type=int,
|
default=10, type=int,
|
||||||
help="Seconds to wait between keystone checks")
|
help="Seconds to wait between keystone checks")
|
||||||
utils._add_logging_arguments(parser)
|
environment._add_logging_arguments(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
utils._configure_logging(args)
|
environment._configure_logging(args)
|
||||||
|
|
||||||
initialize(args.host, args.admin_token, args.admin_email,
|
initialize(args.host, args.admin_token, args.admin_email,
|
||||||
args.admin_password, args.region, args.ssl, args.public,
|
args.admin_password, args.region, args.ssl, args.public,
|
||||||
|
@ -18,8 +18,8 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config import nodes
|
from os_cloud_config import nodes
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
@ -45,18 +45,18 @@ def parse_args():
|
|||||||
parser.add_argument('-n', '--nodes', dest='nodes', required=True,
|
parser.add_argument('-n', '--nodes', dest='nodes', required=True,
|
||||||
help='A JSON file containing a list of nodes that '
|
help='A JSON file containing a list of nodes that '
|
||||||
'are intended to be registered')
|
'are intended to be registered')
|
||||||
utils._add_logging_arguments(parser)
|
environment._add_logging_arguments(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
utils._configure_logging(args)
|
environment._configure_logging(args)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(args.nodes, 'r') as node_file:
|
with open(args.nodes, 'r') as node_file:
|
||||||
nodes_list = json.load(node_file)
|
nodes_list = json.load(node_file)
|
||||||
utils._ensure_environment()
|
environment._ensure()
|
||||||
|
|
||||||
# TODO(StevenK): Filter out registered nodes.
|
# TODO(StevenK): Filter out registered nodes.
|
||||||
nodes.register_all_nodes(args.service_host, nodes_list)
|
nodes.register_all_nodes(args.service_host, nodes_list)
|
||||||
|
@ -17,8 +17,8 @@ import simplejson
|
|||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config import keystone
|
from os_cloud_config import keystone
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
@ -53,13 +53,13 @@ def parse_args():
|
|||||||
parser.add_argument('-r', '--region', dest='region',
|
parser.add_argument('-r', '--region', dest='region',
|
||||||
help='represents the geographic location of the '
|
help='represents the geographic location of the '
|
||||||
'service endpoint')
|
'service endpoint')
|
||||||
utils._add_logging_arguments(parser)
|
environment._add_logging_arguments(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main(stdout=None):
|
def main(stdout=None):
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
utils._configure_logging(args)
|
environment._configure_logging(args)
|
||||||
|
|
||||||
sys.stderr.write(args.services)
|
sys.stderr.write(args.services)
|
||||||
if os.path.isfile(args.services):
|
if os.path.isfile(args.services):
|
||||||
|
@ -18,8 +18,8 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config import neutron
|
from os_cloud_config import neutron
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
@ -56,16 +56,16 @@ def parse_args():
|
|||||||
parser.add_argument('-n', '--network-json', dest='json',
|
parser.add_argument('-n', '--network-json', dest='json',
|
||||||
help='JSON formatted description of the network(s) to '
|
help='JSON formatted description of the network(s) to '
|
||||||
'create', required=True)
|
'create', required=True)
|
||||||
utils._add_logging_arguments(parser)
|
environment._add_logging_arguments(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
utils._configure_logging(args)
|
environment._configure_logging(args)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
utils._ensure_environment()
|
environment._ensure()
|
||||||
with open(args.json, 'r') as jsonfile:
|
with open(args.json, 'r') as jsonfile:
|
||||||
network_desc = json.load(jsonfile)
|
network_desc = json.load(jsonfile)
|
||||||
neutron.initialize_neutron(network_desc)
|
neutron.initialize_neutron(network_desc)
|
||||||
|
0
os_cloud_config/cmd/utils/__init__.py
Normal file
0
os_cloud_config/cmd/utils/__init__.py
Normal file
55
os_cloud_config/cmd/utils/environment.py
Normal file
55
os_cloud_config/cmd/utils/environment.py
Normal 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)
|
0
os_cloud_config/cmd/utils/tests/__init__.py
Normal file
0
os_cloud_config/cmd/utils/tests/__init__.py
Normal file
@ -16,9 +16,9 @@
|
|||||||
import mock
|
import mock
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
from os_cloud_config.cmd.utils import environment
|
||||||
from os_cloud_config import exception
|
from os_cloud_config import exception
|
||||||
from os_cloud_config.tests import base
|
from os_cloud_config.tests import base
|
||||||
from os_cloud_config import utils
|
|
||||||
|
|
||||||
|
|
||||||
class UtilsTest(base.TestCase):
|
class UtilsTest(base.TestCase):
|
||||||
@ -29,7 +29,7 @@ class UtilsTest(base.TestCase):
|
|||||||
"environment variables are required to be set.")
|
"environment variables are required to be set.")
|
||||||
with testtools.ExpectedException(exception.MissingEnvironment,
|
with testtools.ExpectedException(exception.MissingEnvironment,
|
||||||
message):
|
message):
|
||||||
utils._ensure_environment()
|
environment._ensure()
|
||||||
|
|
||||||
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
||||||
'OS_TENANT_NAME': 'a'})
|
'OS_TENANT_NAME': 'a'})
|
||||||
@ -37,9 +37,9 @@ class UtilsTest(base.TestCase):
|
|||||||
message = "OS_USERNAME environment variable is required to be set."
|
message = "OS_USERNAME environment variable is required to be set."
|
||||||
with testtools.ExpectedException(exception.MissingEnvironment,
|
with testtools.ExpectedException(exception.MissingEnvironment,
|
||||||
message):
|
message):
|
||||||
utils._ensure_environment()
|
environment._ensure()
|
||||||
|
|
||||||
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
||||||
'OS_TENANT_NAME': 'a', 'OS_USERNAME': 'a'})
|
'OS_TENANT_NAME': 'a', 'OS_USERNAME': 'a'})
|
||||||
def test_ensure_environment_missing_none(self):
|
def test_ensure_environment_missing_none(self):
|
||||||
self.assertIs(None, utils._ensure_environment())
|
self.assertIs(None, environment._ensure())
|
@ -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)
|
|
Loading…
Reference in New Issue
Block a user