blob: e5e3400ff8774581c0c2bf514794d46b642e2954 [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
25XTRACE=$(set +o | grep xtrace)
26set +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
Adam Spierscb961592013-10-05 12:11:07 +010033# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050034# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010035#
36# - ``FILES`` must be set to the cache dir
37# - ``GLANCE_HOSTPORT``
38#
Peter Stachowski5aeea6a2015-09-22 19:38:02 +000039# upload_image image-url
Ian Wienandaee18c72014-02-21 15:35:08 +110040function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050041 local image_url=$1
Dean Troyerca0e3d02012-04-13 15:58:37 -050042
Dean Troyere9f76672014-07-25 11:09:36 -050043 local image image_fname image_name
44
Dean Troyerca0e3d02012-04-13 15:58:37 -050045 # Create a directory for the downloaded image tarballs.
46 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050047 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080048 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080049 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050050 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +010051 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090052 if [[ $? -ne 0 ]]; then
53 echo "Not found: $image_url"
54 return
55 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080056 fi
Dean Troyere9f76672014-07-25 11:09:36 -050057 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -080058 else
Dean Troyer3324f192014-09-18 09:26:39 -050059 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -080060 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -050061 # unix: ``file:///home/user/path/file``
62 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -050063 image=$(echo $image_url | sed "s/^file:\/\///g")
64 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050065 echo "Not found: $image_url"
66 return
67 fi
68 fi
69
70 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
71 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050072 image_name="${image_fname%.tar.gz}"
Monty Taylor31c313d2015-09-19 11:35:22 -040073 openstack --os-cloud=devstack-admin image create "$image_name" --public --container-format ami --disk-format ami < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -050074 return
75 fi
76
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070077 # vmdk format images
78 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050079 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070080
81 # Before we can upload vmdk type images to glance, we need to know it's
82 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080083 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070084 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070085 # vmdk filename, which is expected in the following format:
86 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080087 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -070088 #
89 # If the filename does not follow the above format then the vsphere
90 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070091
Dean Troyere9f76672014-07-25 11:09:36 -050092 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -080093 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -050094 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080095
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070096 # vmdk adapter type
Dean Troyere9f76672014-07-25 11:09:36 -050097 local vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070098 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
99 vmdk_adapter_type="${vmdk_adapter_type%?}"
100
101 # vmdk disk type
Dean Troyere9f76672014-07-25 11:09:36 -0500102 local vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700103 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800104 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800105
106 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900107 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700108 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
109 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500110 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500111 # Attempt to retrieve the ``*-flat.vmdk``
Dean Troyere9f76672014-07-25 11:09:36 -0500112 local flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800113 flat_fname="${flat_fname#*\"}"
114 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700115 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500116 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800117 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500118 path_len=`expr ${#image_url} - ${#image_fname}`
119 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800120 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900121 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800122 if [[ $flat_url != file* ]]; then
123 if [[ ! -f $FILES/$flat_fname || \
124 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100125 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800126 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500127 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800128 else
Dean Troyere9f76672014-07-25 11:09:36 -0500129 image=$(echo $flat_url | sed "s/^file:\/\///g")
130 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800131 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700132 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800133 fi
134 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500135 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800136 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800137 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
138 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800139 elif [[ -z "$vmdk_create_type" ]]; then
140 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
141 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500142 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800143 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500144 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700145 else
Dean Troyere9f76672014-07-25 11:09:36 -0500146 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
147 path_len=`expr ${#image_url} - ${#image_fname}`
148 local flat_path="${image_url:0:$path_len}"
149 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700150 warn $LINENO "$descriptor_data_pair_msg"`
151 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
152 if [[ $flat_path != file* ]]; then
153 if [[ ! -f $FILES/$descriptor_fname || \
154 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
155 wget -c $descriptor_url -O $FILES/$descriptor_fname
156 fi
157 descriptor_url="$FILES/$descriptor_fname"
158 else
159 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
160 if [[ ! -f $descriptor_url || \
161 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
162 echo "Descriptor not found: $descriptor_url"
163 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800164 fi
165 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800166 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
167 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
168 vmdk_adapter_type="${vmdk_adapter_type%?}"
169 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900170 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700171 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700172 vmdk_disktype="preallocated"
173 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800174
175 # NOTE: For backwards compatibility reasons, colons may be used in place
176 # of semi-colons for property delimiters but they are not permitted
177 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500178 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800179 IFS=':;' read -a props <<< "$property_string"
180 vmdk_disktype="${props[0]:-$vmdk_disktype}"
181 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
182 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700183
Monty Taylor31c313d2015-09-19 11:35:22 -0400184 openstack --os-cloud=devstack-admin 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 -0700185 return
186 fi
187
Mate Lakatbc2ef922013-08-15 18:06:59 +0100188 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500189 # and should not be decompressed prior to loading
190 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500191 image_name="${image_fname%.vhd.tgz}"
192 local force_vm_mode=""
193 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000194 # Cirros VHD image currently only boots in PV mode.
195 # Nova defaults to PV for all VHD images, but
196 # the glance setting is needed for booting
197 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500198 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000199 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400200 openstack \
Monty Taylor31c313d2015-09-19 11:35:22 -0400201 --os-cloud=devstack-admin \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400202 image create \
203 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000204 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500205 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500206 return
207 fi
208
Mate Lakatbc2ef922013-08-15 18:06:59 +0100209 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
210 # and should not be decompressed prior to loading.
211 # Setting metadata, so PV mode is used.
212 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500213 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400214 openstack \
Monty Taylor31c313d2015-09-19 11:35:22 -0400215 --os-cloud=devstack-admin \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400216 image create \
217 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100218 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500219 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100220 return
221 fi
222
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300223 if [[ "$image_url" =~ '.hds' ]]; then
224 image_name="${image_fname%.hds}"
225 vm_mode=${image_name##*-}
226 if [[ $vm_mode != 'exe' && $vm_mode != 'hvm' ]]; then
227 die $LINENO "Unknown vm_mode=${vm_mode} for Virtuozzo image"
228 fi
229
230 openstack \
Monty Taylor31c313d2015-09-19 11:35:22 -0400231 --os-cloud=devstack-admin \
Maxim Nestratov54ee8a82015-07-15 11:47:11 +0300232 image create \
233 "$image_name" --public \
234 --container-format=bare --disk-format=ploop \
235 --property vm_mode=$vm_mode < "${image}"
236 return
237 fi
238
Dean Troyere9f76672014-07-25 11:09:36 -0500239 local kernel=""
240 local ramdisk=""
241 local disk_format=""
242 local container_format=""
243 local unpack=""
244 local img_property=""
245 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500246 *.tar.gz|*.tgz)
247 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500248 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
249 image_name="${image_fname%.tar.gz}" ||
250 image_name="${image_fname%.tgz}"
251 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500252 rm -Rf "$xdir";
253 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500254 tar -zxf $image -C "$xdir"
255 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400256 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500257 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400258 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500259 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400260 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500261 if [[ -z "$image_name" ]]; then
262 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500263 fi
264 ;;
265 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500266 image_name=$(basename "$image" ".img")
Ian Wienandada886d2015-10-07 14:06:26 +1100267 local format
268 format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500269 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500270 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500271 else
Dean Troyere9f76672014-07-25 11:09:36 -0500272 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500273 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500274 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500275 ;;
276 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500277 image_name=$(basename "$image" ".img.gz")
278 disk_format=raw
279 container_format=bare
280 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500281 ;;
282 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500283 image_name=$(basename "$image" ".qcow2")
284 disk_format=qcow2
285 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500286 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100287 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500288 image_name=$(basename "$image" ".iso")
289 disk_format=iso
290 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100291 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300292 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
293 local extension="${image_fname#*.}"
294 image_name=$(basename "$image" ".$extension")
295 disk_format=vhd
296 container_format=bare
297 if [ "${image_fname##*.}" == "gz" ]; then
298 unpack=zcat
299 fi
300 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500301 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500302 esac
303
Rafael Folcoab775872013-12-02 14:04:32 -0200304 if is_arch "ppc64"; then
Dean Troyere9f76672014-07-25 11:09:36 -0500305 img_property="--property hw_cdrom_bus=scsi"
Rafael Folcoab775872013-12-02 14:04:32 -0200306 fi
307
Clark Laughlinfcc3f6e2015-04-07 16:31:47 +0000308 if is_arch "aarch64"; then
309 img_property="--property hw_machine_type=virt --property hw_cdrom_bus=virtio --property os_command_line='console=ttyAMA0'"
310 fi
311
Dean Troyere9f76672014-07-25 11:09:36 -0500312 if [ "$container_format" = "bare" ]; then
313 if [ "$unpack" = "zcat" ]; then
Monty Taylor31c313d2015-09-19 11:35:22 -0400314 openstack --os-cloud=devstack-admin image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < <(zcat --force "${image}")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500315 else
Monty Taylor31c313d2015-09-19 11:35:22 -0400316 openstack --os-cloud=devstack-admin image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500317 fi
318 else
319 # Use glance client to add the kernel the root filesystem.
320 # We parse the results of the first upload to get the glance ID of the
321 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500322 local kernel_id="" ramdisk_id="";
323 if [ -n "$kernel" ]; then
Monty Taylor31c313d2015-09-19 11:35:22 -0400324 kernel_id=$(openstack --os-cloud=devstack-admin 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 -0500325 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500326 if [ -n "$ramdisk" ]; then
Monty Taylor31c313d2015-09-19 11:35:22 -0400327 ramdisk_id=$(openstack --os-cloud=devstack-admin 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 -0500328 fi
Monty Taylor31c313d2015-09-19 11:35:22 -0400329 openstack --os-cloud=devstack-admin 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 -0500330 fi
331}
332
Dean Troyer1a6d4492013-06-03 16:47:36 -0500333
Dean Troyerc1b486a2012-11-05 14:26:09 -0600334# Set the database backend to use
335# When called from stackrc/localrc DATABASE_BACKENDS has not been
336# initialized yet, just save the configuration selection and call back later
337# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100338#
339# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600340function use_database {
341 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600342 # No backends registered means this is likely called from ``localrc``
343 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600344 DATABASE_TYPE=$1
Sean Dague72ad9422015-10-07 11:51:40 -0400345 deprecated "The database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100346 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600347 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100348 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600349 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600350}
351
sridhargaddamb5ab6462015-02-24 07:23:24 +0000352#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
353CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500354
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600355# Wait for an HTTP server to start answering requests
356# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100357function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600358 local timeout=$1
359 local url=$2
sridhargaddamb5ab6462015-02-24 07:23:24 +0000360 timeout $timeout sh -c "while ! $CURL_GET -k --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600361}
362
Dean Troyer1a6d4492013-06-03 16:47:36 -0500363
Nachi Uenofda946e2012-10-24 17:26:02 -0700364# ping check
Sean Dagueaf9bf862015-04-16 08:58:32 -0400365# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``MULTI_HOST``, ``PRIVATE_NETWORK``
366# ping_check <ip> [boot-timeout] [from_net] [expected]
Ian Wienandaee18c72014-02-21 15:35:08 +1100367function ping_check {
Sean Dagueaf9bf862015-04-16 08:58:32 -0400368 local ip=$1
369 local timeout=${2:-30}
370 local from_net=${3:-""}
371 local expected=${4:-True}
372 local op="!"
373 local failmsg="[Fail] Couldn't ping server"
374 local ping_cmd="ping"
Nachi Uenofda946e2012-10-24 17:26:02 -0700375
Sean Dagueaf9bf862015-04-16 08:58:32 -0400376 # if we don't specify a from_net we're expecting things to work
377 # fine from our local box.
378 if [[ -n "$from_net" ]]; then
379 if is_service_enabled neutron; then
380 ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
381 elif [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
382 # there is no way to address the multihost / private case, bail here for compatibility.
383 # TODO: remove this cruft and redo code to handle this at the caller level.
384 return
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700385 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700386 fi
Sean Dagueaf9bf862015-04-16 08:58:32 -0400387
388 # inverse the logic if we're testing no connectivity
389 if [[ "$expected" != "True" ]]; then
390 op=""
391 failmsg="[Fail] Could ping server"
392 fi
393
394 # Because we've transformed this command so many times, print it
395 # out at the end.
396 local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
397 echo "Checking connectivity with $check_command"
398
399 if ! timeout $timeout sh -c "$check_command"; then
400 die $LINENO $failmsg
401 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700402}
403
Nachi Ueno6769b162013-08-12 18:18:56 -0700404# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100405function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700406 local vm_id=$1
407 local network_name=$2
408 local nova_result="$(nova show $vm_id)"
Ian Wienandada886d2015-10-07 14:06:26 +1100409 local ip
410 ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
Nachi Ueno6769b162013-08-12 18:18:56 -0700411 if [[ $ip = "" ]];then
412 echo "$nova_result"
Atsushi SAKAI33c9a672015-11-12 19:50:00 +0900413 die $LINENO "[Fail] Couldn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700414 fi
415 echo $ip
416}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500417
Nachi Uenofda946e2012-10-24 17:26:02 -0700418# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700419
Dean Troyer1a6d4492013-06-03 16:47:36 -0500420# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100421function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400422 if is_service_enabled neutron; then
423 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700424 return
425 fi
426 _ssh_check_novanet "$1" $2 $3 $4 $5
427}
428
Ian Wienandaee18c72014-02-21 15:35:08 +1100429function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700430 local NET_NAME=$1
431 local KEY_FILE=$2
432 local FLOATING_IP=$3
433 local DEFAULT_INSTANCE_USER=$4
434 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600435 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500436 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 -0800437 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700438 fi
439}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500440
Vincent Untz856a11e2012-11-21 16:04:12 +0100441
Vincent Untz856a11e2012-11-21 16:04:12 +0100442# Get the location of the $module-rootwrap executables, where module is cinder
443# or nova.
444# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100445function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100446 local module=$1
447
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100448 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100449}
450
Dean Troyer1a6d4492013-06-03 16:47:36 -0500451
Ian Wienand0488edd2013-04-11 12:04:36 +1000452# Path permissions sanity check
453# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100454function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000455 # Ensure no element of the path has 0700 permissions, which is very
456 # likely to cause issues for daemons. Inspired by default 0700
457 # homedir permissions on RHEL and common practice of making DEST in
458 # the stack user's homedir.
459
Ian Wienandada886d2015-10-07 14:06:26 +1100460 local real_path
461 real_path=$(readlink -f $1)
Ian Wienand0488edd2013-04-11 12:04:36 +1000462 local rebuilt_path=""
463 for i in $(echo ${real_path} | tr "/" " "); do
464 rebuilt_path=$rebuilt_path"/"$i
465
466 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
467 echo "*** DEST path element"
468 echo "*** ${rebuilt_path}"
469 echo "*** appears to have 0700 permissions."
Dean Troyerdc97cb72015-03-28 08:20:50 -0500470 echo "*** This is very likely to cause fatal issues for DevStack daemons."
Ian Wienand0488edd2013-04-11 12:04:36 +1000471
472 if [[ -n "$SKIP_PATH_SANITY" ]]; then
473 return
474 else
475 echo "*** Set SKIP_PATH_SANITY to skip this check"
476 die $LINENO "Invalid path permissions"
477 fi
478 fi
479 done
480}
481
Dean Troyer1a6d4492013-06-03 16:47:36 -0500482
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000483# This function recursively compares versions, and is not meant to be
484# called by anything other than vercmp_numbers below. This function does
485# not work with alphabetic versions.
486#
487# _vercmp_r sep ver1 ver2
488function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400489 typeset sep
490 typeset -a ver1=() ver2=()
491 sep=$1; shift
492 ver1=("${@:1:sep}")
493 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000494
Sean Dague537d4022013-10-22 07:43:22 -0400495 if ((ver1 > ver2)); then
496 echo 1; return 0
497 elif ((ver2 > ver1)); then
498 echo -1; return 0
499 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000500
Sean Dague537d4022013-10-22 07:43:22 -0400501 if ((sep <= 1)); then
502 echo 0; return 0
503 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000504
Sean Dague537d4022013-10-22 07:43:22 -0400505 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000506}
507
508
509# This function compares two versions and is meant to be called by
510# external callers. Please note the function assumes non-alphabetic
511# versions. For example, this will work:
512#
513# vercmp_numbers 1.10 1.4
514#
515# The above will return "1", as 1.10 is greater than 1.4.
516#
517# vercmp_numbers 5.2 6.4
518#
519# The above will return "-1", as 5.2 is less than 6.4.
520#
521# vercmp_numbers 4.0 4.0
522#
523# The above will return "0", as the versions are equal.
524#
525# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100526function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400527 typeset v1=$1 v2=$2 sep
528 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000529
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100530 deprecated "vercmp_numbers is deprecated for more generic vercmp"
531
Sean Dague537d4022013-10-22 07:43:22 -0400532 IFS=. read -ra ver1 <<< "$v1"
533 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000534
Sean Dague537d4022013-10-22 07:43:22 -0400535 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000536}
537
Ian Wienand2ba36cd2015-11-12 13:52:36 +1100538# vercmp ver1 op ver2
539# Compare VER1 to VER2
540# - op is one of < <= == >= >
541# - returns true if satisified
542# e.g.
543# if vercmp 1.0 "<" 2.0; then
544# ...
545# fi
546function vercmp {
547 local v1=$1
548 local op=$2
549 local v2=$3
550 local result
551
552 # sort the two numbers with sort's "-V" argument. Based on if v2
553 # swapped places with v1, we can determine ordering.
554 result=$(echo -e "$v1\n$v2" | sort -V | head -1)
555
556 case $op in
557 "==")
558 [ "$v1" = "$v2" ]
559 return
560 ;;
561 ">")
562 [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
563 return
564 ;;
565 "<")
566 [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
567 return
568 ;;
569 ">=")
570 [ "$result" = "$v2" ]
571 return
572 ;;
573 "<=")
574 [ "$result" = "$v1" ]
575 return
576 ;;
577 *)
578 die $LINENO "unrecognised op: $op"
579 ;;
580 esac
581}
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000582
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700583# This function sets log formatting options for colorizing log
584# output to stdout. It is meant to be called by lib modules.
585# The last two parameters are optional and can be used to specify
586# non-default value for project and user format variables.
587# Defaults are respectively 'project_name' and 'user_name'
588#
589# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100590function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700591 local conf_file=$1
592 local conf_section=$2
593 local project_var=${3:-"project_name"}
594 local user_var=${4:-"user_name"}
595 # Add color to logging output
596 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"
597 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"
598 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
599 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
600}
601
Ian Wienand54e39102014-06-03 16:05:12 +1000602# These functions are provided for basic fall-back functionality for
Dean Troyerdc97cb72015-03-28 08:20:50 -0500603# projects that include parts of DevStack (Grenade). stack.sh will
604# override these with more specific versions for DevStack (with fancy
Ian Wienand54e39102014-06-03 16:05:12 +1000605# spinners, etc). We never override an existing version
606if ! function_exists echo_summary; then
607 function echo_summary {
608 echo $@
609 }
610fi
611if ! function_exists echo_nolog; then
612 function echo_nolog {
613 echo $@
614 }
615fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600616
Sébastien Han36f2f022014-01-06 18:09:26 +0100617
618# create_disk - Create backing disk
619function create_disk {
620 local node_number
621 local disk_image=${1}
622 local storage_data_dir=${2}
623 local loopback_disk_size=${3}
624
625 # Create a loopback disk and format it to XFS.
626 if [[ -e ${disk_image} ]]; then
627 if egrep -q ${storage_data_dir} /proc/mounts; then
628 sudo umount ${storage_data_dir}/drives/sdb1
629 sudo rm -f ${disk_image}
630 fi
631 fi
632
633 sudo mkdir -p ${storage_data_dir}/drives/images
634
635 sudo truncate -s ${loopback_disk_size} ${disk_image}
636
637 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
638 # a single inode. Keeping the default inode size (256) will result in multiple
639 # inodes being used to store xattr. Retrieving the xattr will be slower
640 # since we have to read multiple inodes. This statement is true for both
641 # Swift and Ceph.
642 sudo mkfs.xfs -f -i size=1024 ${disk_image}
643
644 # Mount the disk with mount options to make it as efficient as possible
645 if ! egrep -q ${storage_data_dir} /proc/mounts; then
646 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
647 ${disk_image} ${storage_data_dir}
648 fi
649}
650
Dean Troyer27e32692012-03-16 16:16:56 -0500651# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000652$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500653
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500654# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400655# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400656# End: