blob: 42d08d7c4a4c32a5a937bb8fce9d05232a9c1a97 [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
Dan Smith30d9bf92021-01-19 12:10:52 -080024source ${FUNC_DIR}/inc/async
Dean Troyer7f9aa712012-01-31 12:11:56 -060025
Dean Troyer27e32692012-03-16 16:16:56 -050026# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110027_XTRACE_FUNCTIONS=$(set +o | grep xtrace)
Dean Troyer27e32692012-03-16 16:16:56 -050028set +o xtrace
29
Ian Wienand54e39102014-06-03 16:05:12 +100030# Check if a function already exists
31function function_exists {
32 declare -f -F $1 > /dev/null
33}
Dean Troyer7f9aa712012-01-31 12:11:56 -060034
Sean Dague646085d2016-03-21 17:00:51 -040035# short_source prints out the current location of the caller in a way
36# that strips redundant directories. This is useful for PS4 usage.
37function short_source {
38 saveIFS=$IFS
39 IFS=" "
40 called=($(caller 0))
41 IFS=$saveIFS
42 file=${called[2]}
43 file=${file#$RC_DIR/}
44 printf "%-40s " "$file:${called[1]}:${called[0]}"
45}
John L. Villalovosdaa7a412016-05-05 12:50:52 -070046# PS4 is exported to child shells and uses the 'short_source' function, so
47# export it so child shells have access to the 'short_source' function also.
48export -f short_source
Sean Dague646085d2016-03-21 17:00:51 -040049
Monty Taylord8bb2202017-09-03 12:13:59 -050050# Download a file from a URL
51#
52# Will check cache (in $FILES) or download given URL.
53#
54# Argument is the URL to the remote file
55#
56# Will echo the local path to the file as the output. Will die on
57# failure to download.
58#
59# Files can be pre-cached for CI environments, see EXTRA_CACHE_URLS
60# and tools/image_list.sh
61function get_extra_file {
62 local file_url=$1
63
64 file_name=$(basename "$file_url")
65 if [[ $file_url != file* ]]; then
66 # If the file isn't cache, download it
67 if [[ ! -f $FILES/$file_name ]]; then
yuanke weic652a492017-09-17 22:18:07 +080068 wget --progress=dot:giga -t 2 -c $file_url -O $FILES/$file_name
Monty Taylord8bb2202017-09-03 12:13:59 -050069 if [[ $? -ne 0 ]]; then
70 die "$file_url could not be downloaded"
71 fi
72 fi
73 echo "$FILES/$file_name"
74 return
75 else
76 # just strip the file:// bit and that's the path to the file
77 echo $file_url | sed 's/$file:\/\///g'
78 fi
79}
80
Dan Smith2614c1b2020-07-15 14:41:38 -070081# Generate image property arguments for OSC
82#
83# Arguments: properties, one per, like propname=value
84#
85# Result is --property propname1=value1 --property propname2=value2
86function _image_properties_to_arg {
87 local result=""
88 for property in $*; do
89 result+=" --property $property"
90 done
91 echo $result
92}
93
Abhishek Kekane73ad9762020-06-16 15:20:48 +000094# Upload an image to glance using the configured mechanism
95#
96# Arguments:
97# image name
98# container format
99# disk format
100# path to image file
101# optional properties (format of propname=value)
102#
103function _upload_image {
104 local image_name="$1"
105 shift
106 local container="$1"
107 shift
108 local disk="$1"
109 shift
110 local image="$1"
111 shift
112 local properties
113 local useimport
114
Dan Smith2614c1b2020-07-15 14:41:38 -0700115 properties=$(_image_properties_to_arg $*)
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000116
117 if [[ "$GLANCE_USE_IMPORT_WORKFLOW" == "True" ]]; then
Dan Smith33f8f6e2020-07-21 19:41:48 -0700118 useimport="--import"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000119 fi
120
Dan Smith769adbd2024-05-08 16:58:46 +0000121 openstack --os-cloud=devstack-admin --os-region-name="$REGION_NAME" image create "$image_name" --public --container-format "$container" --disk-format "$disk" $useimport $properties --file $(readlink -f "${image}")
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000122}
Sean Dague646085d2016-03-21 17:00:51 -0400123
Adam Spierscb961592013-10-05 12:11:07 +0100124# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -0500125# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +0100126#
127# - ``FILES`` must be set to the cache dir
128# - ``GLANCE_HOSTPORT``
129#
Peter Stachowski5aeea6a2015-09-22 19:38:02 +0000130# upload_image image-url
Ian Wienandaee18c72014-02-21 15:35:08 +1100131function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -0500132 local image_url=$1
Dean Troyerca0e3d02012-04-13 15:58:37 -0500133
Dean Troyere9f76672014-07-25 11:09:36 -0500134 local image image_fname image_name
135
Yadnesh Kulkarni6b0f0552023-11-23 11:59:49 +0530136 local max_attempts=5
137
Dean Troyerca0e3d02012-04-13 15:58:37 -0500138 # Create a directory for the downloaded image tarballs.
139 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -0500140 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -0800141 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800142 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -0500143 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Yadnesh Kulkarni6b0f0552023-11-23 11:59:49 +0530144 for attempt in `seq $max_attempts`; do
145 local rc=0
146 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname || rc=$?
147 if [[ $rc -ne 0 ]]; then
148 if [[ "$attempt" -eq "$max_attempts" ]]; then
149 echo "Not found: $image_url"
150 return
151 fi
152 echo "Download failed, retrying in $attempt second, attempt: $attempt"
153 sleep $attempt
154 else
155 break
156 fi
157 done
Arnaud Legendre3e439442013-11-15 16:06:03 -0800158 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500159 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -0800160 else
Dean Troyer3324f192014-09-18 09:26:39 -0500161 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -0800162 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -0500163 # unix: ``file:///home/user/path/file``
164 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -0500165 image=$(echo $image_url | sed "s/^file:\/\///g")
166 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500167 echo "Not found: $image_url"
168 return
169 fi
170 fi
171
172 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
173 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500174 image_name="${image_fname%.tar.gz}"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000175 _upload_image "$image_name" ami ami "$image"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500176 return
177 fi
178
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700179 # vmdk format images
180 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500181 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700182
183 # Before we can upload vmdk type images to glance, we need to know it's
184 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800185 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700186 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700187 # vmdk filename, which is expected in the following format:
188 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800189 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700190 #
191 # If the filename does not follow the above format then the vsphere
192 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700193
Dean Troyere9f76672014-07-25 11:09:36 -0500194 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -0800195 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -0500196 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800197
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700198 # vmdk adapter type
Ian Wienand7ae97292016-02-16 14:50:53 +1100199 local vmdk_adapter_type
200 vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700201 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
202 vmdk_adapter_type="${vmdk_adapter_type%?}"
203
204 # vmdk disk type
Ian Wienand7ae97292016-02-16 14:50:53 +1100205 local vmdk_create_type
206 vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700207 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800208 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800209
210 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900211 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700212 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
213 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500214 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500215 # Attempt to retrieve the ``*-flat.vmdk``
Ian Wienand7ae97292016-02-16 14:50:53 +1100216 local flat_fname
217 flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800218 flat_fname="${flat_fname#*\"}"
219 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700220 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500221 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800222 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500223 path_len=`expr ${#image_url} - ${#image_fname}`
224 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800225 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900226 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800227 if [[ $flat_url != file* ]]; then
228 if [[ ! -f $FILES/$flat_fname || \
229 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100230 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800231 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500232 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800233 else
Dean Troyere9f76672014-07-25 11:09:36 -0500234 image=$(echo $flat_url | sed "s/^file:\/\///g")
235 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800236 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700237 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800238 fi
239 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500240 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800241 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800242 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
243 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800244 elif [[ -z "$vmdk_create_type" ]]; then
245 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
246 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500247 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800248 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500249 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700250 else
Dean Troyere9f76672014-07-25 11:09:36 -0500251 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
252 path_len=`expr ${#image_url} - ${#image_fname}`
253 local flat_path="${image_url:0:$path_len}"
254 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700255 warn $LINENO "$descriptor_data_pair_msg"`
256 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
257 if [[ $flat_path != file* ]]; then
258 if [[ ! -f $FILES/$descriptor_fname || \
259 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
260 wget -c $descriptor_url -O $FILES/$descriptor_fname
261 fi
262 descriptor_url="$FILES/$descriptor_fname"
263 else
264 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
265 if [[ ! -f $descriptor_url || \
266 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
267 echo "Descriptor not found: $descriptor_url"
268 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800269 fi
270 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800271 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
272 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
273 vmdk_adapter_type="${vmdk_adapter_type%?}"
274 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900275 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700276 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700277 vmdk_disktype="preallocated"
278 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800279
280 # NOTE: For backwards compatibility reasons, colons may be used in place
281 # of semi-colons for property delimiters but they are not permitted
282 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500283 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800284 IFS=':;' read -a props <<< "$property_string"
285 vmdk_disktype="${props[0]:-$vmdk_disktype}"
286 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
287 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700288
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000289 _upload_image "$image_name" bare vmdk "$image" vmware_disktype="$vmdk_disktype" vmware_adaptertype="$vmdk_adapter_type" hw_vif_model="$vmdk_net_adapter"
290
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700291 return
292 fi
293
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300294 if [[ "$image_url" =~ '.hds' ]]; then
295 image_name="${image_fname%.hds}"
296 vm_mode=${image_name##*-}
297 if [[ $vm_mode != 'exe' && $vm_mode != 'hvm' ]]; then
298 die $LINENO "Unknown vm_mode=${vm_mode} for Virtuozzo image"
299 fi
300
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000301 _upload_image "$image_name" bare ploop "$image" vm_mode=$vm_mode
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300302 return
303 fi
304
Dean Troyere9f76672014-07-25 11:09:36 -0500305 local kernel=""
306 local ramdisk=""
307 local disk_format=""
308 local container_format=""
309 local unpack=""
Dan Smith49ad4852020-07-15 14:54:22 -0700310 local img_property=""
311
312 # NOTE(danms): If we're on libvirt/qemu or libvirt/kvm, set the hw_rng_model
313 # to libvirt in the image properties.
314 if [[ "$VIRT_DRIVER" == "libvirt" ]]; then
315 if [[ "$LIBVIRT_TYPE" == "qemu" || "$LIBVIRT_TYPE" == "kvm" ]]; then
316 img_property="hw_rng_model=virtio"
317 fi
318 fi
319
Dean Troyere9f76672014-07-25 11:09:36 -0500320 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500321 *.tar.gz|*.tgz)
322 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500323 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
324 image_name="${image_fname%.tar.gz}" ||
325 image_name="${image_fname%.tgz}"
326 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500327 rm -Rf "$xdir";
328 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500329 tar -zxf $image -C "$xdir"
330 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400331 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500332 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400333 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500334 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400335 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500336 if [[ -z "$image_name" ]]; then
337 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500338 fi
339 ;;
340 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500341 image_name=$(basename "$image" ".img")
Ian Wienandada886d2015-10-07 14:06:26 +1100342 local format
343 format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500344 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500345 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500346 else
Dean Troyere9f76672014-07-25 11:09:36 -0500347 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500348 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500349 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500350 ;;
351 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500352 image_name=$(basename "$image" ".img.gz")
353 disk_format=raw
354 container_format=bare
355 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500356 ;;
Hongbin Lu3feceb02016-04-17 11:11:58 -0400357 *.img.bz2)
358 image_name=$(basename "$image" ".img.bz2")
359 disk_format=qcow2
360 container_format=bare
361 unpack=bunzip2
362 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500363 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500364 image_name=$(basename "$image" ".qcow2")
365 disk_format=qcow2
366 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500367 ;;
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000368 *.qcow2.xz)
369 image_name=$(basename "$image" ".qcow2.xz")
370 disk_format=qcow2
371 container_format=bare
372 unpack=unxz
373 ;;
Angel Noamf24e2992017-05-11 15:13:29 +0300374 *.raw)
375 image_name=$(basename "$image" ".raw")
376 disk_format=raw
377 container_format=bare
378 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100379 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500380 image_name=$(basename "$image" ".iso")
381 disk_format=iso
382 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100383 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300384 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
385 local extension="${image_fname#*.}"
386 image_name=$(basename "$image" ".$extension")
Lucian Petrut2715fd02017-05-24 11:31:56 +0300387 disk_format=$(echo $image_fname | grep -oP '(?<=\.)vhdx?(?=\.|$)')
Alessandro Pilottica823942014-08-07 02:05:26 +0300388 container_format=bare
389 if [ "${image_fname##*.}" == "gz" ]; then
390 unpack=zcat
391 fi
392 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500393 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500394 esac
395
Rafael Folco72f530f2016-02-09 07:08:38 -0600396 if is_arch "ppc64le" || is_arch "ppc64" || is_arch "ppc"; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000397 img_property="$img_property hw_cdrom_bus=scsi os_command_line=console=hvc0"
Rafael Folcoab775872013-12-02 14:04:32 -0200398 fi
399
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000400 if is_arch "aarch64"; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000401 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 +0000402 fi
403
Dean Troyere9f76672014-07-25 11:09:36 -0500404 if [ "$container_format" = "bare" ]; then
405 if [ "$unpack" = "zcat" ]; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000406 _upload_image "$image_name" $container_format $disk_format <(zcat --force "$image") $img_property
Hongbin Lu3feceb02016-04-17 11:11:58 -0400407 elif [ "$unpack" = "bunzip2" ]; then
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000408 _upload_image "$image_name" $container_format $disk_format <(bunzip2 -cdk "$image") $img_property
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000409 elif [ "$unpack" = "unxz" ]; then
410 # NOTE(brtknr): unxz the file first and cleanup afterwards to
411 # prevent timeout while Glance tries to upload image (e.g. to Swift).
412 local tmp_dir
413 local image_path
414 tmp_dir=$(mktemp -d)
415 image_path="$tmp_dir/$image_name"
416 unxz -cv "${image}" > "$image_path"
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000417 _upload_image "$image_name" $container_format $disk_format "$image_path" $img_property
Bharat Kunwarf70cb702020-04-20 09:53:25 +0000418 rm -rf $tmp_dir
Dean Troyerca0e3d02012-04-13 15:58:37 -0500419 else
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000420 _upload_image "$image_name" $container_format $disk_format "$image" $img_property
Dean Troyerca0e3d02012-04-13 15:58:37 -0500421 fi
422 else
423 # Use glance client to add the kernel the root filesystem.
424 # We parse the results of the first upload to get the glance ID of the
425 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500426 local kernel_id="" ramdisk_id="";
427 if [ -n "$kernel" ]; then
Dan Smith769adbd2024-05-08 16:58:46 +0000428 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 --file $(readlink -f "$kernel") -f value -c id)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500429 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500430 if [ -n "$ramdisk" ]; then
Dan Smith769adbd2024-05-08 16:58:46 +0000431 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 --file $(readlink -f "$ramdisk") -f value -c id)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500432 fi
Abhishek Kekane73ad9762020-06-16 15:20:48 +0000433 _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 -0500434 fi
435}
436
Dean Troyer1a6d4492013-06-03 16:47:36 -0500437
Dean Troyerc1b486a2012-11-05 14:26:09 -0600438# Set the database backend to use
439# When called from stackrc/localrc DATABASE_BACKENDS has not been
440# initialized yet, just save the configuration selection and call back later
441# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100442#
Matt Riedemannb14665f2019-10-17 19:34:05 +0000443# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600444function use_database {
445 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600446 # No backends registered means this is likely called from ``localrc``
447 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600448 DATABASE_TYPE=$1
Sean Dague72ad9422015-10-07 11:51:40 -0400449 deprecated "The database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100450 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600451 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100452 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600453 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600454}
455
sridhargaddamb5ab6462015-02-24 07:23:24 +0000456#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
457CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500458
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600459# Wait for an HTTP server to start answering requests
460# wait_for_service timeout url
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400461#
462# If the service we want is behind a proxy, the proxy may be available
463# before the service. Compliant proxies will return a 503 in this case
464# Loop until we get something else.
465# Also check for the case where there is no proxy and the service just
466# hasn't started yet. curl returns 7 for Failed to connect to host.
Ian Wienandaee18c72014-02-21 15:35:08 +1100467function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600468 local timeout=$1
469 local url=$2
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400470 local rval=0
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900471 time_start "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400472 timeout $timeout bash -x <<EOF || rval=$?
473 while [[ \$( ${CURL_GET} -k --noproxy '*' -s -o /dev/null -w '%{http_code}' ${url} ) == 503 || \$? -eq 7 ]]; do
474 sleep 1
475 done
476EOF
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900477 time_stop "wait_for_service"
Rob Crittenden21e3d1e2016-05-06 12:35:22 -0400478 return $rval
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600479}
480
Sean Daguec2fe9162017-07-28 11:29:18 +0000481function wait_for_compute {
482 local timeout=$1
483 local rval=0
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530484 local compute_hostname
Sean Daguec2fe9162017-07-28 11:29:18 +0000485 time_start "wait_for_service"
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530486 compute_hostname=$(iniget $NOVA_CONF DEFAULT host)
487 if [[ -z $compute_hostname ]]; then
488 compute_hostname=$(hostname)
489 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000490 timeout $timeout bash -x <<EOF || rval=$?
491 ID=""
492 while [[ "\$ID" == "" ]]; do
493 sleep 1
Chris Dentac475bb2018-02-07 18:35:40 +0000494 if [[ "$VIRT_DRIVER" = 'fake' ]]; then
495 # When using the fake driver the compute hostnames have a suffix of 1 to NUMBER_FAKE_NOVA_COMPUTE
496 ID=\$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list --host `hostname`1 --service nova-compute -c ID -f value)
497 else
Prabhat Ranjan6f38cf42018-03-16 16:33:46 +0530498 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 +0000499 fi
Sean Daguec2fe9162017-07-28 11:29:18 +0000500 done
501EOF
502 time_stop "wait_for_service"
503 # Figure out what's happening on platforms where this doesn't work
504 if [[ "$rval" != 0 ]]; then
505 echo "Didn't find service registered by hostname after $timeout seconds"
506 openstack --os-cloud devstack-admin --os-region "$REGION_NAME" compute service list
507 fi
508 return $rval
509}
510
Dean Troyer1a6d4492013-06-03 16:47:36 -0500511
Nachi Uenofda946e2012-10-24 17:26:02 -0700512# ping check
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100513# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``PRIVATE_NETWORK``
Sean Dagueaf9bf862015-04-16 08:58:32 -0400514# ping_check <ip> [boot-timeout] [from_net] [expected]
Ian Wienandaee18c72014-02-21 15:35:08 +1100515function ping_check {
Sean Dagueaf9bf862015-04-16 08:58:32 -0400516 local ip=$1
517 local timeout=${2:-30}
518 local from_net=${3:-""}
519 local expected=${4:-True}
520 local op="!"
521 local failmsg="[Fail] Couldn't ping server"
522 local ping_cmd="ping"
Nachi Uenofda946e2012-10-24 17:26:02 -0700523
Sean Dagueaf9bf862015-04-16 08:58:32 -0400524 # if we don't specify a from_net we're expecting things to work
525 # fine from our local box.
526 if [[ -n "$from_net" ]]; then
Stephen Finucane4b8cba72019-05-21 14:17:11 +0100527 # TODO(stephenfin): Is there any way neutron could be disabled now?
Sean Dagueaf9bf862015-04-16 08:58:32 -0400528 if is_service_enabled neutron; then
529 ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700530 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700531 fi
Sean Dagueaf9bf862015-04-16 08:58:32 -0400532
533 # inverse the logic if we're testing no connectivity
534 if [[ "$expected" != "True" ]]; then
535 op=""
536 failmsg="[Fail] Could ping server"
537 fi
538
539 # Because we've transformed this command so many times, print it
540 # out at the end.
541 local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
542 echo "Checking connectivity with $check_command"
543
544 if ! timeout $timeout sh -c "$check_command"; then
545 die $LINENO $failmsg
546 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700547}
548
Nachi Ueno6769b162013-08-12 18:18:56 -0700549# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100550function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700551 local vm_id=$1
552 local network_name=$2
Ryota MIBU842d54a2017-12-25 16:28:50 +0900553 local addresses
Ian Wienandada886d2015-10-07 14:06:26 +1100554 local ip
Ian Wienand7ae97292016-02-16 14:50:53 +1100555
Ryota MIBU842d54a2017-12-25 16:28:50 +0900556 addresses=$(openstack server show -c addresses -f value "$vm_id")
557 ip=$(echo $addresses | sed -n "s/^.*$network_name=\([0-9\.]*\).*$/\1/p")
Nachi Ueno6769b162013-08-12 18:18:56 -0700558 if [[ $ip = "" ]];then
Ryota MIBU842d54a2017-12-25 16:28:50 +0900559 echo "addresses of server $vm_id : $addresses"
Atsushi SAKAI33c9a672015-11-12 19:50:00 +0900560 die $LINENO "[Fail] Couldn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700561 fi
562 echo $ip
563}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500564
Nachi Uenofda946e2012-10-24 17:26:02 -0700565# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700566
Dean Troyer1a6d4492013-06-03 16:47:36 -0500567# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100568function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400569 if is_service_enabled neutron; then
570 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700571 return
572 fi
573 _ssh_check_novanet "$1" $2 $3 $4 $5
574}
575
Ian Wienandaee18c72014-02-21 15:35:08 +1100576function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700577 local NET_NAME=$1
578 local KEY_FILE=$2
579 local FLOATING_IP=$3
580 local DEFAULT_INSTANCE_USER=$4
581 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600582 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500583 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 -0800584 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700585 fi
586}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500587
Vincent Untz856a11e2012-11-21 16:04:12 +0100588
Vincent Untz856a11e2012-11-21 16:04:12 +0100589# Get the location of the $module-rootwrap executables, where module is cinder
590# or nova.
591# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100592function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100593 local module=$1
594
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100595 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100596}
597
Dean Troyer1a6d4492013-06-03 16:47:36 -0500598
Ian Wienand0488edd2013-04-11 12:04:36 +1000599# Path permissions sanity check
600# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100601function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000602 # Ensure no element of the path has 0700 permissions, which is very
603 # likely to cause issues for daemons. Inspired by default 0700
604 # homedir permissions on RHEL and common practice of making DEST in
605 # the stack user's homedir.
606
Ian Wienandada886d2015-10-07 14:06:26 +1100607 local real_path
608 real_path=$(readlink -f $1)
Ian Wienand0488edd2013-04-11 12:04:36 +1000609 local rebuilt_path=""
610 for i in $(echo ${real_path} | tr "/" " "); do
611 rebuilt_path=$rebuilt_path"/"$i
612
613 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
614 echo "*** DEST path element"
615 echo "*** ${rebuilt_path}"
616 echo "*** appears to have 0700 permissions."
Dean Troyerdc97cb72015-03-28 08:20:50 -0500617 echo "*** This is very likely to cause fatal issues for DevStack daemons."
Ian Wienand0488edd2013-04-11 12:04:36 +1000618
619 if [[ -n "$SKIP_PATH_SANITY" ]]; then
620 return
621 else
622 echo "*** Set SKIP_PATH_SANITY to skip this check"
623 die $LINENO "Invalid path permissions"
624 fi
625 fi
626 done
627}
628
Dean Troyer1a6d4492013-06-03 16:47:36 -0500629
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100630# vercmp ver1 op ver2
631# Compare VER1 to VER2
632# - op is one of < <= == >= >
633# - returns true if satisified
634# e.g.
635# if vercmp 1.0 "<" 2.0; then
636# ...
637# fi
638function vercmp {
639 local v1=$1
640 local op=$2
641 local v2=$3
642 local result
643
644 # sort the two numbers with sort's "-V" argument. Based on if v2
645 # swapped places with v1, we can determine ordering.
646 result=$(echo -e "$v1\n$v2" | sort -V | head -1)
647
648 case $op in
649 "==")
650 [ "$v1" = "$v2" ]
651 return
652 ;;
653 ">")
654 [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
655 return
656 ;;
657 "<")
658 [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
659 return
660 ;;
661 ">=")
662 [ "$result" = "$v2" ]
663 return
664 ;;
665 "<=")
666 [ "$result" = "$v1" ]
667 return
668 ;;
669 *)
670 die $LINENO "unrecognised op: $op"
671 ;;
672 esac
673}
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000674
Sean Dague9751be62016-04-05 12:08:57 -0400675# This sets up defaults we like in devstack for logging for tracking
676# down issues, and makes sure everything is done the same between
677# projects.
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000678# NOTE(jh): Historically this function switched between three different
679# functions: setup_systemd_logging, setup_colorized_logging and
680# setup_standard_logging_identity. Since we always run with systemd now,
681# this could be cleaned up, but the other functions may still be in use
682# by plugins. Since deprecations haven't worked in the past, we'll just
683# leave them in place.
Sean Dague9751be62016-04-05 12:08:57 -0400684function setup_logging {
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000685 setup_systemd_logging $1
Sean Dague9751be62016-04-05 12:08:57 -0400686}
687
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700688# This function sets log formatting options for colorizing log
689# output to stdout. It is meant to be called by lib modules.
Ian Wienandaee18c72014-02-21 15:35:08 +1100690function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700691 local conf_file=$1
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700692 # Add color to logging output
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000693 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"
694 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"
695 iniset $conf_file DEFAULT logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
696 iniset $conf_file DEFAULT logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
melanie witt38dea332024-08-01 23:41:43 +0000697 # Enable or disable color for oslo.log
698 iniset $conf_file DEFAULT log_color $LOG_COLOR
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700699}
700
Sean Dague5edae542017-03-21 20:50:24 -0400701function setup_systemd_logging {
702 local conf_file=$1
Sean Dagueb2bfe562017-05-03 09:58:21 -0400703 # NOTE(sdague): this is a nice to have, and means we're using the
704 # native systemd path, which provides for things like search on
705 # request-id. However, there may be an eventlet interaction here,
706 # so going off for now.
Kirill Zaitsevc0644f32017-05-24 13:00:47 +0300707 USE_JOURNAL=$(trueorfalse False USE_JOURNAL)
Eric Fried8cd310d2017-05-16 13:52:03 -0500708 local pidstr=""
Sean Dagueb2bfe562017-05-03 09:58:21 -0400709 if [[ "$USE_JOURNAL" == "True" ]]; then
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000710 iniset $conf_file DEFAULT use_journal "True"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400711 # if we are using the journal directly, our process id is already correct
Sean Dagueb2bfe562017-05-03 09:58:21 -0400712 else
Eric Fried8cd310d2017-05-16 13:52:03 -0500713 pidstr="(pid=%(process)d) "
Sean Dagueb2bfe562017-05-03 09:58:21 -0400714 fi
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000715 iniset $conf_file DEFAULT logging_debug_format_suffix "{{${pidstr}%(funcName)s %(pathname)s:%(lineno)d}}"
Sean Dagueb2bfe562017-05-03 09:58:21 -0400716
Dr. Jens Harbott6808a342020-01-20 15:52:33 +0000717 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"
718 iniset $conf_file DEFAULT logging_default_format_string "%(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
719 iniset $conf_file DEFAULT logging_exception_prefix "ERROR %(name)s %(instance)s"
melanie witt38dea332024-08-01 23:41:43 +0000720
721 # Enable or disable color for oslo.log
722 iniset $conf_file DEFAULT log_color $LOG_COLOR
Sean Dague5edae542017-03-21 20:50:24 -0400723}
724
Sean Dague9751be62016-04-05 12:08:57 -0400725function setup_standard_logging_identity {
726 local conf_file=$1
727 iniset $conf_file DEFAULT logging_user_identity_format "%(project_name)s %(user_name)s"
728}
729
Ian Wienand54e39102014-06-03 16:05:12 +1000730# These functions are provided for basic fall-back functionality for
Dean Troyerdc97cb72015-03-28 08:20:50 -0500731# projects that include parts of DevStack (Grenade). stack.sh will
732# override these with more specific versions for DevStack (with fancy
Ian Wienand54e39102014-06-03 16:05:12 +1000733# spinners, etc). We never override an existing version
734if ! function_exists echo_summary; then
735 function echo_summary {
736 echo $@
737 }
738fi
739if ! function_exists echo_nolog; then
740 function echo_nolog {
741 echo $@
742 }
743fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600744
Sébastien Han36f2f022014-01-06 18:09:26 +0100745
Dan Smith6766f712020-07-24 15:44:34 -0700746# create_disk - Create, configure, and mount a backing disk
Sébastien Han36f2f022014-01-06 18:09:26 +0100747function create_disk {
748 local node_number
749 local disk_image=${1}
750 local storage_data_dir=${2}
751 local loopback_disk_size=${3}
Dan Smith6766f712020-07-24 15:44:34 -0700752 local key
Sébastien Han36f2f022014-01-06 18:09:26 +0100753
Dan Smith36a575b2020-11-13 06:57:33 -0800754 key=$(echo $disk_image | sed 's#/.##')
755 key="devstack-$key"
Sébastien Han36f2f022014-01-06 18:09:26 +0100756
Dan Smith36a575b2020-11-13 06:57:33 -0800757 destroy_disk $disk_image $storage_data_dir
Sébastien Han36f2f022014-01-06 18:09:26 +0100758
Dan Smith36a575b2020-11-13 06:57:33 -0800759 # Create an empty file of the correct size (and ensure the
760 # directory structure up to that path exists)
761 sudo mkdir -p $(dirname ${disk_image})
Sébastien Han36f2f022014-01-06 18:09:26 +0100762 sudo truncate -s ${loopback_disk_size} ${disk_image}
763
764 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
765 # a single inode. Keeping the default inode size (256) will result in multiple
766 # inodes being used to store xattr. Retrieving the xattr will be slower
767 # since we have to read multiple inodes. This statement is true for both
768 # Swift and Ceph.
769 sudo mkfs.xfs -f -i size=1024 ${disk_image}
770
Dan Smith36a575b2020-11-13 06:57:33 -0800771 # Install a new loopback fstab entry for this disk image, and mount it
Dan Smith6766f712020-07-24 15:44:34 -0700772 echo "$disk_image $storage_data_dir xfs loop,noatime,nodiratime,logbufs=8,comment=$key 0 0" | sudo tee -a /etc/fstab
Dan Smith36a575b2020-11-13 06:57:33 -0800773 sudo mkdir -p $storage_data_dir
Dan Smith6766f712020-07-24 15:44:34 -0700774 sudo mount -v $storage_data_dir
775}
776
777# Unmount, de-configure, and destroy a backing disk
778function destroy_disk {
779 local disk_image=$1
780 local storage_data_dir=$2
Dan Smith36a575b2020-11-13 06:57:33 -0800781 local key
782
783 key=$(echo $disk_image | sed 's#/.##')
784 key="devstack-$key"
Dan Smith6766f712020-07-24 15:44:34 -0700785
786 # Unmount the target, if mounted
787 if egrep -q $storage_data_dir /proc/mounts; then
788 sudo umount $storage_data_dir
789 fi
790
791 # Clear any fstab rules
Dan Smith36a575b2020-11-13 06:57:33 -0800792 sudo sed -i '/.*comment=$key.*/ d' /etc/fstab
Dan Smith6766f712020-07-24 15:44:34 -0700793
794 # Delete the file
Dan Smith36a575b2020-11-13 06:57:33 -0800795 sudo rm -f $disk_image
Sébastien Han36f2f022014-01-06 18:09:26 +0100796}
797
Ihar Hrachyshka7b5c7dc2016-07-15 20:17:13 +0200798
799# set_mtu - Set MTU on a device
800function set_mtu {
801 local dev=$1
802 local mtu=$2
803 sudo ip link set mtu $mtu dev $dev
804}
805
806
Denis Buliga0bf75a42017-02-06 16:56:46 +0200807# running_in_container - Returns true otherwise false
808function running_in_container {
kesperd18d7c82017-03-23 05:52:33 +0000809 [[ $(systemd-detect-virt --container) != 'none' ]]
Denis Buliga0bf75a42017-02-06 16:56:46 +0200810}
811
812
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000813# enable_kernel_bridge_firewall - Enable kernel support for bridge firewalling
814function enable_kernel_bridge_firewall {
815 # Load bridge module. This module provides access to firewall for bridged
816 # frames; and also on older kernels (pre-3.18) it provides sysctl knobs to
817 # enable/disable bridge firewalling
818 sudo modprobe bridge
819 # For newer kernels (3.18+), those sysctl settings are split into a separate
820 # kernel module (br_netfilter). Load it too, if present.
821 sudo modprobe br_netfilter 2>> /dev/null || :
822 # Enable bridge firewalling in case it's disabled in kernel (upstream
823 # default is enabled, but some distributions may decide to change it).
824 # This is at least needed for RHEL 7.2 and earlier releases.
Ihar Hrachyshka3f771b72016-12-17 04:12:24 +0000825 for proto in ip ip6; do
Ihar Hrachyshkab3a210f2016-09-29 13:26:30 +0000826 sudo sysctl -w net.bridge.bridge-nf-call-${proto}tables=1
827 done
828}
829
830
Dan Smith1f55d382017-05-16 08:50:53 -0700831# Set a systemd system override
832#
833# This sets a system-side override in system.conf. A per-service
834# override would be /etc/systemd/system/${service}.service/override.conf
835function set_systemd_override {
836 local key="$1"
837 local value="$2"
838
839 local sysconf="/etc/systemd/system.conf"
840 iniset -sudo "${sysconf}" "Manager" "$key" "$value"
841 echo "Set systemd system override for ${key}=${value}"
842
843 sudo systemctl daemon-reload
844}
845
Matthew Treinish309b99e2017-05-23 15:18:31 -0400846# Get a random port from the local port range
847#
848# This function returns an available port in the local port range. The search
849# order is not truly random, but should be considered a random value by the
850# user because it depends on the state of your local system.
851function get_random_port {
852 read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
853 while true; do
854 for (( port = upper_port ; port >= lower_port ; port-- )); do
855 sudo lsof -i ":$port" &> /dev/null
856 if [[ $? > 0 ]] ; then
857 break 2
858 fi
859 done
860 done
861 echo $port
862}
863
Ian Wienand07cbc442017-06-30 12:29:19 +1000864# Save some state information
865#
866# Write out various useful state information to /etc/devstack-version
Sean Dague2c0faca2017-06-28 09:13:04 -0400867function write_devstack_version {
Dirk Mueller6bab8322018-03-02 21:13:12 +0100868 cat - <<EOF | sudo tee /etc/devstack-version >/dev/null
Ian Wienand07cbc442017-06-30 12:29:19 +1000869DevStack Version: ${DEVSTACK_SERIES}
870Change: $(git log --format="%H %s %ci" -1)
871OS Version: ${os_VENDOR} ${os_RELEASE} ${os_CODENAME}
Sean Dague2c0faca2017-06-28 09:13:04 -0400872EOF
Sean Dague2c0faca2017-06-28 09:13:04 -0400873}
874
Dean Troyer27e32692012-03-16 16:16:56 -0500875# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100876$_XTRACE_FUNCTIONS
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500877
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500878# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400879# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400880# End: