blob: 79b2b37b4eae555ea3f143fe0364b191aeb04138 [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 Troyer490430d2015-01-30 14:38:35 -060016source ${FUNC_DIR}/inc/python
Dean Troyer7f9aa712012-01-31 12:11:56 -060017
Dean Troyer27e32692012-03-16 16:16:56 -050018# Save trace setting
19XTRACE=$(set +o | grep xtrace)
20set +o xtrace
21
Ian Wienand54e39102014-06-03 16:05:12 +100022# Check if a function already exists
23function function_exists {
24 declare -f -F $1 > /dev/null
25}
Dean Troyer7f9aa712012-01-31 12:11:56 -060026
Adam Spierscb961592013-10-05 12:11:07 +010027# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050028# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010029#
30# - ``FILES`` must be set to the cache dir
31# - ``GLANCE_HOSTPORT``
32#
Dean Troyerca0e3d02012-04-13 15:58:37 -050033# upload_image image-url glance-token
Ian Wienandaee18c72014-02-21 15:35:08 +110034function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050035 local image_url=$1
36 local token=$2
37
Dean Troyere9f76672014-07-25 11:09:36 -050038 local image image_fname image_name
39
Dean Troyerca0e3d02012-04-13 15:58:37 -050040 # Create a directory for the downloaded image tarballs.
41 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050042 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080043 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080044 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050045 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +010046 wget --progress=dot:giga -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090047 if [[ $? -ne 0 ]]; then
48 echo "Not found: $image_url"
49 return
50 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080051 fi
Dean Troyere9f76672014-07-25 11:09:36 -050052 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -080053 else
Dean Troyer3324f192014-09-18 09:26:39 -050054 # File based URL (RFC 1738): ``file://host/path``
Arnaud Legendre3e439442013-11-15 16:06:03 -080055 # Remote files are not considered here.
Dean Troyer3324f192014-09-18 09:26:39 -050056 # unix: ``file:///home/user/path/file``
57 # windows: ``file:///C:/Documents%20and%20Settings/user/path/file``
Dean Troyere9f76672014-07-25 11:09:36 -050058 image=$(echo $image_url | sed "s/^file:\/\///g")
59 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050060 echo "Not found: $image_url"
61 return
62 fi
63 fi
64
65 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
66 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050067 image_name="${image_fname%.tar.gz}"
Rob Crittenden18d47782014-03-19 17:47:42 -040068 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 -050069 return
70 fi
71
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070072 # vmdk format images
73 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050074 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070075
76 # Before we can upload vmdk type images to glance, we need to know it's
77 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080078 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070079 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070080 # vmdk filename, which is expected in the following format:
81 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080082 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -070083 #
84 # If the filename does not follow the above format then the vsphere
85 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070086
Dean Troyere9f76672014-07-25 11:09:36 -050087 local vmdk_disktype=""
Sabari Kumar Murugesan88cde0b2014-12-04 17:48:26 -080088 local vmdk_net_adapter="e1000"
Dean Troyere9f76672014-07-25 11:09:36 -050089 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080090
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070091 # vmdk adapter type
Dean Troyere9f76672014-07-25 11:09:36 -050092 local vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070093 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
94 vmdk_adapter_type="${vmdk_adapter_type%?}"
95
96 # vmdk disk type
Dean Troyere9f76672014-07-25 11:09:36 -050097 local vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070098 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -080099 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800100
101 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900102 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700103 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
104 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500105 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Dean Troyer3324f192014-09-18 09:26:39 -0500106 # Attempt to retrieve the ``*-flat.vmdk``
Dean Troyere9f76672014-07-25 11:09:36 -0500107 local flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800108 flat_fname="${flat_fname#*\"}"
109 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700110 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500111 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800112 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500113 path_len=`expr ${#image_url} - ${#image_fname}`
114 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800115 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900116 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800117 if [[ $flat_url != file* ]]; then
118 if [[ ! -f $FILES/$flat_fname || \
119 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
Attila Fazekas057d6ae2015-01-13 14:01:26 +0100120 wget --progress=dot:giga -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800121 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500122 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800123 else
Dean Troyere9f76672014-07-25 11:09:36 -0500124 image=$(echo $flat_url | sed "s/^file:\/\///g")
125 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800126 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700127 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800128 fi
129 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500130 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800131 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800132 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
133 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800134 elif [[ -z "$vmdk_create_type" ]]; then
135 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
136 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500137 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800138 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500139 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700140 else
Dean Troyere9f76672014-07-25 11:09:36 -0500141 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
142 path_len=`expr ${#image_url} - ${#image_fname}`
143 local flat_path="${image_url:0:$path_len}"
144 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700145 warn $LINENO "$descriptor_data_pair_msg"`
146 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
147 if [[ $flat_path != file* ]]; then
148 if [[ ! -f $FILES/$descriptor_fname || \
149 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
150 wget -c $descriptor_url -O $FILES/$descriptor_fname
151 fi
152 descriptor_url="$FILES/$descriptor_fname"
153 else
154 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
155 if [[ ! -f $descriptor_url || \
156 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
157 echo "Descriptor not found: $descriptor_url"
158 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800159 fi
160 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800161 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
162 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
163 vmdk_adapter_type="${vmdk_adapter_type%?}"
164 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900165 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700166 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700167 vmdk_disktype="preallocated"
168 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800169
170 # NOTE: For backwards compatibility reasons, colons may be used in place
171 # of semi-colons for property delimiters but they are not permitted
172 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500173 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800174 IFS=':;' read -a props <<< "$property_string"
175 vmdk_disktype="${props[0]:-$vmdk_disktype}"
176 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
177 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700178
Rob Crittenden18d47782014-03-19 17:47:42 -0400179 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 -0700180 return
181 fi
182
Mate Lakatbc2ef922013-08-15 18:06:59 +0100183 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500184 # and should not be decompressed prior to loading
185 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500186 image_name="${image_fname%.vhd.tgz}"
187 local force_vm_mode=""
188 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000189 # Cirros VHD image currently only boots in PV mode.
190 # Nova defaults to PV for all VHD images, but
191 # the glance setting is needed for booting
192 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500193 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000194 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400195 openstack \
196 --os-token $token \
Rob Crittenden18d47782014-03-19 17:47:42 -0400197 --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400198 image create \
199 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000200 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500201 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500202 return
203 fi
204
Mate Lakatbc2ef922013-08-15 18:06:59 +0100205 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
206 # and should not be decompressed prior to loading.
207 # Setting metadata, so PV mode is used.
208 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500209 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400210 openstack \
211 --os-token $token \
Rob Crittenden18d47782014-03-19 17:47:42 -0400212 --os-url $GLANCE_SERVICE_PROTOCOL://$GLANCE_HOSTPORT \
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400213 image create \
214 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100215 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500216 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100217 return
218 fi
219
Dean Troyere9f76672014-07-25 11:09:36 -0500220 local kernel=""
221 local ramdisk=""
222 local disk_format=""
223 local container_format=""
224 local unpack=""
225 local img_property=""
226 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500227 *.tar.gz|*.tgz)
228 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500229 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
230 image_name="${image_fname%.tar.gz}" ||
231 image_name="${image_fname%.tgz}"
232 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500233 rm -Rf "$xdir";
234 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500235 tar -zxf $image -C "$xdir"
236 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400237 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500238 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400239 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500240 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400241 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500242 if [[ -z "$image_name" ]]; then
243 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500244 fi
245 ;;
246 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500247 image_name=$(basename "$image" ".img")
248 local format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }')
Dean Troyer636a3ff2012-09-14 11:36:07 -0500249 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500250 disk_format=$format
Dean Troyer636a3ff2012-09-14 11:36:07 -0500251 else
Dean Troyere9f76672014-07-25 11:09:36 -0500252 disk_format=raw
Dean Troyer636a3ff2012-09-14 11:36:07 -0500253 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500254 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500255 ;;
256 *.img.gz)
Dean Troyere9f76672014-07-25 11:09:36 -0500257 image_name=$(basename "$image" ".img.gz")
258 disk_format=raw
259 container_format=bare
260 unpack=zcat
Dean Troyerca0e3d02012-04-13 15:58:37 -0500261 ;;
262 *.qcow2)
Dean Troyere9f76672014-07-25 11:09:36 -0500263 image_name=$(basename "$image" ".qcow2")
264 disk_format=qcow2
265 container_format=bare
Dean Troyerca0e3d02012-04-13 15:58:37 -0500266 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100267 *.iso)
Dean Troyere9f76672014-07-25 11:09:36 -0500268 image_name=$(basename "$image" ".iso")
269 disk_format=iso
270 container_format=bare
Jonathan Michalon06802042013-03-21 14:29:58 +0100271 ;;
Alessandro Pilottica823942014-08-07 02:05:26 +0300272 *.vhd|*.vhdx|*.vhd.gz|*.vhdx.gz)
273 local extension="${image_fname#*.}"
274 image_name=$(basename "$image" ".$extension")
275 disk_format=vhd
276 container_format=bare
277 if [ "${image_fname##*.}" == "gz" ]; then
278 unpack=zcat
279 fi
280 ;;
Dean Troyere9f76672014-07-25 11:09:36 -0500281 *) echo "Do not know what to do with $image_fname"; false;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500282 esac
283
Rafael Folcoab775872013-12-02 14:04:32 -0200284 if is_arch "ppc64"; then
Dean Troyere9f76672014-07-25 11:09:36 -0500285 img_property="--property hw_cdrom_bus=scsi"
Rafael Folcoab775872013-12-02 14:04:32 -0200286 fi
287
Dean Troyere9f76672014-07-25 11:09:36 -0500288 if [ "$container_format" = "bare" ]; then
289 if [ "$unpack" = "zcat" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400290 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 -0500291 else
Rob Crittenden18d47782014-03-19 17:47:42 -0400292 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 -0500293 fi
294 else
295 # Use glance client to add the kernel the root filesystem.
296 # We parse the results of the first upload to get the glance ID of the
297 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500298 local kernel_id="" ramdisk_id="";
299 if [ -n "$kernel" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400300 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 -0500301 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500302 if [ -n "$ramdisk" ]; then
Rob Crittenden18d47782014-03-19 17:47:42 -0400303 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 -0500304 fi
Rob Crittenden18d47782014-03-19 17:47:42 -0400305 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 -0500306 fi
307}
308
Dean Troyer1a6d4492013-06-03 16:47:36 -0500309
Dean Troyerc1b486a2012-11-05 14:26:09 -0600310# Set the database backend to use
311# When called from stackrc/localrc DATABASE_BACKENDS has not been
312# initialized yet, just save the configuration selection and call back later
313# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100314#
315# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600316function use_database {
317 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600318 # No backends registered means this is likely called from ``localrc``
319 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600320 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000321 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 +0100322 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600323 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100324 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600325 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600326}
327
sridhargaddamb5ab6462015-02-24 07:23:24 +0000328#Macro for curl statements. curl requires -g option for literal IPv6 addresses.
329CURL_GET="${CURL_GET:-curl -g}"
Dean Troyer1a6d4492013-06-03 16:47:36 -0500330
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600331# Wait for an HTTP server to start answering requests
332# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100333function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600334 local timeout=$1
335 local url=$2
sridhargaddamb5ab6462015-02-24 07:23:24 +0000336 timeout $timeout sh -c "while ! $CURL_GET -k --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600337}
338
Dean Troyer1a6d4492013-06-03 16:47:36 -0500339
Nachi Uenofda946e2012-10-24 17:26:02 -0700340# ping check
341# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500342# ping_check from-net ip boot-timeout expected
Ian Wienandaee18c72014-02-21 15:35:08 +1100343function ping_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400344 if is_service_enabled neutron; then
345 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700346 return
347 fi
348 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700349}
350
351# ping check for nova
352# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
Ian Wienandaee18c72014-02-21 15:35:08 +1100353function _ping_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700354 local from_net=$1
355 local ip=$2
356 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700357 local expected=${4:-"True"}
358 local check_command=""
Sean Dague53753292014-12-04 19:38:15 -0500359 MULTI_HOST=$(trueorfalse False MULTI_HOST)
Nachi Uenofda946e2012-10-24 17:26:02 -0700360 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700361 return
362 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700363 if [[ "$expected" = "True" ]]; then
364 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
365 else
366 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
367 fi
368 if ! timeout $boot_timeout sh -c "$check_command"; then
369 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800370 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700371 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800372 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700373 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700374 fi
375}
376
Nachi Ueno6769b162013-08-12 18:18:56 -0700377# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100378function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700379 local vm_id=$1
380 local network_name=$2
381 local nova_result="$(nova show $vm_id)"
382 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
383 if [[ $ip = "" ]];then
384 echo "$nova_result"
385 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700386 fi
387 echo $ip
388}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500389
Nachi Uenofda946e2012-10-24 17:26:02 -0700390# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700391
Dean Troyer1a6d4492013-06-03 16:47:36 -0500392# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100393function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400394 if is_service_enabled neutron; then
395 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700396 return
397 fi
398 _ssh_check_novanet "$1" $2 $3 $4 $5
399}
400
Ian Wienandaee18c72014-02-21 15:35:08 +1100401function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700402 local NET_NAME=$1
403 local KEY_FILE=$2
404 local FLOATING_IP=$3
405 local DEFAULT_INSTANCE_USER=$4
406 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600407 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500408 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 -0800409 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700410 fi
411}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500412
Vincent Untz856a11e2012-11-21 16:04:12 +0100413
Vincent Untz856a11e2012-11-21 16:04:12 +0100414# Get the location of the $module-rootwrap executables, where module is cinder
415# or nova.
416# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100417function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100418 local module=$1
419
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100420 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100421}
422
Dean Troyer1a6d4492013-06-03 16:47:36 -0500423
Ian Wienand0488edd2013-04-11 12:04:36 +1000424# Path permissions sanity check
425# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100426function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000427 # Ensure no element of the path has 0700 permissions, which is very
428 # likely to cause issues for daemons. Inspired by default 0700
429 # homedir permissions on RHEL and common practice of making DEST in
430 # the stack user's homedir.
431
432 local real_path=$(readlink -f $1)
433 local rebuilt_path=""
434 for i in $(echo ${real_path} | tr "/" " "); do
435 rebuilt_path=$rebuilt_path"/"$i
436
437 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
438 echo "*** DEST path element"
439 echo "*** ${rebuilt_path}"
440 echo "*** appears to have 0700 permissions."
441 echo "*** This is very likely to cause fatal issues for devstack daemons."
442
443 if [[ -n "$SKIP_PATH_SANITY" ]]; then
444 return
445 else
446 echo "*** Set SKIP_PATH_SANITY to skip this check"
447 die $LINENO "Invalid path permissions"
448 fi
449 fi
450 done
451}
452
Dean Troyer1a6d4492013-06-03 16:47:36 -0500453
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000454# This function recursively compares versions, and is not meant to be
455# called by anything other than vercmp_numbers below. This function does
456# not work with alphabetic versions.
457#
458# _vercmp_r sep ver1 ver2
459function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400460 typeset sep
461 typeset -a ver1=() ver2=()
462 sep=$1; shift
463 ver1=("${@:1:sep}")
464 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000465
Sean Dague537d4022013-10-22 07:43:22 -0400466 if ((ver1 > ver2)); then
467 echo 1; return 0
468 elif ((ver2 > ver1)); then
469 echo -1; return 0
470 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000471
Sean Dague537d4022013-10-22 07:43:22 -0400472 if ((sep <= 1)); then
473 echo 0; return 0
474 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000475
Sean Dague537d4022013-10-22 07:43:22 -0400476 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000477}
478
479
480# This function compares two versions and is meant to be called by
481# external callers. Please note the function assumes non-alphabetic
482# versions. For example, this will work:
483#
484# vercmp_numbers 1.10 1.4
485#
486# The above will return "1", as 1.10 is greater than 1.4.
487#
488# vercmp_numbers 5.2 6.4
489#
490# The above will return "-1", as 5.2 is less than 6.4.
491#
492# vercmp_numbers 4.0 4.0
493#
494# The above will return "0", as the versions are equal.
495#
496# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100497function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400498 typeset v1=$1 v2=$2 sep
499 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000500
Sean Dague537d4022013-10-22 07:43:22 -0400501 IFS=. read -ra ver1 <<< "$v1"
502 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000503
Sean Dague537d4022013-10-22 07:43:22 -0400504 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000505}
506
507
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700508# This function sets log formatting options for colorizing log
509# output to stdout. It is meant to be called by lib modules.
510# The last two parameters are optional and can be used to specify
511# non-default value for project and user format variables.
512# Defaults are respectively 'project_name' and 'user_name'
513#
514# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100515function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700516 local conf_file=$1
517 local conf_section=$2
518 local project_var=${3:-"project_name"}
519 local user_var=${4:-"user_name"}
520 # Add color to logging output
521 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"
522 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"
523 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
524 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
525}
526
Ian Wienand54e39102014-06-03 16:05:12 +1000527# These functions are provided for basic fall-back functionality for
528# projects that include parts of devstack (grenade). stack.sh will
529# override these with more specific versions for devstack (with fancy
530# spinners, etc). We never override an existing version
531if ! function_exists echo_summary; then
532 function echo_summary {
533 echo $@
534 }
535fi
536if ! function_exists echo_nolog; then
537 function echo_nolog {
538 echo $@
539 }
540fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600541
Sébastien Han36f2f022014-01-06 18:09:26 +0100542
543# create_disk - Create backing disk
544function create_disk {
545 local node_number
546 local disk_image=${1}
547 local storage_data_dir=${2}
548 local loopback_disk_size=${3}
549
550 # Create a loopback disk and format it to XFS.
551 if [[ -e ${disk_image} ]]; then
552 if egrep -q ${storage_data_dir} /proc/mounts; then
553 sudo umount ${storage_data_dir}/drives/sdb1
554 sudo rm -f ${disk_image}
555 fi
556 fi
557
558 sudo mkdir -p ${storage_data_dir}/drives/images
559
560 sudo truncate -s ${loopback_disk_size} ${disk_image}
561
562 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
563 # a single inode. Keeping the default inode size (256) will result in multiple
564 # inodes being used to store xattr. Retrieving the xattr will be slower
565 # since we have to read multiple inodes. This statement is true for both
566 # Swift and Ceph.
567 sudo mkfs.xfs -f -i size=1024 ${disk_image}
568
569 # Mount the disk with mount options to make it as efficient as possible
570 if ! egrep -q ${storage_data_dir} /proc/mounts; then
571 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
572 ${disk_image} ${storage_data_dir}
573 fi
574}
575
Dean Troyer27e32692012-03-16 16:16:56 -0500576# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000577$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500578
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500579# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400580# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400581# End: