-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·472 lines (354 loc) · 12.1 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env bash
# based on: https://github.com/cameronbraid/jx3-kind/blob/master/jx3-kind.sh
{
set -euo pipefail
COMMAND=${1:-'help'}
DIR=${NAME:-"$(pwd)"}
NAME=${NAME:-"gke-gitea"}
TOKEN=${TOKEN:-}
BOT_USER="${BOT_USER:-jenkins-x-test-bot}"
BOT_PASS="${BOT_PASS:-jenkins-x-test-bot}"
export DEVELOPER_USER="developer"
export DEVELOPER_PASS="developer"
ORG="${ORG:-coders}"
#TEST_NAME="${TEST_NAME:-test-create-spring}"
TEST_NAME="${TEST_NAME:-test-quickstart-node-http}"
DEV_CLUSTER_REPOSITORY="${DEV_CLUSTER_REPOSITORY:-https://github.com/jx3-gitops-repositories/jx3-gke-gsm-gitea}"
# versions
KIND_VERSION=${KIND_VERSION:-"0.10.0"}
JX_VERSION=${JX_VERSION:-"3.1.306"}
KUBECTL_VERSION=${KUBECTL_VERSION:-"1.20.0"}
YQ_VERSION=${YQ_VERSION:-"4.2.0"}
LOG_TIMESTAMPS=${LOG_TIMESTAMPS:-"true"}
LOG_FILE=${LOG_FILE:-"log"}
LOG=${LOG:-"console"} #or file
GITEA_ADMIN_PASSWORD=${GITEA_ADMIN_PASSWORD:-"abcdEFGH"}
export GIT_SCHEME="http"
export GIT_KIND="gitea"
# lets setup git
git config --global --add user.name JenkinsXBot
git config --global --add user.email [email protected]
# write message to console and log
info() {
prefix=""
if [[ "${LOG_TIMESTAMPS}" == "true" ]]; then
prefix="$(date '+%Y-%m-%d %H:%M:%S') "
fi
if [[ "${LOG}" == "file" ]]; then
echo -e "${prefix}$@" >&3
echo -e "${prefix}$@"
else
echo -e "${prefix}$@"
fi
}
# write to console and store some information for error reporting
STEP=""
SUB_STEP=""
step() {
STEP="$@"
SUB_STEP=""
info
info "[$STEP]"
}
# store some additional information for error reporting
substep() {
SUB_STEP="$@"
info " - $SUB_STEP"
}
err() {
if [[ "$STEP" == "" ]]; then
echo "Failed running: ${BASH_COMMAND}"
exit 1
else
if [[ "$SUB_STEP" != "" ]]; then
echo "Failed at [$STEP / $SUB_STEP] running : ${BASH_COMMAND}"
exit 1
else
echo "Failed at [$STEP] running : ${BASH_COMMAND}"
exit 1
fi
fi
}
FILE_NGINX_VALUES=`cat << EOF
controller:
hostPort:
enabled: true
replicaCount: 1
config:
# since the docker registry is being used via ingress
# an alternative to making this global is to convigure the docker-registry ingress to use an annotation
proxy-body-size: 1g
EOF
`
installNginxIngress() {
step "Installing nginx ingress"
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
echo "${FILE_NGINX_VALUES}" | helm upgrade --install nginx --namespace nginx --create-namespace --values - ingress-nginx/ingress-nginx
substep "Waiting for nginx to start"
sleep 2
kubectl wait --namespace nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=100m
IP=""
while [ -z $IP ]; do
IP=$(kubectl get service -n nginx nginx-ingress-nginx-controller -o=jsonpath="{.status.loadBalancer.ingress[0].ip}")
if [[ "$IP" == "" ]]; then
echo "Waiting for nginx LoadBalancer External IP to be resolved..."
sleep 10
fi
done
export GIT_HOST=${GIT_HOST:-"gitea.${IP}.nip.io"}
export GIT_URL="${GIT_SCHEME}://${GIT_HOST}"
echo "using GIT_URL ${GIT_URL}"
}
installGitea() {
step "Installing Gitea at $GIT_HOST"
FILE_GITEA_VALUES_YAML=`cat <<EOF
ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
hosts:
- ${GIT_HOST}
gitea:
admin:
password: ${GITEA_ADMIN_PASSWORD}
config:
server:
DOMAIN: ${GIT_HOST}
PROTOCOL: ${GIT_SCHEME}
ROOT_URL: ${GIT_SCHEME}://${GIT_HOST}
SSH_DOMAIN: ${GIT_HOST}
database:
DB_TYPE: sqlite3
## Note that the intit script checks to see if the IP & port of the database service is accessible, so make sure you set those to something that resolves as successful (since sqlite uses files on disk setting the port & ip won't affect the running of gitea).
HOST: ${IP}:80 # point to the nginx ingress
service:
DISABLE_REGISTRATION: true
database:
builtIn:
postgresql:
enabled: false
image:
version: 1.13.0
EOF
`
helm repo add gitea-charts https://dl.gitea.io/charts/
echo "${FILE_GITEA_VALUES_YAML}" | helm upgrade --install --namespace gitea --create-namespace -f - gitea gitea-charts/gitea
sleep 2
substep "Waiting for Gitea to start"
kubectl wait --namespace gitea \
--for=condition=ready pod \
--selector=app.kubernetes.io/name=gitea \
--timeout=100m
echo "gitea is running at ${GIT_URL}"
sleep 2
echo "running curl -LI -o /dev/null -s ${GIT_URL}/api/v1/admin/users -H '${CURL_AUTH_HEADER}'"
# Verify that gitea is serving
for i in {1..20}; do
echo "curling..."
http_output=`curl -v -LI -H "${CURL_AUTH_HEADER}" -s "${GIT_URL}/api/v1/admin/users" || true`
echo "output of curl ${http_output}"
echo curl -v -LI -s "${GIT_URL}/api/v1/admin/users" "${CURL_GIT_ADMIN_AUTH[@]}"
http_code=`curl -LI -o /dev/null -w '%{http_code}' -H "${CURL_AUTH_HEADER}" -s "${GIT_URL}/api/v1/admin/users" || true`
echo "got response code ${http_code}"
if [[ "${http_code}" = "200" ]]; then
break
fi
sleep 1
done
echo "stopped polling"
if [[ "${http_code}" != "200" ]]; then
info "Gitea didn't startup"
return 1
fi
info "Gitea is up at ${GIT_URL}"
info "Login with username: gitea_admin password: ${GITEA_ADMIN_PASSWORD}"
}
FILE_USER_JSON=`cat << 'EOF'
{
"admin": true,
"email": "[email protected]",
"full_name": "full_name",
"login_name": "login_name",
"must_change_password": false,
"password": "password",
"send_notify": false,
"source_id": 0,
"username": "username"
}
EOF
`
CURL_AUTH_HEADER=""
declare -a CURL_AUTH=()
curlBasicAuth() {
username=$1
password=$2
basic=`echo -n "${username}:${password}" | base64`
CURL_AUTH=("-H" "Authorization: Basic $basic")
CURL_AUTH_HEADER="Authorization: Basic $basic"
}
curlTokenAuth() {
token=$1
CURL_AUTH=("-H" "Authorization: token ${token}")
}
curlBasicAuth "gitea_admin" "${GITEA_ADMIN_PASSWORD}"
CURL_GIT_ADMIN_AUTH=("${CURL_AUTH[@]}")
declare -a CURL_TYPE_JSON=("-H" "Accept: application/json" "-H" "Content-Type: application/json")
# "${GIT_SCHEME}://gitea_admin:${GITEA_ADMIN_PASSWORD}@${GIT_HOST}"
giteaCreateUserAndToken() {
username=$1
password=$2
request=`echo "${FILE_USER_JSON}" \
| yq e '.email="'${username}@example.com'"' - \
| yq e '.full_name="'${username}'"' - \
| yq e '.login_name="'${username}'"' - \
| yq e '.username="'${username}'"' - \
| yq e '.password="'${password}'"' -`
substep "creating ${username} user"
response=`echo "${request}" | curl -s -X POST "${GIT_URL}/api/v1/admin/users" "${CURL_GIT_ADMIN_AUTH[@]}" "${CURL_TYPE_JSON[@]}" --data @-`
# info $request
# info $response
substep "updating ${username} user"
response=`echo "${request}" | curl -s -X PATCH "${GIT_URL}/api/v1/admin/users/${username}" "${CURL_GIT_ADMIN_AUTH[@]}" "${CURL_TYPE_JSON[@]}" --data @-`
# info $response
substep "creating ${username} token"
curlBasicAuth "${username}" "${password}"
response=`curl -s -X POST "${GIT_URL}/api/v1/users/${username}/tokens" "${CURL_AUTH[@]}" "${CURL_TYPE_JSON[@]}" --data '{"name":"jx3"}'`
# info $response
token=`echo "${response}" | yq eval '.sha1' -`
if [[ "$token" == "null" ]]; then
info "Failed to create token for ${username}, json response: \n${response}"
return 1
fi
TOKEN="${token}"
}
configureGiteaOrgAndUsers() {
step "Setting up gitea organisation and users"
giteaCreateUserAndToken "${BOT_USER}" "${BOT_PASS}"
botToken="${TOKEN}"
echo "${botToken}" > "${DIR}/.bot.token"
giteaCreateUserAndToken "${DEVELOPER_USER}" "${DEVELOPER_PASS}"
developerToken="${TOKEN}"
echo "${developerToken}" > "${DIR}/.developer.token"
echo "export GIT_SERVER_URL=\"${GIT_URL}\"" >> ${DIR}/variables.sh
echo "export GIT_SERVER_HOST=\"${GIT_HOST}\"" >> ${DIR}/variables.sh
echo "export GIT_USER=\"${DEVELOPER_USER}\"" >> ${DIR}/variables.sh
echo "export GIT_TOKEN=\"${developerToken}\"" >> ${DIR}/variables.sh
substep "creating ${ORG} organisation"
curlTokenAuth "${developerToken}"
json=`curl -s -X POST "${GIT_URL}/api/v1/orgs" "${CURL_AUTH[@]}" "${CURL_TYPE_JSON[@]}" --data '{"repo_admin_change_team_access": true, "username": "'${ORG}'", "visibility": "private"}'`
# info "${json}"
substep "add ${BOT_USER} an owner of ${ORG} organisation"
substep "find owners team for ${ORG}"
curlTokenAuth "${developerToken}"
json=`curl -s "${GIT_URL}/api/v1/orgs/${ORG}/teams/search?q=owners" "${CURL_AUTH[@]}" "${CURL_TYPE_JSON[@]}"`
id=`echo "${json}" | yq eval '.data[0].id' -`
if [[ "${id}" == "null" ]]; then
info "Unable to find owners team, json response :\n${json}"
return 1
fi
substep "add ${BOT_USER} as member of owners team (#${id}) for ${ORG}"
curlTokenAuth "${developerToken}"
response=`curl -s -X PUT "${GIT_URL}/api/v1/teams/${id}/members/${BOT_USER}" "${CURL_AUTH[@]}" "${CURL_TYPE_JSON[@]}"`
}
loadGitUserTokens() {
botToken=`cat "${DIR}/.bot.token"`
developerToken=`cat "${DIR}/.developer.token"`
}
# resetGitea() {
# #
# #
# # DANGER : THIS WILL REMOVE ALL GITEA DATA
# #
# #
# step "Resetting Gitea"
# substep "Clar gitea data folder which includes the sqlite database and repositories"
# kubectl -n gitea exec gitea-0 -- rm -rf "/data/*"
# substep "Restart gitea pod"
# kubectl -n gitea delete pod gitea-0
# sleep 5
# expectPodsReadyByLabel gitea app.kubernetes.io/name=gitea
# }
help() {
# TODO
info "run 'kind.sh create' or 'kind.sh destroy'"
}
createBootRepo() {
step "creating the dev cluster git repo: ${GIT_URL}/${ORG}/cluster-$NAME-dev from template: ${DEV_CLUSTER_REPOSITORY}"
rm -rf "cluster-$NAME-dev"
export GIT_USERNAME="${BOT_USER}"
export GIT_TOKEN="${TOKEN}"
echo "user $GIT_USERNAME"
echo "token $GIT_TOKEN"
# lets make it public for now since its on a laptop
# --private
jx scm repo create ${GIT_URL}/${ORG}/cluster-$NAME-dev --template ${DEV_CLUSTER_REPOSITORY} --confirm --push-host ${GIT_HOST}
sleep 2
git clone ${GIT_SCHEME}://${DEVELOPER_USER}:${DEVELOPER_PASS}@${GIT_HOST}/${ORG}/cluster-$NAME-dev
cd cluster-$NAME-dev
jx gitops requirements edit --domain "${IP}.nip.io" --git-server ${GIT_URL}
git commit -a -m "fix: upgrade domain and git server"
git push
cd ..
}
installGitOperator() {
step "installing the git operator at url: ${GIT_URL}/${ORG}/cluster-$NAME-dev with user: ${BOT_USER} token: ${BOT_PASS}"
#jx admin operator --url "${GIT_URL}/${ORG}/cluster-$NAME-dev" --username ${BOT_USER} --token ${TOKEN}
FILE_JXGO_VALUES_YAML=`cat <<EOF
bootServiceAccount:
enabled: true
annotations:
iam.gke.io/gcp-service-account: "${TF_VAR_cluster_name}-boot@${TF_VAR_gcp_project}.iam.gserviceaccount.com"
env:
NO_RESOURCE_APPLY: "true"
url: "${GIT_URL}/${ORG}/cluster-$NAME-dev"
username: "${BOT_USER}"
password: "${TOKEN}"
EOF
`
helm repo add jx3 https://storage.googleapis.com/jenkinsxio/charts
echo "${FILE_JXGO_VALUES_YAML}" | helm upgrade --install --namespace jx-git-operator --create-namespace -f - jxgo jx3/jx-git-operator
# lets tail the boot log
jx admin log -w
}
runBDD() {
step "running the BDD tests $TEST_NAME on git server $GIT_URL"
echo "user: ${BOT_USER} token: ${TOKEN}"
helm upgrade --install bdd jx3/jx-bdd --namespace jx --create-namespace --set bdd.approverSecret="bdd-git-approver",bdd.kind="$GIT_KIND",bdd.owner="$ORG",bdd.gitServerHost="gitea-http.gitea",bdd.gitServerURL="$GIT_URL",command.test="make $TEST_NAME",jxgoTag="$JX_VERSION",bdd.user="${BOT_USER}",bdd.token="${TOKEN}"
echo "about to wait for the BDD test to run"
sleep 20
kubectl describe nodes
kubectl get event -n jx -w &
# lets avoid the jx commands thinking we are outside of kubernetes due to $GITHUB-ACTIONS maybe being set..
export JX_KUBERNETES="true"
jx verify job --name jx-bdd -n jx
}
create() {
installNginxIngress
installGitea
configureGiteaOrgAndUsers
createBootRepo
installGitOperator
}
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
if [[ "${COMMAND}" == "ciLoop" ]]; then
ciLoop
elif [[ "${COMMAND}" == "env" ]]; then
:
else
if `function_exists "${COMMAND}"`; then
shift
#initLog
"${COMMAND}" "$@"
else
info "Unknown command : ${COMMAND}"
exit 1
fi
fi
exit 0
}