forked from siamaksade/openshift-cicd-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.sh
executable file
·236 lines (189 loc) · 6.27 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
#!/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
}
while (( "$#" )); do
case "$1" in
install|custom|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"
declare -r parks_prj="$PRJ_PREFIX-parksmap"
command.help() {
cat <<-EOF
Usage:
demo [command] [options]
Example:
demo install --project-prefix mydemo
COMMANDS:
install Sets up the demo and creates namespaces, this accept default applications
uninstall Deletes the demo
custom Sets up defaults, but will ask you for a custom deployment to put under Argo management
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, $parks_prj"
oc get ns $cicd_prj 2>/dev/null || {
oc create ns $cicd_prj
}
oc get ns $dev_prj 2>/dev/null || {
oc create ns $dev_prj
}
oc get ns $stage_prj 2>/dev/null || {
oc create ns $stage_prj
}
oc get ns $parks_prj 2>/dev/null || {
oc create ns $parks_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 "Deploying pipeline and tasks to $cicd_prj namespace"
oc apply -f tasks -n $cicd_prj
sed "s#https://github.com/siamaksade#http://$GITEA_HOSTNAME/gitea#g" pipelines/pipeline-build.yaml | oc apply -f - -n $cicd_prj
oc apply -f triggers -n $cicd_prj
info "Initiatlizing git repository in Gitea and configuring webhooks"
sed "s/@HOSTNAME/$GITEA_HOSTNAME/g" config/gitea-configmap.yaml | oc create -f - -n $cicd_prj
oc rollout status deployment/gitea -n $cicd_prj
oc create -f config/gitea-init-taskrun.yaml -n $cicd_prj
sleep 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: http://$GITEA_HOSTNAME/gitea/spring-petclinic-config
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: stage-spring-petclinic
spec:
destination:
namespace: $stage_prj
source:
repoURL: http://$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
sleep 3
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 label ns $parks_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:
http://$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
4) Check the pipeline run logs in Dev Console or Tekton CLI:
\$ tkn pipeline logs petclinic-build -L -f -n $cicd_prj
You can find further details at:
Gitea Git Server: http://$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.custom() {
read -p "What is the name of your Application? " custom_app
info "Creating namespaces $custom_app"
oc get ns $custom_app 2>/dev/null || {
oc new-project $custom_app
}
read -p "What is the url to the git repo for the deployment? YAML File has to be in a folder " git_url
read -p "Parent folder name " folder
cat << EOF | oc apply -n $cicd_prj -f -
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: dev-$custom_app
spec:
destination:
namespace: $custom_app
server: https://kubernetes.default.svc
project: spring-petclinic
source:
directory:
include: parksmap.yaml
path: $folder/
repoURL: $git_url
targetRevision: HEAD
syncPolicy:
automated:
prune: true
selfHeal: true
EOF
oc label ns $custom_app argocd.argoproj.io/managed-by=$cicd_prj
}
command.start() {
oc create -f runs/pipeline-build-run.yaml -n $cicd_prj
}
command.uninstall() {
oc delete project $dev_prj $stage_prj $cicd_prj $parks_prj
}
main() {
local fn="command.$COMMAND"
valid_command "$fn" || {
err "invalid command '$COMMAND'"
}
cd $SCRIPT_DIR
$fn
return $?
}
main