forked from siamaksade/openshift-cicd-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.sh
executable file
·206 lines (166 loc) · 5.84 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
#!/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|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 "Installing Operator openshift-pipelines"
oc apply -f operators/pipeline-operator.yml
sleep 1m
info "Installing Operator openshift-gitops"
oc apply -f operators/gitops-operator.yml
sleep 1m
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
GOGS_HOSTNAME=$(oc get route gogs -o template --template='{{.spec.host}}' -n $cicd_prj)
info "Deploying pipeline and tasks to $cicd_prj namespace"
oc apply -f tasks -n $cicd_prj
oc apply -f pipelines/pipeline-build-pvc.yaml -n $cicd_prj
sed "s#https://github.com/siamaksade#http://$GOGS_HOSTNAME/gogs#g" pipelines/pipeline-build.yaml | oc apply -f - -n $cicd_prj
oc apply -f triggers -n $cicd_prj
info "Initiatlizing git repository in Gogs and configuring webhooks"
sed "s/@HOSTNAME/$GOGS_HOSTNAME/g" config/gogs-configmap.yaml | oc create -f - -n $cicd_prj
oc rollout status deployment/gogs -n $cicd_prj
oc create -f config/gogs-init-taskrun.yaml -n $cicd_prj
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://$GOGS_HOSTNAME/gogs/spring-petclinic-config
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: stage-spring-petclinic
spec:
destination:
namespace: $stage_prj
source:
repoURL: http://$GOGS_HOSTNAME/gogs/spring-petclinic-config
EOF
oc apply -k argo -n $cicd_prj
oc policy add-role-to-user admin system:serviceaccount:$cicd_prj:argocd-argocd-application-controller -n $dev_prj
oc policy add-role-to-user admin system:serviceaccount:$cicd_prj:argocd-argocd-application-controller -n $stage_prj
sed "s/demo-cicd/$cicd_prj/g" argo/argocd-sa-roles.yaml | oc apply -f - -n $dev_prj
sed "s/demo-cicd/$cicd_prj/g" argo/argocd-sa-roles.yaml | oc apply -f - -n $stage_prj
cat <<EOF | kubectl apply -n $cicd_prj -f -
kind: Secret
apiVersion: v1
metadata:
name: argocd-default-cluster-config
data:
config: $(echo -n '{"tlsClientConfig":{"insecure":false}}' | base64)
name: $(echo -n "in-cluster" | base64)
namespaces: $(echo -n "$cicd_prj,$dev_prj,$stage_prj" | base64)
server: $(echo -n "https://kubernetes.default.svc" | base64)
type: Opaque
EOF
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 Gogs:
http://$GOGS_HOSTNAME/gogs/spring-petclinic.git
2) Log into Gogs with username/password: gogs/gogs
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-deploy-dev -f -n $cicd_prj
You can find further details at:
Gogs Git Server: http://$GOGS_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) [admin pwd: $(oc get secret argocd-cluster -n $cicd_prj -ojsonpath='{.data.admin\.password}' | base64 -d)]
############################################################################
############################################################################
EOF
}
command.start() {
oc create -f runs/pipeline-build-run.yaml -n $cicd_prj
}
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