-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprovision.sh
executable file
·271 lines (234 loc) · 8.91 KB
/
provision.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
#!/bin/bash
echo "###############################################################################"
echo "# MAKE SURE YOU ARE LOGGED IN: #"
echo "# $ oc login http://console.your.openshift.com #"
echo "###############################################################################"
function usage() {
echo
echo "Usage:"
echo " $0 [command] [options]"
echo " $0 --help"
echo
echo "Example:"
echo " $0 deploy --project-suffix mydemo"
echo
echo "COMMANDS:"
echo " deploy Set up the demo projects and deploy demo apps"
echo " delete Clean up and remove demo projects and objects"
echo " idle Make all demo services idle"
echo " unidle Make all demo services unidle"
echo
echo "OPTIONS:"
echo " --user [username] The admin user for the demo projects. mandatory if logged in as system:admin"
echo " --project-suffix [suffix] Suffix to be added to demo project names e.g. ci-SUFFIX. If empty, user will be used as suffix"
echo " --ephemeral Deploy demo without persistent storage. Default false"
echo " --deploy-sonar Deploy SonarQube for static code analysis instead of CheckStyle,FindBug,etc. Default false"
echo " --deploy-che Deploy Eclipse Che as an online IDE for code changes. Default false"
echo " --oc-options oc client options to pass to all oc commands e.g. --server https://my.openshift.com"
echo
}
ARG_USERNAME=
ARG_PROJECT_SUFFIX=
ARG_COMMAND=
ARG_EPHEMERAL=false
ARG_OC_OPS=
ARG_DEPLOY_SONAR=false
ARG_DEPLOY_CHE=false
while :; do
case $1 in
deploy)
ARG_COMMAND=deploy
;;
delete)
ARG_COMMAND=delete
;;
idle)
ARG_COMMAND=idle
;;
unidle)
ARG_COMMAND=unidle
;;
--user)
if [ -n "$2" ]; then
ARG_USERNAME=$2
shift
else
printf 'ERROR: "--user" requires a non-empty value.\n' >&2
usage
exit 255
fi
;;
--project-suffix)
if [ -n "$2" ]; then
ARG_PROJECT_SUFFIX=$2
shift
else
printf 'ERROR: "--project-suffix" requires a non-empty value.\n' >&2
usage
exit 255
fi
;;
--oc-options)
if [ -n "$2" ]; then
ARG_OC_OPS=$2
shift
else
printf 'ERROR: "--oc-options" requires a non-empty value.\n' >&2
usage
exit 255
fi
;;
--ephemeral)
ARG_EPHEMERAL=true
;;
--use-sonar)
ARG_DEPLOY_SONAR=true
;;
--deploy-sonar)
ARG_DEPLOY_SONAR=true
;;
--deploy-che)
ARG_DEPLOY_CHE=true
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
shift
;;
*) # Default case: If no more options then break out of the loop.
break
esac
shift
done
################################################################################
# CONFIGURATION #
################################################################################
LOGGEDIN_USER=$(oc $ARG_OC_OPS whoami)
OPENSHIFT_USER=${ARG_USERNAME:-$LOGGEDIN_USER}
PRJ_SUFFIX=${ARG_PROJECT_SUFFIX:-`echo $OPENSHIFT_USER | sed -e 's/[-@].*//g'`}
GITHUB_ACCOUNT=${GITHUB_ACCOUNT:-OpenShiftDemos}
GITHUB_REF=${GITHUB_REF:-ocp-3.7}
function deploy() {
oc $ARG_OC_OPS new-project dev-$PRJ_SUFFIX --display-name="Tasks - Dev"
oc $ARG_OC_OPS new-project stage-$PRJ_SUFFIX --display-name="Tasks - Stage"
oc $ARG_OC_OPS new-project cicd-$PRJ_SUFFIX --display-name="CI/CD"
sleep 2
oc $ARG_OC_OPS policy add-role-to-user edit system:serviceaccount:cicd-$PRJ_SUFFIX:jenkins -n dev-$PRJ_SUFFIX
oc $ARG_OC_OPS policy add-role-to-user edit system:serviceaccount:cicd-$PRJ_SUFFIX:jenkins -n stage-$PRJ_SUFFIX
if [ $LOGGEDIN_USER == 'system:admin' ] ; then
oc $ARG_OC_OPS adm policy add-role-to-user admin $ARG_USERNAME -n dev-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS adm policy add-role-to-user admin $ARG_USERNAME -n stage-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS adm policy add-role-to-user admin $ARG_USERNAME -n cicd-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS annotate --overwrite namespace dev-$PRJ_SUFFIX demo=openshift-cd-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS annotate --overwrite namespace stage-$PRJ_SUFFIX demo=openshift-cd-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS annotate --overwrite namespace cicd-$PRJ_SUFFIX demo=openshift-cd-$PRJ_SUFFIX >/dev/null 2>&1
oc $ARG_OC_OPS adm pod-network join-projects --to=cicd-$PRJ_SUFFIX dev-$PRJ_SUFFIX stage-$PRJ_SUFFIX >/dev/null 2>&1
fi
sleep 2
oc import-image jenkins:v3.7 --from="registry.access.redhat.com/openshift3/jenkins-2-rhel7" --confirm -n openshift 2>/dev/null
sleep 5
#oc tag jenkins:v3.7 jenkins:latest -n openshift
oc new-app jenkins-ephemeral -n cicd-$PRJ_SUFFIX --param=MEMORY_LIMIT=1024Mi
sleep 2
local template=
local template=openshift/templates/cicd-template.yaml
echo "Using template $template"
# oc $ARG_OC_OPS new-app -f $template --param DEV_PROJECT=dev-$PRJ_SUFFIX --param STAGE_PROJECT=stage-$PRJ_SUFFIX --param=WITH_SONAR=$ARG_DEPLOY_SONAR --param=WITH_CHE=$ARG_DEPLOY_CHE --param=EPHEMERAL=$ARG_EPHEMERAL -n cicd-$PRJ_SUFFIX
}
function make_idle() {
echo_header "Idling Services"
oc $ARG_OC_OPS idle -n dev-$PRJ_SUFFIX --all
oc $ARG_OC_OPS idle -n stage-$PRJ_SUFFIX --all
oc $ARG_OC_OPS idle -n cicd-$PRJ_SUFFIX --all
}
function make_unidle() {
echo_header "Unidling Services"
local _DIGIT_REGEX="^[[:digit:]]*$"
for project in dev-$PRJ_SUFFIX stage-$PRJ_SUFFIX cicd-$PRJ_SUFFIX
do
for dc in $(oc $ARG_OC_OPS get dc -n $project -o=custom-columns=:.metadata.name); do
local replicas=$(oc $ARG_OC_OPS get dc $dc --template='{{ index .metadata.annotations "idling.alpha.openshift.io/previous-scale"}}' -n $project 2>/dev/null)
if [[ $replicas =~ $_DIGIT_REGEX ]]; then
oc $ARG_OC_OPS scale --replicas=$replicas dc $dc -n $project
fi
done
done
}
function set_default_project() {
if [ $LOGGEDIN_USER == 'system:admin' ] ; then
oc $ARG_OC_OPS project default >/dev/null
fi
}
function remove_storage_claim() {
local _DC=$1
local _VOLUME_NAME=$2
local _CLAIM_NAME=$3
local _PROJECT=$4
oc $ARG_OC_OPS volumes dc/$_DC --name=$_VOLUME_NAME --add -t emptyDir --overwrite -n $_PROJECT
oc $ARG_OC_OPS delete pvc $_CLAIM_NAME -n $_PROJECT >/dev/null 2>&1
}
function echo_header() {
echo
echo "########################################################################"
echo $1
echo "########################################################################"
}
################################################################################
# MAIN: DEPLOY DEMO #
################################################################################
if [ "$LOGGEDIN_USER" == 'system:admin' ] && [ -z "$ARG_USERNAME" ] ; then
# for verify and delete, --project-suffix is enough
if [ "$ARG_COMMAND" == "delete" ] || [ "$ARG_COMMAND" == "verify" ] && [ -z "$ARG_PROJECT_SUFFIX" ]; then
echo "--user or --project-suffix must be provided when running $ARG_COMMAND as 'system:admin'"
exit 255
# deploy command
elif [ "$ARG_COMMAND" != "delete" ] && [ "$ARG_COMMAND" != "verify" ] ; then
echo "--user must be provided when running $ARG_COMMAND as 'system:admin'"
exit 255
fi
fi
pushd ~ >/dev/null
START=`date +%s`
echo_header "OpenShift CI/CD Demo ($(date))"
case "$ARG_COMMAND" in
delete)
echo "Delete demo..."
oc $ARG_OC_OPS delete project dev-$PRJ_SUFFIX stage-$PRJ_SUFFIX cicd-$PRJ_SUFFIX
echo
echo "Delete completed successfully!"
;;
idle)
echo "Idling demo..."
make_idle
echo
echo "Idling completed successfully!"
;;
unidle)
echo "Unidling demo..."
make_unidle
echo
echo "Unidling completed successfully!"
;;
deploy)
echo "Deploying demo..."
deploy
echo
echo "Provisioning completed successfully!"
;;
*)
echo "Invalid command specified: '$ARG_COMMAND'"
usage
;;
esac
set_default_project
popd >/dev/null
END=`date +%s`
echo "(Completed in $(( ($END - $START)/60 )) min $(( ($END - $START)%60 )) sec)"
echo