From 2f4686393aab2697c511f7b975149a4ace7e789e Mon Sep 17 00:00:00 2001 From: Mohammad Banikazemi Date: Mon, 30 Nov 2015 15:37:54 -0500 Subject: [PATCH] Completing the basic configuration for Kuryr Adds support for providing a configuration file for specifying Kuryr configuration options. Updates the Kuryr scripts to use and if needed, generate the configuration file. Implements: blueprint kuryr-config Change-Id: I6909ef21da13a7137ffff019383be19306f35152 --- devstack/plugin.sh | 23 ++++++++++++++++++- devstack/settings | 5 ++++ kuryr/__init__.py | 2 -- kuryr/common/config.py | 18 +++++++++------ kuryr/controllers.py | 52 +++++++++++++++++++++++------------------- kuryr/server.py | 8 ++++++- scripts/run_kuryr.sh | 28 ++++++++++++++++++++++- scripts/run_server.py | 2 -- 8 files changed, 101 insertions(+), 37 deletions(-) diff --git a/devstack/plugin.sh b/devstack/plugin.sh index 39f76d8e..ba382e7d 100644 --- a/devstack/plugin.sh +++ b/devstack/plugin.sh @@ -47,6 +47,27 @@ if is_service_enabled kuryr; then echo "Done" fi + if [[ ! -d "${KURYR_CONFIG_DIR}" ]]; then + echo -n "${KURYR_CONFIG_DIR} directory is missing. Creating it... " + sudo mkdir -p ${KURYR_CONFIG_DIR} + echo "Done" + fi + + if [[ ! -f "${KURYR_CONFIG}" ]]; then + if [[ -f "${KURYR_DEFAULT_CONFIG}" ]]; then + echo -n "${KURYR_CONFIG} is missing. Copying the default one... " + sudo cp ${KURYR_DEFAULT_CONFIG} ${KURYR_CONFIG} + echo "Done" + else + echo -n "${KURYR_CONFIG} and the default config missing. Auto generating and copying one... " + cd ${KURYR_HOME} + tox -egenconfig + sudo cp ${KURYR_DEFAULT_CONFIG}.sample ${KURYR_DEFAULT_CONFIG} + sudo cp ${KURYR_DEFAULT_CONFIG} ${KURYR_CONFIG} + cd - + fi + fi + run_process etcd-server "$DEST/etcd/etcd-$ETCD_VERSION-linux-amd64/etcd" wget http://get.docker.com -O install_docker.sh @@ -56,7 +77,7 @@ if is_service_enabled kuryr; then sudo killall docker run_process docker-engine "sudo /usr/bin/docker daemon --cluster-store etcd://localhost:4001" - run_process kuryr "sudo PYTHONPATH=$PYTHONPATH:$DEST/kuryr SERVICE_USER=admin SERVICE_PASSWORD=$SERVICE_PASSWORD SERVICE_TENANT_NAME=admin SERVICE_TOKEN=$SERVICE_TOKEN IDENTITY_URL=http://127.0.0.1:5000/v2.0 python $DEST/kuryr/scripts/run_server.py" + run_process kuryr "sudo PYTHONPATH=$PYTHONPATH:$DEST/kuryr SERVICE_USER=admin SERVICE_PASSWORD=$SERVICE_PASSWORD SERVICE_TENANT_NAME=admin SERVICE_TOKEN=$SERVICE_TOKEN IDENTITY_URL=http://127.0.0.1:5000/v2.0 python $DEST/kuryr/scripts/run_server.py --config-file /etc/kuryr/kuryr.conf" fi if [[ "$1" == "unstack" ]]; then diff --git a/devstack/settings b/devstack/settings index 4f4ad44e..4c9b26ad 100644 --- a/devstack/settings +++ b/devstack/settings @@ -7,3 +7,8 @@ KURYR_DEFAULT_JSON=${KURYR_HOME}/etc/${KURYR_JSON_FILENAME} # https://github.com/docker/docker/blob/c4d45b6a29a91f2fb5d7a51ac36572f2a9b295c6/docs/extend/plugin_api.md#plugin-discovery KURYR_JSON_DIR=${KURYR_JSON_DIR:-/usr/lib/docker/plugins/kuryr} KURYR_JSON=${KURYR_JSON_DIR}/${KURYR_JSON_FILENAME} + +KURYR_CONFIG_FILENAME=kuryr.conf +KURYR_DEFAULT_CONFIG=${KURYR_HOME}/etc/${KURYR_CONFIG_FILENAME} +KURYR_CONFIG_DIR=${KURYR_CONFIG_DIR:-/etc/kuryr} +KURYR_CONFIG=${KURYR_CONFIG_DIR}/${KURYR_CONFIG_FILENAME} diff --git a/kuryr/__init__.py b/kuryr/__init__.py index 679c0999..b6461afa 100644 --- a/kuryr/__init__.py +++ b/kuryr/__init__.py @@ -18,5 +18,3 @@ from kuryr import utils app = utils.make_json_app(__name__) app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.INFO) - -from kuryr.controllers import app # noqa diff --git a/kuryr/common/config.py b/kuryr/common/config.py index 4845eb3b..9f974f3b 100644 --- a/kuryr/common/config.py +++ b/kuryr/common/config.py @@ -64,17 +64,21 @@ keystone_opts = [ default=os.environ.get('SERVICE_TOKEN'), help=_('The admin token.')), ] - - -CONF = cfg.CONF -CONF.register_opts(core_opts) -CONF.register_opts(neutron_opts, group='neutron_client') -CONF.register_opts(keystone_opts, group='keystone_client') - binding_opts = [ cfg.StrOpt('veth_dst_prefix', default='eth', help=('The name prefix of the veth endpoint put inside the ' 'container.')) ] + + +CONF = cfg.CONF +CONF.register_opts(core_opts) +CONF.register_opts(neutron_opts, group='neutron_client') +CONF.register_opts(keystone_opts, group='keystone_client') CONF.register_opts(binding_opts, 'binding') + + +def init(args, **kwargs): + cfg.CONF(args=args, project='kuryr', + version='0.1', **kwargs) diff --git a/kuryr/controllers.py b/kuryr/controllers.py index b5eacb60..3a6bf3f8 100644 --- a/kuryr/controllers.py +++ b/kuryr/controllers.py @@ -28,34 +28,39 @@ from kuryr.common import exceptions from kuryr import schemata from kuryr import utils + MANDATORY_NEUTRON_EXTENSION = "subnet_allocation" -cfg.CONF.import_group('neutron_client', 'kuryr.common.config') -cfg.CONF.import_group('keystone_client', 'kuryr.common.config') -keystone_conf = cfg.CONF.keystone_client -username = keystone_conf.admin_user -tenant_name = keystone_conf.admin_tenant_name -password = keystone_conf.admin_password -auth_token = keystone_conf.admin_token -auth_uri = keystone_conf.auth_uri.rstrip('/') +def neutron_client(): + """Creates the Neutron client for communicating with Neutron.""" + if not hasattr(app, 'neutron'): + cfg.CONF.import_group('neutron_client', 'kuryr.common.config') + cfg.CONF.import_group('keystone_client', 'kuryr.common.config') -neutron_uri = cfg.CONF.neutron_client.neutron_uri -enable_dhcp = cfg.CONF.neutron_client.enable_dhcp + keystone_conf = cfg.CONF.keystone_client + username = keystone_conf.admin_user + tenant_name = keystone_conf.admin_tenant_name + password = keystone_conf.admin_password + auth_token = keystone_conf.admin_token + auth_uri = keystone_conf.auth_uri.rstrip('/') -if username and password: - # Authenticate with password crentials - app.neutron = utils.get_neutron_client( - url=neutron_uri, username=username, tenant_name=tenant_name, - password=password, auth_url=auth_uri) -else: - app.neutron = utils.get_neutron_client_simple( - url=neutron_uri, token=auth_token) + neutron_uri = cfg.CONF.neutron_client.neutron_uri + if username and password: + # Authenticate with password crentials + app.neutron = utils.get_neutron_client( + url=neutron_uri, username=username, tenant_name=tenant_name, + password=password, auth_url=auth_uri) + else: + app.neutron = utils.get_neutron_client_simple( + url=neutron_uri, token=auth_token) + + app.enable_dhcp = cfg.CONF.neutron_client.enable_dhcp + app.neutron.format = 'json' def check_for_neutron_ext_support(): - """Validates for mandatory extension support availability in neutron. - """ + """Validates for mandatory extension support availability in neutron.""" try: app.neutron.show_extension(MANDATORY_NEUTRON_EXTENSION) except n_exceptions.NeutronClientException as e: @@ -71,8 +76,6 @@ SUBNET_POOLS_V4 = [ SUBNET_POOLS_V6 = [ p.strip() for p in os.environ.get('SUBNET_POOLS_V6', 'kuryr6').split(',')] -app.neutron.format = 'json' - def _cache_default_subnetpool_ids(app): """Caches IDs of the default subnetpools as app.DEFAULT_POOL_IDS.""" @@ -147,7 +150,7 @@ def _process_subnet(neutron_network_id, endpoint_id, interface_cidr, 'network_id': neutron_network_id, 'ip_version': cidr.version, 'cidr': subnet_cidr, - 'enable_dhcp': enable_dhcp, + 'enable_dhcp': app.enable_dhcp, } if pool_id: del new_subnet['cidr'] @@ -747,3 +750,6 @@ def network_driver_leave(): app.logger.error('Cleaning the veth pair up was failed.') return flask.jsonify(constants.SCHEMA['SUCCESS']) + + +neutron_client() diff --git a/kuryr/server.py b/kuryr/server.py index 65d2dae1..b8bc9b07 100644 --- a/kuryr/server.py +++ b/kuryr/server.py @@ -10,9 +10,15 @@ # License for the specific language governing permissions and limitations # under the License. -from kuryr import app +import sys def start(): + from kuryr.common import config + config.init(sys.argv[1:]) + + from kuryr import app + from kuryr import controllers + controllers.check_for_neutron_ext_support() app.debug = True app.run("0.0.0.0", port=2377) diff --git a/scripts/run_kuryr.sh b/scripts/run_kuryr.sh index 1925bd1b..5d65a15d 100755 --- a/scripts/run_kuryr.sh +++ b/scripts/run_kuryr.sh @@ -20,6 +20,12 @@ KURYR_DEFAULT_JSON=${KURYR_HOME}/etc/${KURYR_JSON_FILENAME} KURYR_JSON_DIR=${KURYR_JSON_DIR:-/usr/lib/docker/plugins/kuryr} KURYR_JSON=${KURYR_JSON_DIR}/${KURYR_JSON_FILENAME} +KURYR_CONFIG_FILENAME=kuryr.conf +KURYR_DEFAULT_CONFIG=${KURYR_HOME}/etc/${KURYR_CONFIG_FILENAME} +KURYR_CONFIG_DIR=${KURYR_CONFIG_DIR:-/etc/kuryr} +KURYR_CONFIG=${KURYR_CONFIG_DIR}/${KURYR_CONFIG_FILENAME} + + if [[ ! -d "${KURYR_JSON_DIR}" ]]; then echo -n "${KURYR_JSON_DIR} directory is missing. Creating it... " sudo mkdir -p ${KURYR_JSON_DIR} @@ -32,4 +38,24 @@ if [[ ! -f "${KURYR_JSON}" ]]; then echo "Done" fi -PYTHONPATH=${KURYR_HOME} python ${KURYR_HOME}/scripts/run_server.py +if [[ ! -d "${KURYR_CONFIG_DIR}" ]]; then + echo -n "${KURYR_CONFIG_DIR} directory is missing. Creating it... " + sudo mkdir -p ${KURYR_CONFIG_DIR} + echo "Done" +fi + +if [[ ! -f "${KURYR_CONFIG}" ]]; then + if [[ -f "${KURYR_DEFAULT_CONFIG}" ]]; then + echo -n "${KURYR_CONFIG} is missing. Copying the default one... " + sudo cp ${KURYR_DEFAULT_CONFIG} ${KURYR_CONFIG} + echo "Done" + else + echo -n "${KURYR_CONFIG} and the default config missing. Auto generating and copying one... " + cd ${KURYR_HOME} + tox -egenconfig + sudo cp ${KURYR_DEFAULT_CONFIG}.sample ${KURYR_DEFAULT_CONFIG} + sudo cp ${KURYR_DEFAULT_CONFIG} ${KURYR_CONFIG} + fi +fi + +PYTHONPATH=${KURYR_HOME} python ${KURYR_HOME}/scripts/run_server.py --config-file /etc/kuryr/kuryr.conf diff --git a/scripts/run_server.py b/scripts/run_server.py index 16715043..f0b2e5af 100755 --- a/scripts/run_server.py +++ b/scripts/run_server.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from kuryr import controllers from kuryr import server -controllers.check_for_neutron_ext_support() server.start()