blob: ea737deee80b330cdcea33b5f651f38d6ede1a03 [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
15declare -r _DEVSTACK_FUNCTIONS=1
16
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}
44
45
Adam Spierscb961592013-10-05 12:11:07 +010046# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050047# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010048#
49# - ``FILES`` must be set to the cache dir
50# - ``GLANCE_HOSTPORT``
51#
Peter Stachowski5aeea6a2015-09-22 19:38:02 +000052# upload_image image-url
Ian Wienandaee18c72014-02-21 15:35:08 +110053function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050054 local image_url=$1
Dean Troyerca0e3d02012-04-13 15:58:37 -050055
Dean Troyere9f76672014-07-25 11:09:36 -050056 local image image_fname image_name
57
Dean Troyerca0e3d02012-04-13 15:58:37 -050058 # Create a directory for the downloaded image tarballs.
59 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050060 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080061 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080062 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050063 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +010064 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090065 if [[ $? -ne 0 ]]; then
66 echo "Not found: $image_url"
67 return
68 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080069 fi
Dean Troyere9f76672014-07-25 11:09:36 -050070 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -080071 else
Dean Troyer3324f192014-09-18 09:26:39 -050072 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -080073 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -050074 # unix: ``file:///home/user/path/file``
75 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -050076 image=$(echo $image_url | sed "s/^file:\/\///g")
77 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050078 echo "Not found: $image_url"
79 return
80 fi
81 fi
82
83 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
84 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050085 image_name="${image_fname%.tar.gz}"
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +030086 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 -050087 return
88 fi
89
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070090 # vmdk format images
91 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050092 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070093
94 # Before we can upload vmdk type images to glance, we need to know it's
95 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080096 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070097 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070098 # vmdk filename, which is expected in the following format:
99 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800100 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700101 #
102 # If the filename does not follow the above format then the vsphere
103 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700104
Dean Troyere9f76672014-07-25 11:09:36 -0500105 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -0800106 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -0500107 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800108
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700109 # vmdk adapter type
Ian Wienand7ae97292016-02-16 14:50:53 +1100110 local vmdk_adapter_type
111 vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700112 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
113 vmdk_adapter_type="${vmdk_adapter_type%?}"
114
115 # vmdk disk type
Ian Wienand7ae97292016-02-16 14:50:53 +1100116 local vmdk_create_type
117 vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700118 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800119 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800120
121 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900122 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700123 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
124 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500125 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500126 # Attempt to retrieve the ``*-flat.vmdk``
Ian Wienand7ae97292016-02-16 14:50:53 +1100127 local flat_fname
128 flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800129 flat_fname="${flat_fname#*\"}"
130 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700131 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500132 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800133 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500134 path_len=`expr ${#image_url} - ${#image_fname}`
135 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800136 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900137 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800138 if [[ $flat_url != file* ]]; then
139 if [[ ! -f $FILES/$flat_fname || \
140 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100141 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800142 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500143 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800144 else
Dean Troyere9f76672014-07-25 11:09:36 -0500145 image=$(echo $flat_url | sed "s/^file:\/\///g")
146 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800147 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700148 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800149 fi
150 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500151 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800152 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800153 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
154 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800155 elif [[ -z "$vmdk_create_type" ]]; then
156 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
157 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500158 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800159 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500160 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700161 else
Dean Troyere9f76672014-07-25 11:09:36 -0500162 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
163 path_len=`expr ${#image_url} - ${#image_fname}`
164 local flat_path="${image_url:0:$path_len}"
165 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700166 warn $LINENO "$descriptor_data_pair_msg"`
167 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
168 if [[ $flat_path != file* ]]; then
169 if [[ ! -f $FILES/$descriptor_fname || \
170 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
171 wget -c $descriptor_url -O $FILES/$descriptor_fname
172 fi
173 descriptor_url="$FILES/$descriptor_fname"
174 else
175 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
176 if [[ ! -f $descriptor_url || \
177 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
178 echo "Descriptor not found: $descriptor_url"
179 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800180 fi
181 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800182 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
183 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
184 vmdk_adapter_type="${vmdk_adapter_type%?}"
185 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900186 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700187 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700188 vmdk_disktype="preallocated"
189 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800190
191 # NOTE: For backwards compatibility reasons, colons may be used in place
192 # of semi-colons for property delimiters but they are not permitted
193 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500194 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800195 IFS=':;' read -a props <<< "$property_string"
196 vmdk_disktype="${props[0]:-$vmdk_disktype}"
197 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
198 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700199
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300200 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 -0700201 return
202 fi
203
Mate Lakatbc2ef922013-08-15 18:06:59 +0100204 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500205 # and should not be decompressed prior to loading
206 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500207 image_name="${image_fname%.vhd.tgz}"
208 local force_vm_mode=""
209 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000210 # Cirros VHD image currently only boots in PV mode.
211 # Nova defaults to PV for all VHD images, but
212 # the glance setting is needed for booting
213 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500214 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000215 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400216 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300217 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400218 image create \
219 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000220 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500221 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500222 return
223 fi
224
Mate Lakatbc2ef922013-08-15 18:06:59 +0100225 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
226 # and should not be decompressed prior to loading.
227 # Setting metadata, so PV mode is used.
228 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500229 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400230 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300231 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400232 image create \
233 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100234 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500235 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100236 return
237 fi
238
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300239 if [[ "$image_url" =~ '.hds' ]]; then
240 image_name="${image_fname%.hds}"
241 vm_mode=${image_name##*-}
242 if [[ $vm_mode != 'exe' && $vm_mode != 'hvm' ]]; then
243 die $LINENO "Unknown vm_mode=${vm_mode} for Virtuozzo image"
244 fi
245
246 openstack \
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300247 --os-cloud=devstack-admin --os-region-name="$REGION_NAME" \
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300248 image create \
249 "$image_name" --public \
250 --container-format=bare --disk-format=ploop \
251 --property vm_mode=$vm_mode < "${image}"
252 return
253 fi
254
Dean Troyere9f76672014-07-25 11:09:36 -0500255 local kernel=""
256 local ramdisk=""
257 local disk_format=""
258 local container_format=""
259 local unpack=""
260 local img_property=""
261 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500262 *.tar.gz|*.tgz)
263 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500264 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
265 image_name="${image_fname%.tar.gz}" ||
266 image_name="${image_fname%.tgz}"
267 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500268 rm -Rf "$xdir";
269 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500270 tar -zxf $image -C "$xdir"
271 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400272 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500273 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400274 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500275 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400276 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500277 if [[ -z "$image_name" ]]; then
278 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500279 fi
280 ;;
281 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500282 image_name=$(basename "$image" ".img")
Ian Wienandada886d2015-10-07 14:06:26 +1100283 local format
284 format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500285 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500286 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500287 else
Dean Troyere9f76672014-07-25 11:09:36 -0500288 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500289 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500290 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500291 ;;
292 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500293 image_name=$(basename "$image" ".img.gz")
294 disk_format=raw
295 container_format=bare
296 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500297 ;;
Hongbin Lu3feceb02016-04-17 11:11:58 -0400298 *.img.bz2)
299 image_name=$(basename "$image" ".img.bz2")
300 disk_format=qcow2
301 container_format=bare
302 unpack=bunzip2
303 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500304 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500305 image_name=$(basename "$image" ".qcow2")
306 disk_format=qcow2
307 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500308 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100309 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500310 image_name=$(basename "$image" ".iso")
311 disk_format=iso
312 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100313 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300314 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
315 local extension="${image_fname#*.}"
316 image_name=$(basename "$image" ".$extension")
317 disk_format=vhd
318 container_format=bare
319 if [ "${image_fname##*.}" == "gz" ]; then
320 unpack=zcat
321 fi
322 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500323 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500324 esac
325
Rafael Folco72f530f2016-02-09 07:08:38 -0600326 if is_arch "ppc64le" || is_arch "ppc64" || is_arch "ppc"; then
327 img_property="--property hw_disk_bus=scsi --property hw_scsi_model=virtio-scsi --property hw_cdrom_bus=scsi --property os_command_line=console=hvc0"
Rafael Folcoab775872013-12-02 14:04:32 -0200328 fi
329
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000330 if is_arch "aarch64"; then
331 img_property="--property hw_machine_type=virt --property hw_cdrom_bus=virtio --property os_command_line='console=ttyAMA0'"
332 fi
333
Dean Troyere9f76672014-07-25 11:09:36 -0500334 if [ "$container_format" = "bare" ]; then
335 if [ "$unpack" = "zcat" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300336 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 -0400337 elif [ "$unpack" = "bunzip2" ]; then
338 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 -0500339 else
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300340 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 -0500341 fi
342 else
343 # Use glance client to add the kernel the root filesystem.
344 # We parse the results of the first upload to get the glance ID of the
345 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500346 local kernel_id="" ramdisk_id="";
347 if [ -n "$kernel" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300348 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 -0500349 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500350 if [ -n "$ramdisk" ]; then
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300351 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 -0500352 fi
Victor Ryzhenkin878d7d82016-04-27 15:15:52 +0300353 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 -0500354 fi
355}
356
Dean Troyer1a6d4492013-06-03 16:47:36 -0500357
Dean Troyerc1b486a2012-11-05 14:26:09 -0600358# Set the database backend to use
359# When called from stackrc/localrc DATABASE_BACKENDS has not been
360# initialized yet, just save the configuration selection and call back later
361# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100362#
363# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600364function use_database {
365 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600366 # No backends registered means this is likely called from ``localrc``
367 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600368 DATABASE_TYPE=$1
Sean Dague72ad9422015-10-07 11:51:40 -0400369 deprecated "The database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100370 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600371 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100372 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600373 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600374}
375
sridhargaddamb5ab6462015-02-24 07:23:24 +0000376#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
377CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500378
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600379# Wait for an HTTP server to start answering requests
380# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100381function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600382 local timeout=$1
383 local url=$2
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900384 time_start "wait_for_service"
sridhargaddamb5ab6462015-02-24 07:23:24 +0000385 timeout $timeout sh -c "while ! $CURL_GET -k --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900386 time_stop "wait_for_service"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600387}
388
Dean Troyer1a6d4492013-06-03 16:47:36 -0500389
Nachi Uenofda946e2012-10-24 17:26:02 -0700390# ping check
Sean Dagueaf9bf862015-04-16 08:58:32 -0400391# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``MULTI_HOST``, ``PRIVATE_NETWORK``
392# ping_check <ip> [boot-timeout] [from_net] [expected]
Ian Wienandaee18c72014-02-21 15:35:08 +1100393function ping_check {
Sean Dagueaf9bf862015-04-16 08:58:32 -0400394 local ip=$1
395 local timeout=${2:-30}
396 local from_net=${3:-""}
397 local expected=${4:-True}
398 local op="!"
399 local failmsg="[Fail] Couldn't ping server"
400 local ping_cmd="ping"
Nachi Uenofda946e2012-10-24 17:26:02 -0700401
Sean Dagueaf9bf862015-04-16 08:58:32 -0400402 # if we don't specify a from_net we're expecting things to work
403 # fine from our local box.
404 if [[ -n "$from_net" ]]; then
405 if is_service_enabled neutron; then
406 ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
407 elif [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
408 # there is no way to address the multihost / private case, bail here for compatibility.
409 # TODO: remove this cruft and redo code to handle this at the caller level.
410 return
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700411 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700412 fi
Sean Dagueaf9bf862015-04-16 08:58:32 -0400413
414 # inverse the logic if we're testing no connectivity
415 if [[ "$expected" != "True" ]]; then
416 op=""
417 failmsg="[Fail] Could ping server"
418 fi
419
420 # Because we've transformed this command so many times, print it
421 # out at the end.
422 local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
423 echo "Checking connectivity with $check_command"
424
425 if ! timeout $timeout sh -c "$check_command"; then
426 die $LINENO $failmsg
427 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700428}
429
Nachi Ueno6769b162013-08-12 18:18:56 -0700430# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100431function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700432 local vm_id=$1
433 local network_name=$2
Ian Wienand7ae97292016-02-16 14:50:53 +1100434 local nova_result
Ian Wienandada886d2015-10-07 14:06:26 +1100435 local ip
Ian Wienand7ae97292016-02-16 14:50:53 +1100436
437 nova_result="$(nova show $vm_id)"
Ian Wienandada886d2015-10-07 14:06:26 +1100438 ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
Nachi Ueno6769b162013-08-12 18:18:56 -0700439 if [[ $ip = "" ]];then
440 echo "$nova_result"
Atsushi SAKAI33c9a672015-11-12 19:50:00 +0900441 die $LINENO "[Fail] Couldn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700442 fi
443 echo $ip
444}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500445
Nachi Uenofda946e2012-10-24 17:26:02 -0700446# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700447
Dean Troyer1a6d4492013-06-03 16:47:36 -0500448# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100449function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400450 if is_service_enabled neutron; then
451 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700452 return
453 fi
454 _ssh_check_novanet "$1" $2 $3 $4 $5
455}
456
Ian Wienandaee18c72014-02-21 15:35:08 +1100457function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700458 local NET_NAME=$1
459 local KEY_FILE=$2
460 local FLOATING_IP=$3
461 local DEFAULT_INSTANCE_USER=$4
462 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600463 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500464 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 -0800465 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700466 fi
467}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500468
Vincent Untz856a11e2012-11-21 16:04:12 +0100469
Vincent Untz856a11e2012-11-21 16:04:12 +0100470# Get the location of the $module-rootwrap executables, where module is cinder
471# or nova.
472# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100473function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100474 local module=$1
475
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100476 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100477}
478
Dean Troyer1a6d4492013-06-03 16:47:36 -0500479
Ian Wienand0488edd2013-04-11 12:04:36 +1000480# Path permissions sanity check
481# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100482function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000483 # Ensure no element of the path has 0700 permissions, which is very
484 # likely to cause issues for daemons. Inspired by default 0700
485 # homedir permissions on RHEL and common practice of making DEST in
486 # the stack user's homedir.
487
Ian Wienandada886d2015-10-07 14:06:26 +1100488 local real_path
489 real_path=$(readlink -f $1)
Ian Wienand0488edd2013-04-11 12:04:36 +1000490 local rebuilt_path=""
491 for i in $(echo ${real_path} | tr "/" " "); do
492 rebuilt_path=$rebuilt_path"/"$i
493
494 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
495 echo "*** DEST path element"
496 echo "*** ${rebuilt_path}"
497 echo "*** appears to have 0700 permissions."
Dean Troyerdc97cb72015-03-28 08:20:50 -0500498 echo "*** This is very likely to cause fatal issues for DevStack daemons."
Ian Wienand0488edd2013-04-11 12:04:36 +1000499
500 if [[ -n "$SKIP_PATH_SANITY" ]]; then
501 return
502 else
503 echo "*** Set SKIP_PATH_SANITY to skip this check"
504 die $LINENO "Invalid path permissions"
505 fi
506 fi
507 done
508}
509
Dean Troyer1a6d4492013-06-03 16:47:36 -0500510
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000511# This function recursively compares versions, and is not meant to be
512# called by anything other than vercmp_numbers below. This function does
513# not work with alphabetic versions.
514#
515# _vercmp_r sep ver1 ver2
516function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400517 typeset sep
518 typeset -a ver1=() ver2=()
519 sep=$1; shift
520 ver1=("${@:1:sep}")
521 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000522
Sean Dague537d4022013-10-22 07:43:22 -0400523 if ((ver1 > ver2)); then
524 echo 1; return 0
525 elif ((ver2 > ver1)); then
526 echo -1; return 0
527 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000528
Sean Dague537d4022013-10-22 07:43:22 -0400529 if ((sep <= 1)); then
530 echo 0; return 0
531 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000532
Sean Dague537d4022013-10-22 07:43:22 -0400533 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000534}
535
536
537# This function compares two versions and is meant to be called by
538# external callers. Please note the function assumes non-alphabetic
539# versions. For example, this will work:
540#
541# vercmp_numbers 1.10 1.4
542#
543# The above will return "1", as 1.10 is greater than 1.4.
544#
545# vercmp_numbers 5.2 6.4
546#
547# The above will return "-1", as 5.2 is less than 6.4.
548#
549# vercmp_numbers 4.0 4.0
550#
551# The above will return "0", as the versions are equal.
552#
553# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100554function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400555 typeset v1=$1 v2=$2 sep
556 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000557
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100558 deprecated "vercmp_numbers is deprecated for more generic vercmp"
559
Sean Dague537d4022013-10-22 07:43:22 -0400560 IFS=. read -ra ver1 <<< "$v1"
561 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000562
Sean Dague537d4022013-10-22 07:43:22 -0400563 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000564}
565
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100566# vercmp ver1 op ver2
567# Compare VER1 to VER2
568# - op is one of < <= == >= >
569# - returns true if satisified
570# e.g.
571# if vercmp 1.0 "<" 2.0; then
572# ...
573# fi
574function vercmp {
575 local v1=$1
576 local op=$2
577 local v2=$3
578 local result
579
580 # sort the two numbers with sort's "-V" argument. Based on if v2
581 # swapped places with v1, we can determine ordering.
582 result=$(echo -e "$v1\n$v2" | sort -V | head -1)
583
584 case $op in
585 "==")
586 [ "$v1" = "$v2" ]
587 return
588 ;;
589 ">")
590 [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
591 return
592 ;;
593 "<")
594 [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
595 return
596 ;;
597 ">=")
598 [ "$result" = "$v2" ]
599 return
600 ;;
601 "<=")
602 [ "$result" = "$v1" ]
603 return
604 ;;
605 *)
606 die $LINENO "unrecognised op: $op"
607 ;;
608 esac
609}
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000610
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700611# This function sets log formatting options for colorizing log
612# output to stdout. It is meant to be called by lib modules.
613# The last two parameters are optional and can be used to specify
614# non-default value for project and user format variables.
615# Defaults are respectively 'project_name' and 'user_name'
616#
617# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100618function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700619 local conf_file=$1
620 local conf_section=$2
621 local project_var=${3:-"project_name"}
622 local user_var=${4:-"user_name"}
623 # Add color to logging output
624 iniset $conf_file $conf_section logging_context_format_string "%(asctime)s.%(msecs)03d %(color)s%(levelname)s %(name)s [%(request_id)s %("$user_var")s %("$project_var")s%(color)s] %(instance)s%(color)s%(message)s"
625 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"
626 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
627 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
628}
629
Ian Wienand54e39102014-06-03 16:05:12 +1000630# These functions are provided for basic fall-back functionality for
Dean Troyerdc97cb72015-03-28 08:20:50 -0500631# projects that include parts of DevStack (Grenade). stack.sh will
632# override these with more specific versions for DevStack (with fancy
Ian Wienand54e39102014-06-03 16:05:12 +1000633# spinners, etc). We never override an existing version
634if ! function_exists echo_summary; then
635 function echo_summary {
636 echo $@
637 }
638fi
639if ! function_exists echo_nolog; then
640 function echo_nolog {
641 echo $@
642 }
643fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600644
Sébastien Han36f2f022014-01-06 18:09:26 +0100645
646# create_disk - Create backing disk
647function create_disk {
648 local node_number
649 local disk_image=${1}
650 local storage_data_dir=${2}
651 local loopback_disk_size=${3}
652
653 # Create a loopback disk and format it to XFS.
654 if [[ -e ${disk_image} ]]; then
655 if egrep -q ${storage_data_dir} /proc/mounts; then
656 sudo umount ${storage_data_dir}/drives/sdb1
657 sudo rm -f ${disk_image}
658 fi
659 fi
660
661 sudo mkdir -p ${storage_data_dir}/drives/images
662
663 sudo truncate -s ${loopback_disk_size} ${disk_image}
664
665 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
666 # a single inode. Keeping the default inode size (256) will result in multiple
667 # inodes being used to store xattr. Retrieving the xattr will be slower
668 # since we have to read multiple inodes. This statement is true for both
669 # Swift and Ceph.
670 sudo mkfs.xfs -f -i size=1024 ${disk_image}
671
672 # Mount the disk with mount options to make it as efficient as possible
673 if ! egrep -q ${storage_data_dir} /proc/mounts; then
674 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
675 ${disk_image} ${storage_data_dir}
676 fi
677}
678
Dean Troyer27e32692012-03-16 16:16:56 -0500679# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100680$_XTRACE_FUNCTIONS
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500681
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500682# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400683# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400684# End: