-
Notifications
You must be signed in to change notification settings - Fork 270
/
setup.sh
executable file
·131 lines (115 loc) · 4.41 KB
/
setup.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
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
trap "exit 1" TERM
export TOP_PID=$$
NAMESPACE="${2:-containers-quickstarts-tests}"
TRAVIS_REPO_SLUG="${3:-redhat-cop/containers-quickstarts}"
TRAVIS_BRANCH="${4:-master}"
CHAINED_BUILDS=("jenkins-agent-image-mgmt")
TEST_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
. ${TEST_DIR}/common.sh
cluster_up() {
set +e
built=false
while true; do
if [ -z $IP_ADDR ]; then
DEV=$(ip link | awk '/state UP/{ gsub(":", ""); print $2}')
IP_ADDR=$(ip addr show $DEV | awk '/inet /{ gsub("/.*", ""); print $2}')
fi
oc cluster up --public-hostname=${IP_ADDR} --routing-suffix=${IP_ADDR}.nip.io --base-dir=$HOME/ocp
if [ "$?" -eq 0 ]; then
built=true
break
fi
echo "Retrying oc cluster up after failure"
oc cluster down
sleep 5
done
echo "OpenShift Cluster Running"
}
applier() {
echo "${TRAVIS_BRANCH}"
echo "${TRAVIS_REPO_SLUG}"
ansible-galaxy install -r requirements.yml -p galaxy --force
ansible-playbook -i .applier/ galaxy/openshift-applier/playbooks/openshift-cluster-seed.yml -e namespace=${NAMESPACE} -e agent_repo_ref=${TRAVIS_BRANCH} -e repository_url=https://github.com/${TRAVIS_REPO_SLUG}.git
}
test() {
build_type=$1
# Make sure we're logged in, and we've found at least one build to test.
oc status > /dev/null || echo "Please log in before running tests." || exit 1
if [ $(oc get buildconfigs -n ${NAMESPACE} -o jsonpath="{.items[?(@.spec.strategy.type==\"${build_type}\")].metadata.name}" | wc -w) -lt 1 ]; then
echo "Did not find any builds, make sure you've passed the proper arguments."
exit 1
fi
echo "Ensure all ${build_type} Builds are executed..."
for buildConfig in $(oc get buildconfig -n ${NAMESPACE} -o jsonpath="{.items[?(@.spec.strategy.type==\"${build_type}\")].metadata.name}"); do
# Only start BuildConfigs which currently have 0 builds
if [ "$(oc get build -n ${NAMESPACE} -o jsonpath="{.items[?(@.metadata.annotations.openshift\.io/build-config\.name==\"${buildConfig}\")].metadata.name}")" == "" ]; then
[[ ! "${CHAINED_BUILDS[@]}" =~ "${buildConfig}" ]] && oc start-build ${buildConfig} -n ${NAMESPACE}
fi
done
echo "Waiting for all builds to start..."
builds=$(oc get build -n ${NAMESPACE} -o jsonpath="{.items[?(@.spec.strategy.type==\"${build_type}\")].metadata.name}")
while [[ "$(get_build_phases "New" ${builds})" -ne 0 || $(get_build_phases "Pending") -ne 0 ]]; do
echo -ne "New Builds: $(get_build_phases "New"), Pending Builds: $(get_build_phases "Pending")$([ "$TRAVIS" != "true" ] && echo "\r" || echo "\n")"
sleep 1
done
echo "Waiting for all builds to complete..."
while [ $(get_build_phases "Running" ${builds}) -ne 0 ]; do
echo -ne "Running Builds: $(get_build_phases "Running")$([ "$TRAVIS" != "true" ] && echo "\r" || echo "\n")"
sleep 1
done
echo "Check to see how many builds Failed"
if [ $(get_build_phases "Failed" ${builds}) -ne 0 ]; then
echo "Some builds failed. Printing Report"
retry 5 oc get builds -n $NAMESPACE -o custom-columns=NAME:.metadata.name,TYPE:.spec.strategy.type,FROM:.spec.source.type,STATUS:.status.phase,REASON:.status.reason
failed_jobs=$(retry 5 oc get builds -o jsonpath="{.items[?(@.status.phase=='Failed')].metadata.annotations.openshift\.io/build-config\.name}" -n $NAMESPACE) || kill -s TERM $TOP_PID
if [ $build_type == "JenkinsPipeline" ]; then
download_jenkins_logs_for_failed ${failed_jobs} "Complete"
else
download_build_logs_for_failed ${failed_jobs} "Complete"
fi
exit 1
fi
echo "${build_type} build tests completed successfully!"
}
get_build_phases() {
phase=$1
shift
targetted_builds=$@
result=$(retry 5 oc get builds ${targetted_builds} -o jsonpath="{.items[?(@.status.phase==\"${phase}\")].metadata.name}" -n $NAMESPACE) || kill -s TERM $TOP_PID
echo ${result} | wc -w
}
function retry {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** $count))
count=$(($count + 1))
if [ $count -lt $retries ]; then
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
# Process arguments
case $1 in
cluster_up)
cluster_up
;;
applier)
applier
;;
test)
test Docker || exit 1
test JenkinsPipeline
;;
*)
echo "Not an option"
exit 1
esac