-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathdemo.sh
executable file
·164 lines (127 loc) · 4.74 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
#!/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|promote)
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
promote Starts the deploy STAGE 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 edit system:serviceaccount:$stage_prj:default -n $dev_prj
info "Deploying CI/CD infra to $cicd_prj namespace"
oc apply -f cd -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 create -f config/maven-settings-configmap.yaml -n $cicd_prj
oc apply -f config/pipeline-pvc.yaml -n $cicd_prj
sed "s/demo-dev/$dev_prj/g" pipelines/pipeline-deploy-dev.yaml | sed -E "s#https://github.com/siamaksade#http://$GOGS_HOSTNAME/gogs#g" | oc apply -f - -n $cicd_prj
sed "s/demo-dev/$dev_prj/g" pipelines/pipeline-deploy-stage.yaml | sed -E "s/demo-stage/$stage_prj/g" | sed -E "s#https://github.com/siamaksade#http://$GOGS_HOSTNAME/gogs#g" | oc apply -f - -n $cicd_prj
oc apply -f triggers/gogs-triggerbinding.yaml -n $cicd_prj
oc apply -f triggers/triggertemplate.yaml -n $cicd_prj
sed "s/demo-dev/$dev_prj/g" triggers/eventlistener.yaml | oc apply -f - -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
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
Reports Server: http://$(oc get route reports-repo -o template --template='{{.spec.host}}' -n $cicd_prj)
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)
############################################################################
############################################################################
EOF
}
command.start() {
oc create -f runs/pipeline-deploy-dev-run.yaml -n $cicd_prj
}
command.promote() {
oc create -f runs/pipeline-deploy-stage-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