blob: 93035673b5c04f5756b2006c33e51876319cf00d [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions - DevStack-specific functions
Dean Troyer13dc5cc2012-03-27 14:50:45 -05004#
Dean Troyer4a43b7b2012-08-28 17:43:40 -05005# The following variables are assumed to be defined by certain functions:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006#
Dean Troyerd8864fe2014-02-17 11:00:42 -06007# - ``DATABASE_BACKENDS``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01008# - ``ENABLED_SERVICES``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01009# - ``FILES``
10# - ``GLANCE_HOSTPORT``
Dean Troyerd8864fe2014-02-17 11:00:42 -060011#
Dean Troyer13dc5cc2012-03-27 14:50:45 -050012
Ian Wienand4ffb4542015-06-30 11:00:32 +100013# ensure we don't re-source this in the same environment
14[[ -z "$_DEVSTACK_FUNCTIONS" ]] || return 0
Sean Dagueafef8bf2017-03-06 14:07:23 -050015declare -r -g _DEVSTACK_FUNCTIONS=1
Ian Wienand4ffb4542015-06-30 11:00:32 +100016
Dean Troyerdff49a22014-01-30 15:37:40 -060017# Include the common functions
18FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
19source ${FUNC_DIR}/functions-common
Dean Troyerbf2ad702015-03-09 15:16:10 -050020source ${FUNC_DIR}/inc/ini-config
Dean Troyer490430d2015-01-30 14:38:35 -060021source ${FUNC_DIR}/inc/python
Dean Troyer32d6bc62015-03-29 14:16:44 -050022source ${FUNC_DIR}/inc/rootwrap
Dean Troyer7f9aa712012-01-31 12:11:56 -060023
Dean Troyer27e32692012-03-16 16:16:56 -050024# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110025_XTRACE_FUNCTIONS=$(set +o | grep xtrace)
Dean Troyer27e32692012-03-16 16:16:56 -050026set +o xtrace
27
Ian Wienand54e39102014-06-03 16:05:12 +100028# Check if a function already exists
29function function_exists {
30 declare -f -F $1 > /dev/null
31}
Dean Troyer7f9aa712012-01-31 12:11:56 -060032
Sean Dague646085d2016-03-21 17:00:51 -040033# short_source prints out the current location of the caller in a way
34# that strips redundant directories. This is useful for PS4 usage.
35function short_source {
36 saveIFS=$IFS
37 IFS=" "
38 called=($(caller 0))
39 IFS=$saveIFS
40 file=${called[2]}
41 file=${file#$RC_DIR/}
42 printf "%-40s " "$file:${called[1]}:${called[0]}"
43}
John L. Villalovosdaa7a412016-05-05 12:50:52 -070044# PS4 is exported to child shells and uses the 'short_source' function, so
45# export it so child shells have access to the 'short_source' function also.
46export -f short_source
Sean Dague646085d2016-03-21 17:00:51 -040047
Monty Taylord8bb2202017-09-03 12:13:59 -050048# Download a file from a URL
49#
50# Will check cache (in $FILES) or download given URL.
51#
52# Argument is the URL to the remote file
53#
54# Will echo the local path to the file as the output. Will die on
55# failure to download.
56#
57# Files can be pre-cached for CI environments, see EXTRA_CACHE_URLS
58# and tools/image_list.sh
59function get_extra_file {
60 local file_url=$1
61
62 file_name=$(basename "$file_url")
63 if [[ $file_url != file* ]]; then
64 # If the file isn't cache, download it
65 if [[ ! -f $FILES/$file_name ]]; then
yuanke weic652a492017-09-17 22:18:07 +080066 wget --progress=dot:giga -t 2 -c $file_url -O $FILES/$file_name
Monty Taylord8bb2202017-09-03 12:13:59 -050067 if [[ $? -ne 0 ]]; then
68 die "$file_url could not be downloaded"
69 fi
70 fi
71 echo "$FILES/$file_name"
72 return
73 else
74 # just strip the file:// bit and that's the path to the file
75 echo $file_url | sed 's/$file:\/\///g'
76 fi
77}
78
Sean Dague646085d2016-03-21 17:00:51 -040079
Adam Spierscb961592013-10-05 12:11:07 +010080# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050081# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010082#
83# - ``FILES`` must be set to the cache dir
84# - ``GLANCE_HOSTPORT``
85#
Peter Stachowski5aeea6a2015-09-22 19:38:02 +000086# upload_image image-url
Ian Wienandaee18c72014-02-21 15:35:08 +110087function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050088 local image_url=$1
Dean Troyerca0e3d02012-04-13 15:58:37 -050089
Dean Troyere9f76672014-07-25 11:09:36 -050090 local image image_fname image_name
91
Dean Troyerca0e3d02012-04-13 15:58:37 -050092 # Create a directory for the downloaded image tarballs.
93 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050094 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080095 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080096 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050097 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +010098 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090099 if [[ $? -ne 0 ]]; then
100 echo "Not found: $image_url"
101 return
102 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -0800103 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500104 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -0800105 else
Dean Troyer3324f192014-09-18 09:26:39 -0500106 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -0800107 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -0500108 # unix: ``file:///home/user/path/file``
109 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -0500110 image=$(echo $image_url | sed "s/^file:\/\///g")
111 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500112 echo "Not found: $image_url"
113 return
114 fi
115 fi
116
117 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
118 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500119 image_name="${image_fname%.tar.gz}"
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300120 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" --public --container-format ami --disk-format ami < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500121 return
122 fi
123
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700124 # vmdk format images
125 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500126 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700127
128 # Before we can upload vmdk type images to glance, we need to know it's
129 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800130 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700131 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700132 # vmdk filename, which is expected in the following format:
133 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800134 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700135 #
136 # If the filename does not follow the above format then the vsphere
137 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700138
Dean Troyere9f76672014-07-25 11:09:36 -0500139 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -0800140 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -0500141 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800142
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700143 # vmdk adapter type
Ian Wienand7ae97292016-02-16 14:50:53 +1100144 local vmdk_adapter_type
145 vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700146 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
147 vmdk_adapter_type="${vmdk_adapter_type%?}"
148
149 # vmdk disk type
Ian Wienand7ae97292016-02-16 14:50:53 +1100150 local vmdk_create_type
151 vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700152 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800153 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800154
155 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900156 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700157 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
158 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500159 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500160 # Attempt to retrieve the ``*-flat.vmdk``
Ian Wienand7ae97292016-02-16 14:50:53 +1100161 local flat_fname
162 flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800163 flat_fname="${flat_fname#*\"}"
164 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700165 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500166 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800167 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500168 path_len=`expr ${#image_url} - ${#image_fname}`
169 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800170 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900171 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800172 if [[ $flat_url != file* ]]; then
173 if [[ ! -f $FILES/$flat_fname || \
174 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100175 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800176 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500177 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800178 else
Dean Troyere9f76672014-07-25 11:09:36 -0500179 image=$(echo $flat_url | sed "s/^file:\/\///g")
180 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800181 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700182 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800183 fi
184 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500185 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800186 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800187 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
188 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800189 elif [[ -z "$vmdk_create_type" ]]; then
190 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
191 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500192 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800193 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500194 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700195 else
Dean Troyere9f76672014-07-25 11:09:36 -0500196 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
197 path_len=`expr ${#image_url} - ${#image_fname}`
198 local flat_path="${image_url:0:$path_len}"
199 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700200 warn $LINENO "$descriptor_data_pair_msg"`
201 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
202 if [[ $flat_path != file* ]]; then
203 if [[ ! -f $FILES/$descriptor_fname || \
204 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
205 wget -c $descriptor_url -O $FILES/$descriptor_fname
206 fi
207 descriptor_url="$FILES/$descriptor_fname"
208 else
209 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
210 if [[ ! -f $descriptor_url || \
211 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
212 echo "Descriptor not found: $descriptor_url"
213 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800214 fi
215 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800216 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
217 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
218 vmdk_adapter_type="${vmdk_adapter_type%?}"
219 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900220 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700221 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700222 vmdk_disktype="preallocated"
223 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800224
225 # NOTE: For backwards compatibility reasons, colons may be used in place
226 # of semi-colons for property delimiters but they are not permitted
227 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500228 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800229 IFS=':;' read -a props <<< "$property_string"
230 vmdk_disktype="${props[0]:-$vmdk_disktype}"
231 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
232 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700233
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300234 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" --public --container-format bare --disk-format vmdk --property vmware_disktype="$vmdk_disktype" --property vmware_adaptertype="$vmdk_adapter_type" --property hw_vif_model="$vmdk_net_adapter" < "${image}"
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700235 return
236 fi
237
Mate Lakatbc2ef922013-08-15 18:06:59 +0100238 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500239 # and should not be decompressed prior to loading
240 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500241 image_name="${image_fname%.vhd.tgz}"
242 local force_vm_mode=""
243 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000244 # Cirros VHD image currently only boots in PV mode.
245 # Nova defaults to PV for all VHD images, but
246 # the glance setting is needed for booting
247 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500248 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000249 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400250 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300251 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400252 image create \
253 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000254 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500255 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500256 return
257 fi
258
Mate Lakatbc2ef922013-08-15 18:06:59 +0100259 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
260 # and should not be decompressed prior to loading.
261 # Setting metadata, so PV mode is used.
262 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500263 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400264 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300265 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400266 image create \
267 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100268 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500269 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100270 return
271 fi
272
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300273 if [[ "$image_url" =~ '.hds' ]]; then
274 image_name="${image_fname%.hds}"
275 vm_mode=${image_name##*-}
276 if [[ $vm_mode != 'exe' && $vm_mode != 'hvm' ]]; then
277 die $LINENO "Unknown vm_mode=${vm_mode} for Virtuozzo image"
278 fi
279
280 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300281 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300282 image create \
283 "$image_name" --public \
284 --container-format=bare --disk-format=ploop \
285 --property vm_mode=$vm_mode < "${image}"
286 return
287 fi
288
Dean Troyere9f76672014-07-25 11:09:36 -0500289 local kernel=""
290 local ramdisk=""
291 local disk_format=""
292 local container_format=""
293 local unpack=""
294 local img_property=""
295 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500296 *.tar.gz|*.tgz)
297 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500298 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
299 image_name="${image_fname%.tar.gz}" ||
300 image_name="${image_fname%.tgz}"
301 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500302 rm -Rf "$xdir";
303 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500304 tar -zxf $image -C "$xdir"
305 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400306 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500307 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400308 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500309 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400310 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500311 if [[ -z "$image_name" ]]; then
312 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500313 fi
314 ;;
315 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500316 image_name=$(basename "$image" ".img")
Ian Wienandada886d2015-10-07 14:06:26 +1100317 local format
318 format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500319 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500320 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500321 else
Dean Troyere9f76672014-07-25 11:09:36 -0500322 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500323 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500324 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500325 ;;
326 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500327 image_name=$(basename "$image" ".img.gz")
328 disk_format=raw
329 container_format=bare
330 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500331 ;;
Hongbin Lu3feceb02016-04-17 11:11:58 -0400332 *.img.bz2)
333 image_name=$(basename "$image" ".img.bz2")
334 disk_format=qcow2
335 container_format=bare
336 unpack=bunzip2
337 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500338 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500339 image_name=$(basename "$image" ".qcow2")
340 disk_format=qcow2
341 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500342 ;;
Angel Noamf24e2992017-05-11 15:13:29 +0300343 *.raw)
344 image_name=$(basename "$image" ".raw")
345 disk_format=raw
346 container_format=bare
347 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100348 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500349 image_name=$(basename "$image" ".iso")
350 disk_format=iso
351 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100352 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300353 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
354 local extension="${image_fname#*.}"
355 image_name=$(basename "$image" ".$extension")
Lucian Petrut2715fd02017-05-24 11:31:56 +0300356 disk_format=$(echo $image_fname | grep -oP '(?<=\.)vhdx?(?=\.|$)')
Alessandro Pilottica823942014-08-07 02:05:26 +0300357 container_format=bare
358 if [ "${image_fname##*.}" == "gz" ]; then
359 unpack=zcat
360 fi
361 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500362 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500363 esac
364
Rafael Folco72f530f2016-02-09 07:08:38 -0600365 if is_arch "ppc64le" || is_arch "ppc64" || is_arch "ppc"; then
Rafael Folco7b8f1e72017-10-16 19:10:09 +0000366 img_property="--property hw_cdrom_bus=scsi --property os_command_line=console=hvc0"
Rafael Folcoab775872013-12-02 14:04:32 -0200367 fi
368
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000369 if is_arch "aarch64"; then
Kevin Zhaoa9cc38a2016-06-24 04:30:12 -0400370 img_property="--property hw_machine_type=virt --property hw_cdrom_bus=scsi --property hw_scsi_model=virtio-scsi --property os_command_line='console=ttyAMA0'"
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000371 fi
372
Dean Troyere9f76672014-07-25 11:09:36 -0500373 if [ "$container_format" = "bare" ]; then
374 if [ "$unpack" = "zcat" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300375 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < <(zcat --force "${image}")
Hongbin Lu3feceb02016-04-17 11:11:58 -0400376 elif [ "$unpack" = "bunzip2" ]; then
377 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < <(bunzip2 -cdk "${image}")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500378 else
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300379 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500380 fi
381 else
382 # Use glance client to add the kernel the root filesystem.
383 # We parse the results of the first upload to get the glance ID of the
384 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500385 local kernel_id="" ramdisk_id="";
386 if [ -n "$kernel" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300387 kernel_id=$(openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name-kernel" $img_property --public --container-format aki --disk-format aki < "$kernel" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500388 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500389 if [ -n "$ramdisk" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300390 ramdisk_id=$(openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name-ramdisk" $img_property --public --container-format ari --disk-format ari < "$ramdisk" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500391 fi
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300392 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "${image_name%.img}" $img_property --public --container-format ami --disk-format ami ${kernel_id:+--property kernel_id=$kernel_id} ${ramdisk_id:+--property ramdisk_id=$ramdisk_id} < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500393 fi
394}
395
Dean Troyer1a6d4492013-06-03 16:47:36 -0500396
Dean Troyerc1b486a2012-11-05 14:26:09 -0600397# Set the database backend to use
398# When called from stackrc/localrc DATABASE_BACKENDS has not been
399# initialized yet, just save the configuration selection and call back later
400# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100401#
402# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600403function use_database {
404 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600405 # No backends registered means this is likely called from ``localrc``
406 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600407 DATABASE_TYPE=$1
Sean Dague72ad9422015-10-07 11:51:40 -0400408 deprecated "The database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100409 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600410 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100411 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600412 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600413}
414
sridhargaddamb5ab6462015-02-24 07:23:24 +0000415#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
416CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500417
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600418# Wait for an HTTP server to start answering requests
419# wait_for_service timeout url
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400420#
421# If the service we want is behind a proxy, the proxy may be available
422# before the service. Compliant proxies will return a 503 in this case
423# Loop until we get something else.
424# Also check for the case where there is no proxy and the service just
425# hasn't started yet. curl returns 7 for Failed to connect to host.
Ian Wienandaee18c72014-02-21 15:35:08 +1100426function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600427 local timeout=$1
428 local url=$2
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400429 local rval=0
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900430 time_start "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400431 timeout $timeout bash -x <<EOF || rval=$?
432 while [[ \$( ${CURL_GET} -k --noproxy '*' -s -o /dev/null -w '%{http_code}' ${url} ) == 503 || \$? -eq 7 ]]; do
433 sleep 1
434 done
435EOF
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900436 time_stop "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400437 return $rval
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600438}
439
Sean Daguec2fe9162017-07-28 11:29:18 +0000440function wait_for_compute {
441 local timeout=$1
442 local rval=0
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530443 local compute_hostname
Sean Daguec2fe9162017-07-28 11:29:18 +0000444 time_start "wait_for_service"
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530445 compute_hostname=$(iniget $NOVA_CONF DEFAULT host)
446 if [[ -z $compute_hostname ]]; then
447 compute_hostname=$(hostname)
448 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000449 timeout $timeout bash -x <<EOF || rval=$?
450 ID=""
451 while [[ "\$ID" == "" ]]; do
452 sleep 1
Chris Dentac475bb2018-02-07 18:35:40 +0000453 if [[ "$VIRT_DRIVER" = 'fake' ]]; then
454 # When using the fake driver the compute hostnames have a suffix of 1 to NUMBER_FAKE_NOVA_COMPUTE
455 ID=\$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list --host `hostname`1 --service nova-compute -c ID -f value)
456 else
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530457 ID=\$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list --host "$compute_hostname" --service nova-compute -c ID -f value)
Chris Dentac475bb2018-02-07 18:35:40 +0000458 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000459 done
460EOF
461 time_stop "wait_for_service"
462 # Figure out what's happening on platforms where this doesn't work
463 if [[ "$rval" != 0 ]]; then
464 echo "Didn't find service registered by hostname after $timeout seconds"
465 openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list
466 fi
467 return $rval
468}
469
Dean Troyer1a6d4492013-06-03 16:47:36 -0500470
Nachi Uenofda946e2012-10-24 17:26:02 -0700471# ping check
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100472# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``PRIVATE_NETWORK``
Sean Dagueaf9bf862015-04-16 08:58:32 -0400473# ping_check <ip> [boot-timeout] [from_net] [expected]
Ian Wienandaee18c72014-02-21 15:35:08 +1100474function ping_check {
Sean Dagueaf9bf862015-04-16 08:58:32 -0400475 local ip=$1
476 local timeout=${2:-30}
477 local from_net=${3:-""}
478 local expected=${4:-True}
479 local op="!"
480 local failmsg="[Fail] Couldn't ping server"
481 local ping_cmd="ping"
Nachi Uenofda946e2012-10-24 17:26:02 -0700482
Sean Dagueaf9bf862015-04-16 08:58:32 -0400483 # if we don't specify a from_net we're expecting things to work
484 # fine from our local box.
485 if [[ -n "$from_net" ]]; then
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100486 # TODO(stephenfin): Is there any way neutron could be disabled now?
Sean Dagueaf9bf862015-04-16 08:58:32 -0400487 if is_service_enabled neutron; then
488 ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700489 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700490 fi
Sean Dagueaf9bf862015-04-16 08:58:32 -0400491
492 # inverse the logic if we're testing no connectivity
493 if [[ "$expected" != "True" ]]; then
494 op=""
495 failmsg="[Fail] Could ping server"
496 fi
497
498 # Because we've transformed this command so many times, print it
499 # out at the end.
500 local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
501 echo "Checking connectivity with $check_command"
502
503 if ! timeout $timeout sh -c "$check_command"; then
504 die $LINENO $failmsg
505 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700506}
507
Nachi Ueno6769b162013-08-12 18:18:56 -0700508# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100509function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700510 local vm_id=$1
511 local network_name=$2
Ryota MIBU842d54a2017-12-25 16:28:50 +0900512 local addresses
Ian Wienandada886d2015-10-07 14:06:26 +1100513 local ip
Ian Wienand7ae97292016-02-16 14:50:53 +1100514
Ryota MIBU842d54a2017-12-25 16:28:50 +0900515 addresses=$(openstack server show -c addresses -f value "$vm_id")
516 ip=$(echo $addresses | sed -n "s/^.*$network_name=\([0-9\.]*\).*$/\1/p")
Nachi Ueno6769b162013-08-12 18:18:56 -0700517 if [[ $ip = "" ]];then
Ryota MIBU842d54a2017-12-25 16:28:50 +0900518 echo "addresses of server $vm_id : $addresses"
Atsushi SAKAI33c9a672015-11-12 19:50:00 +0900519 die $LINENO "[Fail] Couldn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700520 fi
521 echo $ip
522}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500523
Nachi Uenofda946e2012-10-24 17:26:02 -0700524# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700525
Dean Troyer1a6d4492013-06-03 16:47:36 -0500526# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100527function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400528 if is_service_enabled neutron; then
529 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700530 return
531 fi
532 _ssh_check_novanet "$1" $2 $3 $4 $5
533}
534
Ian Wienandaee18c72014-02-21 15:35:08 +1100535function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700536 local NET_NAME=$1
537 local KEY_FILE=$2
538 local FLOATING_IP=$3
539 local DEFAULT_INSTANCE_USER=$4
540 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600541 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500542 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success; do sleep 1; done"; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800543 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700544 fi
545}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500546
Vincent Untz856a11e2012-11-21 16:04:12 +0100547
Vincent Untz856a11e2012-11-21 16:04:12 +0100548# Get the location of the $module-rootwrap executables, where module is cinder
549# or nova.
550# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100551function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100552 local module=$1
553
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100554 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100555}
556
Dean Troyer1a6d4492013-06-03 16:47:36 -0500557
Ian Wienand0488edd2013-04-11 12:04:36 +1000558# Path permissions sanity check
559# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100560function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000561 # Ensure no element of the path has 0700 permissions, which is very
562 # likely to cause issues for daemons. Inspired by default 0700
563 # homedir permissions on RHEL and common practice of making DEST in
564 # the stack user's homedir.
565
Ian Wienandada886d2015-10-07 14:06:26 +1100566 local real_path
567 real_path=$(readlink -f $1)
Ian Wienand0488edd2013-04-11 12:04:36 +1000568 local rebuilt_path=""
569 for i in $(echo ${real_path} | tr "/" " "); do
570 rebuilt_path=$rebuilt_path"/"$i
571
572 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
573 echo "*** DEST path element"
574 echo "*** ${rebuilt_path}"
575 echo "*** appears to have 0700 permissions."
Dean Troyerdc97cb72015-03-28 08:20:50 -0500576 echo "*** This is very likely to cause fatal issues for DevStack daemons."
Ian Wienand0488edd2013-04-11 12:04:36 +1000577
578 if [[ -n "$SKIP_PATH_SANITY" ]]; then
579 return
580 else
581 echo "*** Set SKIP_PATH_SANITY to skip this check"
582 die $LINENO "Invalid path permissions"
583 fi
584 fi
585 done
586}
587
Dean Troyer1a6d4492013-06-03 16:47:36 -0500588
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100589# vercmp ver1 op ver2
590# Compare VER1 to VER2
591# - op is one of < <= == >= >
592# - returns true if satisified
593# e.g.
594# if vercmp 1.0 "<" 2.0; then
595# ...
596# fi
597function vercmp {
598 local v1=$1
599 local op=$2
600 local v2=$3
601 local result
602
603 # sort the two numbers with sort's "-V" argument. Based on if v2
604 # swapped places with v1, we can determine ordering.
605 result=$(echo -e "$v1\n$v2" | sort -V | head -1)
606
607 case $op in
608 "==")
609 [ "$v1" = "$v2" ]
610 return
611 ;;
612 ">")
613 [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
614 return
615 ;;
616 "<")
617 [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
618 return
619 ;;
620 ">=")
621 [ "$result" = "$v2" ]
622 return
623 ;;
624 "<=")
625 [ "$result" = "$v1" ]
626 return
627 ;;
628 *)
629 die $LINENO "unrecognised op: $op"
630 ;;
631 esac
632}
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000633
Sean Dague9751be62016-04-05 12:08:57 -0400634# This sets up defaults we like in devstack for logging for tracking
635# down issues, and makes sure everything is done the same between
636# projects.
637function setup_logging {
638 local conf_file=$1
639 local other_cond=${2:-"False"}
Sean Dague5edae542017-03-21 20:50:24 -0400640 if [[ "$USE_SYSTEMD" == "True" ]]; then
641 setup_systemd_logging $conf_file
642 elif [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ] && [ "$other_cond" == "False" ]; then
Sean Dague9751be62016-04-05 12:08:57 -0400643 setup_colorized_logging $conf_file
644 else
645 setup_standard_logging_identity $conf_file
646 fi
647}
648
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700649# This function sets log formatting options for colorizing log
650# output to stdout. It is meant to be called by lib modules.
651# The last two parameters are optional and can be used to specify
652# non-default value for project and user format variables.
653# Defaults are respectively 'project_name' and 'user_name'
654#
655# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100656function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700657 local conf_file=$1
Sean Dagueb6753ce2016-04-05 11:52:44 -0400658 local conf_section="DEFAULT"
659 local project_var="project_name"
660 local user_var="user_name"
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700661 # Add color to logging output
Sean Dagueb6753ce2016-04-05 11:52:44 -0400662 iniset $conf_file $conf_section logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %("$project_var")s %("$user_var")s%(color)s] %(instance)s%(color)s%(message)s"
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700663 iniset $conf_file $conf_section logging_default_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
664 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
665 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
666}
667
Sean Dague5edae542017-03-21 20:50:24 -0400668function setup_systemd_logging {
669 local conf_file=$1
670 local conf_section="DEFAULT"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400671 # NOTE(sdague): this is a nice to have, and means we're using the
672 # native systemd path, which provides for things like search on
673 # request-id. However, there may be an eventlet interaction here,
674 # so going off for now.
Kirill Zaitsevc0644f32017-05-24 13:00:47 +0300675 USE_JOURNAL=$(trueorfalse False USE_JOURNAL)
Eric Fried8cd310d2017-05-16 13:52:03 -0500676 local pidstr=""
Sean Dagueb2bfe562017-05-03 09:58:21 -0400677 if [[ "$USE_JOURNAL" == "True" ]]; then
678 iniset $conf_file $conf_section use_journal "True"
679 # if we are using the journal directly, our process id is already correct
Sean Dagueb2bfe562017-05-03 09:58:21 -0400680 else
Eric Fried8cd310d2017-05-16 13:52:03 -0500681 pidstr="(pid=%(process)d) "
Sean Dagueb2bfe562017-05-03 09:58:21 -0400682 fi
Eric Fried8cd310d2017-05-16 13:52:03 -0500683 iniset $conf_file $conf_section logging_debug_format_suffix "{{${pidstr}%(funcName)s %(pathname)s:%(lineno)d}}"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400684
Sean Daguee123ede2017-05-23 15:53:48 -0400685 iniset $conf_file $conf_section logging_context_format_string "%(color)s%(levelname)s %(name)s [%(global_request_id)s %(request_id)s %(project_name)s %(user_name)s%(color)s] %(instance)s%(color)s%(message)s"
Eric Fried8cd310d2017-05-16 13:52:03 -0500686 iniset $conf_file $conf_section logging_default_format_string "%(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
687 iniset $conf_file $conf_section logging_exception_prefix "ERROR %(name)s %(instance)s"
Sean Dague5edae542017-03-21 20:50:24 -0400688}
689
Sean Dague9751be62016-04-05 12:08:57 -0400690function setup_standard_logging_identity {
691 local conf_file=$1
692 iniset $conf_file DEFAULT logging_user_identity_format "%(project_name)s %(user_name)s"
693}
694
Ian Wienand54e39102014-06-03 16:05:12 +1000695# These functions are provided for basic fall-back functionality for
Dean Troyerdc97cb72015-03-28 08:20:50 -0500696# projects that include parts of DevStack (Grenade). stack.sh will
697# override these with more specific versions for DevStack (with fancy
Ian Wienand54e39102014-06-03 16:05:12 +1000698# spinners, etc). We never override an existing version
699if ! function_exists echo_summary; then
700 function echo_summary {
701 echo $@
702 }
703fi
704if ! function_exists echo_nolog; then
705 function echo_nolog {
706 echo $@
707 }
708fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600709
Sébastien Han36f2f022014-01-06 18:09:26 +0100710
711# create_disk - Create backing disk
712function create_disk {
713 local node_number
714 local disk_image=${1}
715 local storage_data_dir=${2}
716 local loopback_disk_size=${3}
717
718 # Create a loopback disk and format it to XFS.
719 if [[ -e ${disk_image} ]]; then
720 if egrep -q ${storage_data_dir} /proc/mounts; then
liumk38a23d92017-12-13 15:09:02 +0800721 sudo umount ${storage_data_dir}
Sébastien Han36f2f022014-01-06 18:09:26 +0100722 sudo rm -f ${disk_image}
723 fi
724 fi
725
726 sudo mkdir -p ${storage_data_dir}/drives/images
727
728 sudo truncate -s ${loopback_disk_size} ${disk_image}
729
730 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
731 # a single inode. Keeping the default inode size (256) will result in multiple
732 # inodes being used to store xattr. Retrieving the xattr will be slower
733 # since we have to read multiple inodes. This statement is true for both
734 # Swift and Ceph.
735 sudo mkfs.xfs -f -i size=1024 ${disk_image}
736
737 # Mount the disk with mount options to make it as efficient as possible
738 if ! egrep -q ${storage_data_dir} /proc/mounts; then
Lee Yarwood5d7d8912018-12-03 14:21:06 +0000739 sudo mount -t xfs -o loop,noatime,nodiratime,logbufs=8 \
Sébastien Han36f2f022014-01-06 18:09:26 +0100740 ${disk_image} ${storage_data_dir}
741 fi
742}
743
Ihar Hrachyshka7b5c7dc2016-07-15 20:17:13 +0200744
745# set_mtu - Set MTU on a device
746function set_mtu {
747 local dev=$1
748 local mtu=$2
749 sudo ip link set mtu $mtu dev $dev
750}
751
752
Denis Buliga0bf75a42017-02-06 16:56:46 +0200753# running_in_container - Returns true otherwise false
754function running_in_container {
kesperd18d7c82017-03-23 05:52:33 +0000755 [[ $(systemd-detect-virt --container) != 'none' ]]
Denis Buliga0bf75a42017-02-06 16:56:46 +0200756}
757
758
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000759# enable_kernel_bridge_firewall - Enable kernel support for bridge firewalling
760function enable_kernel_bridge_firewall {
761 # Load bridge module. This module provides access to firewall for bridged
762 # frames; and also on older kernels (pre-3.18) it provides sysctl knobs to
763 # enable/disable bridge firewalling
764 sudo modprobe bridge
765 # For newer kernels (3.18+), those sysctl settings are split into a separate
766 # kernel module (br_netfilter). Load it too, if present.
767 sudo modprobe br_netfilter 2>> /dev/null || :
768 # Enable bridge firewalling in case it's disabled in kernel (upstream
769 # default is enabled, but some distributions may decide to change it).
770 # This is at least needed for RHEL 7.2 and earlier releases.
Ihar Hrachyshka3f771b72016-12-17 04:12:24 +0000771 for proto in ip ip6; do
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000772 sudo sysctl -w net.bridge.bridge-nf-call-${proto}tables=1
773 done
774}
775
776
Dan Smith1f55d382017-05-16 08:50:53 -0700777# Set a systemd system override
778#
779# This sets a system-side override in system.conf. A per-service
780# override would be /etc/systemd/system/${service}.service/override.conf
781function set_systemd_override {
782 local key="$1"
783 local value="$2"
784
785 local sysconf="/etc/systemd/system.conf"
786 iniset -sudo "${sysconf}" "Manager" "$key" "$value"
787 echo "Set systemd system override for ${key}=${value}"
788
789 sudo systemctl daemon-reload
790}
791
Matthew Treinish309b99e2017-05-23 15:18:31 -0400792# Get a random port from the local port range
793#
794# This function returns an available port in the local port range. The search
795# order is not truly random, but should be considered a random value by the
796# user because it depends on the state of your local system.
797function get_random_port {
798 read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
799 while true; do
800 for (( port = upper_port ; port >= lower_port ; port-- )); do
801 sudo lsof -i ":$port" &> /dev/null
802 if [[ $? > 0 ]] ; then
803 break 2
804 fi
805 done
806 done
807 echo $port
808}
809
Ian Wienand07cbc442017-06-30 12:29:19 +1000810# Save some state information
811#
812# Write out various useful state information to /etc/devstack-version
Sean Dague2c0faca2017-06-28 09:13:04 -0400813function write_devstack_version {
Dirk Mueller6bab8322018-03-02 21:13:12 +0100814 cat - <<EOF | sudo tee /etc/devstack-version >/dev/null
Ian Wienand07cbc442017-06-30 12:29:19 +1000815DevStack Version: ${DEVSTACK_SERIES}
816Change: $(git log --format="%H %s %ci" -1)
817OS Version: ${os_VENDOR} ${os_RELEASE} ${os_CODENAME}
Sean Dague2c0faca2017-06-28 09:13:04 -0400818EOF
Sean Dague2c0faca2017-06-28 09:13:04 -0400819}
820
Dean Troyer27e32692012-03-16 16:16:56 -0500821# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100822$_XTRACE_FUNCTIONS
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500823
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500824# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400825# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400826# End: