blob: 6ca2fb5ca66c6966c7d3a1b26ccc9486f0391b9b [file] [log] [blame]
Dean Troyer73f6f252012-09-17 11:22:21 -05001# lib/glance
Dean Troyer6d04fd72012-12-21 11:03:37 -06002# Functions to control the configuration and operation of the **Glance** service
Dean Troyer73f6f252012-09-17 11:22:21 -05003
4# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - ``functions`` file
7# - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
8# - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
9# - ``SERVICE_HOST``
10# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Dean Troyer73f6f252012-09-17 11:22:21 -050011
12# ``stack.sh`` calls the entry points in this order:
13#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014# - install_glance
15# - configure_glance
16# - init_glance
17# - start_glance
18# - stop_glance
19# - cleanup_glance
Dean Troyer73f6f252012-09-17 11:22:21 -050020
21# Save trace setting
22XTRACE=$(set +o | grep xtrace)
23set +o xtrace
24
25
26# Defaults
27# --------
28
Dean Troyer73f6f252012-09-17 11:22:21 -050029# Set up default directories
30GLANCE_DIR=$DEST/glance
Flavio Percoco4f78f8f2014-09-09 09:37:42 +020031GLANCE_STORE_DIR=$DEST/glance_store
Dean Troyer73f6f252012-09-17 11:22:21 -050032GLANCECLIENT_DIR=$DEST/python-glanceclient
33GLANCE_CACHE_DIR=${GLANCE_CACHE_DIR:=$DATA_DIR/glance/cache}
34GLANCE_IMAGE_DIR=${GLANCE_IMAGE_DIR:=$DATA_DIR/glance/images}
Dean Troyerbc071bc2012-10-01 14:06:44 -050035GLANCE_AUTH_CACHE_DIR=${GLANCE_AUTH_CACHE_DIR:-/var/cache/glance}
Dean Troyer73f6f252012-09-17 11:22:21 -050036
37GLANCE_CONF_DIR=${GLANCE_CONF_DIR:-/etc/glance}
Pawel Koniszewski76e39252014-09-06 07:06:46 -040038GLANCE_METADEF_DIR=$GLANCE_CONF_DIR/metadefs
Dean Troyer73f6f252012-09-17 11:22:21 -050039GLANCE_REGISTRY_CONF=$GLANCE_CONF_DIR/glance-registry.conf
40GLANCE_API_CONF=$GLANCE_CONF_DIR/glance-api.conf
41GLANCE_REGISTRY_PASTE_INI=$GLANCE_CONF_DIR/glance-registry-paste.ini
42GLANCE_API_PASTE_INI=$GLANCE_CONF_DIR/glance-api-paste.ini
43GLANCE_CACHE_CONF=$GLANCE_CONF_DIR/glance-cache.conf
44GLANCE_POLICY_JSON=$GLANCE_CONF_DIR/policy.json
Dirk Mueller46d1ba62013-09-09 14:31:37 +020045GLANCE_SCHEMA_JSON=$GLANCE_CONF_DIR/schema-image.json
Dean Troyer73f6f252012-09-17 11:22:21 -050046
47# Support entry points installation of console scripts
48if [[ -d $GLANCE_DIR/bin ]]; then
49 GLANCE_BIN_DIR=$GLANCE_DIR/bin
50else
Jakub Ruzicka4196d552013-01-30 15:35:54 +010051 GLANCE_BIN_DIR=$(get_python_exec_prefix)
Dean Troyer73f6f252012-09-17 11:22:21 -050052fi
53
54# Glance connection info. Note the port must be specified.
55GLANCE_HOSTPORT=${GLANCE_HOSTPORT:-$SERVICE_HOST:9292}
56
Dean Troyer4237f592014-01-29 16:22:11 -060057# Tell Tempest this project is present
58TEMPEST_SERVICES+=,glance
59
Dean Troyer73f6f252012-09-17 11:22:21 -050060
Dean Troyercc6b4432013-04-08 15:38:03 -050061# Functions
62# ---------
Dean Troyer73f6f252012-09-17 11:22:21 -050063
Dean Troyere4fa7212014-01-15 15:04:49 -060064# Test if any Glance services are enabled
65# is_glance_enabled
66function is_glance_enabled {
67 [[ ,${ENABLED_SERVICES} =~ ,"g-" ]] && return 0
68 return 1
69}
70
Dean Troyer73f6f252012-09-17 11:22:21 -050071# cleanup_glance() - Remove residual data files, anything left over from previous
72# runs that a clean run would need to clean up
Ian Wienandaee18c72014-02-21 15:35:08 +110073function cleanup_glance {
Dean Troyer73f6f252012-09-17 11:22:21 -050074 # kill instances (nova)
75 # delete image files (glance)
Dean Troyer995eb922013-03-07 16:11:40 -060076 sudo rm -rf $GLANCE_CACHE_DIR $GLANCE_IMAGE_DIR $GLANCE_AUTH_CACHE_DIR
Dean Troyer73f6f252012-09-17 11:22:21 -050077}
78
Dean Troyer73f6f252012-09-17 11:22:21 -050079# configure_glance() - Set config files, create data dirs, etc
Ian Wienandaee18c72014-02-21 15:35:08 +110080function configure_glance {
Dean Troyer73f6f252012-09-17 11:22:21 -050081 if [[ ! -d $GLANCE_CONF_DIR ]]; then
82 sudo mkdir -p $GLANCE_CONF_DIR
83 fi
Attila Fazekas91b8d132013-01-06 22:40:09 +010084 sudo chown $STACK_USER $GLANCE_CONF_DIR
Dean Troyer73f6f252012-09-17 11:22:21 -050085
Pawel Koniszewski76e39252014-09-06 07:06:46 -040086 if [[ ! -d $GLANCE_METADEF_DIR ]]; then
87 sudo mkdir -p $GLANCE_METADEF_DIR
88 fi
89 sudo chown $STACK_USER $GLANCE_METADEF_DIR
90
Dean Troyer73f6f252012-09-17 11:22:21 -050091 # Copy over our glance configurations and update them
92 cp $GLANCE_DIR/etc/glance-registry.conf $GLANCE_REGISTRY_CONF
Ben Nemec03997942013-08-10 09:56:16 -050093 iniset $GLANCE_REGISTRY_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
Dean Troyer73f6f252012-09-17 11:22:21 -050094 inicomment $GLANCE_REGISTRY_CONF DEFAULT log_file
Attila Fazekas7e79d912013-03-03 12:23:04 +010095 local dburl=`database_connection_url glance`
Terry Wilson428af5a2012-11-01 16:12:39 -040096 iniset $GLANCE_REGISTRY_CONF DEFAULT sql_connection $dburl
Dean Troyer73f6f252012-09-17 11:22:21 -050097 iniset $GLANCE_REGISTRY_CONF DEFAULT use_syslog $SYSLOG
98 iniset $GLANCE_REGISTRY_CONF paste_deploy flavor keystone
Brant Knudson05952372014-09-19 17:22:22 -050099 configure_auth_token_middleware $GLANCE_REGISTRY_CONF glance $GLANCE_AUTH_CACHE_DIR/registry
Boris Pavlovic0d029242014-08-13 19:18:56 +0400100 if is_service_enabled qpid || [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
101 iniset $GLANCE_REGISTRY_CONF DEFAULT notification_driver messaging
102 fi
103 iniset_rpc_backend glance $GLANCE_REGISTRY_CONF DEFAULT
Dean Troyer73f6f252012-09-17 11:22:21 -0500104
105 cp $GLANCE_DIR/etc/glance-api.conf $GLANCE_API_CONF
Ben Nemec03997942013-08-10 09:56:16 -0500106 iniset $GLANCE_API_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
Dean Troyer73f6f252012-09-17 11:22:21 -0500107 inicomment $GLANCE_API_CONF DEFAULT log_file
Terry Wilson428af5a2012-11-01 16:12:39 -0400108 iniset $GLANCE_API_CONF DEFAULT sql_connection $dburl
Dean Troyer73f6f252012-09-17 11:22:21 -0500109 iniset $GLANCE_API_CONF DEFAULT use_syslog $SYSLOG
Dean Troyer73f6f252012-09-17 11:22:21 -0500110 iniset $GLANCE_API_CONF DEFAULT image_cache_dir $GLANCE_CACHE_DIR/
111 iniset $GLANCE_API_CONF paste_deploy flavor keystone+cachemanagement
Brant Knudson05952372014-09-19 17:22:22 -0500112 configure_auth_token_middleware $GLANCE_API_CONF glance $GLANCE_AUTH_CACHE_DIR/api
Julien Danjou19a38142014-01-30 15:49:53 +0100113 if is_service_enabled qpid || [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; then
114 iniset $GLANCE_API_CONF DEFAULT notification_driver messaging
Eoghan Glynnad80ead2012-09-27 09:36:33 +0100115 fi
Eoghan Glynn8c11f562013-03-01 12:09:01 +0000116 iniset_rpc_backend glance $GLANCE_API_CONF DEFAULT
Mate Lakatbc2ef922013-08-15 18:06:59 +0100117 if [ "$VIRT_DRIVER" = 'xenserver' ]; then
118 iniset $GLANCE_API_CONF DEFAULT container_formats "ami,ari,aki,bare,ovf,tgz"
119 iniset $GLANCE_API_CONF DEFAULT disk_formats "ami,ari,aki,vhd,raw,iso"
120 fi
Dean Troyer73f6f252012-09-17 11:22:21 -0500121
Flavio Percocofe65e2d2014-09-03 11:51:00 +0200122 # Store specific configs
123 iniset $GLANCE_API_CONF DEFAULT filesystem_store_datadir $GLANCE_IMAGE_DIR/
124
125 # NOTE(flaper87): Until Glance is fully migrated, set these configs in both
126 # sections.
127 iniset $GLANCE_API_CONF glance_store filesystem_store_datadir $GLANCE_IMAGE_DIR/
128
Dean Troyer05bd7b82014-09-16 17:25:33 -0500129 iniset $GLANCE_API_CONF DEFAULT workers "$API_WORKERS"
Bob Ball2f720502014-08-28 14:50:04 +0100130
Dean Troyerc77b9322013-03-29 10:51:01 -0500131 # Store the images in swift if enabled.
132 if is_service_enabled s-proxy; then
133 iniset $GLANCE_API_CONF DEFAULT default_store swift
Jamie Lennox3561d7f2014-05-21 17:18:43 +1000134 iniset $GLANCE_API_CONF DEFAULT swift_store_auth_address $KEYSTONE_SERVICE_URI/v2.0/
Attila Fazekas85a85f82014-01-21 11:13:55 +0100135 iniset $GLANCE_API_CONF DEFAULT swift_store_user $SERVICE_TENANT_NAME:glance-swift
Dean Troyerc77b9322013-03-29 10:51:01 -0500136 iniset $GLANCE_API_CONF DEFAULT swift_store_key $SERVICE_PASSWORD
137 iniset $GLANCE_API_CONF DEFAULT swift_store_create_container_on_put True
Flavio Percoco355fc862013-11-29 14:27:35 +0100138
Flavio Percococ3e5b772014-01-23 13:48:16 +0100139 iniset $GLANCE_API_CONF DEFAULT known_stores "glance.store.filesystem.Store, glance.store.http.Store, glance.store.swift.Store"
Flavio Percocofe65e2d2014-09-03 11:51:00 +0200140
141 # NOTE(flaper87): Until Glance is fully migrated, set these configs in both
142 # sections.
143 iniset $GLANCE_API_CONF glance_store default_store swift
144 iniset $GLANCE_API_CONF glance_store swift_store_auth_address $KEYSTONE_SERVICE_URI/v2.0/
145 iniset $GLANCE_API_CONF glance_store swift_store_user $SERVICE_TENANT_NAME:glance-swift
146 iniset $GLANCE_API_CONF glance_store swift_store_key $SERVICE_PASSWORD
147 iniset $GLANCE_API_CONF glance_store swift_store_create_container_on_put True
148 iniset $GLANCE_API_CONF glance_store stores "file, http, swift"
Dean Troyerc77b9322013-03-29 10:51:01 -0500149 fi
150
Dean Troyer73f6f252012-09-17 11:22:21 -0500151 cp -p $GLANCE_DIR/etc/glance-registry-paste.ini $GLANCE_REGISTRY_PASTE_INI
152
153 cp -p $GLANCE_DIR/etc/glance-api-paste.ini $GLANCE_API_PASTE_INI
154
155 cp $GLANCE_DIR/etc/glance-cache.conf $GLANCE_CACHE_CONF
Ben Nemec03997942013-08-10 09:56:16 -0500156 iniset $GLANCE_CACHE_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
Dean Troyer73f6f252012-09-17 11:22:21 -0500157 inicomment $GLANCE_CACHE_CONF DEFAULT log_file
158 iniset $GLANCE_CACHE_CONF DEFAULT use_syslog $SYSLOG
Dean Troyer73f6f252012-09-17 11:22:21 -0500159 iniset $GLANCE_CACHE_CONF DEFAULT image_cache_dir $GLANCE_CACHE_DIR/
160 iniuncomment $GLANCE_CACHE_CONF DEFAULT auth_url
Jamie Lennox3561d7f2014-05-21 17:18:43 +1000161 iniset $GLANCE_CACHE_CONF DEFAULT auth_url $KEYSTONE_AUTH_URI/v2.0
Dean Troyer73f6f252012-09-17 11:22:21 -0500162 iniuncomment $GLANCE_CACHE_CONF DEFAULT auth_tenant_name
163 iniset $GLANCE_CACHE_CONF DEFAULT admin_tenant_name $SERVICE_TENANT_NAME
164 iniuncomment $GLANCE_CACHE_CONF DEFAULT auth_user
165 iniset $GLANCE_CACHE_CONF DEFAULT admin_user glance
166 iniuncomment $GLANCE_CACHE_CONF DEFAULT auth_password
167 iniset $GLANCE_CACHE_CONF DEFAULT admin_password $SERVICE_PASSWORD
168
Flavio Percocofe65e2d2014-09-03 11:51:00 +0200169 # Store specific confs
170 # NOTE(flaper87): Until Glance is fully migrated, set these configs in both
171 # sections.
172 iniset $GLANCE_CACHE_CONF DEFAULT filesystem_store_datadir $GLANCE_IMAGE_DIR/
173 iniset $GLANCE_CACHE_CONF glance_store filesystem_store_datadir $GLANCE_IMAGE_DIR/
174
Dean Troyer73f6f252012-09-17 11:22:21 -0500175 cp -p $GLANCE_DIR/etc/policy.json $GLANCE_POLICY_JSON
Dirk Mueller46d1ba62013-09-09 14:31:37 +0200176 cp -p $GLANCE_DIR/etc/schema-image.json $GLANCE_SCHEMA_JSON
Pawel Koniszewski76e39252014-09-06 07:06:46 -0400177
178 cp -p $GLANCE_DIR/etc/metadefs/*.json $GLANCE_METADEF_DIR
Dean Troyer73f6f252012-09-17 11:22:21 -0500179}
180
Dean Troyer42a59c22014-03-03 14:31:29 -0600181# create_glance_accounts() - Set up common required glance accounts
182
183# Project User Roles
184# ------------------------------------------------------------------
185# SERVICE_TENANT_NAME glance service
186# SERVICE_TENANT_NAME glance-swift ResellerAdmin (if Swift is enabled)
187
188function create_glance_accounts {
189 if is_service_enabled g-api; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100190
Dean Troyer16ef9762014-08-19 19:31:34 -0500191 local glance_user=$(get_or_create_user "glance" \
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200192 "$SERVICE_PASSWORD" $SERVICE_TENANT_NAME)
Dean Troyer16ef9762014-08-19 19:31:34 -0500193 get_or_add_user_role service $glance_user $SERVICE_TENANT_NAME
Bartosz Górski0abde392014-02-28 14:15:19 +0100194
Dean Troyer42a59c22014-03-03 14:31:29 -0600195 # required for swift access
196 if is_service_enabled s-proxy; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100197
Dean Troyer16ef9762014-08-19 19:31:34 -0500198 local glance_swift_user=$(get_or_create_user "glance-swift" \
Bartosz Górski0abde392014-02-28 14:15:19 +0100199 "$SERVICE_PASSWORD" $SERVICE_TENANT_NAME "glance-swift@example.com")
Dean Troyer16ef9762014-08-19 19:31:34 -0500200 get_or_add_user_role "ResellerAdmin" $glance_swift_user $SERVICE_TENANT_NAME
Dean Troyer42a59c22014-03-03 14:31:29 -0600201 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100202
Dean Troyer42a59c22014-03-03 14:31:29 -0600203 if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100204
Dean Troyer16ef9762014-08-19 19:31:34 -0500205 local glance_service=$(get_or_create_service "glance" \
Bartosz Górski0abde392014-02-28 14:15:19 +0100206 "image" "Glance Image Service")
Dean Troyer16ef9762014-08-19 19:31:34 -0500207 get_or_create_endpoint $glance_service \
Bartosz Górski0abde392014-02-28 14:15:19 +0100208 "$REGION_NAME" \
209 "http://$GLANCE_HOSTPORT" \
210 "http://$GLANCE_HOSTPORT" \
211 "http://$GLANCE_HOSTPORT"
Dean Troyer42a59c22014-03-03 14:31:29 -0600212 fi
213 fi
214}
215
Dean Troyerf03bafe2013-02-12 10:58:28 -0600216# create_glance_cache_dir() - Part of the init_glance() process
Ian Wienandaee18c72014-02-21 15:35:08 +1100217function create_glance_cache_dir {
Dean Troyerf03bafe2013-02-12 10:58:28 -0600218 # Create cache dir
219 sudo mkdir -p $GLANCE_AUTH_CACHE_DIR/api
220 sudo chown $STACK_USER $GLANCE_AUTH_CACHE_DIR/api
221 rm -f $GLANCE_AUTH_CACHE_DIR/api/*
222 sudo mkdir -p $GLANCE_AUTH_CACHE_DIR/registry
223 sudo chown $STACK_USER $GLANCE_AUTH_CACHE_DIR/registry
224 rm -f $GLANCE_AUTH_CACHE_DIR/registry/*
225}
226
Dean Troyer73f6f252012-09-17 11:22:21 -0500227# init_glance() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function init_glance {
Dean Troyer73f6f252012-09-17 11:22:21 -0500229 # Delete existing images
230 rm -rf $GLANCE_IMAGE_DIR
231 mkdir -p $GLANCE_IMAGE_DIR
232
233 # Delete existing cache
234 rm -rf $GLANCE_CACHE_DIR
235 mkdir -p $GLANCE_CACHE_DIR
236
Dean Troyerf03bafe2013-02-12 10:58:28 -0600237 # (Re)create glance database
Terry Wilson428af5a2012-11-01 16:12:39 -0400238 recreate_database glance utf8
Dean Troyer73f6f252012-09-17 11:22:21 -0500239
Dean Troyerf03bafe2013-02-12 10:58:28 -0600240 # Migrate glance database
Jianing Yang16312732013-12-22 10:47:39 +0800241 $GLANCE_BIN_DIR/glance-manage db_sync
Dean Troyerbc071bc2012-10-01 14:06:44 -0500242
Pawel Koniszewski76e39252014-09-06 07:06:46 -0400243 # Load metadata definitions
244 $GLANCE_BIN_DIR/glance-manage db_load_metadefs
245
Dean Troyerf03bafe2013-02-12 10:58:28 -0600246 create_glance_cache_dir
Dean Troyer73f6f252012-09-17 11:22:21 -0500247}
248
249# install_glanceclient() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100250function install_glanceclient {
Dean Troyer73f6f252012-09-17 11:22:21 -0500251 git_clone $GLANCECLIENT_REPO $GLANCECLIENT_DIR $GLANCECLIENT_BRANCH
Sean Daguee4f0cd72013-04-01 15:56:07 -0400252 setup_develop $GLANCECLIENT_DIR
Dean Troyer73f6f252012-09-17 11:22:21 -0500253}
254
255# install_glance() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100256function install_glance {
Flavio Percoco4f78f8f2014-09-09 09:37:42 +0200257 # Install glance_store from git so we make sure we're testing
258 # the latest code.
259 git_clone $GLANCE_STORE_REPO $GLANCE_STORE_DIR $GLANCE_STORE_BRANCH
260 setup_develop $GLANCE_STORE_DIR
261
Dean Troyer73f6f252012-09-17 11:22:21 -0500262 git_clone $GLANCE_REPO $GLANCE_DIR $GLANCE_BRANCH
Sean Daguee4f0cd72013-04-01 15:56:07 -0400263 setup_develop $GLANCE_DIR
Dean Troyer73f6f252012-09-17 11:22:21 -0500264}
265
266# start_glance() - Start running processes, including screen
Ian Wienandaee18c72014-02-21 15:35:08 +1100267function start_glance {
Chris Dent2f27a0e2014-09-09 13:46:02 +0100268 run_process g-reg "$GLANCE_BIN_DIR/glance-registry --config-file=$GLANCE_CONF_DIR/glance-registry.conf"
269 run_process g-api "$GLANCE_BIN_DIR/glance-api --config-file=$GLANCE_CONF_DIR/glance-api.conf"
Dean Troyer73f6f252012-09-17 11:22:21 -0500270 echo "Waiting for g-api ($GLANCE_HOSTPORT) to start..."
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800271 if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget --no-proxy -q -O- http://$GLANCE_HOSTPORT; do sleep 1; done"; then
Sean Dague101b4242013-10-22 08:47:11 -0400272 die $LINENO "g-api did not start"
Dean Troyer73f6f252012-09-17 11:22:21 -0500273 fi
274}
275
Dean Troyer699a29f2012-09-10 14:10:27 -0500276# stop_glance() - Stop running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100277function stop_glance {
Dean Troyer73f6f252012-09-17 11:22:21 -0500278 # Kill the Glance screen windows
Chris Dent2f27a0e2014-09-09 13:46:02 +0100279 stop_process g-api
280 stop_process g-reg
Dean Troyer73f6f252012-09-17 11:22:21 -0500281}
282
Dean Troyercc6b4432013-04-08 15:38:03 -0500283
Dean Troyer73f6f252012-09-17 11:22:21 -0500284# Restore xtrace
285$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400286
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100287# Tell emacs to use shell-script-mode
288## Local variables:
289## mode: shell-script
290## End: