-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsetupLocalProxy.sh
executable file
·284 lines (255 loc) · 8.42 KB
/
setupLocalProxy.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
#!/usr/bin/env bash
#
# Used to run Proxy locally.
#
# Stop the script if any commands fail
set -o errexit
set -o pipefail
# XYZ = ${XYZ:-8000}
# if env variable $XYZ is set, use that for XYZ, otherwise use 8000
LOCAL_IDLER_PORT=${LOCAL_IDLER_PORT:-9001}
LOCAL_TENANT_PORT=${LOCAL_TENANT_PORT:-9002}
LOCAL_POSTGRES_PORT=${LOCAL_POSTGRES_PORT:-5432}
###############################################################################
# Prints help message
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
printHelp() {
cat << EOF
Usage: ${0##*/} [start|stop|env|unset]
This script is used to run the Jenkins Proxy on localhost.
As a prerequisite OPENSHIFT_API_TOKEN needs to be exported.
In your shell (from the root of fabric8-jenkins-proxy):
> export DSAAS_PREVIEW_TOKEN=<dsaas-preview token>
> ./scripts/${0##*/} start
# Run command below this in a separate terminal so that we can see logs of the above command.
> export DSAAS_PREVIEW_TOKEN=<dsaas-preview token>
> eval \$(./scripts/${0##*/} env)
> fabric8-jenkins-proxy
EOF
}
###############################################################################
# Wraps oc command custom config location
# Globals:
# None
# Arguments:
# Passes all arguments to oc command
# Returns:
# None
###############################################################################
loc() {
oc --config $(dirname $0)/config $@
}
###############################################################################
# Ensures login to OpenShift
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
login() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
loc login https://api.dsaas-stg.openshift.com -n dsaas-preview --token=${DSAAS_PREVIEW_TOKEN} >/dev/null
}
generateCertificates() {
# generate only if needed
[[ -r server.key ]] && [[ -r server.crt ]] && return
openssl req -newkey rsa:2048 -nodes -keyout ./server.key -x509 -days 365 -out ./server.crt
}
###############################################################################
# Retrieves the required Auth token
# Globals:
# JC_AUTH_TOKEN - Auth token for the auth service
# Arguments:
# None
# Returns:
# None
###############################################################################
setTokens() {
pod=$(loc get pods -l deploymentconfig=jenkins-proxy -o json | jq -r '.items[0].metadata.name')
if [ "${pod}" == "null" ] ; then
echo "WARN: Unable to determine Proxy pod name"
return
fi
export JC_AUTH_TOKEN=$(loc exec ${pod} env | grep JC_AUTH_TOKEN | sed -e 's/JC_AUTH_TOKEN=//')
}
###############################################################################
# Forwards the jenkins-idler service to localhost
# Globals:
# LOCAL_IDLER_PORT - local Idler port
# Arguments:
# None
# Returns:
# None
###############################################################################
forwardIdler() {
pod=$(loc get pods -l deploymentconfig=jenkins-idler -o json | jq -r '.items[0].metadata.name')
if [ "${pod}" == "null" ] ; then
echo "WARN: Unable to determine Idler pod name"
return
fi
port=$(loc get pods -l deploymentconfig=jenkins-idler -o json | jq -r '.items[0].spec.containers[0].ports[0].containerPort')
if lsof -Pi :${LOCAL_IDLER_PORT} -sTCP:LISTEN -t >/dev/null ; then
echo "INFO: Local Idler port ${LOCAL_IDLER_PORT} already listening. Skipping oc port-forward"
return
fi
while :
do
loc port-forward ${pod} ${LOCAL_IDLER_PORT}:${port}
echo "Idler port forward stopped with exit code $?. Respawning.." >&2
sleep 1
done
echo "Idler port forward stopped." >&2
}
###############################################################################
# Forwards the f8tenant service to localhost
# Globals:
# LOCAL_TENANT_PORT - local tenant port
# Arguments:
# None
# Returns:
# None
###############################################################################
forwardTenant() {
pod=$(loc get pods -l deploymentconfig=f8tenant -o json | jq -r '.items[0].metadata.name')
if [ "${pod}" == "null" ] ; then
echo "WARN: Unable to determine Tenant pod name"
return
fi
port=$(loc get pods -l deploymentconfig=f8tenant -o json | jq -r '.items[0].spec.containers[0].ports[0].containerPort')
if lsof -Pi :${LOCAL_TENANT_PORT} -sTCP:LISTEN -t >/dev/null ; then
echo "INFO: Local Tenant port ${LOCAL_TENANT_PORT} already listening. Skipping oc port-forward"
return
fi
while :
do
loc port-forward ${pod} ${LOCAL_TENANT_PORT}:${port}
echo "Tenant port forward stopped with exit code $?. Respawning.." >&2
sleep 1
done
echo "Tenant port forward stopped." >&2
}
###############################################################################
# Runs a Postgres Docker container on port 5432
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
runPostgres() {
container=$(docker ps -q --filter "name=postgres")
if [ -z "${container}" ] ; then
docker run --name postgres -e POSTGRES_PASSWORD=postgres -d -p ${LOCAL_POSTGRES_PORT}:5432 postgres 1>&2
fi
}
###############################################################################
# Starts the port forwarding as well as the Postgres Docker container.
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
start() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
generateCertificates
login
loc get pods > /dev/null
[ "$?" -ne 0 ] && echo "Your OpenShift token is not valid" && exit 1
forwardIdler &
forwardTenant &
runPostgres &
}
###############################################################################
# Displays the required environment settings for evaluation.
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
env() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
login
setTokens
echo export JC_WIT_API_URL=https://api.prod-preview.openshift.io
echo export JC_REDIRECT_URL=https://localhost:8080
echo export JC_ENABLE_HTTPS=true
echo export JC_AUTH_URL=https://auth.prod-preview.openshift.io
echo export JC_POSTGRES_PORT=${LOCAL_POSTGRES_PORT}
echo export JC_POSTGRES_HOST=localhost
echo export JC_POSTGRES_USER=postgres
echo export JC_POSTGRES_PASSWORD=postgres
echo export JC_POSTGRES_DATABASE=postgres
echo export JC_AUTH_TOKEN=${JC_AUTH_TOKEN}
echo export JC_IDLER_API_URL=http://localhost:${LOCAL_IDLER_PORT}
echo export JC_F8TENANT_API_URL=http://localhost:${LOCAL_TENANT_PORT}
echo export JC_OSO_CLUSTERS="'{\"https://api.free-stg.openshift.com/\": \"1b7d.free-stg.openshiftapps.com\"}'"
echo export JC_INDEX_PATH=static/html/index.html
}
###############################################################################
# Unsets the exported Idler environment variables.
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
unsetEnv() {
echo unset JC_WIT_API_URL
echo unset JC_REDIRECT_URL
echo unset JC_ENABLE_HTTPS
echo unset JC_AUTH_URL
echo unset JC_POSTGRES_PORT
echo unset JC_POSTGRES_HOST
echo unset JC_POSTGRES_USER
echo unset JC_POSTGRES_PASSWORD
echo unset JC_POSTGRES_DATABASE
echo unset JC_AUTH_TOKEN
echo unset JC_IDLER_API_URL
echo unset JC_F8TENANT_API_URL
echo unset JC_OSO_CLUSTERS
}
###############################################################################
# Stops oc-port forwarding and Docker container
# Globals:
# None
# Arguments:
# None
# Returns:
# None
###############################################################################
stop() {
pids=$(pgrep -a -f -d " " "setupLocalProxy.sh start")
pids+=$(pgrep -a -f -d " " "oc --config $(dirname $0)/config")
kill -9 ${pids}
docker rm -f postgres
}
case "$1" in
start)
start
;;
stop)
stop
;;
env)
env
;;
unset)
unsetEnv
;;
*)
printHelp
esac