blob: cc1ca6cb25da358278674f09382e13e6a99e7c4b [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
Eric Fried2468cea2019-07-25 13:18:58 -050021source ${FUNC_DIR}/inc/meta-config
Dean Troyer490430d2015-01-30 14:38:35 -060022source ${FUNC_DIR}/inc/python
Dean Troyer32d6bc62015-03-29 14:16:44 -050023source ${FUNC_DIR}/inc/rootwrap
Dean Troyer7f9aa712012-01-31 12:11:56 -060024
Dean Troyer27e32692012-03-16 16:16:56 -050025# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110026_XTRACE_FUNCTIONS=$(set +o | grep xtrace)
Dean Troyer27e32692012-03-16 16:16:56 -050027set +o xtrace
28
Ian Wienand54e39102014-06-03 16:05:12 +100029# Check if a function already exists
30function function_exists {
31 declare -f -F $1 > /dev/null
32}
Dean Troyer7f9aa712012-01-31 12:11:56 -060033
Sean Dague646085d2016-03-21 17:00:51 -040034# short_source prints out the current location of the caller in a way
35# that strips redundant directories. This is useful for PS4 usage.
36function short_source {
37 saveIFS=$IFS
38 IFS=" "
39 called=($(caller 0))
40 IFS=$saveIFS
41 file=${called[2]}
42 file=${file#$RC_DIR/}
43 printf "%-40s " "$file:${called[1]}:${called[0]}"
44}
John L. Villalovosdaa7a412016-05-05 12:50:52 -070045# PS4 is exported to child shells and uses the 'short_source' function, so
46# export it so child shells have access to the 'short_source' function also.
47export -f short_source
Sean Dague646085d2016-03-21 17:00:51 -040048
Monty Taylord8bb2202017-09-03 12:13:59 -050049# Download a file from a URL
50#
51# Will check cache (in $FILES) or download given URL.
52#
53# Argument is the URL to the remote file
54#
55# Will echo the local path to the file as the output. Will die on
56# failure to download.
57#
58# Files can be pre-cached for CI environments, see EXTRA_CACHE_URLS
59# and tools/image_list.sh
60function get_extra_file {
61 local file_url=$1
62
63 file_name=$(basename "$file_url")
64 if [[ $file_url != file* ]]; then
65 # If the file isn't cache, download it
66 if [[ ! -f $FILES/$file_name ]]; then
yuanke weic652a492017-09-17 22:18:07 +080067 wget --progress=dot:giga -t 2 -c $file_url -O $FILES/$file_name
Monty Taylord8bb2202017-09-03 12:13:59 -050068 if [[ $? -ne 0 ]]; then
69 die "$file_url could not be downloaded"
70 fi
71 fi
72 echo "$FILES/$file_name"
73 return
74 else
75 # just strip the file:// bit and that's the path to the file
76 echo $file_url | sed 's/$file:\/\///g'
77 fi
78}
79
Dan Smith2614c1b2020-07-15 14:41:38 -070080# Generate image property arguments for OSC
81#
82# Arguments: properties, one per, like propname=value
83#
84# Result is --property propname1=value1 --property propname2=value2
85function _image_properties_to_arg {
86 local result=""
87 for property in $*; do
88 result+=" --property $property"
89 done
90 echo $result
91}
92
Abhishek Kekane73ad9762020-06-16 15:20:48 +000093# Upload an image to glance using the configured mechanism
94#
95# Arguments:
96# image name
97# container format
98# disk format
99# path to image file
100# optional properties (format of propname=value)
101#
102function _upload_image {
103 local image_name="$1"
104 shift
105 local container="$1"
106 shift
107 local disk="$1"
108 shift
109 local image="$1"
110 shift
111 local properties
112 local useimport
113
Dan Smith2614c1b2020-07-15 14:41:38 -0700114 properties=$(_image_properties_to_arg $*)
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000115
116 if [[ "$GLANCE_USE_IMPORT_WORKFLOW" == "True" ]]; then
Dan Smith33f8f6e2020-07-21 19:41:48 -0700117 useimport="--import"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000118 fi
119
120 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" --public --container-format "$container" --disk-format "$disk" $useimport $properties < "${image}"
121}
Sean Dague646085d2016-03-21 17:00:51 -0400122
Adam Spierscb961592013-10-05 12:11:07 +0100123# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -0500124# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +0100125#
126# - ``FILES`` must be set to the cache dir
127# - ``GLANCE_HOSTPORT``
128#
Peter Stachowski5aeea6a2015-09-22 19:38:02 +0000129# upload_image image-url
Ian Wienandaee18c72014-02-21 15:35:08 +1100130function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500131 local image_url=$1
Dean Troyerca0e3d02012-04-13 15:58:37 -0500132
Dean Troyere9f76672014-07-25 11:09:36 -0500133 local image image_fname image_name
134
Dean Troyerca0e3d02012-04-13 15:58:37 -0500135 # Create a directory for the downloaded image tarballs.
136 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -0500137 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -0800138 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800139 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -0500140 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100141 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900142 if [[ $? -ne 0 ]]; then
143 echo "Not found: $image_url"
144 return
145 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -0800146 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500147 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -0800148 else
Dean Troyer3324f192014-09-18 09:26:39 -0500149 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -0800150 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -0500151 # unix: ``file:///home/user/path/file``
152 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -0500153 image=$(echo $image_url | sed "s/^file:\/\///g")
154 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500155 echo "Not found: $image_url"
156 return
157 fi
158 fi
159
160 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
161 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500162 image_name="${image_fname%.tar.gz}"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000163 _upload_image "$image_name" ami ami "$image"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500164 return
165 fi
166
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700167 # vmdk format images
168 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500169 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700170
171 # Before we can upload vmdk type images to glance, we need to know it's
172 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800173 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700174 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700175 # vmdk filename, which is expected in the following format:
176 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800177 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700178 #
179 # If the filename does not follow the above format then the vsphere
180 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700181
Dean Troyere9f76672014-07-25 11:09:36 -0500182 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -0800183 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -0500184 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800185
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700186 # vmdk adapter type
Ian Wienand7ae97292016-02-16 14:50:53 +1100187 local vmdk_adapter_type
188 vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700189 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
190 vmdk_adapter_type="${vmdk_adapter_type%?}"
191
192 # vmdk disk type
Ian Wienand7ae97292016-02-16 14:50:53 +1100193 local vmdk_create_type
194 vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700195 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800196 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800197
198 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900199 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700200 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
201 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500202 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500203 # Attempt to retrieve the ``*-flat.vmdk``
Ian Wienand7ae97292016-02-16 14:50:53 +1100204 local flat_fname
205 flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800206 flat_fname="${flat_fname#*\"}"
207 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700208 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500209 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800210 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500211 path_len=`expr ${#image_url} - ${#image_fname}`
212 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800213 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900214 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800215 if [[ $flat_url != file* ]]; then
216 if [[ ! -f $FILES/$flat_fname || \
217 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100218 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800219 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500220 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800221 else
Dean Troyere9f76672014-07-25 11:09:36 -0500222 image=$(echo $flat_url | sed "s/^file:\/\///g")
223 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800224 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700225 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800226 fi
227 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500228 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800229 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800230 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
231 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800232 elif [[ -z "$vmdk_create_type" ]]; then
233 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
234 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500235 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800236 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500237 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700238 else
Dean Troyere9f76672014-07-25 11:09:36 -0500239 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
240 path_len=`expr ${#image_url} - ${#image_fname}`
241 local flat_path="${image_url:0:$path_len}"
242 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700243 warn $LINENO "$descriptor_data_pair_msg"`
244 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
245 if [[ $flat_path != file* ]]; then
246 if [[ ! -f $FILES/$descriptor_fname || \
247 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
248 wget -c $descriptor_url -O $FILES/$descriptor_fname
249 fi
250 descriptor_url="$FILES/$descriptor_fname"
251 else
252 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
253 if [[ ! -f $descriptor_url || \
254 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
255 echo "Descriptor not found: $descriptor_url"
256 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800257 fi
258 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800259 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
260 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
261 vmdk_adapter_type="${vmdk_adapter_type%?}"
262 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900263 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700264 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700265 vmdk_disktype="preallocated"
266 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800267
268 # NOTE: For backwards compatibility reasons, colons may be used in place
269 # of semi-colons for property delimiters but they are not permitted
270 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500271 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800272 IFS=':;' read -a props <<< "$property_string"
273 vmdk_disktype="${props[0]:-$vmdk_disktype}"
274 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
275 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700276
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000277 _upload_image "$image_name" bare vmdk "$image" vmware_disktype="$vmdk_disktype" vmware_adaptertype="$vmdk_adapter_type" hw_vif_model="$vmdk_net_adapter"
278
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700279 return
280 fi
281
Mate Lakatbc2ef922013-08-15 18:06:59 +0100282 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500283 # and should not be decompressed prior to loading
284 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500285 image_name="${image_fname%.vhd.tgz}"
286 local force_vm_mode=""
287 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000288 # Cirros VHD image currently only boots in PV mode.
289 # Nova defaults to PV for all VHD images, but
290 # the glance setting is needed for booting
291 # directly from volume.
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000292 force_vm_mode="vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000293 fi
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000294 _upload_image "$image_name" ovf vhd "$image" $force_vm_mode
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500295 return
296 fi
297
Mate Lakatbc2ef922013-08-15 18:06:59 +0100298 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
299 # and should not be decompressed prior to loading.
300 # Setting metadata, so PV mode is used.
301 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500302 image_name="${image_fname%.xen-raw.tgz}"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000303 _upload_image "$image_name" tgz raw "$image" vm_mode=xen
Mate Lakatbc2ef922013-08-15 18:06:59 +0100304 return
305 fi
306
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300307 if [[ "$image_url" =~ '.hds' ]]; then
308 image_name="${image_fname%.hds}"
309 vm_mode=${image_name##*-}
310 if [[ $vm_mode != 'exe' && $vm_mode != 'hvm' ]]; then
311 die $LINENO "Unknown vm_mode=${vm_mode} for Virtuozzo image"
312 fi
313
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000314 _upload_image "$image_name" bare ploop "$image" vm_mode=$vm_mode
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300315 return
316 fi
317
Dean Troyere9f76672014-07-25 11:09:36 -0500318 local kernel=""
319 local ramdisk=""
320 local disk_format=""
321 local container_format=""
322 local unpack=""
Dan Smith49ad4852020-07-15 14:54:22 -0700323 local img_property=""
324
325 # NOTE(danms): If we're on libvirt/qemu or libvirt/kvm, set the hw_rng_model
326 # to libvirt in the image properties.
327 if [[ "$VIRT_DRIVER" == "libvirt" ]]; then
328 if [[ "$LIBVIRT_TYPE" == "qemu" || "$LIBVIRT_TYPE" == "kvm" ]]; then
329 img_property="hw_rng_model=virtio"
330 fi
331 fi
332
Dean Troyere9f76672014-07-25 11:09:36 -0500333 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500334 *.tar.gz|*.tgz)
335 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500336 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
337 image_name="${image_fname%.tar.gz}" ||
338 image_name="${image_fname%.tgz}"
339 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500340 rm -Rf "$xdir";
341 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500342 tar -zxf $image -C "$xdir"
343 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400344 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500345 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400346 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500347 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400348 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500349 if [[ -z "$image_name" ]]; then
350 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500351 fi
352 ;;
353 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500354 image_name=$(basename "$image" ".img")
Ian Wienandada886d2015-10-07 14:06:26 +1100355 local format
356 format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500357 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500358 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500359 else
Dean Troyere9f76672014-07-25 11:09:36 -0500360 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500361 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500362 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500363 ;;
364 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500365 image_name=$(basename "$image" ".img.gz")
366 disk_format=raw
367 container_format=bare
368 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500369 ;;
Hongbin Lu3feceb02016-04-17 11:11:58 -0400370 *.img.bz2)
371 image_name=$(basename "$image" ".img.bz2")
372 disk_format=qcow2
373 container_format=bare
374 unpack=bunzip2
375 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500376 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500377 image_name=$(basename "$image" ".qcow2")
378 disk_format=qcow2
379 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500380 ;;
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000381 *.qcow2.xz)
382 image_name=$(basename "$image" ".qcow2.xz")
383 disk_format=qcow2
384 container_format=bare
385 unpack=unxz
386 ;;
Angel Noamf24e2992017-05-11 15:13:29 +0300387 *.raw)
388 image_name=$(basename "$image" ".raw")
389 disk_format=raw
390 container_format=bare
391 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100392 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500393 image_name=$(basename "$image" ".iso")
394 disk_format=iso
395 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100396 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300397 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
398 local extension="${image_fname#*.}"
399 image_name=$(basename "$image" ".$extension")
Lucian Petrut2715fd02017-05-24 11:31:56 +0300400 disk_format=$(echo $image_fname | grep -oP '(?<=\.)vhdx?(?=\.|$)')
Alessandro Pilottica823942014-08-07 02:05:26 +0300401 container_format=bare
402 if [ "${image_fname##*.}" == "gz" ]; then
403 unpack=zcat
404 fi
405 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500406 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500407 esac
408
Rafael Folco72f530f2016-02-09 07:08:38 -0600409 if is_arch "ppc64le" || is_arch "ppc64" || is_arch "ppc"; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000410 img_property="$img_property hw_cdrom_bus=scsi os_command_line=console=hvc0"
Rafael Folcoab775872013-12-02 14:04:32 -0200411 fi
412
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000413 if is_arch "aarch64"; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000414 img_property="$img_property hw_machine_type=virt hw_cdrom_bus=scsi hw_scsi_model=virtio-scsi os_command_line='console=ttyAMA0'"
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000415 fi
416
Dean Troyere9f76672014-07-25 11:09:36 -0500417 if [ "$container_format" = "bare" ]; then
418 if [ "$unpack" = "zcat" ]; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000419 _upload_image "$image_name" $container_format $disk_format <(zcat --force "$image") $img_property
Hongbin Lu3feceb02016-04-17 11:11:58 -0400420 elif [ "$unpack" = "bunzip2" ]; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000421 _upload_image "$image_name" $container_format $disk_format <(bunzip2 -cdk "$image") $img_property
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000422 elif [ "$unpack" = "unxz" ]; then
423 # NOTE(brtknr): unxz the file first and cleanup afterwards to
424 # prevent timeout while Glance tries to upload image (e.g. to Swift).
425 local tmp_dir
426 local image_path
427 tmp_dir=$(mktemp -d)
428 image_path="$tmp_dir/$image_name"
429 unxz -cv "${image}" > "$image_path"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000430 _upload_image "$image_name" $container_format $disk_format "$image_path" $img_property
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000431 rm -rf $tmp_dir
Dean Troyerca0e3d02012-04-13 15:58:37 -0500432 else
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000433 _upload_image "$image_name" $container_format $disk_format "$image" $img_property
Dean Troyerca0e3d02012-04-13 15:58:37 -0500434 fi
435 else
436 # Use glance client to add the kernel the root filesystem.
437 # We parse the results of the first upload to get the glance ID of the
438 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500439 local kernel_id="" ramdisk_id="";
440 if [ -n "$kernel" ]; then
Dan Smith2614c1b2020-07-15 14:41:38 -0700441 kernel_id=$(openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name-kernel" $(_image_properties_to_arg $img_property) --public --container-format aki --disk-format aki < "$kernel" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500442 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500443 if [ -n "$ramdisk" ]; then
Dan Smith2614c1b2020-07-15 14:41:38 -0700444 ramdisk_id=$(openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name-ramdisk" $(_image_properties_to_arg $img_property) --public --container-format ari --disk-format ari < "$ramdisk" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500445 fi
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000446 _upload_image "${image_name%.img}" ami ami "$image" ${kernel_id:+ kernel_id=$kernel_id} ${ramdisk_id:+ ramdisk_id=$ramdisk_id} $img_property
Dean Troyerca0e3d02012-04-13 15:58:37 -0500447 fi
448}
449
Dean Troyer1a6d4492013-06-03 16:47:36 -0500450
Dean Troyerc1b486a2012-11-05 14:26:09 -0600451# Set the database backend to use
452# When called from stackrc/localrc DATABASE_BACKENDS has not been
453# initialized yet, just save the configuration selection and call back later
454# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100455#
Matt Riedemannb14665f2019-10-17 19:34:05 +0000456# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600457function use_database {
458 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600459 # No backends registered means this is likely called from ``localrc``
460 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600461 DATABASE_TYPE=$1
Sean Dague72ad9422015-10-07 11:51:40 -0400462 deprecated "The database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100463 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600464 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100465 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600466 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600467}
468
sridhargaddamb5ab6462015-02-24 07:23:24 +0000469#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
470CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500471
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600472# Wait for an HTTP server to start answering requests
473# wait_for_service timeout url
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400474#
475# If the service we want is behind a proxy, the proxy may be available
476# before the service. Compliant proxies will return a 503 in this case
477# Loop until we get something else.
478# Also check for the case where there is no proxy and the service just
479# hasn't started yet. curl returns 7 for Failed to connect to host.
Ian Wienandaee18c72014-02-21 15:35:08 +1100480function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600481 local timeout=$1
482 local url=$2
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400483 local rval=0
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900484 time_start "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400485 timeout $timeout bash -x <<EOF || rval=$?
486 while [[ \$( ${CURL_GET} -k --noproxy '*' -s -o /dev/null -w '%{http_code}' ${url} ) == 503 || \$? -eq 7 ]]; do
487 sleep 1
488 done
489EOF
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900490 time_stop "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400491 return $rval
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600492}
493
Sean Daguec2fe9162017-07-28 11:29:18 +0000494function wait_for_compute {
495 local timeout=$1
496 local rval=0
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530497 local compute_hostname
Sean Daguec2fe9162017-07-28 11:29:18 +0000498 time_start "wait_for_service"
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530499 compute_hostname=$(iniget $NOVA_CONF DEFAULT host)
500 if [[ -z $compute_hostname ]]; then
501 compute_hostname=$(hostname)
502 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000503 timeout $timeout bash -x <<EOF || rval=$?
504 ID=""
505 while [[ "\$ID" == "" ]]; do
506 sleep 1
Chris Dentac475bb2018-02-07 18:35:40 +0000507 if [[ "$VIRT_DRIVER" = 'fake' ]]; then
508 # When using the fake driver the compute hostnames have a suffix of 1 to NUMBER_FAKE_NOVA_COMPUTE
509 ID=\$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list --host `hostname`1 --service nova-compute -c ID -f value)
510 else
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530511 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 +0000512 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000513 done
514EOF
515 time_stop "wait_for_service"
516 # Figure out what's happening on platforms where this doesn't work
517 if [[ "$rval" != 0 ]]; then
518 echo "Didn't find service registered by hostname after $timeout seconds"
519 openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list
520 fi
521 return $rval
522}
523
Dean Troyer1a6d4492013-06-03 16:47:36 -0500524
Nachi Uenofda946e2012-10-24 17:26:02 -0700525# ping check
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100526# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``PRIVATE_NETWORK``
Sean Dagueaf9bf862015-04-16 08:58:32 -0400527# ping_check <ip> [boot-timeout] [from_net] [expected]
Ian Wienandaee18c72014-02-21 15:35:08 +1100528function ping_check {
Sean Dagueaf9bf862015-04-16 08:58:32 -0400529 local ip=$1
530 local timeout=${2:-30}
531 local from_net=${3:-""}
532 local expected=${4:-True}
533 local op="!"
534 local failmsg="[Fail] Couldn't ping server"
535 local ping_cmd="ping"
Nachi Uenofda946e2012-10-24 17:26:02 -0700536
Sean Dagueaf9bf862015-04-16 08:58:32 -0400537 # if we don't specify a from_net we're expecting things to work
538 # fine from our local box.
539 if [[ -n "$from_net" ]]; then
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100540 # TODO(stephenfin): Is there any way neutron could be disabled now?
Sean Dagueaf9bf862015-04-16 08:58:32 -0400541 if is_service_enabled neutron; then
542 ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700543 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700544 fi
Sean Dagueaf9bf862015-04-16 08:58:32 -0400545
546 # inverse the logic if we're testing no connectivity
547 if [[ "$expected" != "True" ]]; then
548 op=""
549 failmsg="[Fail] Could ping server"
550 fi
551
552 # Because we've transformed this command so many times, print it
553 # out at the end.
554 local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
555 echo "Checking connectivity with $check_command"
556
557 if ! timeout $timeout sh -c "$check_command"; then
558 die $LINENO $failmsg
559 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700560}
561
Nachi Ueno6769b162013-08-12 18:18:56 -0700562# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100563function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700564 local vm_id=$1
565 local network_name=$2
Ryota MIBU842d54a2017-12-25 16:28:50 +0900566 local addresses
Ian Wienandada886d2015-10-07 14:06:26 +1100567 local ip
Ian Wienand7ae97292016-02-16 14:50:53 +1100568
Ryota MIBU842d54a2017-12-25 16:28:50 +0900569 addresses=$(openstack server show -c addresses -f value "$vm_id")
570 ip=$(echo $addresses | sed -n "s/^.*$network_name=\([0-9\.]*\).*$/\1/p")
Nachi Ueno6769b162013-08-12 18:18:56 -0700571 if [[ $ip = "" ]];then
Ryota MIBU842d54a2017-12-25 16:28:50 +0900572 echo "addresses of server $vm_id : $addresses"
Atsushi SAKAI33c9a672015-11-12 19:50:00 +0900573 die $LINENO "[Fail] Couldn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700574 fi
575 echo $ip
576}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500577
Nachi Uenofda946e2012-10-24 17:26:02 -0700578# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700579
Dean Troyer1a6d4492013-06-03 16:47:36 -0500580# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100581function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400582 if is_service_enabled neutron; then
583 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700584 return
585 fi
586 _ssh_check_novanet "$1" $2 $3 $4 $5
587}
588
Ian Wienandaee18c72014-02-21 15:35:08 +1100589function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700590 local NET_NAME=$1
591 local KEY_FILE=$2
592 local FLOATING_IP=$3
593 local DEFAULT_INSTANCE_USER=$4
594 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600595 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500596 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 -0800597 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700598 fi
599}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500600
Vincent Untz856a11e2012-11-21 16:04:12 +0100601
Vincent Untz856a11e2012-11-21 16:04:12 +0100602# Get the location of the $module-rootwrap executables, where module is cinder
603# or nova.
604# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100605function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100606 local module=$1
607
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100608 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100609}
610
Dean Troyer1a6d4492013-06-03 16:47:36 -0500611
Ian Wienand0488edd2013-04-11 12:04:36 +1000612# Path permissions sanity check
613# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100614function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000615 # Ensure no element of the path has 0700 permissions, which is very
616 # likely to cause issues for daemons. Inspired by default 0700
617 # homedir permissions on RHEL and common practice of making DEST in
618 # the stack user's homedir.
619
Ian Wienandada886d2015-10-07 14:06:26 +1100620 local real_path
621 real_path=$(readlink -f $1)
Ian Wienand0488edd2013-04-11 12:04:36 +1000622 local rebuilt_path=""
623 for i in $(echo ${real_path} | tr "/" " "); do
624 rebuilt_path=$rebuilt_path"/"$i
625
626 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
627 echo "*** DEST path element"
628 echo "*** ${rebuilt_path}"
629 echo "*** appears to have 0700 permissions."
Dean Troyerdc97cb72015-03-28 08:20:50 -0500630 echo "*** This is very likely to cause fatal issues for DevStack daemons."
Ian Wienand0488edd2013-04-11 12:04:36 +1000631
632 if [[ -n "$SKIP_PATH_SANITY" ]]; then
633 return
634 else
635 echo "*** Set SKIP_PATH_SANITY to skip this check"
636 die $LINENO "Invalid path permissions"
637 fi
638 fi
639 done
640}
641
Dean Troyer1a6d4492013-06-03 16:47:36 -0500642
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100643# vercmp ver1 op ver2
644# Compare VER1 to VER2
645# - op is one of < <= == >= >
646# - returns true if satisified
647# e.g.
648# if vercmp 1.0 "<" 2.0; then
649# ...
650# fi
651function vercmp {
652 local v1=$1
653 local op=$2
654 local v2=$3
655 local result
656
657 # sort the two numbers with sort's "-V" argument. Based on if v2
658 # swapped places with v1, we can determine ordering.
659 result=$(echo -e "$v1\n$v2" | sort -V | head -1)
660
661 case $op in
662 "==")
663 [ "$v1" = "$v2" ]
664 return
665 ;;
666 ">")
667 [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
668 return
669 ;;
670 "<")
671 [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
672 return
673 ;;
674 ">=")
675 [ "$result" = "$v2" ]
676 return
677 ;;
678 "<=")
679 [ "$result" = "$v1" ]
680 return
681 ;;
682 *)
683 die $LINENO "unrecognised op: $op"
684 ;;
685 esac
686}
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000687
Sean Dague9751be62016-04-05 12:08:57 -0400688# This sets up defaults we like in devstack for logging for tracking
689# down issues, and makes sure everything is done the same between
690# projects.
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000691# NOTE(jh): Historically this function switched between three different
692# functions: setup_systemd_logging, setup_colorized_logging and
693# setup_standard_logging_identity. Since we always run with systemd now,
694# this could be cleaned up, but the other functions may still be in use
695# by plugins. Since deprecations haven't worked in the past, we'll just
696# leave them in place.
Sean Dague9751be62016-04-05 12:08:57 -0400697function setup_logging {
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000698 setup_systemd_logging $1
Sean Dague9751be62016-04-05 12:08:57 -0400699}
700
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700701# This function sets log formatting options for colorizing log
702# output to stdout. It is meant to be called by lib modules.
Ian Wienandaee18c72014-02-21 15:35:08 +1100703function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700704 local conf_file=$1
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700705 # Add color to logging output
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000706 iniset $conf_file DEFAULT logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %(project_name)s %(user_name)s%(color)s] %(instance)s%(color)s%(message)s"
707 iniset $conf_file DEFAULT logging_default_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
708 iniset $conf_file DEFAULT logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
709 iniset $conf_file DEFAULT logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700710}
711
Sean Dague5edae542017-03-21 20:50:24 -0400712function setup_systemd_logging {
713 local conf_file=$1
Sean Dagueb2bfe562017-05-03 09:58:21 -0400714 # NOTE(sdague): this is a nice to have, and means we're using the
715 # native systemd path, which provides for things like search on
716 # request-id. However, there may be an eventlet interaction here,
717 # so going off for now.
Kirill Zaitsevc0644f32017-05-24 13:00:47 +0300718 USE_JOURNAL=$(trueorfalse False USE_JOURNAL)
Eric Fried8cd310d2017-05-16 13:52:03 -0500719 local pidstr=""
Sean Dagueb2bfe562017-05-03 09:58:21 -0400720 if [[ "$USE_JOURNAL" == "True" ]]; then
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000721 iniset $conf_file DEFAULT use_journal "True"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400722 # if we are using the journal directly, our process id is already correct
Sean Dagueb2bfe562017-05-03 09:58:21 -0400723 else
Eric Fried8cd310d2017-05-16 13:52:03 -0500724 pidstr="(pid=%(process)d) "
Sean Dagueb2bfe562017-05-03 09:58:21 -0400725 fi
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000726 iniset $conf_file DEFAULT logging_debug_format_suffix "{{${pidstr}%(funcName)s %(pathname)s:%(lineno)d}}"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400727
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000728 iniset $conf_file DEFAULT 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"
729 iniset $conf_file DEFAULT logging_default_format_string "%(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
730 iniset $conf_file DEFAULT logging_exception_prefix "ERROR %(name)s %(instance)s"
Sean Dague5edae542017-03-21 20:50:24 -0400731}
732
Sean Dague9751be62016-04-05 12:08:57 -0400733function setup_standard_logging_identity {
734 local conf_file=$1
735 iniset $conf_file DEFAULT logging_user_identity_format "%(project_name)s %(user_name)s"
736}
737
Ian Wienand54e39102014-06-03 16:05:12 +1000738# These functions are provided for basic fall-back functionality for
Dean Troyerdc97cb72015-03-28 08:20:50 -0500739# projects that include parts of DevStack (Grenade). stack.sh will
740# override these with more specific versions for DevStack (with fancy
Ian Wienand54e39102014-06-03 16:05:12 +1000741# spinners, etc). We never override an existing version
742if ! function_exists echo_summary; then
743 function echo_summary {
744 echo $@
745 }
746fi
747if ! function_exists echo_nolog; then
748 function echo_nolog {
749 echo $@
750 }
751fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600752
Sébastien Han36f2f022014-01-06 18:09:26 +0100753
754# create_disk - Create backing disk
755function create_disk {
756 local node_number
757 local disk_image=${1}
758 local storage_data_dir=${2}
759 local loopback_disk_size=${3}
760
761 # Create a loopback disk and format it to XFS.
762 if [[ -e ${disk_image} ]]; then
763 if egrep -q ${storage_data_dir} /proc/mounts; then
liumk38a23d92017-12-13 15:09:02 +0800764 sudo umount ${storage_data_dir}
Sébastien Han36f2f022014-01-06 18:09:26 +0100765 sudo rm -f ${disk_image}
766 fi
767 fi
768
769 sudo mkdir -p ${storage_data_dir}/drives/images
770
771 sudo truncate -s ${loopback_disk_size} ${disk_image}
772
773 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
774 # a single inode. Keeping the default inode size (256) will result in multiple
775 # inodes being used to store xattr. Retrieving the xattr will be slower
776 # since we have to read multiple inodes. This statement is true for both
777 # Swift and Ceph.
778 sudo mkfs.xfs -f -i size=1024 ${disk_image}
779
780 # Mount the disk with mount options to make it as efficient as possible
781 if ! egrep -q ${storage_data_dir} /proc/mounts; then
Lee Yarwood5d7d8912018-12-03 14:21:06 +0000782 sudo mount -t xfs -o loop,noatime,nodiratime,logbufs=8 \
Sébastien Han36f2f022014-01-06 18:09:26 +0100783 ${disk_image} ${storage_data_dir}
784 fi
785}
786
Ihar Hrachyshka7b5c7dc2016-07-15 20:17:13 +0200787
788# set_mtu - Set MTU on a device
789function set_mtu {
790 local dev=$1
791 local mtu=$2
792 sudo ip link set mtu $mtu dev $dev
793}
794
795
Denis Buliga0bf75a42017-02-06 16:56:46 +0200796# running_in_container - Returns true otherwise false
797function running_in_container {
kesperd18d7c82017-03-23 05:52:33 +0000798 [[ $(systemd-detect-virt --container) != 'none' ]]
Denis Buliga0bf75a42017-02-06 16:56:46 +0200799}
800
801
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000802# enable_kernel_bridge_firewall - Enable kernel support for bridge firewalling
803function enable_kernel_bridge_firewall {
804 # Load bridge module. This module provides access to firewall for bridged
805 # frames; and also on older kernels (pre-3.18) it provides sysctl knobs to
806 # enable/disable bridge firewalling
807 sudo modprobe bridge
808 # For newer kernels (3.18+), those sysctl settings are split into a separate
809 # kernel module (br_netfilter). Load it too, if present.
810 sudo modprobe br_netfilter 2>> /dev/null || :
811 # Enable bridge firewalling in case it's disabled in kernel (upstream
812 # default is enabled, but some distributions may decide to change it).
813 # This is at least needed for RHEL 7.2 and earlier releases.
Ihar Hrachyshka3f771b72016-12-17 04:12:24 +0000814 for proto in ip ip6; do
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000815 sudo sysctl -w net.bridge.bridge-nf-call-${proto}tables=1
816 done
817}
818
819
Dan Smith1f55d382017-05-16 08:50:53 -0700820# Set a systemd system override
821#
822# This sets a system-side override in system.conf. A per-service
823# override would be /etc/systemd/system/${service}.service/override.conf
824function set_systemd_override {
825 local key="$1"
826 local value="$2"
827
828 local sysconf="/etc/systemd/system.conf"
829 iniset -sudo "${sysconf}" "Manager" "$key" "$value"
830 echo "Set systemd system override for ${key}=${value}"
831
832 sudo systemctl daemon-reload
833}
834
Matthew Treinish309b99e2017-05-23 15:18:31 -0400835# Get a random port from the local port range
836#
837# This function returns an available port in the local port range. The search
838# order is not truly random, but should be considered a random value by the
839# user because it depends on the state of your local system.
840function get_random_port {
841 read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
842 while true; do
843 for (( port = upper_port ; port >= lower_port ; port-- )); do
844 sudo lsof -i ":$port" &> /dev/null
845 if [[ $? > 0 ]] ; then
846 break 2
847 fi
848 done
849 done
850 echo $port
851}
852
Ian Wienand07cbc442017-06-30 12:29:19 +1000853# Save some state information
854#
855# Write out various useful state information to /etc/devstack-version
Sean Dague2c0faca2017-06-28 09:13:04 -0400856function write_devstack_version {
Dirk Mueller6bab8322018-03-02 21:13:12 +0100857 cat - <<EOF | sudo tee /etc/devstack-version >/dev/null
Ian Wienand07cbc442017-06-30 12:29:19 +1000858DevStack Version: ${DEVSTACK_SERIES}
859Change: $(git log --format="%H %s %ci" -1)
860OS Version: ${os_VENDOR} ${os_RELEASE} ${os_CODENAME}
Sean Dague2c0faca2017-06-28 09:13:04 -0400861EOF
Sean Dague2c0faca2017-06-28 09:13:04 -0400862}
863
Dean Troyer27e32692012-03-16 16:16:56 -0500864# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100865$_XTRACE_FUNCTIONS
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500866
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500867# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400868# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400869# End: