blob: 76f704792e37a3a5cee790f99f69ad941bd9fbfb [file] [log] [blame]
Dean Troyerdff49a22014-01-30 15:37:40 -06001# functions - DevStack-specific functions
Dean Troyer13dc5cc2012-03-27 14:50:45 -05002#
Dean Troyer4a43b7b2012-08-28 17:43:40 -05003# The following variables are assumed to be defined by certain functions:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01004#
Dean Troyerd8864fe2014-02-17 11:00:42 -06005# - ``DATABASE_BACKENDS``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006# - ``ENABLED_SERVICES``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007# - ``FILES``
8# - ``GLANCE_HOSTPORT``
Dean Troyerd8864fe2014-02-17 11:00:42 -06009#
Dean Troyer13dc5cc2012-03-27 14:50:45 -050010
Dean Troyerdff49a22014-01-30 15:37:40 -060011# Include the common functions
12FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
13source ${FUNC_DIR}/functions-common
Dean Troyer7f9aa712012-01-31 12:11:56 -060014
Dean Troyer27e32692012-03-16 16:16:56 -050015# Save trace setting
16XTRACE=$(set +o | grep xtrace)
17set +o xtrace
18
Ian Wienand54e39102014-06-03 16:05:12 +100019# Check if a function already exists
20function function_exists {
21 declare -f -F $1 > /dev/null
22}
Dean Troyer7f9aa712012-01-31 12:11:56 -060023
Chris Buccella610af8c2013-11-05 12:56:34 +000024# Checks if installed Apache is <= given version
25# $1 = x.y.z (version string of Apache)
26function check_apache_version {
27 local cmd="apachectl"
28 if ! [[ -x $(which apachectl 2>/dev/null) ]]; then
29 cmd="/usr/sbin/apachectl"
30 fi
31
32 local version=$($cmd -v | grep version | grep -Po 'Apache/\K[^ ]*')
33 expr "$version" '>=' $1 > /dev/null
34}
35
Dean Troyer13dc5cc2012-03-27 14:50:45 -050036
Ian Wienand31dcd3e2013-07-16 13:36:34 +100037# Cleanup anything from /tmp on unstack
38# clean_tmp
39function cleanup_tmp {
40 local tmp_dir=${TMPDIR:-/tmp}
41
42 # see comments in pip_install
43 sudo rm -rf ${tmp_dir}/pip-build.*
44}
45
Dean Troyer1a6d4492013-06-03 16:47:36 -050046
Adam Spierscb961592013-10-05 12:11:07 +010047# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050048# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010049#
50# - ``FILES`` must be set to the cache dir
51# - ``GLANCE_HOSTPORT``
52#
Dean Troyerca0e3d02012-04-13 15:58:37 -050053# upload_image image-url glance-token
Ian Wienandaee18c72014-02-21 15:35:08 +110054function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050055 local image_url=$1
56 local token=$2
57
Dean Troyere9f76672014-07-25 11:09:36 -050058 local image image_fname image_name
59
Dean Troyerca0e3d02012-04-13 15:58:37 -050060 # Create a directory for the downloaded image tarballs.
61 mkdir -p $FILES/images
Dean Troyere9f76672014-07-25 11:09:36 -050062 image_fname=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080063 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080064 # Downloads the image (uec ami+akistyle), then extracts it.
Dean Troyere9f76672014-07-25 11:09:36 -050065 if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then
66 wget -c $image_url -O $FILES/$image_fname
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090067 if [[ $? -ne 0 ]]; then
68 echo "Not found: $image_url"
69 return
70 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080071 fi
Dean Troyere9f76672014-07-25 11:09:36 -050072 image="$FILES/${image_fname}"
Arnaud Legendre3e439442013-11-15 16:06:03 -080073 else
74 # File based URL (RFC 1738): file://host/path
75 # Remote files are not considered here.
76 # *nix: file:///home/user/path/file
77 # windows: file:///C:/Documents%20and%20Settings/user/path/file
Dean Troyere9f76672014-07-25 11:09:36 -050078 image=$(echo $image_url | sed "s/^file:\/\///g")
79 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050080 echo "Not found: $image_url"
81 return
82 fi
83 fi
84
85 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
86 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050087 image_name="${image_fname%.tar.gz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -040088 openstack --os-token $token --os-url http://$GLANCE_HOSTPORT image create "$image_name" --public --container-format ami --disk-format ami < "${image}"
Dean Troyerca0e3d02012-04-13 15:58:37 -050089 return
90 fi
91
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070092 # vmdk format images
93 if [[ "$image_url" =~ '.vmdk' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -050094 image_name="${image_fname%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070095
96 # Before we can upload vmdk type images to glance, we need to know it's
97 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080098 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070099 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700100 # vmdk filename, which is expected in the following format:
101 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800102 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700103 #
104 # If the filename does not follow the above format then the vsphere
105 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700106
Dean Troyere9f76672014-07-25 11:09:36 -0500107 local vmdk_disktype=""
108 local vmdk_net_adapter=""
109 local path_len
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800110
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700111 # vmdk adapter type
Dean Troyere9f76672014-07-25 11:09:36 -0500112 local vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700113 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
114 vmdk_adapter_type="${vmdk_adapter_type%?}"
115
116 # vmdk disk type
Dean Troyere9f76672014-07-25 11:09:36 -0500117 local vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700118 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800119 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800120
121 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900122 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700123 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
124 vmdk_disktype="sparse"
Dean Troyere9f76672014-07-25 11:09:36 -0500125 elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800126 # Attempt to retrieve the *-flat.vmdk
Dean Troyere9f76672014-07-25 11:09:36 -0500127 local flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800128 flat_fname="${flat_fname#*\"}"
129 flat_fname="${flat_fname%?}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700130 if [[ -z "$flat_fname" ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500131 flat_fname="$image_name-flat.vmdk"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800132 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500133 path_len=`expr ${#image_url} - ${#image_fname}`
134 local flat_url="${image_url:0:$path_len}$flat_fname"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800135 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900136 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800137 if [[ $flat_url != file* ]]; then
138 if [[ ! -f $FILES/$flat_fname || \
139 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
140 wget -c $flat_url -O $FILES/$flat_fname
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800141 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500142 image="$FILES/${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800143 else
Dean Troyere9f76672014-07-25 11:09:36 -0500144 image=$(echo $flat_url | sed "s/^file:\/\///g")
145 if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800146 echo "Flat disk not found: $flat_url"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700147 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800148 fi
149 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500150 image_name="${flat_fname}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800151 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800152 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
153 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800154 elif [[ -z "$vmdk_create_type" ]]; then
155 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
156 # to retrieve appropriate metadata
Dean Troyere9f76672014-07-25 11:09:36 -0500157 if [[ ${image_name: -5} != "-flat" ]]; then
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800158 warn $LINENO "Expected filename suffix: '-flat'."`
Dean Troyere9f76672014-07-25 11:09:36 -0500159 `" Filename provided: ${image_name}"
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700160 else
Dean Troyere9f76672014-07-25 11:09:36 -0500161 descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk"
162 path_len=`expr ${#image_url} - ${#image_fname}`
163 local flat_path="${image_url:0:$path_len}"
164 local descriptor_url=$flat_path$descriptor_fname
Sreeram Yerrapragada9c6d2842014-03-10 14:12:58 -0700165 warn $LINENO "$descriptor_data_pair_msg"`
166 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
167 if [[ $flat_path != file* ]]; then
168 if [[ ! -f $FILES/$descriptor_fname || \
169 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
170 wget -c $descriptor_url -O $FILES/$descriptor_fname
171 fi
172 descriptor_url="$FILES/$descriptor_fname"
173 else
174 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
175 if [[ ! -f $descriptor_url || \
176 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
177 echo "Descriptor not found: $descriptor_url"
178 return 1
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800179 fi
180 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800181 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
182 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
183 vmdk_adapter_type="${vmdk_adapter_type%?}"
184 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900185 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700186 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700187 vmdk_disktype="preallocated"
188 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800189
190 # NOTE: For backwards compatibility reasons, colons may be used in place
191 # of semi-colons for property delimiters but they are not permitted
192 # characters in NTFS filesystems.
Dean Troyere9f76672014-07-25 11:09:36 -0500193 property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800194 IFS=':;' read -a props <<< "$property_string"
195 vmdk_disktype="${props[0]:-$vmdk_disktype}"
196 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
197 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700198
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400199 openstack --os-token $token --os-url http://$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 -0700200 return
201 fi
202
Mate Lakatbc2ef922013-08-15 18:06:59 +0100203 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500204 # and should not be decompressed prior to loading
205 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500206 image_name="${image_fname%.vhd.tgz}"
207 local force_vm_mode=""
208 if [[ "$image_name" =~ 'cirros' ]]; then
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000209 # Cirros VHD image currently only boots in PV mode.
210 # Nova defaults to PV for all VHD images, but
211 # the glance setting is needed for booting
212 # directly from volume.
Dean Troyere9f76672014-07-25 11:09:36 -0500213 force_vm_mode="--property vm_mode=xen"
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000214 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400215 openstack \
216 --os-token $token \
217 --os-url http://$GLANCE_HOSTPORT \
218 image create \
219 "$image_name" --public \
Bob Ballf1a2dbf2014-03-19 11:08:54 +0000220 --container-format=ovf --disk-format=vhd \
Dean Troyere9f76672014-07-25 11:09:36 -0500221 $force_vm_mode < "${image}"
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500222 return
223 fi
224
Mate Lakatbc2ef922013-08-15 18:06:59 +0100225 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
226 # and should not be decompressed prior to loading.
227 # Setting metadata, so PV mode is used.
228 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Dean Troyere9f76672014-07-25 11:09:36 -0500229 image_name="${image_fname%.xen-raw.tgz}"
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400230 openstack \
231 --os-token $token \
232 --os-url http://$GLANCE_HOSTPORT \
233 image create \
234 "$image_name" --public \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100235 --container-format=tgz --disk-format=raw \
Dean Troyere9f76672014-07-25 11:09:36 -0500236 --property vm_mode=xen < "${image}"
Mate Lakatbc2ef922013-08-15 18:06:59 +0100237 return
238 fi
239
Dean Troyere9f76672014-07-25 11:09:36 -0500240 local kernel=""
241 local ramdisk=""
242 local disk_format=""
243 local container_format=""
244 local unpack=""
245 local img_property=""
246 case "$image_fname" in
Dean Troyerca0e3d02012-04-13 15:58:37 -0500247 *.tar.gz|*.tgz)
248 # Extract ami and aki files
Dean Troyere9f76672014-07-25 11:09:36 -0500249 [ "${image_fname%.tar.gz}" != "$image_fname" ] &&
250 image_name="${image_fname%.tar.gz}" ||
251 image_name="${image_fname%.tgz}"
252 local xdir="$FILES/images/$image_name"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500253 rm -Rf "$xdir";
254 mkdir "$xdir"
Dean Troyere9f76672014-07-25 11:09:36 -0500255 tar -zxf $image -C "$xdir"
256 kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400257 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500258 ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400259 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500260 image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400261 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyere9f76672014-07-25 11:09:36 -0500262 if [[ -z "$image_name" ]]; then
263 image_name=$(basename "$image" ".img")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500264 fi
265 ;;
266 *.img)
Dean Troyere9f76672014-07-25 11:09:36 -0500267 image_name=$(basename "$image" ".img")
268 local 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
Dean Troyere9f76672014-07-25 11:09:36 -0500308 if [ "$container_format" = "bare" ]; then
309 if [ "$unpack" = "zcat" ]; then
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400310 openstack --os-token $token --os-url http://$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 -0500311 else
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400312 openstack --os-token $token --os-url http://$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 -0500313 fi
314 else
315 # Use glance client to add the kernel the root filesystem.
316 # We parse the results of the first upload to get the glance ID of the
317 # kernel for use when uploading the root filesystem.
Dean Troyere9f76672014-07-25 11:09:36 -0500318 local kernel_id="" ramdisk_id="";
319 if [ -n "$kernel" ]; then
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400320 kernel_id=$(openstack --os-token $token --os-url http://$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 -0500321 fi
Dean Troyere9f76672014-07-25 11:09:36 -0500322 if [ -n "$ramdisk" ]; then
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400323 ramdisk_id=$(openstack --os-token $token --os-url http://$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 -0500324 fi
Steve Martinelli8d3ac2d2014-08-02 23:47:15 -0400325 openstack --os-token $token --os-url http://$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 -0500326 fi
327}
328
Dean Troyer1a6d4492013-06-03 16:47:36 -0500329
Dean Troyerc1b486a2012-11-05 14:26:09 -0600330# Set the database backend to use
331# When called from stackrc/localrc DATABASE_BACKENDS has not been
332# initialized yet, just save the configuration selection and call back later
333# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100334#
335# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600336function use_database {
337 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600338 # No backends registered means this is likely called from ``localrc``
339 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600340 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000341 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 +0100342 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600343 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100344 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600345 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600346}
347
Dean Troyer1a6d4492013-06-03 16:47:36 -0500348
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600349# Wait for an HTTP server to start answering requests
350# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100351function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600352 local timeout=$1
353 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800354 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600355}
356
Dean Troyer1a6d4492013-06-03 16:47:36 -0500357
Nachi Uenofda946e2012-10-24 17:26:02 -0700358# ping check
359# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500360# ping_check from-net ip boot-timeout expected
Ian Wienandaee18c72014-02-21 15:35:08 +1100361function ping_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400362 if is_service_enabled neutron; then
363 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700364 return
365 fi
366 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700367}
368
369# ping check for nova
370# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
Ian Wienandaee18c72014-02-21 15:35:08 +1100371function _ping_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700372 local from_net=$1
373 local ip=$2
374 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700375 local expected=${4:-"True"}
376 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700377 MULTI_HOST=`trueorfalse False $MULTI_HOST`
378 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700379 return
380 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700381 if [[ "$expected" = "True" ]]; then
382 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
383 else
384 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
385 fi
386 if ! timeout $boot_timeout sh -c "$check_command"; then
387 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800388 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700389 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800390 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700391 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700392 fi
393}
394
Nachi Ueno6769b162013-08-12 18:18:56 -0700395# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100396function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700397 local vm_id=$1
398 local network_name=$2
399 local nova_result="$(nova show $vm_id)"
400 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
401 if [[ $ip = "" ]];then
402 echo "$nova_result"
403 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700404 fi
405 echo $ip
406}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500407
Nachi Uenofda946e2012-10-24 17:26:02 -0700408# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700409
Dean Troyer1a6d4492013-06-03 16:47:36 -0500410# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100411function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400412 if is_service_enabled neutron; then
413 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700414 return
415 fi
416 _ssh_check_novanet "$1" $2 $3 $4 $5
417}
418
Ian Wienandaee18c72014-02-21 15:35:08 +1100419function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700420 local NET_NAME=$1
421 local KEY_FILE=$2
422 local FLOATING_IP=$3
423 local DEFAULT_INSTANCE_USER=$4
424 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600425 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500426 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 -0800427 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700428 fi
429}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500430
Vincent Untz856a11e2012-11-21 16:04:12 +0100431
Vincent Untz856a11e2012-11-21 16:04:12 +0100432# Get the location of the $module-rootwrap executables, where module is cinder
433# or nova.
434# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100435function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100436 local module=$1
437
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100438 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100439}
440
Dean Troyer1a6d4492013-06-03 16:47:36 -0500441
Ian Wienand0488edd2013-04-11 12:04:36 +1000442# Path permissions sanity check
443# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100444function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000445 # Ensure no element of the path has 0700 permissions, which is very
446 # likely to cause issues for daemons. Inspired by default 0700
447 # homedir permissions on RHEL and common practice of making DEST in
448 # the stack user's homedir.
449
450 local real_path=$(readlink -f $1)
451 local rebuilt_path=""
452 for i in $(echo ${real_path} | tr "/" " "); do
453 rebuilt_path=$rebuilt_path"/"$i
454
455 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
456 echo "*** DEST path element"
457 echo "*** ${rebuilt_path}"
458 echo "*** appears to have 0700 permissions."
459 echo "*** This is very likely to cause fatal issues for devstack daemons."
460
461 if [[ -n "$SKIP_PATH_SANITY" ]]; then
462 return
463 else
464 echo "*** Set SKIP_PATH_SANITY to skip this check"
465 die $LINENO "Invalid path permissions"
466 fi
467 fi
468 done
469}
470
Dean Troyer1a6d4492013-06-03 16:47:36 -0500471
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000472# This function recursively compares versions, and is not meant to be
473# called by anything other than vercmp_numbers below. This function does
474# not work with alphabetic versions.
475#
476# _vercmp_r sep ver1 ver2
477function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400478 typeset sep
479 typeset -a ver1=() ver2=()
480 sep=$1; shift
481 ver1=("${@:1:sep}")
482 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000483
Sean Dague537d4022013-10-22 07:43:22 -0400484 if ((ver1 > ver2)); then
485 echo 1; return 0
486 elif ((ver2 > ver1)); then
487 echo -1; return 0
488 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000489
Sean Dague537d4022013-10-22 07:43:22 -0400490 if ((sep <= 1)); then
491 echo 0; return 0
492 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000493
Sean Dague537d4022013-10-22 07:43:22 -0400494 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000495}
496
497
498# This function compares two versions and is meant to be called by
499# external callers. Please note the function assumes non-alphabetic
500# versions. For example, this will work:
501#
502# vercmp_numbers 1.10 1.4
503#
504# The above will return "1", as 1.10 is greater than 1.4.
505#
506# vercmp_numbers 5.2 6.4
507#
508# The above will return "-1", as 5.2 is less than 6.4.
509#
510# vercmp_numbers 4.0 4.0
511#
512# The above will return "0", as the versions are equal.
513#
514# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100515function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400516 typeset v1=$1 v2=$2 sep
517 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000518
Sean Dague537d4022013-10-22 07:43:22 -0400519 IFS=. read -ra ver1 <<< "$v1"
520 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000521
Sean Dague537d4022013-10-22 07:43:22 -0400522 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000523}
524
525
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700526# This function sets log formatting options for colorizing log
527# output to stdout. It is meant to be called by lib modules.
528# The last two parameters are optional and can be used to specify
529# non-default value for project and user format variables.
530# Defaults are respectively 'project_name' and 'user_name'
531#
532# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100533function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700534 local conf_file=$1
535 local conf_section=$2
536 local project_var=${3:-"project_name"}
537 local user_var=${4:-"user_name"}
538 # Add color to logging output
539 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"
540 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"
541 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
542 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
543}
544
Ian Wienand54e39102014-06-03 16:05:12 +1000545# These functions are provided for basic fall-back functionality for
546# projects that include parts of devstack (grenade). stack.sh will
547# override these with more specific versions for devstack (with fancy
548# spinners, etc). We never override an existing version
549if ! function_exists echo_summary; then
550 function echo_summary {
551 echo $@
552 }
553fi
554if ! function_exists echo_nolog; then
555 function echo_nolog {
556 echo $@
557 }
558fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600559
Sébastien Han36f2f022014-01-06 18:09:26 +0100560
561# create_disk - Create backing disk
562function create_disk {
563 local node_number
564 local disk_image=${1}
565 local storage_data_dir=${2}
566 local loopback_disk_size=${3}
567
568 # Create a loopback disk and format it to XFS.
569 if [[ -e ${disk_image} ]]; then
570 if egrep -q ${storage_data_dir} /proc/mounts; then
571 sudo umount ${storage_data_dir}/drives/sdb1
572 sudo rm -f ${disk_image}
573 fi
574 fi
575
576 sudo mkdir -p ${storage_data_dir}/drives/images
577
578 sudo truncate -s ${loopback_disk_size} ${disk_image}
579
580 # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in
581 # a single inode. Keeping the default inode size (256) will result in multiple
582 # inodes being used to store xattr. Retrieving the xattr will be slower
583 # since we have to read multiple inodes. This statement is true for both
584 # Swift and Ceph.
585 sudo mkfs.xfs -f -i size=1024 ${disk_image}
586
587 # Mount the disk with mount options to make it as efficient as possible
588 if ! egrep -q ${storage_data_dir} /proc/mounts; then
589 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
590 ${disk_image} ${storage_data_dir}
591 fi
592}
593
Dean Troyer27e32692012-03-16 16:16:56 -0500594# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000595$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500596
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500597# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400598# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400599# End: