blob: 6979c6c155794e62182abf3b8b05c4b97c74796c [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
Monty Taylor408a4a72013-08-02 15:43:47 -040047# ``pip install -e`` the package, which processes the dependencies
48# using pip before running `setup.py develop`
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000049#
50# Updates the dependencies in project_dir from the
51# openstack/requirements global list before installing anything.
52#
Dean Troyerd8864fe2014-02-17 11:00:42 -060053# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
Dean Troyerbbafb1b2012-06-11 16:51:39 -050054# setup_develop directory
55function setup_develop() {
Sean Dague6c844632013-07-31 06:50:14 -040056 local project_dir=$1
Sean Dague6c844632013-07-31 06:50:14 -040057
58 echo "cd $REQUIREMENTS_DIR; $SUDO_CMD python update.py $project_dir"
59
Dean Troyer62d1d692013-08-01 17:40:40 -050060 # Don't update repo if local changes exist
IWAMOTO Toshihiro0b8f6e02014-01-23 12:02:34 +090061 # Don't use buggy "git diff --quiet"
62 (cd $project_dir && git diff --exit-code >/dev/null)
Doug Hellmannc3431bf2013-09-06 15:30:22 -040063 local update_requirements=$?
64
65 if [ $update_requirements -eq 0 ]; then
Dean Troyer62d1d692013-08-01 17:40:40 -050066 (cd $REQUIREMENTS_DIR; \
67 $SUDO_CMD python update.py $project_dir)
68 fi
Sean Dague6c844632013-07-31 06:50:14 -040069
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000070 setup_develop_no_requirements_update $project_dir
Doug Hellmannc3431bf2013-09-06 15:30:22 -040071
Sean Daguefd98edb2013-10-24 14:57:59 -040072 # We've just gone and possibly modified the user's source tree in an
73 # automated way, which is considered bad form if it's a development
74 # tree because we've screwed up their next git checkin. So undo it.
75 #
76 # However... there are some circumstances, like running in the gate
77 # where we really really want the overridden version to stick. So provide
78 # a variable that tells us whether or not we should UNDO the requirements
79 # changes (this will be set to False in the OpenStack ci gate)
DennyZhang89d41ca2013-11-01 15:41:01 -050080 if [ $UNDO_REQUIREMENTS = "True" ]; then
Sean Daguefd98edb2013-10-24 14:57:59 -040081 if [ $update_requirements -eq 0 ]; then
82 (cd $project_dir && git reset --hard)
83 fi
Doug Hellmannc3431bf2013-09-06 15:30:22 -040084 fi
Dean Troyerbbafb1b2012-06-11 16:51:39 -050085}
86
Dean Troyerdff49a22014-01-30 15:37:40 -060087
Doug Hellmannaaac4ee2013-11-18 22:12:46 +000088# ``pip install -e`` the package, which processes the dependencies
89# using pip before running `setup.py develop`
90# Uses globals ``STACK_USER``
91# setup_develop_no_requirements_update directory
92function setup_develop_no_requirements_update() {
93 local project_dir=$1
94
95 pip_install -e $project_dir
96 # ensure that further actions can do things like setup.py sdist
97 safe_chown -R $STACK_USER $1/*.egg-info
98}
99
Dean Troyerbbafb1b2012-06-11 16:51:39 -0500100
Adam Spierscb961592013-10-05 12:11:07 +0100101# Retrieve an image from a URL and upload into Glance.
Dean Troyerca0e3d02012-04-13 15:58:37 -0500102# Uses the following variables:
Adam Spierscb961592013-10-05 12:11:07 +0100103#
104# - ``FILES`` must be set to the cache dir
105# - ``GLANCE_HOSTPORT``
106#
Dean Troyerca0e3d02012-04-13 15:58:37 -0500107# upload_image image-url glance-token
108function upload_image() {
109 local image_url=$1
110 local token=$2
111
112 # Create a directory for the downloaded image tarballs.
113 mkdir -p $FILES/images
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800114 IMAGE_FNAME=`basename "$image_url"`
Arnaud Legendre3e439442013-11-15 16:06:03 -0800115 if [[ $image_url != file* ]]; then
116 # Downloads the image (uec ami+aki style), then extracts it.
Arnaud Legendre3e439442013-11-15 16:06:03 -0800117 if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900118 wget -c $image_url -O $FILES/$IMAGE_FNAME
119 if [[ $? -ne 0 ]]; then
120 echo "Not found: $image_url"
121 return
122 fi
Arnaud Legendre3e439442013-11-15 16:06:03 -0800123 fi
124 IMAGE="$FILES/${IMAGE_FNAME}"
125 else
126 # File based URL (RFC 1738): file://host/path
127 # Remote files are not considered here.
128 # *nix: file:///home/user/path/file
129 # windows: file:///C:/Documents%20and%20Settings/user/path/file
130 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
131 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500132 echo "Not found: $image_url"
133 return
134 fi
135 fi
136
137 # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
138 if [[ "$image_url" =~ 'openvz' ]]; then
Dean Troyerca0e3d02012-04-13 15:58:37 -0500139 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
140 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}"
141 return
142 fi
143
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700144 # vmdk format images
145 if [[ "$image_url" =~ '.vmdk' ]]; then
Sreeram Yerrapragadacbaff862013-07-24 19:49:23 -0700146 IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700147
148 # Before we can upload vmdk type images to glance, we need to know it's
149 # disk type, storage adapter, and networking adapter. These values are
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800150 # passed to glance as custom properties.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700151 # We take these values from the vmdk file if populated. Otherwise, we use
Ryan Hsua6273b92013-09-04 23:51:29 -0700152 # vmdk filename, which is expected in the following format:
153 #
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800154 # <name>-<disk type>;<storage adapter>;<network adapter>
Ryan Hsua6273b92013-09-04 23:51:29 -0700155 #
156 # If the filename does not follow the above format then the vsphere
157 # driver will supply default values.
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700158
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800159 vmdk_adapter_type=""
160 vmdk_disktype=""
161 vmdk_net_adapter=""
162
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700163 # vmdk adapter type
164 vmdk_adapter_type="$(head -25 $IMAGE | grep -a -F -m 1 'ddb.adapterType =' $IMAGE)"
165 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
166 vmdk_adapter_type="${vmdk_adapter_type%?}"
167
168 # vmdk disk type
169 vmdk_create_type="$(head -25 $IMAGE | grep -a -F -m 1 'createType=' $IMAGE)"
170 vmdk_create_type="${vmdk_create_type#*\"}"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800171 vmdk_create_type="${vmdk_create_type%\"*}"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800172
173 descriptor_data_pair_msg="Monolithic flat and VMFS disks "`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900174 `"should use a descriptor-data pair."
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700175 if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then
176 vmdk_disktype="sparse"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800177 elif [[ "$vmdk_create_type" = "monolithicFlat" || \
178 "$vmdk_create_type" = "vmfs" ]]; then
179 # Attempt to retrieve the *-flat.vmdk
180 flat_fname="$(head -25 $IMAGE | grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE)"
181 flat_fname="${flat_fname#*\"}"
182 flat_fname="${flat_fname%?}"
183 if [[ -z "$flat_name" ]]; then
184 flat_fname="$IMAGE_NAME-flat.vmdk"
185 fi
186 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
187 flat_url="${image_url:0:$path_len}$flat_fname"
188 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900189 `" Attempt to retrieve the *-flat.vmdk: $flat_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800190 if [[ $flat_url != file* ]]; then
191 if [[ ! -f $FILES/$flat_fname || \
192 "$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then
193 wget -c $flat_url -O $FILES/$flat_fname
194 if [[ $? -ne 0 ]]; then
195 echo "Flat disk not found: $flat_url"
196 flat_found=false
197 fi
198 fi
199 if $flat_found; then
200 IMAGE="$FILES/${flat_fname}"
201 fi
202 else
203 IMAGE=$(echo $flat_url | sed "s/^file:\/\///g")
204 if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
205 echo "Flat disk not found: $flat_url"
206 flat_found=false
207 fi
208 if ! $flat_found; then
209 IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
210 fi
211 fi
212 if $flat_found; then
213 IMAGE_NAME="${flat_fname}"
214 fi
215 vmdk_disktype="preallocated"
Arnaud Legendre8dad4bd2014-02-03 17:57:39 -0800216 elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then
217 vmdk_disktype="streamOptimized"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800218 elif [[ -z "$vmdk_create_type" ]]; then
219 # *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk)
220 # to retrieve appropriate metadata
221 if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then
222 warn $LINENO "Expected filename suffix: '-flat'."`
223 `" Filename provided: ${IMAGE_NAME}"
224 else
225 descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk"
226 path_len=`expr ${#image_url} - ${#IMAGE_FNAME}`
227 flat_path="${image_url:0:$path_len}"
228 descriptor_url=$flat_path$descriptor_fname
229 warn $LINENO "$descriptor_data_pair_msg"`
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900230 `" Attempt to retrieve the descriptor *.vmdk: $descriptor_url"
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800231 if [[ $flat_path != file* ]]; then
232 if [[ ! -f $FILES/$descriptor_fname || \
233 "$(stat -c "%s" $FILES/$descriptor_fname)" = "0" ]]; then
234 wget -c $descriptor_url -O $FILES/$descriptor_fname
235 if [[ $? -ne 0 ]]; then
236 warn $LINENO "Descriptor not found $descriptor_url"
237 descriptor_found=false
238 fi
239 fi
240 descriptor_url="$FILES/$descriptor_fname"
241 else
242 descriptor_url=$(echo $descriptor_url | sed "s/^file:\/\///g")
243 if [[ ! -f $descriptor_url || \
244 "$(stat -c "%s" $descriptor_url)" == "0" ]]; then
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900245 warn $LINENO "Descriptor not found $descriptor_url"
246 descriptor_found=false
Arnaud Legendre90bcd2f2013-11-22 16:05:39 -0800247 fi
248 fi
249 if $descriptor_found; then
250 vmdk_adapter_type="$(head -25 $descriptor_url |"`
251 `"grep -a -F -m 1 'ddb.adapterType =' $descriptor_url)"
252 vmdk_adapter_type="${vmdk_adapter_type#*\"}"
253 vmdk_adapter_type="${vmdk_adapter_type%?}"
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900254 fi
255 fi
Isaku Yamahata6681a4f2014-01-10 15:28:29 +0900256 vmdk_disktype="preallocated"
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700257 else
Arnaud Legendre5ea53ee2013-11-01 16:42:54 -0700258 vmdk_disktype="preallocated"
259 fi
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800260
261 # NOTE: For backwards compatibility reasons, colons may be used in place
262 # of semi-colons for property delimiters but they are not permitted
263 # characters in NTFS filesystems.
Arnaud Legendreb93cd642014-01-23 17:12:21 -0800264 property_string=`echo "$IMAGE_NAME" | grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$'`
Ryan Hsubfb3e5e2013-11-11 21:20:14 -0800265 IFS=':;' read -a props <<< "$property_string"
266 vmdk_disktype="${props[0]:-$vmdk_disktype}"
267 vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}"
268 vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}"
Ryan Hsua6273b92013-09-04 23:51:29 -0700269
Ryan Hsu49f44862013-10-03 22:27:03 -0700270 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 -0700271 return
272 fi
273
Mate Lakatbc2ef922013-08-15 18:06:59 +0100274 # XenServer-vhd-ovf-format images are provided as .vhd.tgz
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500275 # and should not be decompressed prior to loading
276 if [[ "$image_url" =~ '.vhd.tgz' ]]; then
Davanum Srinivas316ed6c2013-02-06 15:29:49 -0500277 IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
278 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}"
279 return
280 fi
281
Mate Lakatbc2ef922013-08-15 18:06:59 +0100282 # .xen-raw.tgz suggests a Xen capable raw image inside a tgz.
283 # and should not be decompressed prior to loading.
284 # Setting metadata, so PV mode is used.
285 if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
Mate Lakatbc2ef922013-08-15 18:06:59 +0100286 IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
287 glance \
Sean Dague537d4022013-10-22 07:43:22 -0400288 --os-auth-token $token \
289 --os-image-url http://$GLANCE_HOSTPORT \
290 image-create \
Mate Lakatbc2ef922013-08-15 18:06:59 +0100291 --name "$IMAGE_NAME" --is-public=True \
292 --container-format=tgz --disk-format=raw \
293 --property vm_mode=xen < "${IMAGE}"
294 return
295 fi
296
Dean Troyerca0e3d02012-04-13 15:58:37 -0500297 KERNEL=""
298 RAMDISK=""
299 DISK_FORMAT=""
300 CONTAINER_FORMAT=""
301 UNPACK=""
302 case "$IMAGE_FNAME" in
303 *.tar.gz|*.tgz)
304 # Extract ami and aki files
305 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
306 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
307 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
308 xdir="$FILES/images/$IMAGE_NAME"
309 rm -Rf "$xdir";
310 mkdir "$xdir"
311 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
312 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400313 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500314 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400315 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500316 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
Sean Dague537d4022013-10-22 07:43:22 -0400317 [ -f "$f" ] && echo "$f" && break; done; true)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500318 if [[ -z "$IMAGE_NAME" ]]; then
319 IMAGE_NAME=$(basename "$IMAGE" ".img")
320 fi
321 ;;
322 *.img)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500323 IMAGE_NAME=$(basename "$IMAGE" ".img")
Dean Troyer636a3ff2012-09-14 11:36:07 -0500324 format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
325 if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
326 DISK_FORMAT=$format
327 else
328 DISK_FORMAT=raw
329 fi
Dean Troyerca0e3d02012-04-13 15:58:37 -0500330 CONTAINER_FORMAT=bare
331 ;;
332 *.img.gz)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500333 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
334 DISK_FORMAT=raw
335 CONTAINER_FORMAT=bare
336 UNPACK=zcat
337 ;;
338 *.qcow2)
Dean Troyerca0e3d02012-04-13 15:58:37 -0500339 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
340 DISK_FORMAT=qcow2
341 CONTAINER_FORMAT=bare
342 ;;
Jonathan Michalon06802042013-03-21 14:29:58 +0100343 *.iso)
Jonathan Michalon06802042013-03-21 14:29:58 +0100344 IMAGE_NAME=$(basename "$IMAGE" ".iso")
345 DISK_FORMAT=iso
346 CONTAINER_FORMAT=bare
347 ;;
Dean Troyerca0e3d02012-04-13 15:58:37 -0500348 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
349 esac
350
Rafael Folcoab775872013-12-02 14:04:32 -0200351 if is_arch "ppc64"; then
352 IMG_PROPERTY="--property hw_disk_bus=scsi --property hw_cdrom_bus=scsi"
353 fi
354
Dean Troyerca0e3d02012-04-13 15:58:37 -0500355 if [ "$CONTAINER_FORMAT" = "bare" ]; then
356 if [ "$UNPACK" = "zcat" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200357 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 -0500358 else
Rafael Folcoab775872013-12-02 14:04:32 -0200359 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 -0500360 fi
361 else
362 # Use glance client to add the kernel the root filesystem.
363 # We parse the results of the first upload to get the glance ID of the
364 # kernel for use when uploading the root filesystem.
365 KERNEL_ID=""; RAMDISK_ID="";
366 if [ -n "$KERNEL" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200367 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 -0500368 fi
369 if [ -n "$RAMDISK" ]; then
Rafael Folcoab775872013-12-02 14:04:32 -0200370 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 -0500371 fi
Rafael Folcoab775872013-12-02 14:04:32 -0200372 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 -0500373 fi
374}
375
Dean Troyer1a6d4492013-06-03 16:47:36 -0500376
Dean Troyerc1b486a2012-11-05 14:26:09 -0600377# Set the database backend to use
378# When called from stackrc/localrc DATABASE_BACKENDS has not been
379# initialized yet, just save the configuration selection and call back later
380# to validate it.
Adam Spierscb961592013-10-05 12:11:07 +0100381#
382# ``$1`` - the name of the database backend to use (mysql, postgresql, ...)
Dean Troyerc1b486a2012-11-05 14:26:09 -0600383function use_database {
384 if [[ -z "$DATABASE_BACKENDS" ]]; then
Dean Troyerafc29fe2013-02-07 15:56:24 -0600385 # No backends registered means this is likely called from ``localrc``
386 # This is now deprecated usage
Dean Troyerc1b486a2012-11-05 14:26:09 -0600387 DATABASE_TYPE=$1
Bob Ball3aa88872013-02-28 17:39:41 +0000388 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 +0100389 else
Dean Troyerafc29fe2013-02-07 15:56:24 -0600390 # This should no longer get called...here for posterity
Attila Fazekas251d3b52012-12-16 15:05:44 +0100391 use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1
Dean Troyerc1b486a2012-11-05 14:26:09 -0600392 fi
Dean Troyerc1b486a2012-11-05 14:26:09 -0600393}
394
Dean Troyer1a6d4492013-06-03 16:47:36 -0500395
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600396# Wait for an HTTP server to start answering requests
397# wait_for_service timeout url
398function wait_for_service() {
399 local timeout=$1
400 local url=$2
JUN JIE NAN0aa85342013-09-13 15:47:09 +0800401 timeout $timeout sh -c "while ! curl --noproxy '*' -s $url >/dev/null; do sleep 1; done"
Dean Troyer3a3a2ba2012-12-11 15:26:24 -0600402}
403
Dean Troyer1a6d4492013-06-03 16:47:36 -0500404
Nachi Uenofda946e2012-10-24 17:26:02 -0700405# ping check
406# Uses globals ``ENABLED_SERVICES``
Dean Troyer1a6d4492013-06-03 16:47:36 -0500407# ping_check from-net ip boot-timeout expected
Nachi Uenofda946e2012-10-24 17:26:02 -0700408function ping_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400409 if is_service_enabled neutron; then
410 _ping_check_neutron "$1" $2 $3 $4
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700411 return
412 fi
413 _ping_check_novanet "$1" $2 $3 $4
Nachi Uenofda946e2012-10-24 17:26:02 -0700414}
415
416# ping check for nova
417# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
418function _ping_check_novanet() {
419 local from_net=$1
420 local ip=$2
421 local boot_timeout=$3
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700422 local expected=${4:-"True"}
423 local check_command=""
Nachi Uenofda946e2012-10-24 17:26:02 -0700424 MULTI_HOST=`trueorfalse False $MULTI_HOST`
425 if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
Nachi Uenofda946e2012-10-24 17:26:02 -0700426 return
427 fi
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700428 if [[ "$expected" = "True" ]]; then
429 check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
430 else
431 check_command="while ping -c1 -w1 $ip; do sleep 1; done"
432 fi
433 if ! timeout $boot_timeout sh -c "$check_command"; then
434 if [[ "$expected" = "True" ]]; then
Nachi Ueno07115eb2013-02-26 12:38:18 -0800435 die $LINENO "[Fail] Couldn't ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700436 else
Nachi Ueno07115eb2013-02-26 12:38:18 -0800437 die $LINENO "[Fail] Could ping server"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700438 fi
Nachi Uenofda946e2012-10-24 17:26:02 -0700439 fi
440}
441
Nachi Ueno6769b162013-08-12 18:18:56 -0700442# Get ip of instance
443function get_instance_ip(){
444 local vm_id=$1
445 local network_name=$2
446 local nova_result="$(nova show $vm_id)"
447 local ip=$(echo "$nova_result" | grep "$network_name" | get_field 2)
448 if [[ $ip = "" ]];then
449 echo "$nova_result"
450 die $LINENO "[Fail] Coudn't get ipaddress of VM"
Nachi Ueno6769b162013-08-12 18:18:56 -0700451 fi
452 echo $ip
453}
Dean Troyer1a6d4492013-06-03 16:47:36 -0500454
Nachi Uenofda946e2012-10-24 17:26:02 -0700455# ssh check
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700456
Dean Troyer1a6d4492013-06-03 16:47:36 -0500457# ssh_check net-name key-file floating-ip default-user active-timeout
Nachi Uenofda946e2012-10-24 17:26:02 -0700458function ssh_check() {
Mark McClainb05c8762013-07-06 23:29:39 -0400459 if is_service_enabled neutron; then
460 _ssh_check_neutron "$1" $2 $3 $4 $5
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700461 return
462 fi
463 _ssh_check_novanet "$1" $2 $3 $4 $5
464}
465
466function _ssh_check_novanet() {
Nachi Uenofda946e2012-10-24 17:26:02 -0700467 local NET_NAME=$1
468 local KEY_FILE=$2
469 local FLOATING_IP=$3
470 local DEFAULT_INSTANCE_USER=$4
471 local ACTIVE_TIMEOUT=$5
Dean Troyer6931c132012-11-07 16:51:21 -0600472 local probe_cmd=""
Dean Troyercc6b4432013-04-08 15:38:03 -0500473 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 -0800474 die $LINENO "server didn't become ssh-able!"
Nachi Uenofda946e2012-10-24 17:26:02 -0700475 fi
476}
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500477
Vincent Untz856a11e2012-11-21 16:04:12 +0100478
Vincent Untz856a11e2012-11-21 16:04:12 +0100479# Get the location of the $module-rootwrap executables, where module is cinder
480# or nova.
481# get_rootwrap_location module
482function get_rootwrap_location() {
483 local module=$1
484
Jakub Ruzicka4196d552013-01-30 15:35:54 +0100485 echo "$(get_python_exec_prefix)/$module-rootwrap"
Vincent Untz856a11e2012-11-21 16:04:12 +0100486}
487
Dean Troyer1a6d4492013-06-03 16:47:36 -0500488
Ian Wienand0488edd2013-04-11 12:04:36 +1000489# Path permissions sanity check
490# check_path_perm_sanity path
491function check_path_perm_sanity() {
492 # Ensure no element of the path has 0700 permissions, which is very
493 # likely to cause issues for daemons. Inspired by default 0700
494 # homedir permissions on RHEL and common practice of making DEST in
495 # the stack user's homedir.
496
497 local real_path=$(readlink -f $1)
498 local rebuilt_path=""
499 for i in $(echo ${real_path} | tr "/" " "); do
500 rebuilt_path=$rebuilt_path"/"$i
501
502 if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then
503 echo "*** DEST path element"
504 echo "*** ${rebuilt_path}"
505 echo "*** appears to have 0700 permissions."
506 echo "*** This is very likely to cause fatal issues for devstack daemons."
507
508 if [[ -n "$SKIP_PATH_SANITY" ]]; then
509 return
510 else
511 echo "*** Set SKIP_PATH_SANITY to skip this check"
512 die $LINENO "Invalid path permissions"
513 fi
514 fi
515 done
516}
517
Dean Troyer1a6d4492013-06-03 16:47:36 -0500518
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000519# This function recursively compares versions, and is not meant to be
520# called by anything other than vercmp_numbers below. This function does
521# not work with alphabetic versions.
522#
523# _vercmp_r sep ver1 ver2
524function _vercmp_r {
Sean Dague537d4022013-10-22 07:43:22 -0400525 typeset sep
526 typeset -a ver1=() ver2=()
527 sep=$1; shift
528 ver1=("${@:1:sep}")
529 ver2=("${@:sep+1}")
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000530
Sean Dague537d4022013-10-22 07:43:22 -0400531 if ((ver1 > ver2)); then
532 echo 1; return 0
533 elif ((ver2 > ver1)); then
534 echo -1; return 0
535 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000536
Sean Dague537d4022013-10-22 07:43:22 -0400537 if ((sep <= 1)); then
538 echo 0; return 0
539 fi
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000540
Sean Dague537d4022013-10-22 07:43:22 -0400541 _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000542}
543
544
545# This function compares two versions and is meant to be called by
546# external callers. Please note the function assumes non-alphabetic
547# versions. For example, this will work:
548#
549# vercmp_numbers 1.10 1.4
550#
551# The above will return "1", as 1.10 is greater than 1.4.
552#
553# vercmp_numbers 5.2 6.4
554#
555# The above will return "-1", as 5.2 is less than 6.4.
556#
557# vercmp_numbers 4.0 4.0
558#
559# The above will return "0", as the versions are equal.
560#
561# vercmp_numbers ver1 ver2
562vercmp_numbers() {
Sean Dague537d4022013-10-22 07:43:22 -0400563 typeset v1=$1 v2=$2 sep
564 typeset -a ver1 ver2
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000565
Sean Dague537d4022013-10-22 07:43:22 -0400566 IFS=. read -ra ver1 <<< "$v1"
567 IFS=. read -ra ver2 <<< "$v2"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000568
Sean Dague537d4022013-10-22 07:43:22 -0400569 _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
Kyle Mestery51a3f1f2013-06-13 11:47:56 +0000570}
571
572
Salvatore Orlando05ae8332013-08-20 14:51:08 -0700573# This function sets log formatting options for colorizing log
574# output to stdout. It is meant to be called by lib modules.
575# The last two parameters are optional and can be used to specify
576# non-default value for project and user format variables.
577# Defaults are respectively 'project_name' and 'user_name'
578#
579# setup_colorized_logging something.conf SOMESECTION
580function setup_colorized_logging() {
581 local conf_file=$1
582 local conf_section=$2
583 local project_var=${3:-"project_name"}
584 local user_var=${4:-"user_name"}
585 # Add color to logging output
586 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"
587 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"
588 iniset $conf_file $conf_section logging_debug_format_suffix "from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
589 iniset $conf_file $conf_section logging_exception_prefix "%(color)s%(asctime)s.%(msecs)03d TRACE %(name)s %(instance)s"
590}
591
Dean Troyerdff49a22014-01-30 15:37:40 -0600592
Dean Troyer27e32692012-03-16 16:16:56 -0500593# Restore xtrace
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000594$XTRACE
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500595
Dean Troyer4a43b7b2012-08-28 17:43:40 -0500596# Local variables:
Sean Dague584d90e2013-03-29 14:34:53 -0400597# mode: shell-script
Andrew Laskif900bd72012-09-05 17:23:14 -0400598# End: