blob: 3101111c639e9c4ccb724d37a73282e34e8f601d [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# - ``REQUIREMENTS_DIR``
10# - ``STACK_USER``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010011# - ``TRACK_DEPENDS``
Dean Troyerd8864fe2014-02-17 11:00:42 -060012# - ``UNDO_REQUIREMENTS``
13#
Dean Troyer13dc5cc2012-03-27 14:50:45 -050014
Dean Troyerdff49a22014-01-30 15:37:40 -060015# Include the common functions
16FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
17source ${FUNC_DIR}/functions-common
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
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
54function upload_image() {
55 local image_url=$1
56 local token=$2
57
58 # Create a directory for the downloaded image tarballs.
59 mkdir -p $FILES/images
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -080060 IMAGE_FNAME=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080061 if [[ $image_url != file* ]]; then
62 # Downloads the image (uec ami+aki style), then extracts it.
Arnaud Legendre3e439442013-11-15 16:06:03 -080063 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090064 wget -c $image_url -O $FILES/$IMAGE_FNAME
65 if [[ $? -ne 0 ]]; then
66 echo "Not found: $image_url"
67 return
68 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080069 fi
70 IMAGE="$FILES/${IMAGE_FNAME}"
71 else
72 # File based URL (RFC 1738): file://host/path
73 # Remote files are not considered here.
74 # *nix: file:///home/user/path/file
75 # windows: file:///C:/Documents%20and%20Settings/user/path/file
76 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
77 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050078 echo "Not found: $image_url"
79 return
80 fi
81 fi
82
83 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
84 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050085 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
86 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format ami --disk-format ami < "${IMAGE}"
87 return
88 fi
89
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070090 # vmdk format images
91 if [[ "$image_url" =~ '.vmdk' ]]; then
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070092 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070093
94 # Before we can upload vmdk type images to glance, we need to know it's
95 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080096 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070097 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070098 # vmdk filename, which is expected in the following format:
99 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800100 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700101 #
102 # If the filename does not follow the above format then the vsphere
103 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700104
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800105 vmdk_adapter_type=""
106 vmdk_disktype=""
107 vmdk_net_adapter=""
108
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700109 # vmdk adapter type
110 vmdk_adapter_type="$(head -25 $IMAGE | grep -a -F -m 1 'ddb.adapterType =' $IMAGE)"
111 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
112 vmdk_adapter_type="${vmdk_adapter_type%?}"
113
114 # vmdk disk type
115 vmdk_create_type="$(head -25 $IMAGE | grep -a -F -m 1 'createType=' $IMAGE)"
116 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800117 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800118
119 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900120 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700121 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
122 vmdk_disktype="sparse"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800123 elif [[ "$vmdk_create_type" = "monolithicFlat" || \
124 "$vmdk_create_type" = "vmfs" ]]; then
125 # Attempt to retrieve the *-flat.vmdk
126 flat_fname="$(head -25 $IMAGE | grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE)"
127 flat_fname="${flat_fname#*\"}"
128 flat_fname="${flat_fname%?}"
129 if [[ -z "$flat_name" ]]; then
130 flat_fname="$IMAGE_NAME-flat.vmdk"
131 fi
132 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
133 flat_url="${image_url:0:$path_len}$flat_fname"
134 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900135 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800136 if [[ $flat_url != file* ]]; then
137 if [[ ! -f $FILES/$flat_fname || \
138 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
139 wget -c $flat_url -O $FILES/$flat_fname
140 if [[ $? -ne 0 ]]; then
141 echo "Flat disk not found: $flat_url"
142 flat_found=false
143 fi
144 fi
145 if $flat_found; then
146 IMAGE="$FILES/${flat_fname}"
147 fi
148 else
149 IMAGE=$(echo $flat_url | sed "s/^file:\/\///g")
150 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
151 echo "Flat disk not found: $flat_url"
152 flat_found=false
153 fi
154 if ! $flat_found; then
155 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
156 fi
157 fi
158 if $flat_found; then
159 IMAGE_NAME="${flat_fname}"
160 fi
161 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800162 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
163 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800164 elif [[ -z "$vmdk_create_type" ]]; then
165 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
166 # to retrieve appropriate metadata
167 if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then
168 warn $LINENO "Expected filename suffix: '-flat'."`
169 `" Filename provided: ${IMAGE_NAME}"
170 else
171 descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk"
172 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
173 flat_path="${image_url:0:$path_len}"
174 descriptor_url=$flat_path$descriptor_fname
175 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900176 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800177 if [[ $flat_path != file* ]]; then
178 if [[ ! -f $FILES/$descriptor_fname || \
179 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
180 wget -c $descriptor_url -O $FILES/$descriptor_fname
181 if [[ $? -ne 0 ]]; then
182 warn $LINENO "Descriptor not found $descriptor_url"
183 descriptor_found=false
184 fi
185 fi
186 descriptor_url="$FILES/$descriptor_fname"
187 else
188 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
189 if [[ ! -f $descriptor_url || \
190 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900191 warn $LINENO "Descriptor not found $descriptor_url"
192 descriptor_found=false
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800193 fi
194 fi
195 if $descriptor_found; then
196 vmdk_adapter_type="$(head -25 $descriptor_url |"`
197 `"grep -a -F -m 1 'ddb.adapterType =' $descriptor_url)"
198 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
199 vmdk_adapter_type="${vmdk_adapter_type%?}"
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900200 fi
201 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900202 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700203 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700204 vmdk_disktype="preallocated"
205 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800206
207 # NOTE: For backwards compatibility reasons, colons may be used in place
208 # of semi-colons for property delimiters but they are not permitted
209 # characters in NTFS filesystems.
Arnaud Legendreb93cd642014-01-23 17:12:21 -0800210 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$'`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800211 IFS=':;' read -a props <<< "$property_string"
212 vmdk_disktype="${props[0]:-$vmdk_disktype}"
213 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
214 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700215
Ryan Hsu49f44862013-10-03 22:27:03 -0700216 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --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 -0700217 return
218 fi
219
Mate Lakatbc2ef922013-08-15 18:06:59 +0100220 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500221 # and should not be decompressed prior to loading
222 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500223 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
224 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=ovf --disk-format=vhd < "${IMAGE}"
225 return
226 fi
227
Mate Lakatbc2ef922013-08-15 18:06:59 +0100228 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
229 # and should not be decompressed prior to loading.
230 # Setting metadata, so PV mode is used.
231 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Mate Lakatbc2ef922013-08-15 18:06:59 +0100232 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
233 glance \
Sean Dague537d4022013-10-22 07:43:22 -0400234 --os-auth-token $token \
235 --os-image-url http://$GLANCE_HOSTPORT \
236 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100237 --name "$IMAGE_NAME" --is-public=True \
238 --container-format=tgz --disk-format=raw \
239 --property vm_mode=xen < "${IMAGE}"
240 return
241 fi
242
Dean Troyerca0e3d02012-04-13 15:58:37 -0500243 KERNEL=""
244 RAMDISK=""
245 DISK_FORMAT=""
246 CONTAINER_FORMAT=""
247 UNPACK=""
248 case "$IMAGE_FNAME" in
249 *.tar.gz|*.tgz)
250 # Extract ami and aki files
251 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
252 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
253 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
254 xdir="$FILES/images/$IMAGE_NAME"
255 rm -Rf "$xdir";
256 mkdir "$xdir"
257 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
258 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400259 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500260 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400261 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500262 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400263 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500264 if [[ -z "$IMAGE_NAME" ]]; then
265 IMAGE_NAME=$(basename "$IMAGE" ".img")
266 fi
267 ;;
268 *.img)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500269 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500270 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
271 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
272 DISK_FORMAT=$format
273 else
274 DISK_FORMAT=raw
275 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500276 CONTAINER_FORMAT=bare
277 ;;
278 *.img.gz)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500279 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
280 DISK_FORMAT=raw
281 CONTAINER_FORMAT=bare
282 UNPACK=zcat
283 ;;
284 *.qcow2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500285 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
286 DISK_FORMAT=qcow2
287 CONTAINER_FORMAT=bare
288 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100289 *.iso)
Jonathan Michalon06802042013-03-21 14:29:58 +0100290 IMAGE_NAME=$(basename "$IMAGE" ".iso")
291 DISK_FORMAT=iso
292 CONTAINER_FORMAT=bare
293 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500294 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
295 esac
296
Rafael Folcoab775872013-12-02 14:04:32 -0200297 if is_arch "ppc64"; then
298 IMG_PROPERTY="--property hw_disk_bus=scsi --property hw_cdrom_bus=scsi"
299 fi
300
Dean Troyerca0e3d02012-04-13 15:58:37 -0500301 if [ "$CONTAINER_FORMAT" = "bare" ]; then
302 if [ "$UNPACK" = "zcat" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200303 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
Dean Troyerca0e3d02012-04-13 15:58:37 -0500304 else
Rafael Folcoab775872013-12-02 14:04:32 -0200305 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
Dean Troyerca0e3d02012-04-13 15:58:37 -0500306 fi
307 else
308 # Use glance client to add the kernel the root filesystem.
309 # We parse the results of the first upload to get the glance ID of the
310 # kernel for use when uploading the root filesystem.
311 KERNEL_ID=""; RAMDISK_ID="";
312 if [ -n "$KERNEL" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200313 KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" $IMG_PROPERTY --is-public True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500314 fi
315 if [ -n "$RAMDISK" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200316 RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" $IMG_PROPERTY --is-public True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500317 fi
Rafael Folcoab775872013-12-02 14:04:32 -0200318 glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" $IMG_PROPERTY --is-public True --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 -0500319 fi
320}
321
Dean Troyer1a6d4492013-06-03 16:47:36 -0500322
Dean Troyerc1b486a2012-11-05 14:26:09 -0600323# Set the database backend to use
324# When called from stackrc/localrc DATABASE_BACKENDS has not been
325# initialized yet, just save the configuration selection and call back later
326# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100327#
328# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600329function use_database {
330 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600331 # No backends registered means this is likely called from ``localrc``
332 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600333 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000334 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 +0100335 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600336 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100337 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600338 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600339}
340
Dean Troyer1a6d4492013-06-03 16:47:36 -0500341
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600342# Wait for an HTTP server to start answering requests
343# wait_for_service timeout url
344function wait_for_service() {
345 local timeout=$1
346 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800347 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600348}
349
Dean Troyer1a6d4492013-06-03 16:47:36 -0500350
Nachi Uenofda946e2012-10-24 17:26:02 -0700351# ping check
352# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500353# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -0700354function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400355 if is_service_enabled neutron; then
356 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700357 return
358 fi
359 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700360}
361
362# ping check for nova
363# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
364function _ping_check_novanet() {
365 local from_net=$1
366 local ip=$2
367 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700368 local expected=${4:-"True"}
369 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700370 MULTI_HOST=`trueorfalse False $MULTI_HOST`
371 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700372 return
373 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700374 if [[ "$expected" = "True" ]]; then
375 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
376 else
377 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
378 fi
379 if ! timeout $boot_timeout sh -c "$check_command"; then
380 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800381 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700382 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800383 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700384 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700385 fi
386}
387
Nachi Ueno6769b162013-08-12 18:18:56 -0700388# Get ip of instance
389function get_instance_ip(){
390 local vm_id=$1
391 local network_name=$2
392 local nova_result="$(nova show $vm_id)"
393 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
394 if [[ $ip = "" ]];then
395 echo "$nova_result"
396 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700397 fi
398 echo $ip
399}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500400
Nachi Uenofda946e2012-10-24 17:26:02 -0700401# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700402
Dean Troyer1a6d4492013-06-03 16:47:36 -0500403# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -0700404function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400405 if is_service_enabled neutron; then
406 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700407 return
408 fi
409 _ssh_check_novanet "$1" $2 $3 $4 $5
410}
411
412function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -0700413 local NET_NAME=$1
414 local KEY_FILE=$2
415 local FLOATING_IP=$3
416 local DEFAULT_INSTANCE_USER=$4
417 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600418 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500419 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 -0800420 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700421 fi
422}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500423
Vincent Untz856a11e2012-11-21 16:04:12 +0100424
Vincent Untz856a11e2012-11-21 16:04:12 +0100425# Get the location of the $module-rootwrap executables, where module is cinder
426# or nova.
427# get_rootwrap_location module
428function get_rootwrap_location() {
429 local module=$1
430
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100431 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100432}
433
Dean Troyer1a6d4492013-06-03 16:47:36 -0500434
Ian Wienand0488edd2013-04-11 12:04:36 +1000435# Path permissions sanity check
436# check_path_perm_sanity path
437function check_path_perm_sanity() {
438 # Ensure no element of the path has 0700 permissions, which is very
439 # likely to cause issues for daemons. Inspired by default 0700
440 # homedir permissions on RHEL and common practice of making DEST in
441 # the stack user's homedir.
442
443 local real_path=$(readlink -f $1)
444 local rebuilt_path=""
445 for i in $(echo ${real_path} | tr "/" " "); do
446 rebuilt_path=$rebuilt_path"/"$i
447
448 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
449 echo "*** DEST path element"
450 echo "*** ${rebuilt_path}"
451 echo "*** appears to have 0700 permissions."
452 echo "*** This is very likely to cause fatal issues for devstack daemons."
453
454 if [[ -n "$SKIP_PATH_SANITY" ]]; then
455 return
456 else
457 echo "*** Set SKIP_PATH_SANITY to skip this check"
458 die $LINENO "Invalid path permissions"
459 fi
460 fi
461 done
462}
463
Dean Troyer1a6d4492013-06-03 16:47:36 -0500464
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000465# This function recursively compares versions, and is not meant to be
466# called by anything other than vercmp_numbers below. This function does
467# not work with alphabetic versions.
468#
469# _vercmp_r sep ver1 ver2
470function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400471 typeset sep
472 typeset -a ver1=() ver2=()
473 sep=$1; shift
474 ver1=("${@:1:sep}")
475 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000476
Sean Dague537d4022013-10-22 07:43:22 -0400477 if ((ver1 > ver2)); then
478 echo 1; return 0
479 elif ((ver2 > ver1)); then
480 echo -1; return 0
481 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000482
Sean Dague537d4022013-10-22 07:43:22 -0400483 if ((sep <= 1)); then
484 echo 0; return 0
485 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000486
Sean Dague537d4022013-10-22 07:43:22 -0400487 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000488}
489
490
491# This function compares two versions and is meant to be called by
492# external callers. Please note the function assumes non-alphabetic
493# versions. For example, this will work:
494#
495# vercmp_numbers 1.10 1.4
496#
497# The above will return "1", as 1.10 is greater than 1.4.
498#
499# vercmp_numbers 5.2 6.4
500#
501# The above will return "-1", as 5.2 is less than 6.4.
502#
503# vercmp_numbers 4.0 4.0
504#
505# The above will return "0", as the versions are equal.
506#
507# vercmp_numbers ver1 ver2
508vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -0400509 typeset v1=$1 v2=$2 sep
510 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000511
Sean Dague537d4022013-10-22 07:43:22 -0400512 IFS=. read -ra ver1 <<< "$v1"
513 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000514
Sean Dague537d4022013-10-22 07:43:22 -0400515 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000516}
517
518
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700519# This function sets log formatting options for colorizing log
520# output to stdout. It is meant to be called by lib modules.
521# The last two parameters are optional and can be used to specify
522# non-default value for project and user format variables.
523# Defaults are respectively 'project_name' and 'user_name'
524#
525# setup_colorized_logging something.conf SOMESECTION
526function setup_colorized_logging() {
527 local conf_file=$1
528 local conf_section=$2
529 local project_var=${3:-"project_name"}
530 local user_var=${4:-"user_name"}
531 # Add color to logging output
532 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"
533 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"
534 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
535 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
536}
537
Dean Troyerdff49a22014-01-30 15:37:40 -0600538
Dean Troyer27e32692012-03-16 16:16:56 -0500539# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000540$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500541
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500542# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400543# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400544# End: