From 37c085fd6c63f01a6891be4cc55124eee90bed68 Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Mon, 4 Mar 2019 18:36:19 +0100 Subject: [PATCH] Add DevStack plugin This patch adds a DevStack plugin to cinderlib so it can be installed directly by DevStack and used for CI jobs as well as development. When installing it will also get the Cinder configuration and generate a .py file with the right initialization of the different backends that have been configured in /etc/cinder/cinder.conf by the DevStack Cinder job and its plugins. Change-Id: I12f6b0e1bc047f1915e4f7532ea59495477f9b4a --- cinderlib/cmd/__init__.py | 0 .../cmd/cinder_cfg_to_python.py | 12 ++++-- .../functional => cmd}/cinder_to_yaml.py | 0 cinderlib/tests/functional/base_tests.py | 2 +- devstack/README.rst | 37 +++++++++++++++++ devstack/override-defaults | 7 ++++ devstack/plugin.sh | 40 +++++++++++++++++++ devstack/settings | 9 +++++ setup.cfg | 2 + 9 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 cinderlib/cmd/__init__.py rename tools/cinder-cfg-to-python.py => cinderlib/cmd/cinder_cfg_to_python.py (94%) rename cinderlib/{tests/functional => cmd}/cinder_to_yaml.py (100%) create mode 100644 devstack/README.rst create mode 100644 devstack/override-defaults create mode 100644 devstack/plugin.sh create mode 100644 devstack/settings diff --git a/cinderlib/cmd/__init__.py b/cinderlib/cmd/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/cinder-cfg-to-python.py b/cinderlib/cmd/cinder_cfg_to_python.py similarity index 94% rename from tools/cinder-cfg-to-python.py rename to cinderlib/cmd/cinder_cfg_to_python.py index 9460b80..3f9c6fa 100755 --- a/tools/cinder-cfg-to-python.py +++ b/cinderlib/cmd/cinder_cfg_to_python.py @@ -35,7 +35,7 @@ import sys import six -from cinderlib.tests.functional import cinder_to_yaml +from cinderlib.cmd import cinder_to_yaml def _to_str(value): @@ -44,7 +44,7 @@ def _to_str(value): return value -def main(source, dest): +def convert(source, dest): config = cinder_to_yaml.convert(source) result = ['import cinderlib as cl'] for backend in config['backends']: @@ -57,7 +57,11 @@ def main(source, dest): f.write('\n\n'.join(result) + '\n') -if __name__ == '__main__': +def main(): source = '/etc/cinder/cinder.conf' if len(sys.argv) < 2 else sys.argv[1] dest = '/dev/stdout' if len(sys.argv) < 3 else sys.argv[2] - main(source, dest) + convert(source, dest) + + +if __name__ == '__main__': + main() diff --git a/cinderlib/tests/functional/cinder_to_yaml.py b/cinderlib/cmd/cinder_to_yaml.py similarity index 100% rename from cinderlib/tests/functional/cinder_to_yaml.py rename to cinderlib/cmd/cinder_to_yaml.py diff --git a/cinderlib/tests/functional/base_tests.py b/cinderlib/tests/functional/base_tests.py index 621f23e..b772a29 100644 --- a/cinderlib/tests/functional/base_tests.py +++ b/cinderlib/tests/functional/base_tests.py @@ -24,7 +24,7 @@ import unittest2 import yaml import cinderlib -from cinderlib.tests.functional import cinder_to_yaml +from cinderlib.cmd import cinder_to_yaml def set_backend(func, new_name, backend_name): diff --git a/devstack/README.rst b/devstack/README.rst new file mode 100644 index 0000000..2c93237 --- /dev/null +++ b/devstack/README.rst @@ -0,0 +1,37 @@ +Cinderlib DevStack Plugin +========================= + +This directory contains the cinderlib DevStack plugin. + +To configure cinderlib with DevStack, you will need to enable this plugin by +adding one line to the [[local|localrc]] section of your local.conf file. + +To enable the plugin, add a line of the form:: + + enable_plugin cinderlib [GITREF] + +where:: + + is the URL of a cinderlib repository + [GITREF] is an optional git ref (branch/ref/tag). The default is master. + +For example:: + + enable_plugin cinderlib https://git.openstack.org/openstack/cinderlib + +Another example using Stein's stable branch:: + + enable_plugin cinderlib https://git.openstack.org/openstack/cinderlib stable/stein + +The cinderlib DevStack plugin will install cinderlib from Git by default, but +it can be installed from PyPi using the ``CINDERLIB_FROM_GIT`` configuration +option:: + + CINDERLIB_FROM_GIT=False + +The plugin will also generate the code equivalent to the deployed Cinder's +configuration in ``$CINDERLIB_SAMPLE_DIR/cinderlib.py`` which defaults to the +same directory where the Cinder configuration is saved. + +For more information, see the `DevStack plugin documentation +`_. diff --git a/devstack/override-defaults b/devstack/override-defaults new file mode 100644 index 0000000..90adc86 --- /dev/null +++ b/devstack/override-defaults @@ -0,0 +1,7 @@ +ALL_LIBS+=" cinderlib" +CINDERLIB_FROM_GIT=$(trueorfalse True CINDERLIB_FROM_GIT) + +if [[ "$CINDERLIB_FROM_GIT" == "True" ]]; then + PROJECTS="openstack/cinderlib $PROJECTS" + LIBS_FROM_GIT="cinderlib,$LIBS_FROM_GIT" +fi diff --git a/devstack/plugin.sh b/devstack/plugin.sh new file mode 100644 index 0000000..91fded3 --- /dev/null +++ b/devstack/plugin.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# plugin.sh - DevStack plugin.sh dispatch script for cinderlib + +_XTRACE_CINDERLIB=$(set +o | grep xtrace) + +function install_cinderlib { + if use_library_from_git "cinderlib"; then + git_clone_by_name "cinderlib" + setup_dev_lib "cinderlib" + else + pip_install cinderlib + fi +} + +stable_compare="stable/[a-r]" +# Cinderlib only makes sense if Cinder is enabled and we are in stein or later +if [[ ! "${GITBRANCH["cinderlib"]}" =~ $stable_compare ]] && is_service_enabled cinder; then + + if [[ "$1" == "stack" && "$2" == "install" ]]; then + # Perform installation of service source + echo_summary "Installing cinderlib" + install_cinderlib + + # Plugins such as Ceph configure themselves at post-config, so we have to + # configure ourselves at the next stage, "extra" + elif [[ "$1" == "stack" && "$2" == "extra" ]]; then + # Generate the cinderlib configuration + echo_summary "Generating cinderlib initialization example python code" + sudo cinder-cfg-to-cinderlib-code $CINDER_CONF $CINDERLIB_SAMPLE + fi + + if [[ "$1" == "clean" || "$1" == "unstack" ]]; then + echo_summary "Removing cinderlib and its code example from cinder.conf" + sudo rm -f $CINDERLIB_SAMPLE + pip_uninstall cinderlib + fi +fi + +# Restore xtrace +$_XTRACE_CINDERLIB diff --git a/devstack/settings b/devstack/settings new file mode 100644 index 0000000..7f4728e --- /dev/null +++ b/devstack/settings @@ -0,0 +1,9 @@ +# Defaults +# -------- + +# Set up default directories +CINDERLIB_SAMPLE_DIR=${CINDERLIB_CONF_DIR:-/etc/cinder} +CINDERLIB_SAMPLE=$CINDERLIB_SAMPLE_DIR/cinderlib.py +CINDERLIB_FROM_GIT=$(trueorfalse True CINDERLIB_FROM_GIT) + +define_plugin cinderlib diff --git a/setup.cfg b/setup.cfg index 71727ed..9e64e70 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,8 @@ cinderlib.persistence.storage = memory = cinderlib.persistence.memory:MemoryPersistence db = cinderlib.persistence.dbms:DBPersistence memory_db = cinderlib.persistence.dbms:MemoryDBPersistence +console_scripts = + cinder-cfg-to-cinderlib-code = cinderlib.cmd.cinder_cfg_to_python:main [egg_info] tag_build =