From 5bbd78e326e7726229bb94f887f18f8b27bb7a14 Mon Sep 17 00:00:00 2001 From: dineshbhor Date: Sun, 8 Apr 2018 19:38:25 -0700 Subject: [PATCH] Deploy masakari-api with uwsgi As per community goal [1], this patch allows deploying masakari-api with uwsgi. NOTE: This patch takes reference from the nova implementation of deploying nova-api with uwsgi. [1] https://governance.openstack.org/tc/goals/pike/deploy-api-in-wsgi.html Change-Id: I469d4b6df49ad3489722feae0b795c20a6a3913b --- devstack/plugin.sh | 55 +++++++++++++++---- devstack/settings | 4 ++ masakari/cmd/api.py | 41 +++++++++++++- .../wsgi-applications-3ed7d6b89f1a5785.yaml | 8 +++ setup.cfg | 3 + 5 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 releasenotes/notes/wsgi-applications-3ed7d6b89f1a5785.yaml diff --git a/devstack/plugin.sh b/devstack/plugin.sh index 4e3d281d..17d44c92 100644 --- a/devstack/plugin.sh +++ b/devstack/plugin.sh @@ -25,6 +25,10 @@ if is_service_enabled tls-proxy; then MASAKARI_SERVICE_PROTOCOL="https" fi +# Toggle for deploying Masakari under a wsgi server. +MASAKARI_USE_MOD_WSGI=${MASAKARI_USE_MOD_WSGI:-True} + + # Functions # --------- @@ -52,11 +56,19 @@ function create_masakari_accounts { local masakari_service=$(get_or_create_service "masakari" \ "instance-ha" "OpenStack High Availability") - get_or_create_endpoint $masakari_service \ - "$REGION_NAME" \ - "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" \ - "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" \ - "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" + if [ "$MASAKARI_USE_MOD_WSGI" == "False" ]; then + get_or_create_endpoint $masakari_service \ + "$REGION_NAME" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST:$MASAKARI_SERVICE_PORT/v1/\$(tenant_id)s" + else + get_or_create_endpoint $masakari_service \ + "$REGION_NAME" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST/instance-ha/v1/\$(tenant_id)s" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST/instance-ha/v1/\$(tenant_id)s" \ + "$MASAKARI_SERVICE_PROTOCOL://$SERVICE_HOST/instance-ha/v1/\$(tenant_id)s" + fi fi } @@ -69,6 +81,10 @@ function cleanup_masakari { # Clean up dirs rm -fr $MASAKARI_AUTH_CACHE_DIR/* rm -fr $MASAKARI_CONF_DIR/* + + if [ "$MASAKARI_USE_MOD_WSGI" == "True" ]; then + remove_uwsgi_config "$MASAKARI_UWSGI_CONF" "$MASAKARI_UWSGI" + fi } # iniset_conditional() - Sets the value in the inifile, but only if it's @@ -127,6 +143,10 @@ function configure_masakari { if is_service_enabled tls-proxy; then iniset $MASAKARI_CONF DEFAULT masakari_api_listen_port $MASAKARI_SERVICE_PORT_INT fi + + if [ "$MASAKARI_USE_MOD_WSGI" == "True" ]; then + write_uwsgi_config "$MASAKARI_UWSGI_CONF" "$MASAKARI_UWSGI" "/instance-ha" + fi } # install_masakari() - Collect source and prepare @@ -157,20 +177,35 @@ function init_masakari { get_or_add_user_project_role admin ${ADMIN_ALT_USERNAME} ${ALT_TENANT_NAME} } -# start_masakari() - Start running processes, including screen +# start_masakari() - Start running processes function start_masakari { + local masakari_url - if is_service_enabled tls-proxy; then - start_tls_proxy masakari-service '*' $MASAKARI_SERVICE_PORT $SERVICE_HOST $MASAKARI_SERVICE_PORT_INT + if [[ "$ENABLED_SERVICES" =~ "masakari-api" ]]; then + if [ "$MASAKARI_USE_MOD_WSGI" == "False" ]; then + run_process masakari-api "$MASAKARI_BIN_DIR/masakari-api --config-file=$MASAKARI_CONF --debug" + masakari_url=$MASAKARI_SERVICE_PROTOCOL://$MASAKARI_SERVICE_HOST:$MASAKARI_SERVICE_PORT + # Start proxy if tls enabled + if is_service_enabled tls_proxy; then + start_tls_proxy masakari-service '*' $MASAKARI_SERVICE_PORT $SERVICE_HOST $MASAKARI_SERVICE_PORT_INT + fi + else + run_process "masakari-api" "$MASAKARI_BIN_DIR/uwsgi --procname-prefix masakari-api --ini $MASAKARI_UWSGI_CONF" + masakari_url=$MASAKARI_SERVICE_PROTOCOL://$MASAKARI_SERVICE_HOST/instance-ha/v1 + fi + fi + + echo "Waiting for Masakari API to start..." + if ! wait_for_service $SERVICE_TIMEOUT $masakari_url; then + die $LINENO "masakari-api did not start" fi - run_process masakari-api "$MASAKARI_BIN_DIR/masakari-api --config-file=$MASAKARI_CONF --debug" run_process masakari-engine "$MASAKARI_BIN_DIR/masakari-engine --config-file=$MASAKARI_CONF --debug" } # stop_masakari() - Stop running processes function stop_masakari { - # Kill the masakari screen windows + # Kill the masakari services local serv for serv in masakari-engine masakari-api; do stop_process $serv diff --git a/devstack/settings b/devstack/settings index 454f31b2..1f761f34 100644 --- a/devstack/settings +++ b/devstack/settings @@ -17,6 +17,7 @@ MASAKARI_LOCAL_API_PASTE_INI=${MASAKARI_LOCAL_API_PASTE_INI:-${MASAKARI_LOCAL_CO MASAKARI_LOCAL_POLICY_JSON=${MASAKARI_LOCAL_POLICY_JSON:-${MASAKARI_LOCAL_CONF_DIR}/policy.json} MASAKARI_AUTH_CACHE_DIR=${MASAKARI_AUTH_CACHE_DIR:-/var/cache/masakari} +MASAKARI_SERVICE_HOST=${MASAKARI_SERVICE_HOST:-$SERVICE_HOST} MASAKARI_SERVICE_PROTOCOL=${MASAKARI_SERVICE_PROTOCOL:-http} # Support entry points installation of console scripts @@ -31,5 +32,8 @@ MASAKARI_MANAGE=$MASAKARI_BIN_DIR/masakari-manage MASAKARI_SERVICE_PORT=${MASAKARI_SERVICE_PORT:-15868} MASAKARI_SERVICE_PORT_INT=${MASAKARI_SERVICE_PORT_INT:-25868} +MASAKARI_UWSGI=$MASAKARI_BIN_DIR/masakari-wsgi +MASAKARI_UWSGI_CONF=$MASAKARI_CONF_DIR/masakari-api-uwsgi.ini + enable_service masakari masakari-api masakari-engine diff --git a/masakari/cmd/api.py b/masakari/cmd/api.py index 4f8ac251..e05e2961 100644 --- a/masakari/cmd/api.py +++ b/masakari/cmd/api.py @@ -16,22 +16,36 @@ """Starter script for Masakari API. """ - +import os import sys from oslo_log import log as logging +from oslo_service import _options as service_opts +from paste import deploy import six +from masakari.common import config import masakari.conf -from masakari import config +from masakari import config as api_config from masakari import exception from masakari import objects +from masakari import rpc from masakari import service +from masakari import version +CONFIG_FILES = ['api-paste.ini', 'masakari.conf'] CONF = masakari.conf.CONF +def _get_config_files(env=None): + if env is None: + env = os.environ + dirname = env.get('OS_MASAKARI_CONFIG_DIR', '/etc/masakari').strip() + return [os.path.join(dirname, config_file) + for config_file in CONFIG_FILES] + + def main(): config.parse_args(sys.argv) logging.setup(CONF, "masakari") @@ -54,3 +68,26 @@ def main(): sys.exit(1) launcher.wait() + + +def initialize_application(): + conf_files = _get_config_files() + api_config.parse_args([], default_config_files=conf_files) + logging.setup(CONF, "masakari") + + objects.register_all() + CONF(sys.argv[1:], project='masakari', version=version.version_string()) + + # NOTE: Dump conf at debug (log_options option comes from oslo.service) + # This is gross but we don't have a public hook into oslo.service to + # register these options, so we are doing it manually for now; + # remove this when we have a hook method into oslo.service. + CONF.register_opts(service_opts.service_opts) + if CONF.log_options: + CONF.log_opt_values(logging.getLogger(__name__), logging.DEBUG) + + config.set_middleware_defaults() + rpc.init(CONF) + conf = conf_files[0] + + return deploy.loadapp('config:%s' % conf, name="masakari_api") diff --git a/releasenotes/notes/wsgi-applications-3ed7d6b89f1a5785.yaml b/releasenotes/notes/wsgi-applications-3ed7d6b89f1a5785.yaml new file mode 100644 index 00000000..0a3e9f4a --- /dev/null +++ b/releasenotes/notes/wsgi-applications-3ed7d6b89f1a5785.yaml @@ -0,0 +1,8 @@ +--- +upgrade: + - | + WSGI application script ``masakari-wsgi`` is now available. It allows + running the masakari APIs using a WSGI server of choice (for example + nginx and uwsgi, apache2 with mod_proxy_uwsgi or gunicorn). + The eventlet-based servers are still available, but the WSGI options will + allow greater deployment flexibility. diff --git a/setup.cfg b/setup.cfg index 707ba547..de29af74 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,9 @@ console_scripts = masakari-engine = masakari.cmd.engine:main masakari-manage = masakari.cmd.manage:main +wsgi_scripts = + masakari-wsgi = masakari.cmd.api:initialize_application + masakari.database.migration_backend = sqlalchemy = oslo_db.sqlalchemy.migration