blob: e439ef6dbed616ea31f73443f7e392028e345355 [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
Dean Troyer7f9aa712012-01-31 12:11:56 -060019
Chris Buccella610af8c2013-11-05 12:56:34 +000020# Checks if installed Apache is <= given version
21# $1 = x.y.z (version string of Apache)
22function check_apache_version {
23 local cmd="apachectl"
24 if ! [[ -x $(which apachectl 2>/dev/null) ]]; then
25 cmd="/usr/sbin/apachectl"
26 fi
27
28 local version=$($cmd -v | grep version | grep -Po 'Apache/\K[^ ]*')
29 expr "$version" '>=' $1 > /dev/null
30}
31
Dean Troyer13dc5cc2012-03-27 14:50:45 -050032
Ian Wienand31dcd3e2013-07-16 13:36:34 +100033# Cleanup anything from /tmp on unstack
34# clean_tmp
35function cleanup_tmp {
36 local tmp_dir=${TMPDIR:-/tmp}
37
38 # see comments in pip_install
39 sudo rm -rf ${tmp_dir}/pip-build.*
40}
41
Dean Troyer1a6d4492013-06-03 16:47:36 -050042
Adam Spierscb961592013-10-05 12:11:07 +010043# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050044# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010045#
46# - ``FILES`` must be set to the cache dir
47# - ``GLANCE_HOSTPORT``
48#
Dean Troyerca0e3d02012-04-13 15:58:37 -050049# upload_image image-url glance-token
Ian Wienandaee18c72014-02-21 15:35:08 +110050function upload_image {
Dean Troyerca0e3d02012-04-13 15:58:37 -050051 local image_url=$1
52 local token=$2
53
54 # Create a directory for the downloaded image tarballs.
55 mkdir -p $FILES/images
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -080056 IMAGE_FNAME=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -080057 if [[ $image_url != file* ]]; then
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -080058 # Downloads the image (uec ami+akistyle), then extracts it.
Arnaud Legendre3e439442013-11-15 16:06:03 -080059 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +090060 wget -c $image_url -O $FILES/$IMAGE_FNAME
61 if [[ $? -ne 0 ]]; then
62 echo "Not found: $image_url"
63 return
64 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -080065 fi
66 IMAGE="$FILES/${IMAGE_FNAME}"
67 else
68 # File based URL (RFC 1738): file://host/path
69 # Remote files are not considered here.
70 # *nix: file:///home/user/path/file
71 # windows: file:///C:/Documents%20and%20Settings/user/path/file
72 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
73 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050074 echo "Not found: $image_url"
75 return
76 fi
77 fi
78
79 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
80 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -050081 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
82 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}"
83 return
84 fi
85
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070086 # vmdk format images
87 if [[ "$image_url" =~ '.vmdk' ]]; then
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -070088 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -070089
90 # Before we can upload vmdk type images to glance, we need to know it's
91 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080092 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -070093 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -070094 # vmdk filename, which is expected in the following format:
95 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -080096 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -070097 #
98 # If the filename does not follow the above format then the vsphere
99 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700100
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800101 vmdk_adapter_type=""
102 vmdk_disktype=""
103 vmdk_net_adapter=""
104
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700105 # vmdk adapter type
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800106 vmdk_adapter_type="$(head -25 $IMAGE | { grep -a -F -m 1 'ddb.adapterType =' $IMAGE || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700107 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
108 vmdk_adapter_type="${vmdk_adapter_type%?}"
109
110 # vmdk disk type
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800111 vmdk_create_type="$(head -25 $IMAGE | { grep -a -F -m 1 'createType=' $IMAGE || true; })"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700112 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800113 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800114
115 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900116 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700117 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
118 vmdk_disktype="sparse"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800119 elif [[ "$vmdk_create_type" = "monolithicFlat" || \
120 "$vmdk_create_type" = "vmfs" ]]; then
121 # Attempt to retrieve the *-flat.vmdk
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800122 flat_fname="$(head -25 $IMAGE | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE || true; })"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800123 flat_fname="${flat_fname#*\"}"
124 flat_fname="${flat_fname%?}"
125 if [[ -z "$flat_name" ]]; then
126 flat_fname="$IMAGE_NAME-flat.vmdk"
127 fi
128 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
129 flat_url="${image_url:0:$path_len}$flat_fname"
130 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900131 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800132 if [[ $flat_url != file* ]]; then
133 if [[ ! -f $FILES/$flat_fname || \
134 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
135 wget -c $flat_url -O $FILES/$flat_fname
136 if [[ $? -ne 0 ]]; then
137 echo "Flat disk not found: $flat_url"
138 flat_found=false
139 fi
140 fi
141 if $flat_found; then
142 IMAGE="$FILES/${flat_fname}"
143 fi
144 else
145 IMAGE=$(echo $flat_url | sed "s/^file:\/\///g")
146 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
147 echo "Flat disk not found: $flat_url"
148 flat_found=false
149 fi
150 if ! $flat_found; then
151 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
152 fi
153 fi
154 if $flat_found; then
155 IMAGE_NAME="${flat_fname}"
156 fi
157 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800158 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
159 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800160 elif [[ -z "$vmdk_create_type" ]]; then
161 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
162 # to retrieve appropriate metadata
163 if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then
164 warn $LINENO "Expected filename suffix: '-flat'."`
165 `" Filename provided: ${IMAGE_NAME}"
Ryan Hsu99b622a2014-03-05 15:35:49 -0800166 fi
167
168 descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk"
169 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
170 flat_path="${image_url:0:$path_len}"
171 descriptor_url=$flat_path$descriptor_fname
172 warn $LINENO "$descriptor_data_pair_msg"`
173 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
174 if [[ $flat_path != file* ]]; then
175 if [[ ! -f $FILES/$descriptor_fname || \
176 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
177 wget -c $descriptor_url -O $FILES/$descriptor_fname
178 if [[ $? -ne 0 ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900179 warn $LINENO "Descriptor not found $descriptor_url"
180 descriptor_found=false
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800181 fi
182 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800183 descriptor_url="$FILES/$descriptor_fname"
184 else
185 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
186 if [[ ! -f $descriptor_url || \
187 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
188 warn $LINENO "Descriptor not found $descriptor_url"
189 descriptor_found=false
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900190 fi
191 fi
Ryan Hsu99b622a2014-03-05 15:35:49 -0800192 if $descriptor_found; then
193 vmdk_adapter_type="$(head -25 $descriptor_url | { grep -a -F -m 1 'ddb.adapterType =' $descriptor_url || true; })"
194 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
195 vmdk_adapter_type="${vmdk_adapter_type%?}"
196 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900197 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700198 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700199 vmdk_disktype="preallocated"
200 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800201
202 # NOTE: For backwards compatibility reasons, colons may be used in place
203 # of semi-colons for property delimiters but they are not permitted
204 # characters in NTFS filesystems.
Sreeram Yerrapragada314af0a2014-03-03 21:34:45 -0800205 property_string=`echo "$IMAGE_NAME" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800206 IFS=':;' read -a props <<< "$property_string"
207 vmdk_disktype="${props[0]:-$vmdk_disktype}"
208 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
209 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700210
Ryan Hsu49f44862013-10-03 22:27:03 -0700211 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 -0700212 return
213 fi
214
Mate Lakatbc2ef922013-08-15 18:06:59 +0100215 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500216 # and should not be decompressed prior to loading
217 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500218 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
219 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}"
220 return
221 fi
222
Mate Lakatbc2ef922013-08-15 18:06:59 +0100223 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
224 # and should not be decompressed prior to loading.
225 # Setting metadata, so PV mode is used.
226 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Mate Lakatbc2ef922013-08-15 18:06:59 +0100227 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
228 glance \
Sean Dague537d4022013-10-22 07:43:22 -0400229 --os-auth-token $token \
230 --os-image-url http://$GLANCE_HOSTPORT \
231 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100232 --name "$IMAGE_NAME" --is-public=True \
233 --container-format=tgz --disk-format=raw \
234 --property vm_mode=xen < "${IMAGE}"
235 return
236 fi
237
Dean Troyerca0e3d02012-04-13 15:58:37 -0500238 KERNEL=""
239 RAMDISK=""
240 DISK_FORMAT=""
241 CONTAINER_FORMAT=""
242 UNPACK=""
243 case "$IMAGE_FNAME" in
244 *.tar.gz|*.tgz)
245 # Extract ami and aki files
246 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
247 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
248 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
249 xdir="$FILES/images/$IMAGE_NAME"
250 rm -Rf "$xdir";
251 mkdir "$xdir"
252 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
253 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400254 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500255 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400256 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500257 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400258 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500259 if [[ -z "$IMAGE_NAME" ]]; then
260 IMAGE_NAME=$(basename "$IMAGE" ".img")
261 fi
262 ;;
263 *.img)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500264 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500265 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
266 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
267 DISK_FORMAT=$format
268 else
269 DISK_FORMAT=raw
270 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500271 CONTAINER_FORMAT=bare
272 ;;
273 *.img.gz)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500274 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
275 DISK_FORMAT=raw
276 CONTAINER_FORMAT=bare
277 UNPACK=zcat
278 ;;
279 *.qcow2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500280 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
281 DISK_FORMAT=qcow2
282 CONTAINER_FORMAT=bare
283 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100284 *.iso)
Jonathan Michalon06802042013-03-21 14:29:58 +0100285 IMAGE_NAME=$(basename "$IMAGE" ".iso")
286 DISK_FORMAT=iso
287 CONTAINER_FORMAT=bare
288 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500289 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
290 esac
291
Rafael Folcoab775872013-12-02 14:04:32 -0200292 if is_arch "ppc64"; then
Rafael Folco0b03e7a2014-03-14 11:14:57 -0300293 IMG_PROPERTY="--property hw_cdrom_bus=scsi"
Rafael Folcoab775872013-12-02 14:04:32 -0200294 fi
295
Dean Troyerca0e3d02012-04-13 15:58:37 -0500296 if [ "$CONTAINER_FORMAT" = "bare" ]; then
297 if [ "$UNPACK" = "zcat" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200298 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 -0500299 else
Rafael Folcoab775872013-12-02 14:04:32 -0200300 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 -0500301 fi
302 else
303 # Use glance client to add the kernel the root filesystem.
304 # We parse the results of the first upload to get the glance ID of the
305 # kernel for use when uploading the root filesystem.
306 KERNEL_ID=""; RAMDISK_ID="";
307 if [ -n "$KERNEL" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200308 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 -0500309 fi
310 if [ -n "$RAMDISK" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200311 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 -0500312 fi
Rafael Folcoab775872013-12-02 14:04:32 -0200313 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 -0500314 fi
315}
316
Dean Troyer1a6d4492013-06-03 16:47:36 -0500317
Dean Troyerc1b486a2012-11-05 14:26:09 -0600318# Set the database backend to use
319# When called from stackrc/localrc DATABASE_BACKENDS has not been
320# initialized yet, just save the configuration selection and call back later
321# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100322#
323# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600324function use_database {
325 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600326 # No backends registered means this is likely called from ``localrc``
327 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600328 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000329 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 +0100330 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600331 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100332 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600333 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600334}
335
Dean Troyer1a6d4492013-06-03 16:47:36 -0500336
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600337# Wait for an HTTP server to start answering requests
338# wait_for_service timeout url
Ian Wienandaee18c72014-02-21 15:35:08 +1100339function wait_for_service {
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600340 local timeout=$1
341 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800342 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600343}
344
Dean Troyer1a6d4492013-06-03 16:47:36 -0500345
Nachi Uenofda946e2012-10-24 17:26:02 -0700346# ping check
347# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500348# ping_check from-net ip boot-timeout expected
Ian Wienandaee18c72014-02-21 15:35:08 +1100349function ping_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400350 if is_service_enabled neutron; then
351 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700352 return
353 fi
354 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700355}
356
357# ping check for nova
358# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
Ian Wienandaee18c72014-02-21 15:35:08 +1100359function _ping_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700360 local from_net=$1
361 local ip=$2
362 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700363 local expected=${4:-"True"}
364 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700365 MULTI_HOST=`trueorfalse False $MULTI_HOST`
366 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700367 return
368 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700369 if [[ "$expected" = "True" ]]; then
370 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
371 else
372 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
373 fi
374 if ! timeout $boot_timeout sh -c "$check_command"; then
375 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800376 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700377 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800378 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700379 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700380 fi
381}
382
Nachi Ueno6769b162013-08-12 18:18:56 -0700383# Get ip of instance
Ian Wienandaee18c72014-02-21 15:35:08 +1100384function get_instance_ip {
Nachi Ueno6769b162013-08-12 18:18:56 -0700385 local vm_id=$1
386 local network_name=$2
387 local nova_result="$(nova show $vm_id)"
388 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
389 if [[ $ip = "" ]];then
390 echo "$nova_result"
391 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700392 fi
393 echo $ip
394}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500395
Nachi Uenofda946e2012-10-24 17:26:02 -0700396# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700397
Dean Troyer1a6d4492013-06-03 16:47:36 -0500398# ssh_check net-name key-file floating-ip default-user active-timeout
Ian Wienandaee18c72014-02-21 15:35:08 +1100399function ssh_check {
Mark McClainb05c8762013-07-06 23:29:39 -0400400 if is_service_enabled neutron; then
401 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700402 return
403 fi
404 _ssh_check_novanet "$1" $2 $3 $4 $5
405}
406
Ian Wienandaee18c72014-02-21 15:35:08 +1100407function _ssh_check_novanet {
Nachi Uenofda946e2012-10-24 17:26:02 -0700408 local NET_NAME=$1
409 local KEY_FILE=$2
410 local FLOATING_IP=$3
411 local DEFAULT_INSTANCE_USER=$4
412 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600413 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500414 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 -0800415 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700416 fi
417}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500418
Vincent Untz856a11e2012-11-21 16:04:12 +0100419
Vincent Untz856a11e2012-11-21 16:04:12 +0100420# Get the location of the $module-rootwrap executables, where module is cinder
421# or nova.
422# get_rootwrap_location module
Ian Wienandaee18c72014-02-21 15:35:08 +1100423function get_rootwrap_location {
Vincent Untz856a11e2012-11-21 16:04:12 +0100424 local module=$1
425
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100426 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100427}
428
Dean Troyer1a6d4492013-06-03 16:47:36 -0500429
Ian Wienand0488edd2013-04-11 12:04:36 +1000430# Path permissions sanity check
431# check_path_perm_sanity path
Ian Wienandaee18c72014-02-21 15:35:08 +1100432function check_path_perm_sanity {
Ian Wienand0488edd2013-04-11 12:04:36 +1000433 # Ensure no element of the path has 0700 permissions, which is very
434 # likely to cause issues for daemons. Inspired by default 0700
435 # homedir permissions on RHEL and common practice of making DEST in
436 # the stack user's homedir.
437
438 local real_path=$(readlink -f $1)
439 local rebuilt_path=""
440 for i in $(echo ${real_path} | tr "/" " "); do
441 rebuilt_path=$rebuilt_path"/"$i
442
443 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
444 echo "*** DEST path element"
445 echo "*** ${rebuilt_path}"
446 echo "*** appears to have 0700 permissions."
447 echo "*** This is very likely to cause fatal issues for devstack daemons."
448
449 if [[ -n "$SKIP_PATH_SANITY" ]]; then
450 return
451 else
452 echo "*** Set SKIP_PATH_SANITY to skip this check"
453 die $LINENO "Invalid path permissions"
454 fi
455 fi
456 done
457}
458
Dean Troyer1a6d4492013-06-03 16:47:36 -0500459
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000460# This function recursively compares versions, and is not meant to be
461# called by anything other than vercmp_numbers below. This function does
462# not work with alphabetic versions.
463#
464# _vercmp_r sep ver1 ver2
465function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400466 typeset sep
467 typeset -a ver1=() ver2=()
468 sep=$1; shift
469 ver1=("${@:1:sep}")
470 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000471
Sean Dague537d4022013-10-22 07:43:22 -0400472 if ((ver1 > ver2)); then
473 echo 1; return 0
474 elif ((ver2 > ver1)); then
475 echo -1; return 0
476 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000477
Sean Dague537d4022013-10-22 07:43:22 -0400478 if ((sep <= 1)); then
479 echo 0; return 0
480 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000481
Sean Dague537d4022013-10-22 07:43:22 -0400482 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000483}
484
485
486# This function compares two versions and is meant to be called by
487# external callers. Please note the function assumes non-alphabetic
488# versions. For example, this will work:
489#
490# vercmp_numbers 1.10 1.4
491#
492# The above will return "1", as 1.10 is greater than 1.4.
493#
494# vercmp_numbers 5.2 6.4
495#
496# The above will return "-1", as 5.2 is less than 6.4.
497#
498# vercmp_numbers 4.0 4.0
499#
500# The above will return "0", as the versions are equal.
501#
502# vercmp_numbers ver1 ver2
Ian Wienandaee18c72014-02-21 15:35:08 +1100503function vercmp_numbers {
Sean Dague537d4022013-10-22 07:43:22 -0400504 typeset v1=$1 v2=$2 sep
505 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000506
Sean Dague537d4022013-10-22 07:43:22 -0400507 IFS=. read -ra ver1 <<< "$v1"
508 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000509
Sean Dague537d4022013-10-22 07:43:22 -0400510 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000511}
512
513
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700514# This function sets log formatting options for colorizing log
515# output to stdout. It is meant to be called by lib modules.
516# The last two parameters are optional and can be used to specify
517# non-default value for project and user format variables.
518# Defaults are respectively 'project_name' and 'user_name'
519#
520# setup_colorized_logging something.conf SOMESECTION
Ian Wienandaee18c72014-02-21 15:35:08 +1100521function setup_colorized_logging {
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700522 local conf_file=$1
523 local conf_section=$2
524 local project_var=${3:-"project_name"}
525 local user_var=${4:-"user_name"}
526 # Add color to logging output
527 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"
528 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"
529 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
530 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
531}
532
Dean Troyerdff49a22014-01-30 15:37:40 -0600533
Dean Troyer27e32692012-03-16 16:16:56 -0500534# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000535$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500536
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500537# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400538# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400539# End: