lib/glance
e263c82e
 #!/bin/bash
 #
73f6f25b
 # lib/glance
6d04fd7b
 # Functions to control the configuration and operation of the **Glance** service
73f6f25b
 
 # Dependencies:
6a5aa7c6
 #
 # - ``functions`` file
 # - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
 # - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
 # - ``SERVICE_HOST``
 # - ``KEYSTONE_TOKEN_FORMAT`` must be defined
73f6f25b
 
 # ``stack.sh`` calls the entry points in this order:
 #
6a5aa7c6
 # - install_glance
 # - configure_glance
 # - init_glance
 # - start_glance
 # - stop_glance
 # - cleanup_glance
73f6f25b
 
 # Save trace setting
523f4880
 _XTRACE_GLANCE=$(set +o | grep xtrace)
73f6f25b
 set +o xtrace
 
 
 # Defaults
 # --------
 
 # Set up default directories
e08ab104
 GITDIR["python-glanceclient"]=$DEST/python-glanceclient
aecd189f
 GITDIR["glance_store"]=$DEST/glance_store
73f6f25b
 GLANCE_DIR=$DEST/glance
aed60792
 
 # Glance virtual environment
 if [[ ${USE_VENV} = True ]]; then
     PROJECT_VENV["glance"]=${GLANCE_DIR}.venv
     GLANCE_BIN_DIR=${PROJECT_VENV["glance"]}/bin
 else
     GLANCE_BIN_DIR=$(get_python_exec_prefix)
 fi
 
6f91da9a
 # Glance multi-store configuration
 # Boolean flag to enable multiple store configuration for glance
 GLANCE_ENABLE_MULTIPLE_STORES=$(trueorfalse False GLANCE_ENABLE_MULTIPLE_STORES)
 
 # Comma separated list for configuring multiple file stores of glance,
 # for example; GLANCE_MULTIPLE_FILE_STORES = fast,cheap,slow
 GLANCE_MULTIPLE_FILE_STORES=${GLANCE_MULTIPLE_FILE_STORES:-fast}
 
 # Default store/backend for glance, must be one of the store specified
 # in GLANCE_MULTIPLE_FILE_STORES option.
 GLANCE_DEFAULT_BACKEND=${GLANCE_DEFAULT_BACKEND:-fast}
 
73f6f25b
 GLANCE_CACHE_DIR=${GLANCE_CACHE_DIR:=$DATA_DIR/glance/cache}
6f91da9a
 
09eea0b2
 # Full Glance functionality requires running in standalone mode. If we are
 # not in uwsgi mode, then we are standalone, otherwise allow separate control.
 if [[ "$WSGI_MODE" != "uwsgi" ]]; then
     GLANCE_STANDALONE=True
 fi
cc0821a5
 GLANCE_STANDALONE=${GLANCE_STANDALONE:-True}
09eea0b2
 
6f91da9a
 # File path for each store specified in GLANCE_MULTIPLE_FILE_STORES, the store
 # identifier will be appended to this path at runtime. If GLANCE_MULTIPLE_FILE_STORES
 # has fast,cheap specified then filepath will be generated like $DATA_DIR/glance/fast
 # and $DATA_DIR/glance/cheap.
 GLANCE_MULTISTORE_FILE_IMAGE_DIR=${GLANCE_MULTISTORE_FILE_IMAGE_DIR:=$DATA_DIR/glance}
73f6f25b
 GLANCE_IMAGE_DIR=${GLANCE_IMAGE_DIR:=$DATA_DIR/glance/images}
fa898f51
 GLANCE_LOCK_DIR=${GLANCE_LOCK_DIR:=$DATA_DIR/glance/locks}
6f91da9a
 GLANCE_STAGING_DIR=${GLANCE_MULTISTORE_FILE_IMAGE_DIR:=$DATA_DIR/os_glance_staging_store}
 GLANCE_TASKS_DIR=${GLANCE_MULTISTORE_FILE_IMAGE_DIR:=$DATA_DIR/os_glance_tasks_store}
73f6f25b
 
73ad9760
 GLANCE_USE_IMPORT_WORKFLOW=$(trueorfalse False GLANCE_USE_IMPORT_WORKFLOW)
 
73f6f25b
 GLANCE_CONF_DIR=${GLANCE_CONF_DIR:-/etc/glance}
76e3925d
 GLANCE_METADEF_DIR=$GLANCE_CONF_DIR/metadefs
73f6f25b
 GLANCE_API_CONF=$GLANCE_CONF_DIR/glance-api.conf
 GLANCE_API_PASTE_INI=$GLANCE_CONF_DIR/glance-api-paste.ini
 GLANCE_CACHE_CONF=$GLANCE_CONF_DIR/glance-cache.conf
46d1ba6e
 GLANCE_SCHEMA_JSON=$GLANCE_CONF_DIR/schema-image.json
f4f01c63
 GLANCE_SWIFT_STORE_CONF=$GLANCE_CONF_DIR/glance-swift-store.conf
6a25fb96
 GLANCE_IMAGE_IMPORT_CONF=$GLANCE_CONF_DIR/glance-image-import.conf
73f6f25b
 
f3b2f4c8
 if is_service_enabled tls-proxy; then
18d4778c
     GLANCE_SERVICE_PROTOCOL="https"
 fi
 
73f6f25b
 # Glance connection info.  Note the port must be specified.
18d4778c
 GLANCE_SERVICE_HOST=${GLANCE_SERVICE_HOST:-$SERVICE_HOST}
dc7b4294
 GLANCE_SERVICE_LISTEN_ADDRESS=${GLANCE_SERVICE_LISTEN_ADDRESS:-$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)}
18d4778c
 GLANCE_SERVICE_PORT=${GLANCE_SERVICE_PORT:-9292}
 GLANCE_SERVICE_PORT_INT=${GLANCE_SERVICE_PORT_INT:-19292}
 GLANCE_HOSTPORT=${GLANCE_HOSTPORT:-$GLANCE_SERVICE_HOST:$GLANCE_SERVICE_PORT}
 GLANCE_SERVICE_PROTOCOL=${GLANCE_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
1fa65363
 GLANCE_UWSGI=$GLANCE_BIN_DIR/glance-wsgi-api
2f7df51c
 GLANCE_UWSGI_CONF=$GLANCE_CONF_DIR/glance-uwsgi.ini
1fa65363
 # If wsgi mode is uwsgi run glance under uwsgi, else default to eventlet
 # TODO(mtreinish): Remove the eventlet path here and in all the similar
 # conditionals below after the Pike release
 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
     GLANCE_URL="$GLANCE_SERVICE_PROTOCOL://$GLANCE_SERVICE_HOST/image"
 else
     GLANCE_URL="$GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT"
 fi
73f6f25b
 
cc6b4435
 # Functions
 # ---------
73f6f25b
 
e4fa7213
 # Test if any Glance services are enabled
 # is_glance_enabled
 function is_glance_enabled {
902158bb
     [[ ,${DISABLED_SERVICES} =~ ,"glance" ]] && return 1
e4fa7213
     [[ ,${ENABLED_SERVICES} =~ ,"g-" ]] && return 0
     return 1
 }
 
73f6f25b
 # cleanup_glance() - Remove residual data files, anything left over from previous
 # runs that a clean run would need to clean up
aee18c74
 function cleanup_glance {
73f6f25b
     # delete image files (glance)
8ab64b32
     sudo rm -rf $GLANCE_CACHE_DIR $GLANCE_IMAGE_DIR
6f91da9a
 
     # Cleanup multiple stores directories
     if [[ "$GLANCE_ENABLE_MULTIPLE_STORES" == "True" ]]; then
         local store file_dir
         for store in $(echo $GLANCE_MULTIPLE_FILE_STORES | tr "," "\n"); do
             file_dir="${GLANCE_MULTISTORE_FILE_IMAGE_DIR}/${store}/"
             sudo rm -rf $file_dir
         done
 
         # Cleanup reserved stores directories
         sudo rm -rf $GLANCE_STAGING_DIR $GLANCE_TASKS_DIR
     fi
73f6f25b
 }
 
 # configure_glance() - Set config files, create data dirs, etc
aee18c74
 function configure_glance {
8421c2b9
     sudo install -d -o $STACK_USER $GLANCE_CONF_DIR $GLANCE_METADEF_DIR
76e3925d
 
0ae57876
     # Set non-default configuration options for the API server
ada886dd
     local dburl
     dburl=`database_connection_url glance`
6f91da9a
 
f2577fc5
     # Configure multiple stores
     if [[ "$GLANCE_ENABLE_MULTIPLE_STORES" == "True" ]]; then
         local store enabled_backends
         enabled_backends=""
         for store in $(echo $GLANCE_MULTIPLE_FILE_STORES | tr "," "\n"); do
             enabled_backends+="${store}:file,"
         done
         iniset $GLANCE_API_CONF DEFAULT enabled_backends ${enabled_backends::-1}
     fi
 
03997942
     iniset $GLANCE_API_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
ceca15db
     iniset $GLANCE_API_CONF database connection $dburl
73f6f25b
     iniset $GLANCE_API_CONF DEFAULT use_syslog $SYSLOG
     iniset $GLANCE_API_CONF DEFAULT image_cache_dir $GLANCE_CACHE_DIR/
5fe60c1a
     iniset $GLANCE_API_CONF oslo_concurrency lock_path $GLANCE_LOCK_DIR
73f6f25b
     iniset $GLANCE_API_CONF paste_deploy flavor keystone+cachemanagement
8ab64b32
     configure_keystone_authtoken_middleware $GLANCE_API_CONF glance
45da777d
     iniset $GLANCE_API_CONF oslo_messaging_notifications driver messagingv2
2dd110ce
     iniset_rpc_backend glance $GLANCE_API_CONF
bc2ef929
     if [ "$VIRT_DRIVER" = 'xenserver' ]; then
         iniset $GLANCE_API_CONF DEFAULT container_formats "ami,ari,aki,bare,ovf,tgz"
         iniset $GLANCE_API_CONF DEFAULT disk_formats "ami,ari,aki,vhd,raw,iso"
     fi
19354585
     if [ "$VIRT_DRIVER" = 'libvirt' ] && [ "$LIBVIRT_TYPE" = 'parallels' ]; then
         iniset $GLANCE_API_CONF DEFAULT disk_formats "ami,ari,aki,vhd,vmdk,raw,qcow2,vdi,iso,ploop"
     fi
73f6f25b
 
6f91da9a
     # Glance multiple store Store specific configs
     if [[ "$GLANCE_ENABLE_MULTIPLE_STORES" == "True" ]]; then
         iniset $GLANCE_API_CONF glance_store default_backend $GLANCE_DEFAULT_BACKEND
         local store
         for store in $(echo $GLANCE_MULTIPLE_FILE_STORES | tr "," "\n"); do
             iniset $GLANCE_API_CONF $store filesystem_store_datadir "${GLANCE_MULTISTORE_FILE_IMAGE_DIR}/${store}/"
         done
 
         # Glance configure reserved stores
         iniset $GLANCE_API_CONF os_glance_staging_store filesystem_store_datadir "${GLANCE_MULTISTORE_FILE_IMAGE_DIR}/os_glance_staging_store/"
         iniset $GLANCE_API_CONF os_glance_tasks_store filesystem_store_datadir "${GLANCE_MULTISTORE_FILE_IMAGE_DIR}/os_glance_tasks_store/"
     else
         # Store specific configs
         iniset $GLANCE_API_CONF glance_store filesystem_store_datadir $GLANCE_IMAGE_DIR/
     fi
fe65e2df
 
a44dd9a7
     # CORS feature support - to allow calls from Horizon by default
     if [ -n "$GLANCE_CORS_ALLOWED_ORIGIN" ]; then
         iniset $GLANCE_API_CONF cors allowed_origin "$GLANCE_CORS_ALLOWED_ORIGIN"
     else
         iniset $GLANCE_API_CONF cors allowed_origin "http://$SERVICE_HOST"
     fi
 
6f91da9a
     # No multiple stores for swift yet
c77b932e
     # Store the images in swift if enabled.
     if is_service_enabled s-proxy; then
fe65e2df
         iniset $GLANCE_API_CONF glance_store default_store swift
         iniset $GLANCE_API_CONF glance_store swift_store_create_container_on_put True
f4f01c63
 
         iniset $GLANCE_API_CONF glance_store swift_store_config_file $GLANCE_SWIFT_STORE_CONF
         iniset $GLANCE_API_CONF glance_store default_swift_reference ref1
fe65e2df
         iniset $GLANCE_API_CONF glance_store stores "file, http, swift"
9cbd02d8
         if is_service_enabled tls-proxy; then
             iniset $GLANCE_API_CONF glance_store swift_store_cacert $SSL_BUNDLE_FILE
         fi
7159b4ba
         iniset $GLANCE_API_CONF DEFAULT graceful_shutdown_timeout "$SERVICE_GRACEFUL_SHUTDOWN_TIMEOUT"
f4f01c63
 
7580a0c3
         iniset $GLANCE_SWIFT_STORE_CONF ref1 user $SERVICE_PROJECT_NAME:glance-swift
b65cf0aa
 
f4f01c63
         iniset $GLANCE_SWIFT_STORE_CONF ref1 key $SERVICE_PASSWORD
9cbd02d8
         iniset $GLANCE_SWIFT_STORE_CONF ref1 auth_address $KEYSTONE_SERVICE_URI/v3
19c5e62a
         iniset $GLANCE_SWIFT_STORE_CONF ref1 auth_version 3
c77b932e
     fi
 
1fa65363
     # We need to tell glance what it's public endpoint is so that the version
     # discovery document will be correct
     iniset $GLANCE_API_CONF DEFAULT public_endpoint $GLANCE_URL
 
18d4778c
     if is_service_enabled tls-proxy; then
         iniset $GLANCE_API_CONF DEFAULT bind_port $GLANCE_SERVICE_PORT_INT
32c00890
         iniset $GLANCE_API_CONF keystone_authtoken identity_uri $KEYSTONE_SERVICE_URI
18d4778c
     fi
 
701276a8
     # Format logging
9751be66
     setup_logging $GLANCE_API_CONF
701276a8
 
73f6f25b
     cp -p $GLANCE_DIR/etc/glance-api-paste.ini $GLANCE_API_PASTE_INI
 
44a19b4f
     # Set non-default configuration options for the glance-cache
03997942
     iniset $GLANCE_CACHE_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
73f6f25b
     iniset $GLANCE_CACHE_CONF DEFAULT use_syslog $SYSLOG
     iniset $GLANCE_CACHE_CONF DEFAULT image_cache_dir $GLANCE_CACHE_DIR/
32c00890
     iniset $GLANCE_CACHE_CONF DEFAULT auth_url $KEYSTONE_SERVICE_URI
7580a0c3
     iniset $GLANCE_CACHE_CONF DEFAULT admin_tenant_name $SERVICE_PROJECT_NAME
73f6f25b
     iniset $GLANCE_CACHE_CONF DEFAULT admin_user glance
     iniset $GLANCE_CACHE_CONF DEFAULT admin_password $SERVICE_PASSWORD
 
fe65e2df
     # Store specific confs
     iniset $GLANCE_CACHE_CONF glance_store filesystem_store_datadir $GLANCE_IMAGE_DIR/
 
6a25fb96
     # Set default configuration options for the glance-image-import
     iniset $GLANCE_IMAGE_IMPORT_CONF image_import_opts image_import_plugins []
     iniset $GLANCE_IMAGE_IMPORT_CONF inject_metadata_properties ignore_user_roles admin
     iniset $GLANCE_IMAGE_IMPORT_CONF inject_metadata_properties inject
 
46d1ba6e
     cp -p $GLANCE_DIR/etc/schema-image.json $GLANCE_SCHEMA_JSON
76e3925d
 
     cp -p $GLANCE_DIR/etc/metadefs/*.json $GLANCE_METADEF_DIR
18d4778c
 
f3b2f4c8
     if is_service_enabled tls-proxy; then
18d4778c
         CINDER_SERVICE_HOST=${CINDER_SERVICE_HOST:-$SERVICE_HOST}
         CINDER_SERVICE_PORT=${CINDER_SERVICE_PORT:-8776}
 
b43810a3
         iniset $GLANCE_API_CONF DEFAULT cinder_endpoint_template "https://$CINDER_SERVICE_HOST:$CINDER_SERVICE_PORT/v3/%(project_id)s"
         iniset $GLANCE_CACHE_CONF DEFAULT cinder_endpoint_template "https://$CINDER_SERVICE_HOST:$CINDER_SERVICE_PORT/v3/%(project_id)s"
18d4778c
     fi
1fa65363
 
09eea0b2
     if [[ "$GLANCE_STANDALONE" == False ]]; then
1fa65363
         write_local_uwsgi_http_config "$GLANCE_UWSGI_CONF" "$GLANCE_UWSGI" "/image"
     else
09eea0b2
         write_local_proxy_http_config glance "http://$GLANCE_SERVICE_HOST:$GLANCE_SERVICE_PORT_INT" "/image"
1fa65363
         iniset $GLANCE_API_CONF DEFAULT bind_host $GLANCE_SERVICE_LISTEN_ADDRESS
fcbf3e97
         iniset $GLANCE_API_CONF DEFAULT bind_port $GLANCE_SERVICE_PORT_INT
1fa65363
         iniset $GLANCE_API_CONF DEFAULT workers "$API_WORKERS"
     fi
73f6f25b
 }
 
42a59c2b
 # create_glance_accounts() - Set up common required glance accounts
 
dd622935
 # Project              User            Roles
 # ---------------------------------------------------------------------
7580a0c3
 # SERVICE_PROJECT_NAME  glance          service
 # SERVICE_PROJECT_NAME  glance-swift    ResellerAdmin (if Swift is enabled)
 # SERVICE_PROJECT_NAME  glance-search   search (if Search is enabled)
42a59c2b
 
 function create_glance_accounts {
     if is_service_enabled g-api; then
0abde393
 
85ff5323
         create_service_user "glance"
0abde393
 
42a59c2b
         # required for swift access
         if is_service_enabled s-proxy; then
cbcbd8f3
             create_service_user "glance-swift" "ResellerAdmin"
42a59c2b
         fi
0abde393
 
985e958d
         get_or_create_service "glance" "image" "Glance Image Service"
ae4578be
         get_or_create_endpoint \
985e958d
             "image" \
             "$REGION_NAME" \
1fa65363
             "$GLANCE_URL"
890342ed
 
         # Note(frickler): Crude workaround for https://bugs.launchpad.net/glance-store/+bug/1620999
         service_domain_id=$(get_or_create_domain $SERVICE_DOMAIN_NAME)
         iniset $GLANCE_SWIFT_STORE_CONF ref1 project_domain_id $service_domain_id
         iniset $GLANCE_SWIFT_STORE_CONF ref1 user_domain_id $service_domain_id
42a59c2b
     fi
 }
 
73f6f25b
 # init_glance() - Initialize databases, etc.
aee18c74
 function init_glance {
73f6f25b
     # Delete existing images
     rm -rf $GLANCE_IMAGE_DIR
     mkdir -p $GLANCE_IMAGE_DIR
 
09e860fc
     # (Re)create glance database
     recreate_database glance
73f6f25b
 
09e860fc
     time_start "dbsync"
     # Migrate glance database
     $GLANCE_BIN_DIR/glance-manage --config-file $GLANCE_CONF_DIR/glance-api.conf db_sync
bc071bce
 
09e860fc
     # Load metadata definitions
     $GLANCE_BIN_DIR/glance-manage --config-file $GLANCE_CONF_DIR/glance-api.conf db_load_metadefs
     time_stop "dbsync"
73f6f25b
 }
 
 # install_glanceclient() - Collect source and prepare
aee18c74
 function install_glanceclient {
e08ab104
     if use_library_from_git "python-glanceclient"; then
         git_clone_by_name "python-glanceclient"
         setup_dev_lib "python-glanceclient"
8df690c5
         sudo install -D -m 0644 -o $STACK_USER {${GITDIR["python-glanceclient"]}/tools/,/etc/bash_completion.d/}glance.bash_completion
5cb19069
     fi
73f6f25b
 }
 
 # install_glance() - Collect source and prepare
aee18c74
 function install_glance {
be26306b
     local glance_store_extras=()
 
     if is_service_enabled cinder; then
         glance_store_extras=("cinder" "${glance_store_extras[@]}")
     fi
 
     if is_service_enabled swift; then
         glance_store_extras=("swift" "${glance_store_extras[@]}")
     fi
 
4f78f8f3
     # Install glance_store from git so we make sure we're testing
     # the latest code.
ee5ae7b9
     if use_library_from_git "glance_store"; then
         git_clone_by_name "glance_store"
be26306b
         setup_dev_lib "glance_store" $(join_extras "${glance_store_extras[@]}")
     else
         # we still need to pass extras
         pip_install_gr_extras glance-store $(join_extras "${glance_store_extras[@]}")
ee5ae7b9
     fi
4f78f8f3
 
73f6f25b
     git_clone $GLANCE_REPO $GLANCE_DIR $GLANCE_BRANCH
dd622935
 
     setup_develop $GLANCE_DIR
73f6f25b
 }
 
0eebeb41
 # start_glance() - Start running processes
aee18c74
 function start_glance {
18d4778c
     local service_protocol=$GLANCE_SERVICE_PROTOCOL
     if is_service_enabled tls-proxy; then
1fa65363
         if [[ "$WSGI_MODE" != "uwsgi" ]]; then
             start_tls_proxy glance-service '*' $GLANCE_SERVICE_PORT $GLANCE_SERVICE_HOST $GLANCE_SERVICE_PORT_INT
         fi
18d4778c
     fi
 
09eea0b2
     if [[ "$GLANCE_STANDALONE" == False ]]; then
312517d5
         run_process g-api "$(which uwsgi) --procname-prefix glance-api --ini $GLANCE_UWSGI_CONF"
1fa65363
     else
96269d8e
         run_process g-api "$GLANCE_BIN_DIR/glance-api --config-dir=$GLANCE_CONF_DIR"
1fa65363
     fi
18d4778c
 
1fa65363
     echo "Waiting for g-api ($GLANCE_SERVICE_HOST) to start..."
     if ! wait_for_service $SERVICE_TIMEOUT $GLANCE_URL; then
101b4248
         die $LINENO "g-api did not start"
73f6f25b
     fi
 }
 
699a29f7
 # stop_glance() - Stop running processes
aee18c74
 function stop_glance {
2f27a0ed
     stop_process g-api
73f6f25b
 }
 
 # Restore xtrace
523f4880
 $_XTRACE_GLANCE
584d90ec
 
6a5aa7c6
 # Tell emacs to use shell-script-mode
 ## Local variables:
 ## mode: shell-script
 ## End: