blob: 5eae7fe51067f056ba487717d34c648061e728d2 [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#
5# - ``ENABLED_SERVICES``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006# - ``FILES``
7# - ``GLANCE_HOSTPORT``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01008# - ``TRACK_DEPENDS``
Dean Troyer13dc5cc2012-03-27 14:50:45 -05009
Dean Troyerdff49a22014-01-30 15:37:40 -060010# Include the common functions
11FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
12source ${FUNC_DIR}/functions-common
Dean Troyer7f9aa712012-01-31 12:11:56 -060013
Dean Troyer27e32692012-03-16 16:16:56 -050014# Save trace setting
15XTRACE=$(set +o | grep xtrace)
16set +o xtrace
17
Dean Troyer7f9aa712012-01-31 12:11:56 -060018
Chris Buccella610af8c2013-11-05 12:56:34 +000019# Checks if installed Apache is <= given version
20# $1 = x.y.z (version string of Apache)
21function check_apache_version {
22 local cmd="apachectl"
23 if ! [[ -x $(which apachectl 2>/dev/null) ]]; then
24 cmd="/usr/sbin/apachectl"
25 fi
26
27 local version=$($cmd -v | grep version | grep -Po 'Apache/\K[^ ]*')
28 expr "$version" '>=' $1 > /dev/null
29}
30
Dean Troyer13dc5cc2012-03-27 14:50:45 -050031
Ian Wienand31dcd3e2013-07-16 13:36:34 +100032# Cleanup anything from /tmp on unstack
33# clean_tmp
34function cleanup_tmp {
35 local tmp_dir=${TMPDIR:-/tmp}
36
37 # see comments in pip_install
38 sudo rm -rf ${tmp_dir}/pip-build.*
39}
40
Dean Troyer1a6d4492013-06-03 16:47:36 -050041
Monty Taylor408a4a72013-08-02 15:43:47 -040042# ``pip install -e`` the package, which processes the dependencies
43# using pip before running `setup.py develop`
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000044#
45# Updates the dependencies in project_dir from the
46# openstack/requirements global list before installing anything.
47#
48# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``
Dean Troyerbbafb1b2012-06-11 16:51:39 -050049# setup_develop directory
50function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -040051 local project_dir=$1
Sean Dague6c844632013-07-31 06:50:14 -040052
53 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
54
Dean Troyer62d1d692013-08-01 17:40:40 -050055 # Don't update repo if local changes exist
IWAMOTO Toshihiro0b8f6e02014-01-23 12:02:34 +090056 # Don't use buggy "git diff --quiet"
57 (cd $project_dir && git diff --exit-code >/dev/null)
Doug Hellmannc3431bf2013-09-06 15:30:22 -040058 local update_requirements=$?
59
60 if [ $update_requirements -eq 0 ]; then
Dean Troyer62d1d692013-08-01 17:40:40 -050061 (cd $REQUIREMENTS_DIR; \
62 $SUDO_CMD python update.py $project_dir)
63 fi
Sean Dague6c844632013-07-31 06:50:14 -040064
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000065 setup_develop_no_requirements_update $project_dir
Doug Hellmannc3431bf2013-09-06 15:30:22 -040066
Sean Daguefd98edb2013-10-24 14:57:59 -040067 # We've just gone and possibly modified the user's source tree in an
68 # automated way, which is considered bad form if it's a development
69 # tree because we've screwed up their next git checkin. So undo it.
70 #
71 # However... there are some circumstances, like running in the gate
72 # where we really really want the overridden version to stick. So provide
73 # a variable that tells us whether or not we should UNDO the requirements
74 # changes (this will be set to False in the OpenStack ci gate)
DennyZhang89d41ca2013-11-01 15:41:01 -050075 if [ $UNDO_REQUIREMENTS = "True" ]; then
Sean Daguefd98edb2013-10-24 14:57:59 -040076 if [ $update_requirements -eq 0 ]; then
77 (cd $project_dir && git reset --hard)
78 fi
Doug Hellmannc3431bf2013-09-06 15:30:22 -040079 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -050080}
81
Dean Troyerdff49a22014-01-30 15:37:40 -060082
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000083# ``pip install -e`` the package, which processes the dependencies
84# using pip before running `setup.py develop`
85# Uses globals ``STACK_USER``
86# setup_develop_no_requirements_update directory
87function setup_develop_no_requirements_update() {
88 local project_dir=$1
89
90 pip_install -e $project_dir
91 # ensure that further actions can do things like setup.py sdist
92 safe_chown -R $STACK_USER $1/*.egg-info
93}
94
Dean Troyerbbafb1b2012-06-11 16:51:39 -050095
Adam Spierscb961592013-10-05 12:11:07 +010096# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -050097# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +010098#
99# - ``FILES`` must be set to the cache dir
100# - ``GLANCE_HOSTPORT``
101#
Dean Troyerca0e3d02012-04-13 15:58:37 -0500102# upload_image image-url glance-token
103function upload_image() {
104 local image_url=$1
105 local token=$2
106
107 # Create a directory for the downloaded image tarballs.
108 mkdir -p $FILES/images
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800109 IMAGE_FNAME=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -0800110 if [[ $image_url != file* ]]; then
111 # Downloads the image (uec ami+aki style), then extracts it.
Arnaud Legendre3e439442013-11-15 16:06:03 -0800112 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900113 wget -c $image_url -O $FILES/$IMAGE_FNAME
114 if [[ $? -ne 0 ]]; then
115 echo "Not found: $image_url"
116 return
117 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -0800118 fi
119 IMAGE="$FILES/${IMAGE_FNAME}"
120 else
121 # File based URL (RFC 1738): file://host/path
122 # Remote files are not considered here.
123 # *nix: file:///home/user/path/file
124 # windows: file:///C:/Documents%20and%20Settings/user/path/file
125 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
126 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500127 echo "Not found: $image_url"
128 return
129 fi
130 fi
131
132 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
133 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500134 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
135 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}"
136 return
137 fi
138
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700139 # vmdk format images
140 if [[ "$image_url" =~ '.vmdk' ]]; then
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700141 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700142
143 # Before we can upload vmdk type images to glance, we need to know it's
144 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800145 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700146 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700147 # vmdk filename, which is expected in the following format:
148 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800149 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700150 #
151 # If the filename does not follow the above format then the vsphere
152 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700153
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800154 vmdk_adapter_type=""
155 vmdk_disktype=""
156 vmdk_net_adapter=""
157
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700158 # vmdk adapter type
159 vmdk_adapter_type="$(head -25 $IMAGE | grep -a -F -m 1 'ddb.adapterType =' $IMAGE)"
160 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
161 vmdk_adapter_type="${vmdk_adapter_type%?}"
162
163 # vmdk disk type
164 vmdk_create_type="$(head -25 $IMAGE | grep -a -F -m 1 'createType=' $IMAGE)"
165 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800166 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800167
168 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900169 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700170 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
171 vmdk_disktype="sparse"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800172 elif [[ "$vmdk_create_type" = "monolithicFlat" || \
173 "$vmdk_create_type" = "vmfs" ]]; then
174 # Attempt to retrieve the *-flat.vmdk
175 flat_fname="$(head -25 $IMAGE | grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE)"
176 flat_fname="${flat_fname#*\"}"
177 flat_fname="${flat_fname%?}"
178 if [[ -z "$flat_name" ]]; then
179 flat_fname="$IMAGE_NAME-flat.vmdk"
180 fi
181 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
182 flat_url="${image_url:0:$path_len}$flat_fname"
183 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900184 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800185 if [[ $flat_url != file* ]]; then
186 if [[ ! -f $FILES/$flat_fname || \
187 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
188 wget -c $flat_url -O $FILES/$flat_fname
189 if [[ $? -ne 0 ]]; then
190 echo "Flat disk not found: $flat_url"
191 flat_found=false
192 fi
193 fi
194 if $flat_found; then
195 IMAGE="$FILES/${flat_fname}"
196 fi
197 else
198 IMAGE=$(echo $flat_url | sed "s/^file:\/\///g")
199 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
200 echo "Flat disk not found: $flat_url"
201 flat_found=false
202 fi
203 if ! $flat_found; then
204 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
205 fi
206 fi
207 if $flat_found; then
208 IMAGE_NAME="${flat_fname}"
209 fi
210 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800211 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
212 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800213 elif [[ -z "$vmdk_create_type" ]]; then
214 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
215 # to retrieve appropriate metadata
216 if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then
217 warn $LINENO "Expected filename suffix: '-flat'."`
218 `" Filename provided: ${IMAGE_NAME}"
219 else
220 descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk"
221 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
222 flat_path="${image_url:0:$path_len}"
223 descriptor_url=$flat_path$descriptor_fname
224 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900225 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800226 if [[ $flat_path != file* ]]; then
227 if [[ ! -f $FILES/$descriptor_fname || \
228 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
229 wget -c $descriptor_url -O $FILES/$descriptor_fname
230 if [[ $? -ne 0 ]]; then
231 warn $LINENO "Descriptor not found $descriptor_url"
232 descriptor_found=false
233 fi
234 fi
235 descriptor_url="$FILES/$descriptor_fname"
236 else
237 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
238 if [[ ! -f $descriptor_url || \
239 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900240 warn $LINENO "Descriptor not found $descriptor_url"
241 descriptor_found=false
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800242 fi
243 fi
244 if $descriptor_found; then
245 vmdk_adapter_type="$(head -25 $descriptor_url |"`
246 `"grep -a -F -m 1 'ddb.adapterType =' $descriptor_url)"
247 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
248 vmdk_adapter_type="${vmdk_adapter_type%?}"
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900249 fi
250 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900251 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700252 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700253 vmdk_disktype="preallocated"
254 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800255
256 # NOTE: For backwards compatibility reasons, colons may be used in place
257 # of semi-colons for property delimiters but they are not permitted
258 # characters in NTFS filesystems.
Arnaud Legendreb93cd642014-01-23 17:12:21 -0800259 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$'`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800260 IFS=':;' read -a props <<< "$property_string"
261 vmdk_disktype="${props[0]:-$vmdk_disktype}"
262 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
263 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700264
Ryan Hsu49f44862013-10-03 22:27:03 -0700265 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 -0700266 return
267 fi
268
Mate Lakatbc2ef922013-08-15 18:06:59 +0100269 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500270 # and should not be decompressed prior to loading
271 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500272 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
273 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}"
274 return
275 fi
276
Mate Lakatbc2ef922013-08-15 18:06:59 +0100277 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
278 # and should not be decompressed prior to loading.
279 # Setting metadata, so PV mode is used.
280 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Mate Lakatbc2ef922013-08-15 18:06:59 +0100281 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
282 glance \
Sean Dague537d4022013-10-22 07:43:22 -0400283 --os-auth-token $token \
284 --os-image-url http://$GLANCE_HOSTPORT \
285 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100286 --name "$IMAGE_NAME" --is-public=True \
287 --container-format=tgz --disk-format=raw \
288 --property vm_mode=xen < "${IMAGE}"
289 return
290 fi
291
Dean Troyerca0e3d02012-04-13 15:58:37 -0500292 KERNEL=""
293 RAMDISK=""
294 DISK_FORMAT=""
295 CONTAINER_FORMAT=""
296 UNPACK=""
297 case "$IMAGE_FNAME" in
298 *.tar.gz|*.tgz)
299 # Extract ami and aki files
300 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
301 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
302 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
303 xdir="$FILES/images/$IMAGE_NAME"
304 rm -Rf "$xdir";
305 mkdir "$xdir"
306 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
307 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400308 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500309 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400310 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500311 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400312 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500313 if [[ -z "$IMAGE_NAME" ]]; then
314 IMAGE_NAME=$(basename "$IMAGE" ".img")
315 fi
316 ;;
317 *.img)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500318 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500319 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
320 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
321 DISK_FORMAT=$format
322 else
323 DISK_FORMAT=raw
324 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500325 CONTAINER_FORMAT=bare
326 ;;
327 *.img.gz)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500328 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
329 DISK_FORMAT=raw
330 CONTAINER_FORMAT=bare
331 UNPACK=zcat
332 ;;
333 *.qcow2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500334 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
335 DISK_FORMAT=qcow2
336 CONTAINER_FORMAT=bare
337 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100338 *.iso)
Jonathan Michalon06802042013-03-21 14:29:58 +0100339 IMAGE_NAME=$(basename "$IMAGE" ".iso")
340 DISK_FORMAT=iso
341 CONTAINER_FORMAT=bare
342 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500343 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
344 esac
345
Rafael Folcoab775872013-12-02 14:04:32 -0200346 if is_arch "ppc64"; then
347 IMG_PROPERTY="--property hw_disk_bus=scsi --property hw_cdrom_bus=scsi"
348 fi
349
Dean Troyerca0e3d02012-04-13 15:58:37 -0500350 if [ "$CONTAINER_FORMAT" = "bare" ]; then
351 if [ "$UNPACK" = "zcat" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200352 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 -0500353 else
Rafael Folcoab775872013-12-02 14:04:32 -0200354 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 -0500355 fi
356 else
357 # Use glance client to add the kernel the root filesystem.
358 # We parse the results of the first upload to get the glance ID of the
359 # kernel for use when uploading the root filesystem.
360 KERNEL_ID=""; RAMDISK_ID="";
361 if [ -n "$KERNEL" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200362 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 -0500363 fi
364 if [ -n "$RAMDISK" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200365 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 -0500366 fi
Rafael Folcoab775872013-12-02 14:04:32 -0200367 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 -0500368 fi
369}
370
Dean Troyer1a6d4492013-06-03 16:47:36 -0500371
Dean Troyerc1b486a2012-11-05 14:26:09 -0600372# Set the database backend to use
373# When called from stackrc/localrc DATABASE_BACKENDS has not been
374# initialized yet, just save the configuration selection and call back later
375# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100376#
377# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600378function use_database {
379 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600380 # No backends registered means this is likely called from ``localrc``
381 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600382 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000383 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 +0100384 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600385 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100386 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600387 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600388}
389
Dean Troyer1a6d4492013-06-03 16:47:36 -0500390
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600391# Wait for an HTTP server to start answering requests
392# wait_for_service timeout url
393function wait_for_service() {
394 local timeout=$1
395 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800396 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600397}
398
Dean Troyer1a6d4492013-06-03 16:47:36 -0500399
Nachi Uenofda946e2012-10-24 17:26:02 -0700400# ping check
401# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500402# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -0700403function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400404 if is_service_enabled neutron; then
405 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700406 return
407 fi
408 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700409}
410
411# ping check for nova
412# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
413function _ping_check_novanet() {
414 local from_net=$1
415 local ip=$2
416 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700417 local expected=${4:-"True"}
418 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700419 MULTI_HOST=`trueorfalse False $MULTI_HOST`
420 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700421 return
422 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700423 if [[ "$expected" = "True" ]]; then
424 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
425 else
426 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
427 fi
428 if ! timeout $boot_timeout sh -c "$check_command"; then
429 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800430 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700431 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800432 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700433 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700434 fi
435}
436
Nachi Ueno6769b162013-08-12 18:18:56 -0700437# Get ip of instance
438function get_instance_ip(){
439 local vm_id=$1
440 local network_name=$2
441 local nova_result="$(nova show $vm_id)"
442 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
443 if [[ $ip = "" ]];then
444 echo "$nova_result"
445 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700446 fi
447 echo $ip
448}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500449
Nachi Uenofda946e2012-10-24 17:26:02 -0700450# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700451
Dean Troyer1a6d4492013-06-03 16:47:36 -0500452# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -0700453function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400454 if is_service_enabled neutron; then
455 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700456 return
457 fi
458 _ssh_check_novanet "$1" $2 $3 $4 $5
459}
460
461function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -0700462 local NET_NAME=$1
463 local KEY_FILE=$2
464 local FLOATING_IP=$3
465 local DEFAULT_INSTANCE_USER=$4
466 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600467 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500468 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 -0800469 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700470 fi
471}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500472
Vincent Untz856a11e2012-11-21 16:04:12 +0100473
Vincent Untz856a11e2012-11-21 16:04:12 +0100474# Get the location of the $module-rootwrap executables, where module is cinder
475# or nova.
476# get_rootwrap_location module
477function get_rootwrap_location() {
478 local module=$1
479
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100480 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100481}
482
Dean Troyer1a6d4492013-06-03 16:47:36 -0500483
Ian Wienand0488edd2013-04-11 12:04:36 +1000484# Path permissions sanity check
485# check_path_perm_sanity path
486function check_path_perm_sanity() {
487 # Ensure no element of the path has 0700 permissions, which is very
488 # likely to cause issues for daemons. Inspired by default 0700
489 # homedir permissions on RHEL and common practice of making DEST in
490 # the stack user's homedir.
491
492 local real_path=$(readlink -f $1)
493 local rebuilt_path=""
494 for i in $(echo ${real_path} | tr "/" " "); do
495 rebuilt_path=$rebuilt_path"/"$i
496
497 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
498 echo "*** DEST path element"
499 echo "*** ${rebuilt_path}"
500 echo "*** appears to have 0700 permissions."
501 echo "*** This is very likely to cause fatal issues for devstack daemons."
502
503 if [[ -n "$SKIP_PATH_SANITY" ]]; then
504 return
505 else
506 echo "*** Set SKIP_PATH_SANITY to skip this check"
507 die $LINENO "Invalid path permissions"
508 fi
509 fi
510 done
511}
512
Dean Troyer1a6d4492013-06-03 16:47:36 -0500513
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000514# This function recursively compares versions, and is not meant to be
515# called by anything other than vercmp_numbers below. This function does
516# not work with alphabetic versions.
517#
518# _vercmp_r sep ver1 ver2
519function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400520 typeset sep
521 typeset -a ver1=() ver2=()
522 sep=$1; shift
523 ver1=("${@:1:sep}")
524 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000525
Sean Dague537d4022013-10-22 07:43:22 -0400526 if ((ver1 > ver2)); then
527 echo 1; return 0
528 elif ((ver2 > ver1)); then
529 echo -1; return 0
530 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000531
Sean Dague537d4022013-10-22 07:43:22 -0400532 if ((sep <= 1)); then
533 echo 0; return 0
534 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000535
Sean Dague537d4022013-10-22 07:43:22 -0400536 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000537}
538
539
540# This function compares two versions and is meant to be called by
541# external callers. Please note the function assumes non-alphabetic
542# versions. For example, this will work:
543#
544# vercmp_numbers 1.10 1.4
545#
546# The above will return "1", as 1.10 is greater than 1.4.
547#
548# vercmp_numbers 5.2 6.4
549#
550# The above will return "-1", as 5.2 is less than 6.4.
551#
552# vercmp_numbers 4.0 4.0
553#
554# The above will return "0", as the versions are equal.
555#
556# vercmp_numbers ver1 ver2
557vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -0400558 typeset v1=$1 v2=$2 sep
559 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000560
Sean Dague537d4022013-10-22 07:43:22 -0400561 IFS=. read -ra ver1 <<< "$v1"
562 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000563
Sean Dague537d4022013-10-22 07:43:22 -0400564 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000565}
566
567
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700568# This function sets log formatting options for colorizing log
569# output to stdout. It is meant to be called by lib modules.
570# The last two parameters are optional and can be used to specify
571# non-default value for project and user format variables.
572# Defaults are respectively 'project_name' and 'user_name'
573#
574# setup_colorized_logging something.conf SOMESECTION
575function setup_colorized_logging() {
576 local conf_file=$1
577 local conf_section=$2
578 local project_var=${3:-"project_name"}
579 local user_var=${4:-"user_name"}
580 # Add color to logging output
581 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"
582 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"
583 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
584 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
585}
586
Dean Troyerdff49a22014-01-30 15:37:40 -0600587
Dean Troyer27e32692012-03-16 16:16:56 -0500588# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000589$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500590
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500591# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400592# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400593# End: