blob: 9adbfe7cf643612bc1d6af4c70813774fd098705 [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
Dean Troyerdff49a22014-01-30 15:37:40 -060013# Include the common functions
14FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
15source ${FUNC_DIR}/functions-common
Dean Troyerbf2ad702015-03-09 15:16:10 -050016source ${FUNC_DIR}/inc/ini-config
Dean Troyer490430d2015-01-30 14:38:35 -060017source ${FUNC_DIR}/inc/python
Dean Troyer7f9aa712012-01-31 12:11:56 -060018
Dean Troyer27e32692012-03-16 16:16:56 -050019# Save trace setting
20XTRACE=$(set +o | grep xtrace)
21set +o xtrace
22
Ian Wienand54e39102014-06-03 16:05:12 +100023# Check if a function already exists
24function function_exists {
25 declare -f -F $1 > /dev/null
26}
Dean Troyer7f9aa712012-01-31 12:11:56 -060027
Adam Spierscb961592013-10-05 12:11:07 +010028# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050029# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010030#
31# - ``FILES`` must be set to the cache dir
32# - ``GLANCE_HOSTPORT``
33#
Dean Troyerca0e3d02012-04-13 15:58:37 -050034# upload_image image-url glance-token
Ian Wienandaee18c72014-02-21 15:35:08 +110035function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050036 local image_url=$1
37 local token=$2
38
Dean Troyere9f76672014-07-25 11:09:36 -050039 local image image_fname image_name
40
Dean Troyerca0e3d02012-04-13 15:58:37 -050041 # Create a directory for the downloaded image tarballs.
42 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050043 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080044 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080045 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050046 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +010047 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090048 if [[ $? -ne 0 ]]; then
49 echo "Not found: $image_url"
50 return
51 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080052 fi
Dean Troyere9f76672014-07-25 11:09:36 -050053 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -080054 else
Dean Troyer3324f192014-09-18 09:26:39 -050055 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -080056 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -050057 # unix: ``file:///home/user/path/file``
58 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -050059 image=$(echo $image_url | sed "s/^file:\/\///g")
60 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050061 echo "Not found: $image_url"
62 return
63 fi
64 fi
65
66 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
67 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050068 image_name="${image_fname%.tar.gz}"
Rob Crittenden18d47782014-03-19 17:47:42 -040069 openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT image create "$image_name" --public --container-format ami --disk-format ami < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -050070 return
71 fi
72
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070073 # vmdk format images
74 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050075 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070076
77 # Before we can upload vmdk type images to glance, we need to know it's
78 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080079 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070080 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070081 # vmdk filename, which is expected in the following format:
82 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080083 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -070084 #
85 # If the filename does not follow the above format then the vsphere
86 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070087
Dean Troyere9f76672014-07-25 11:09:36 -050088 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -080089 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -050090 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080091
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070092 # vmdk adapter type
Dean Troyere9f76672014-07-25 11:09:36 -050093 local vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070094 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
95 vmdk_adapter_type="${vmdk_adapter_type%?}"
96
97 # vmdk disk type
Dean Troyere9f76672014-07-25 11:09:36 -050098 local vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070099 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800100 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800101
102 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900103 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700104 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
105 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500106 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500107 # Attempt to retrieve the ``*-flat.vmdk``
Dean Troyere9f76672014-07-25 11:09:36 -0500108 local flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800109 flat_fname="${flat_fname#*\"}"
110 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700111 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500112 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800113 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500114 path_len=`expr ${#image_url} - ${#image_fname}`
115 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800116 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900117 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800118 if [[ $flat_url != file* ]]; then
119 if [[ ! -f $FILES/$flat_fname || \
120 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100121 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800122 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500123 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800124 else
Dean Troyere9f76672014-07-25 11:09:36 -0500125 image=$(echo $flat_url | sed "s/^file:\/\///g")
126 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800127 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700128 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800129 fi
130 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500131 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800132 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800133 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
134 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800135 elif [[ -z "$vmdk_create_type" ]]; then
136 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
137 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500138 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800139 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500140 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700141 else
Dean Troyere9f76672014-07-25 11:09:36 -0500142 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
143 path_len=`expr ${#image_url} - ${#image_fname}`
144 local flat_path="${image_url:0:$path_len}"
145 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700146 warn $LINENO "$descriptor_data_pair_msg"`
147 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
148 if [[ $flat_path != file* ]]; then
149 if [[ ! -f $FILES/$descriptor_fname || \
150 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
151 wget -c $descriptor_url -O $FILES/$descriptor_fname
152 fi
153 descriptor_url="$FILES/$descriptor_fname"
154 else
155 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
156 if [[ ! -f $descriptor_url || \
157 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
158 echo "Descriptor not found: $descriptor_url"
159 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800160 fi
161 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800162 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
163 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
164 vmdk_adapter_type="${vmdk_adapter_type%?}"
165 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900166 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700167 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700168 vmdk_disktype="preallocated"
169 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800170
171 # NOTE: For backwards compatibility reasons, colons may be used in place
172 # of semi-colons for property delimiters but they are not permitted
173 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500174 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800175 IFS=':;' read -a props <<< "$property_string"
176 vmdk_disktype="${props[0]:-$vmdk_disktype}"
177 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
178 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700179
Rob Crittenden18d47782014-03-19 17:47:42 -0400180 openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT 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 -0700181 return
182 fi
183
Mate Lakatbc2ef922013-08-15 18:06:59 +0100184 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500185 # and should not be decompressed prior to loading
186 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500187 image_name="${image_fname%.vhd.tgz}"
188 local force_vm_mode=""
189 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000190 # Cirros VHD image currently only boots in PV mode.
191 # Nova defaults to PV for all VHD images, but
192 # the glance setting is needed for booting
193 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500194 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000195 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400196 openstack \
197 --os-token $token \
Rob Crittenden18d47782014-03-19 17:47:42 -0400198 --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400199 image create \
200 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000201 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500202 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500203 return
204 fi
205
Mate Lakatbc2ef922013-08-15 18:06:59 +0100206 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
207 # and should not be decompressed prior to loading.
208 # Setting metadata, so PV mode is used.
209 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500210 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400211 openstack \
212 --os-token $token \
Rob Crittenden18d47782014-03-19 17:47:42 -0400213 --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400214 image create \
215 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100216 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500217 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100218 return
219 fi
220
Dean Troyere9f76672014-07-25 11:09:36 -0500221 local kernel=""
222 local ramdisk=""
223 local disk_format=""
224 local container_format=""
225 local unpack=""
226 local img_property=""
227 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500228 *.tar.gz|*.tgz)
229 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500230 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
231 image_name="${image_fname%.tar.gz}" ||
232 image_name="${image_fname%.tgz}"
233 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500234 rm -Rf "$xdir";
235 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500236 tar -zxf $image -C "$xdir"
237 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400238 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500239 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400240 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500241 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400242 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500243 if [[ -z "$image_name" ]]; then
244 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500245 fi
246 ;;
247 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500248 image_name=$(basename "$image" ".img")
249 local format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500250 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500251 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500252 else
Dean Troyere9f76672014-07-25 11:09:36 -0500253 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500254 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500255 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500256 ;;
257 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500258 image_name=$(basename "$image" ".img.gz")
259 disk_format=raw
260 container_format=bare
261 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500262 ;;
263 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500264 image_name=$(basename "$image" ".qcow2")
265 disk_format=qcow2
266 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500267 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100268 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500269 image_name=$(basename "$image" ".iso")
270 disk_format=iso
271 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100272 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300273 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
274 local extension="${image_fname#*.}"
275 image_name=$(basename "$image" ".$extension")
276 disk_format=vhd
277 container_format=bare
278 if [ "${image_fname##*.}" == "gz" ]; then
279 unpack=zcat
280 fi
281 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500282 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500283 esac
284
Rafael Folcoab775872013-12-02 14:04:32 -0200285 if is_arch "ppc64"; then
Dean Troyere9f76672014-07-25 11:09:36 -0500286 img_property="--property hw_cdrom_bus=scsi"
Rafael Folcoab775872013-12-02 14:04:32 -0200287 fi
288
Dean Troyere9f76672014-07-25 11:09:36 -0500289 if [ "$container_format" = "bare" ]; then
290 if [ "$unpack" = "zcat" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400291 openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT 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 -0500292 else
Rob Crittenden18d47782014-03-19 17:47:42 -0400293 openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT image create "$image_name" $img_property --public --container-format=$container_format --disk-format $disk_format < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500294 fi
295 else
296 # Use glance client to add the kernel the root filesystem.
297 # We parse the results of the first upload to get the glance ID of the
298 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500299 local kernel_id="" ramdisk_id="";
300 if [ -n "$kernel" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400301 kernel_id=$(openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT 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 -0500302 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500303 if [ -n "$ramdisk" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400304 ramdisk_id=$(openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT 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 -0500305 fi
Rob Crittenden18d47782014-03-19 17:47:42 -0400306 openstack --os-token $token --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT 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 -0500307 fi
308}
309
Dean Troyer1a6d4492013-06-03 16:47:36 -0500310
Dean Troyerc1b486a2012-11-05 14:26:09 -0600311# Set the database backend to use
312# When called from stackrc/localrc DATABASE_BACKENDS has not been
313# initialized yet, just save the configuration selection and call back later
314# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100315#
316# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600317function use_database {
318 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600319 # No backends registered means this is likely called from ``localrc``
320 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600321 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000322 DEPRECATED_TEXT="$DEPRECATED_TEXT\nThe database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc\n"
Attila Fazekas251d3b52012-12-16 15:05:44 +0100323 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600324 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100325 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600326 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600327}
328
sridhargaddamb5ab6462015-02-24 07:23:24 +0000329#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
330CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500331
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600332# Wait for an HTTP server to start answering requests
333# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100334function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600335 local timeout=$1
336 local url=$2
sridhargaddamb5ab6462015-02-24 07:23:24 +0000337 timeout $timeout sh -c "while ! $CURL_GET -k --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600338}
339
Dean Troyer1a6d4492013-06-03 16:47:36 -0500340
Nachi Uenofda946e2012-10-24 17:26:02 -0700341# ping check
342# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500343# ping_check from-net ip boot-timeout expected
Ian Wienandaee18c72014-02-21 15:35:08 +1100344function ping_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400345 if is_service_enabled neutron; then
346 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700347 return
348 fi
349 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700350}
351
352# ping check for nova
353# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
Ian Wienandaee18c72014-02-21 15:35:08 +1100354function _ping_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700355 local from_net=$1
356 local ip=$2
357 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700358 local expected=${4:-"True"}
359 local check_command=""
Sean Dague53753292014-12-04 19:38:15 -0500360 MULTI_HOST=$(trueorfalse False MULTI_HOST)
Nachi Uenofda946e2012-10-24 17:26:02 -0700361 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700362 return
363 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700364 if [[ "$expected" = "True" ]]; then
365 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
366 else
367 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
368 fi
369 if ! timeout $boot_timeout sh -c "$check_command"; then
370 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800371 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700372 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800373 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700374 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700375 fi
376}
377
Nachi Ueno6769b162013-08-12 18:18:56 -0700378# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100379function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700380 local vm_id=$1
381 local network_name=$2
382 local nova_result="$(nova show $vm_id)"
383 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
384 if [[ $ip = "" ]];then
385 echo "$nova_result"
386 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700387 fi
388 echo $ip
389}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500390
Nachi Uenofda946e2012-10-24 17:26:02 -0700391# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700392
Dean Troyer1a6d4492013-06-03 16:47:36 -0500393# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100394function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400395 if is_service_enabled neutron; then
396 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700397 return
398 fi
399 _ssh_check_novanet "$1" $2 $3 $4 $5
400}
401
Ian Wienandaee18c72014-02-21 15:35:08 +1100402function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700403 local NET_NAME=$1
404 local KEY_FILE=$2
405 local FLOATING_IP=$3
406 local DEFAULT_INSTANCE_USER=$4
407 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600408 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500409 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 -0800410 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700411 fi
412}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500413
Vincent Untz856a11e2012-11-21 16:04:12 +0100414
Vincent Untz856a11e2012-11-21 16:04:12 +0100415# Get the location of the $module-rootwrap executables, where module is cinder
416# or nova.
417# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100418function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100419 local module=$1
420
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100421 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100422}
423
Dean Troyer1a6d4492013-06-03 16:47:36 -0500424
Ian Wienand0488edd2013-04-11 12:04:36 +1000425# Path permissions sanity check
426# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100427function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000428 # Ensure no element of the path has 0700 permissions, which is very
429 # likely to cause issues for daemons. Inspired by default 0700
430 # homedir permissions on RHEL and common practice of making DEST in
431 # the stack user's homedir.
432
433 local real_path=$(readlink -f $1)
434 local rebuilt_path=""
435 for i in $(echo ${real_path} | tr "/" " "); do
436 rebuilt_path=$rebuilt_path"/"$i
437
438 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
439 echo "*** DEST path element"
440 echo "*** ${rebuilt_path}"
441 echo "*** appears to have 0700 permissions."
442 echo "*** This is very likely to cause fatal issues for devstack daemons."
443
444 if [[ -n "$SKIP_PATH_SANITY" ]]; then
445 return
446 else
447 echo "*** Set SKIP_PATH_SANITY to skip this check"
448 die $LINENO "Invalid path permissions"
449 fi
450 fi
451 done
452}
453
Dean Troyer1a6d4492013-06-03 16:47:36 -0500454
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000455# This function recursively compares versions, and is not meant to be
456# called by anything other than vercmp_numbers below. This function does
457# not work with alphabetic versions.
458#
459# _vercmp_r sep ver1 ver2
460function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400461 typeset sep
462 typeset -a ver1=() ver2=()
463 sep=$1; shift
464 ver1=("${@:1:sep}")
465 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000466
Sean Dague537d4022013-10-22 07:43:22 -0400467 if ((ver1 > ver2)); then
468 echo 1; return 0
469 elif ((ver2 > ver1)); then
470 echo -1; return 0
471 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000472
Sean Dague537d4022013-10-22 07:43:22 -0400473 if ((sep <= 1)); then
474 echo 0; return 0
475 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000476
Sean Dague537d4022013-10-22 07:43:22 -0400477 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000478}
479
480
481# This function compares two versions and is meant to be called by
482# external callers. Please note the function assumes non-alphabetic
483# versions. For example, this will work:
484#
485# vercmp_numbers 1.10 1.4
486#
487# The above will return "1", as 1.10 is greater than 1.4.
488#
489# vercmp_numbers 5.2 6.4
490#
491# The above will return "-1", as 5.2 is less than 6.4.
492#
493# vercmp_numbers 4.0 4.0
494#
495# The above will return "0", as the versions are equal.
496#
497# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100498function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400499 typeset v1=$1 v2=$2 sep
500 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000501
Sean Dague537d4022013-10-22 07:43:22 -0400502 IFS=. read -ra ver1 <<< "$v1"
503 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000504
Sean Dague537d4022013-10-22 07:43:22 -0400505 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000506}
507
508
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700509# This function sets log formatting options for colorizing log
510# output to stdout. It is meant to be called by lib modules.
511# The last two parameters are optional and can be used to specify
512# non-default value for project and user format variables.
513# Defaults are respectively 'project_name' and 'user_name'
514#
515# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100516function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700517 local conf_file=$1
518 local conf_section=$2
519 local project_var=${3:-"project_name"}
520 local user_var=${4:-"user_name"}
521 # Add color to logging output
522 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"
523 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"
524 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
525 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
526}
527
Ian Wienand54e39102014-06-03 16:05:12 +1000528# These functions are provided for basic fall-back functionality for
529# projects that include parts of devstack (grenade). stack.sh will
530# override these with more specific versions for devstack (with fancy
531# spinners, etc). We never override an existing version
532if ! function_exists echo_summary; then
533 function echo_summary {
534 echo $@
535 }
536fi
537if ! function_exists echo_nolog; then
538 function echo_nolog {
539 echo $@
540 }
541fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600542
Sébastien Han36f2f022014-01-06 18:09:26 +0100543
544# create_disk - Create backing disk
545function create_disk {
546 local node_number
547 local disk_image=${1}
548 local storage_data_dir=${2}
549 local loopback_disk_size=${3}
550
551 # Create a loopback disk and format it to XFS.
552 if [[ -e ${disk_image} ]]; then
553 if egrep -q ${storage_data_dir} /proc/mounts; then
554 sudo umount ${storage_data_dir}/drives/sdb1
555 sudo rm -f ${disk_image}
556 fi
557 fi
558
559 sudo mkdir -p ${storage_data_dir}/drives/images
560
561 sudo truncate -s ${loopback_disk_size} ${disk_image}
562
563 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
564 # a single inode. Keeping the default inode size (256) will result in multiple
565 # inodes being used to store xattr. Retrieving the xattr will be slower
566 # since we have to read multiple inodes. This statement is true for both
567 # Swift and Ceph.
568 sudo mkfs.xfs -f -i size=1024 ${disk_image}
569
570 # Mount the disk with mount options to make it as efficient as possible
571 if ! egrep -q ${storage_data_dir} /proc/mounts; then
572 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
573 ${disk_image} ${storage_data_dir}
574 fi
575}
576
Dean Troyer27e32692012-03-16 16:16:56 -0500577# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000578$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500579
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500580# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400581# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400582# End: