-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathdemo.sh
executable file
·304 lines (252 loc) · 8.21 KB
/
demo.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
#!/bin/bash
set -e -u -o pipefail
declare -r SCRIPT_DIR=$(cd -P $(dirname $0) && pwd)
declare PRJ_PREFIX="demo"
declare COMMAND="help"
valid_command() {
local fn=$1; shift
[[ $(type -t "$fn") == "function" ]]
}
info() {
printf "\n# INFO: $@\n"
}
err() {
printf "\n# ERROR: $1\n"
exit 1
}
wait_seconds() {
local count=${1:-5}
for i in {1..$count}
do
echo "."
sleep 1
done
printf "\n"
}
case "$OSTYPE" in
darwin*) PLATFORM="OSX" ;;
linux*) PLATFORM="LINUX" ;;
bsd*) PLATFORM="BSD" ;;
*) PLATFORM="UNKNOWN" ;;
esac
cross_sed() {
if [[ "$PLATFORM" == "OSX" || "$PLATFORM" == "BSD" ]]; then
sed -i "" "$1" "$2"
elif [ "$PLATFORM" == "LINUX" ]; then
sed -i "$1" "$2"
fi
}
while (( "$#" )); do
case "$1" in
install|uninstall|start)
COMMAND=$1
shift
;;
-p|--project-prefix)
PRJ_PREFIX=$2
shift 2
;;
--)
shift
break
;;
-*|--*)
err "Error: Unsupported flag $1"
;;
*)
break
esac
done
declare -r dev_prj="$PRJ_PREFIX-dev"
declare -r stage_prj="$PRJ_PREFIX-stage"
declare -r cicd_prj="$PRJ_PREFIX-cicd"
command.help() {
cat <<-EOF
Usage:
demo [command] [options]
Example:
demo install --project-prefix mydemo
COMMANDS:
install Sets up the demo and creates namespaces
uninstall Deletes the demo
start Starts the deploy DEV pipeline
help Help about this command
OPTIONS:
-p|--project-prefix [string] Prefix to be added to demo project names e.g. PREFIX-dev
EOF
}
command.install() {
oc version >/dev/null 2>&1 || err "no oc binary found"
info "Creating namespaces $cicd_prj, $dev_prj, $stage_prj"
oc get ns $cicd_prj 2>/dev/null || {
oc new-project $cicd_prj
}
oc get ns $dev_prj 2>/dev/null || {
oc new-project $dev_prj
}
oc get ns $stage_prj 2>/dev/null || {
oc new-project $stage_prj
}
info "Configure service account permissions for pipeline"
oc policy add-role-to-user edit system:serviceaccount:$cicd_prj:pipeline -n $dev_prj
oc policy add-role-to-user edit system:serviceaccount:$cicd_prj:pipeline -n $stage_prj
oc policy add-role-to-user system:image-puller system:serviceaccount:$dev_prj:default -n $cicd_prj
oc policy add-role-to-user system:image-puller system:serviceaccount:$stage_prj:default -n $cicd_prj
info "Deploying CI/CD infra to $cicd_prj namespace"
oc apply -f infra -n $cicd_prj
GITEA_HOSTNAME=$(oc get route gitea -o template --template='{{.spec.host}}' -n $cicd_prj)
info "Initiatlizing git repository in Gitea and configuring webhooks"
WEBHOOK_URL=$(oc get route pipelines-as-code-controller -n pipelines-as-code -o template --template="{{.spec.host}}" --ignore-not-found)
if [ -z "$WEBHOOK_URL" ]; then
WEBHOOK_URL=$(oc get route pipelines-as-code-controller -n openshift-pipelines -o template --template="{{.spec.host}}")
fi
sed "s/@HOSTNAME/$GITEA_HOSTNAME/g" config/gitea-configmap.yaml | oc create -f - -n $cicd_prj
oc rollout status deployment/gitea -n $cicd_prj
sed "s#@webhook-url@#https://$WEBHOOK_URL#g" config/gitea-init-taskrun.yaml | oc create -f - -n $cicd_prj
wait_seconds 20
while oc get taskrun -n $cicd_prj | grep Running >/dev/null 2>/dev/null
do
echo "waiting for Gitea init..."
wait_seconds 5
done
echo "Waiting for source code to be imported to Gitea..."
while true;
do
result=$(curl --write-out '%{response_code}' --head --silent --output /dev/null https://$GITEA_HOSTNAME/gitea/spring-petclinic)
if [ "$result" == "200" ]; then
break
fi
wait_seconds 5
done
wait_seconds 5
info "Updated pipelinerun values for the demo environment"
tmp_dir=$(mktemp -d)
pushd $tmp_dir
git clone https://$GITEA_HOSTNAME/gitea/spring-petclinic
cd spring-petclinic
git config user.email "[email protected]"
git config user.name "openshift-pipelines"
cat .tekton/build.yaml | grep -A 2 GIT_REPOSITORY
cross_sed "s#https://github.com/siamaksade/spring-petclinic-config#https://$GITEA_HOSTNAME/gitea/spring-petclinic-config#g" .tekton/build.yaml
cat .tekton/build.yaml | grep -A 2 GIT_REPOSITORY
git status
git add .tekton/build.yaml
git commit -m "Updated manifests git url"
git remote add auth-origin https://gitea:openshift@$GITEA_HOSTNAME/gitea/spring-petclinic
git push auth-origin cicd-demo
popd
info "Configuring pipelines-as-code"
TASKRUN_NAME=$(oc get taskrun -n $cicd_prj -o jsonpath="{.items[0].metadata.name}")
GITEA_TOKEN=$(oc logs $TASKRUN_NAME-pod -n $cicd_prj | grep Token | sed 's/^## Token: \(.*\) ##$/\1/g')
cat << EOF > /tmp/tmp-pac-repository.yaml
---
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
kind: Repository
metadata:
name: spring-petclinic
namespace: $cicd_prj
spec:
url: https://$GITEA_HOSTNAME/gitea/spring-petclinic
git_provider:
user: "git"
url: https://$GITEA_HOSTNAME
secret:
name: "gitea"
key: token
webhook_secret:
name: "gitea"
key: "webhook"
---
apiVersion: v1
kind: Secret
metadata:
name: gitea
namespace: $cicd_prj
type: Opaque
stringData:
token: "$GITEA_TOKEN"
webhook: ""
EOF
oc apply -f /tmp/tmp-pac-repository.yaml -n $cicd_prj
wait_seconds 10
info "Configure Argo CD"
cat << EOF > argo/tmp-argocd-app-patch.yaml
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: dev-spring-petclinic
spec:
destination:
namespace: $dev_prj
source:
repoURL: https://$GITEA_HOSTNAME/gitea/spring-petclinic-config
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: stage-spring-petclinic
spec:
destination:
namespace: $stage_prj
source:
repoURL: https://$GITEA_HOSTNAME/gitea/spring-petclinic-config
EOF
oc apply -k argo -n $cicd_prj
info "Wait for Argo CD route..."
until oc get route argocd-server -n $cicd_prj >/dev/null 2>/dev/null
do
wait_seconds 5
done
info "Grants permissions to ArgoCD instances to manage resources in target namespaces"
oc label ns $dev_prj argocd.argoproj.io/managed-by=$cicd_prj
oc label ns $stage_prj argocd.argoproj.io/managed-by=$cicd_prj
oc project $cicd_prj
cat <<-EOF
############################################################################
############################################################################
Demo is installed! Give it a few minutes to finish deployments and then:
1) Go to spring-petclinic Git repository in Gitea:
https://$GITEA_HOSTNAME/gitea/spring-petclinic.git
2) Log into Gitea with username/password: gitea/openshift
3) Edit a file in the repository and commit to trigger the pipeline (alternatively, create a pull-request)
4) Check the pipeline run logs in Dev Console or Tekton CLI:
\$ opc pac logs -n $cicd_prj
You can find further details at:
Gitea Git Server: https://$GITEA_HOSTNAME/explore/repos
SonarQube: https://$(oc get route sonarqube -o template --template='{{.spec.host}}' -n $cicd_prj)
Sonatype Nexus: http://$(oc get route nexus -o template --template='{{.spec.host}}' -n $cicd_prj)
Argo CD: http://$(oc get route argocd-server -o template --template='{{.spec.host}}' -n $cicd_prj) [login with OpenShift credentials]
############################################################################
############################################################################
EOF
}
command.start() {
GITEA_HOSTNAME=$(oc get route gitea -o template --template='{{.spec.host}}' -n $cicd_prj)
info "Pushing a change to https://$GITEA_HOSTNAME/gitea/spring-petclinic-config"
tmp_dir=$(mktemp -d)
pushd $tmp_dir
git clone https://$GITEA_HOSTNAME/gitea/spring-petclinic
cd spring-petclinic
git config user.email "[email protected]"
git config user.name "openshift-pipelines"
echo " " >> readme.md
git add readme.md
git commit -m "Updated readme.md"
git remote add auth-origin https://gitea:openshift@$GITEA_HOSTNAME/gitea/spring-petclinic
git push auth-origin cicd-demo
popd
}
command.uninstall() {
oc delete project $dev_prj $stage_prj $cicd_prj
}
main() {
local fn="command.$COMMAND"
valid_command "$fn" || {
err "invalid command '$COMMAND'"
}
cd $SCRIPT_DIR
$fn
return $?
}
main