forked from kyma-project/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision-vm-and-start-kyma-k3d.sh
executable file
·200 lines (165 loc) · 8.3 KB
/
provision-vm-and-start-kyma-k3d.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# This script is designed to provision a new vm and start kyma.It takes an optional positional parameter using --image flag
# Use this flag to specify the custom image for provisining vms. If no flag is provided, the latest custom image is used.
set -o errexit
readonly SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly TEST_INFRA_SOURCES_DIR="$(cd "${SCRIPT_DIR}/../../" && pwd)"
# shellcheck source=prow/scripts/lib/log.sh
source "${TEST_INFRA_SOURCES_DIR}/prow/scripts/lib/log.sh"
# shellcheck source=prow/scripts/lib/utils.sh
source "${TEST_INFRA_SOURCES_DIR}/prow/scripts/lib/utils.sh"
# shellcheck source=prow/scripts/lib/gcp.sh
source "$TEST_INFRA_SOURCES_DIR/prow/scripts/lib/gcp.sh"
if [[ "${BUILD_TYPE}" == "pr" ]]; then
log::info "Execute Job Guard"
"${TEST_INFRA_SOURCES_DIR}/development/jobguard/scripts/run.sh"
fi
function cleanup() {
# TODO - collect junit results
log::info "Stopping instance kyma-integration-test-${RANDOM_ID}"
log::info "It will be removed automatically by cleaner job"
# do not fail the job regardless of the vm deletion result
set +e
#shellcheck disable=SC2088
if [[ "$ISTIO_INTEGRATION_ENABLED" == "true" ]]; then
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/components/istio/junit-report.xml" "${ARTIFACTS}"
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/components/istio/reports/*.html" "${ARTIFACTS}"
elif [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_GATEWAY ]] || [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_VALIDATOR ]] || [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_RUNTIME_AGENT ]]; then
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/components/application-connector/junit-report.xml" "${ARTIFACTS}"
elif [[ "$API_GATEWAY_INTEGRATION" == "true" ]]; then
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/components/api-gateway/junit-report.xml" "${ARTIFACTS}"
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/components/api-gateway/reports/*.html" "${ARTIFACTS}/report.html"
else
utils::receive_from_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "~/kyma/tests/fast-integration/junit_kyma-fast-integration.xml" "${ARTIFACTS}"
fi
gcloud compute instances stop --async --zone="${ZONE}" "kyma-integration-test-${RANDOM_ID}"
log::info "End of cleanup"
}
function testCustomImage() {
CUSTOM_IMAGE="$1"
IMAGE_EXISTS=$(gcloud compute images list --filter "name:${CUSTOM_IMAGE}" | tail -n +2 | awk '{print $1}')
if [[ -z "$IMAGE_EXISTS" ]]; then
log::error "${CUSTOM_IMAGE} is invalid, it is not available in GCP images list, the script will terminate ..." && exit 1
fi
}
gcp::authenticate \
-c "${GOOGLE_APPLICATION_CREDENTIALS}"
RANDOM_ID=$(openssl rand -hex 4)
LABELS=""
if [[ -z "${PULL_NUMBER}" ]]; then
LABELS=(--labels "branch=$PULL_BASE_REF,job-name=kyma-integration")
else
LABELS=(--labels "pull-number=$PULL_NUMBER,job-name=kyma-integration")
fi
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case ${key} in
--image)
IMAGE="$2"
testCustomImage "${IMAGE}"
shift
shift
;;
--*)
echo "Unknown flag ${1}"
exit 1
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ -z "$IMAGE" ]]; then
log::info "Provisioning vm using the latest default custom image ..."
IMAGE=$(gcloud compute images list --sort-by "~creationTimestamp" \
--filter "family:custom images AND labels.default:yes" --limit=1 | tail -n +2 | awk '{print $1}')
if [[ -z "$IMAGE" ]]; then
log::error "There are no default custom images, the script will exit ..." && exit 1
fi
fi
ZONE_LIMIT=${ZONE_LIMIT:-5}
EU_ZONES=$(gcloud compute zones list --filter="name~europe" --limit="${ZONE_LIMIT}" | tail -n +2 | awk '{print $1}')
STARTTIME=$(date +%s)
for ZONE in ${EU_ZONES}; do
log::info "Attempting to create a new instance named kyma-integration-test-${RANDOM_ID} in zone ${ZONE} using image ${IMAGE}"
gcloud compute instances create "kyma-integration-test-${RANDOM_ID}" \
--metadata enable-oslogin=TRUE \
--image "${IMAGE}" \
--machine-type n2-standard-4 \
--zone "${ZONE}" \
--boot-disk-size 200 "${LABELS[@]}" && \
log::info "Created kyma-integration-test-${RANDOM_ID} in zone ${ZONE}" && break
log::error "Could not create machine in zone ${ZONE}"
done || exit 1
ENDTIME=$(date +%s)
echo "VM creation time: $((ENDTIME - STARTTIME)) seconds."
trap cleanup exit INT
log::info "Preparing environment variables for the instance"
export GARDENER_ZONE=${ZONE}
export CLUSTER_NAME=kyma-integration-test-${RANDOM_ID}
envVars=(
COMPASS_TENANT
COMPASS_HOST
COMPASS_CLIENT_ID
COMPASS_CLIENT_SECRET
COMPASS_INTEGRATION_ENABLED
CENTRAL_APPLICATION_CONNECTIVITY_ENABLED
APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_GATEWAY
APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_VALIDATOR
APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_RUNTIME_AGENT
TELEMETRY_ENABLED
TELEMETRY_TRACING_ENABLED
ISTIO_INTEGRATION_ENABLED
API_GATEWAY_INTEGRATION
GARDENER_ZONE
CLUSTER_NAME
KYMA_PROFILE
RECONCILATION_TEST
K8S_VERSION
)
utils::save_env_file "${envVars[@]}"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" ".env" "~/.env"
log::info "Copying Kyma to the instance"
#shellcheck disable=SC2088
utils::compress_send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "/home/prow/go/src/github.com/kyma-project/kyma" "~/kyma"
if [[ -v API_GATEWAY_INTEGRATION ]]; then
log::info "Copying components file for API-gateway tests"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-api-gateway-components.yaml" "~/kyma-integration-k3d-api-gateway-components.yaml"
fi
if [[ -v COMPASS_INTEGRATION_ENABLED ]]; then
log::info "Copying components file for compass tests"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-compass-components.yaml" "~/kyma-integration-k3d-compass-components.yaml"
fi
if [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_GATEWAY ]]; then
log::info "Copying components file for application connector component tests for Kyma OS setup"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-app-connector-components-os.yaml" "~/kyma-integration-k3d-app-connector-components-os.yaml"
fi
if [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_VALIDATOR ]] || [[ -v APPLICATION_CONNECTOR_COMPONENT_TESTS_ENABLED_RUNTIME_AGENT ]]; then
log::info "Copying components file for application connector component tests for Kyma SKR setup"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-app-connector-components-skr.yaml" "~/kyma-integration-k3d-app-connector-components-skr.yaml"
fi
if [[ -v TELEMETRY_ENABLED ]]; then
log::info "Copying components file for telemetry tests"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-telemetry-components.yaml" "~/kyma-integration-k3d-telemetry-components.yaml"
log::info "Triggering the installation"
utils::ssh_to_vm_with_script -z "${ZONE}" -n "kyma-integration-test-${RANDOM_ID}" -c "sudo bash" -p "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-telemetry.sh"
log::success "all done"
exit 0
fi
if [[ -v ISTIO_INTEGRATION_ENABLED ]]; then
log::info "Copying components file for istio tests"
#shellcheck disable=SC2088
utils::send_to_vm "${ZONE}" "kyma-integration-test-${RANDOM_ID}" "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d-istio-components.yaml" "~/kyma-integration-k3d-istio-components.yaml"
fi
log::info "Triggering the installation"
utils::ssh_to_vm_with_script -z "${ZONE}" -n "kyma-integration-test-${RANDOM_ID}" -c "sudo bash" -p "${SCRIPT_DIR}/cluster-integration/kyma-integration-k3d.sh"
log::success "all done"