-
Notifications
You must be signed in to change notification settings - Fork 23
/
get-resource.sh
executable file
·122 lines (106 loc) · 5.67 KB
/
get-resource.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash -xe
# Check and set http(s)_proxy. Required for cURL to use a proxy
export http_proxy=${http_proxy:-$HTTP_PROXY}
export https_proxy=${https_proxy:-$HTTPS_PROXY}
export no_proxy=${no_proxy:-$NO_PROXY}
export CURL_CA_BUNDLE=${CURL_CA_BUNDLE:-/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem}
export IP_OPTIONS=${IP_OPTIONS:-}
# Which image should we use
export RHCOS_IMAGE_URL=${1:-$RHCOS_IMAGE_URL}
if [ -z "$RHCOS_IMAGE_URL" ] ; then
echo "No image URL provided"
exit 1
fi
# When provided by openshift-installer the URL is like
# "https://releases-art-rhcos.svc.ci.openshift.org/art/storage/releases/rhcos-4.2/420.8.20190708.2/rhcos-420.8.20190708.2-openstack.qcow2.gz?sha256=xxx"
# NOTE: strip sha256 query string in the image url
RHCOS_IMAGE_URL_STRIPPED=`echo $RHCOS_IMAGE_URL | cut -f 1 -d \?`
if [[ $RHCOS_IMAGE_URL_STRIPPED =~ qcow2(\.[gx]z)?$ ]]; then
RHCOS_IMAGE_FILENAME_RAW=$(basename $RHCOS_IMAGE_URL_STRIPPED)
RHCOS_IMAGE_FILENAME_QCOW=${RHCOS_IMAGE_FILENAME_RAW/%.[gx]z}
IMAGE_FILENAME_EXTENSION=${RHCOS_IMAGE_FILENAME_RAW/$RHCOS_IMAGE_FILENAME_QCOW}
IMAGE_URL=$(dirname $RHCOS_IMAGE_URL_STRIPPED)
else
echo "Unexpected image format $RHCOS_IMAGE_URL"
exit 1
fi
RHCOS_IMAGE_FILENAME_COMPRESSED=${RHCOS_IMAGE_FILENAME_QCOW/-openstack/-compressed}
RHCOS_IMAGE_FILENAME_CACHED=cached-${RHCOS_IMAGE_FILENAME_QCOW}
FFILENAME="rhcos-ootpa-latest.qcow2"
mkdir -p /shared/html/images
# Added to fix image path incompatibility between 4.7 and 4.8
# as filed in https://bugzilla.redhat.com/show_bug.cgi?id=1972572
if [[ ${RHCOS_IMAGE_FILENAME_QCOW} == *"-openstack"* && -s "/shared/html/images/$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_COMPRESSED.md5sum" && ! -s "/shared/html/images/$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum" ]]; then
cd /shared/html/images
mv "${RHCOS_IMAGE_FILENAME_QCOW}/$RHCOS_IMAGE_FILENAME_COMPRESSED.md5sum" "${RHCOS_IMAGE_FILENAME_QCOW}/$RHCOS_IMAGE_FILENAME_CACHED.md5sum"
mv "${RHCOS_IMAGE_FILENAME_QCOW}/$RHCOS_IMAGE_FILENAME_COMPRESSED" "${RHCOS_IMAGE_FILENAME_QCOW}/$RHCOS_IMAGE_FILENAME_CACHED"
ln -sf "$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED" $FFILENAME
ln -sf "$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum" "$FFILENAME.md5sum"
fi
mkdir -p /shared/tmp
TMPDIR=$(mktemp -d -p /shared/tmp)
trap "rm -fr $TMPDIR" EXIT
cd $TMPDIR
# curl doesn't handle NO_PROXY the same way as code written in golang
# clear the proxy variables if needed to mimic handling them the golang way
if clearproxy "${IMAGE_URL}/${RHCOS_IMAGE_FILENAME_RAW}" ; then
unset HTTP_PROXY http_proxy HTTPS_PROXY https_proxy
fi
# We have a file in the cache that matches the one we want, use it
if [ -s "/shared/html/images/$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum" ]; then
echo "$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum found, contents:"
cat /shared/html/images/$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum
else
CONNECT_TIMEOUT=120
MAX_ATTEMPTS=5
for i in $(seq ${MAX_ATTEMPTS}); do
if ! curl -v -g --compressed -L --fail --connect-timeout ${CONNECT_TIMEOUT} -o "${RHCOS_IMAGE_FILENAME_RAW}" "${IMAGE_URL}/${RHCOS_IMAGE_FILENAME_RAW}"; then
if (( ${i} == ${MAX_ATTEMPTS} )); then
echo "Download failed."
exit 1
else
SLEEP_TIME=$((i*i))
echo "Download failed, retrying after ${SLEEP_TIME} seconds..."
sleep ${SLEEP_TIME}
fi
else
break
fi
done
if [[ $IMAGE_FILENAME_EXTENSION == .gz ]]; then
gzip -d "$RHCOS_IMAGE_FILENAME_RAW"
elif [[ $IMAGE_FILENAME_EXTENSION == .xz ]]; then
unxz "$RHCOS_IMAGE_FILENAME_RAW"
fi
if [ -n "$IP_OPTIONS" ] ; then
BOOT_DISK=$(LIBGUESTFS_BACKEND=direct virt-filesystems -a "$RHCOS_IMAGE_FILENAME_QCOW" -l | grep boot | cut -f1 -d" ")
# Iterate all available ostree boot configurations to support RHCOS/FCOS seamlessly
BOOT_ENTRIES=$(LIBGUESTFS_BACKEND=direct virt-ls -a "$RHCOS_IMAGE_FILENAME_QCOW" -m "$BOOT_DISK" /boot/loader/entries)
for BOOT_ENTRY in $BOOT_ENTRIES; do
if [[ $BOOT_ENTRY =~ ostree-1-.*.conf ]] ; then
LIBGUESTFS_BACKEND=direct virt-edit -a "$RHCOS_IMAGE_FILENAME_QCOW" -m "$BOOT_DISK" "/boot/loader/entries/$BOOT_ENTRY" -e "s/^options/options ${IP_OPTIONS}/"
fi
done
fi
# For compatibity we need to keep both $RHCOS_IMAGE_FILENAME_QCOW and $RHCOS_IMAGE_FILENAME_CACHED
# RHCOS_IMAGE_FILENAME_QCOW is downloaded by the image-cache pods when they initialize
# RHCOS_IMAGE_FILENAME_CACHED is used by IPA when provisioning
ln -sf "$RHCOS_IMAGE_FILENAME_QCOW" "$RHCOS_IMAGE_FILENAME_CACHED"
md5sum "$RHCOS_IMAGE_FILENAME_CACHED" | cut -f 1 -d " " > "$RHCOS_IMAGE_FILENAME_CACHED.md5sum"
fi
if [ -s "${RHCOS_IMAGE_FILENAME_CACHED}.md5sum" ] ; then
cd /shared/html/images
chmod 755 $TMPDIR
rm -rf $RHCOS_IMAGE_FILENAME_QCOW
mv $TMPDIR $RHCOS_IMAGE_FILENAME_QCOW
ln -sf "$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED" $FFILENAME
ln -sf "$RHCOS_IMAGE_FILENAME_QCOW/$RHCOS_IMAGE_FILENAME_CACHED.md5sum" "$FFILENAME.md5sum"
fi
# For backwards compatibility, if the rhcos image name contains -openstack, we want to
# create a symlink to the original in the old format. The old format had a substitution
# which required the existence of -openstack in the name.
if [[ ${RHCOS_IMAGE_FILENAME_QCOW} == *"-openstack"* ]] ; then
cd "/shared/html/images/${RHCOS_IMAGE_FILENAME_QCOW}"
ln -sf "$RHCOS_IMAGE_FILENAME_CACHED" "$RHCOS_IMAGE_FILENAME_COMPRESSED"
ln -sf "$RHCOS_IMAGE_FILENAME_CACHED.md5sum" "$RHCOS_IMAGE_FILENAME_COMPRESSED.md5sum"
fi