-
Notifications
You must be signed in to change notification settings - Fork 34
/
configure.sh
executable file
·273 lines (234 loc) · 9.8 KB
/
configure.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
#!/usr/bin/env bash
set -ue
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ODS_CORE_DIR=${SCRIPT_DIR%/*}
echo_done(){
echo -e "\033[92mDONE\033[39m: $1"
}
echo_warn(){
echo -e "\033[93mWARN\033[39m: $1"
}
echo_error(){
echo -e "\033[31mERROR\033[39m: $1"
}
echo_info(){
echo -e "\033[94mINFO\033[39m: $1"
}
ADMIN_USER="admin"
ADMIN_DEFAULT_PASSWORD=
ADMIN_PASSWORD=
DEVELOPER_PASSWORD=
NEXUS_URL=
LOCAL_CONTAINER_ID=
NAMESPACE="ods"
NEXUS_DC="nexus"
INSECURE=""
HTTP_PROXY=
HTTPS_PROXY=
NO_PROXY=
function usage {
printf "Setup Nexus.\n\n"
printf "This script will ask interactively for parameters by default.\n"
printf "However, you can also pass them directly. Usage:\n\n"
printf "\t-h|--help\t\tPrint usage\n"
printf "\t-v|--verbose\t\tEnable verbose mode\n"
printf "\t-i|--insecure\t\tAllow insecure server connections when using SSL\n"
printf "\n"
printf "\t-n|--nexus\t\t\tNexus URL, e.g. 'https://nexus.example.com'\n"
printf "\t-l|--local-container-id\t\tLocal container ID\n"
printf "\t-a|--admin-password\t\tAdmin password\n"
printf "\t-d|--developer-password\t\tDeveloper password\n"
printf "\t-n|--namespace\t\t\tNamespace (defaults to '%s')\n" "${NAMESPACE}"
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--verbose) set -x;;
-h|--help) usage; exit 0;;
-i|--insecure) INSECURE="--insecure";;
-l|--local-container-id) LOCAL_CONTAINER_ID="$2"; shift;;
-l=*|--local-container-id=*) LOCAL_CONTAINER_ID="${1#*=}";;
-a|--admin-password) ADMIN_PASSWORD="$2"; shift;;
-a=*|--admin-password=*) ADMIN_PASSWORD="${1#*=}";;
-d|--developer-password) DEVELOPER_PASSWORD="$2"; shift;;
-d=*|--developer-password=*) DEVELOPER_PASSWORD="${1#*=}";;
-n|--nexus) NEXUS_URL="$2"; shift;;
-n=*|--nexus=*) NEXUS_URL="${1#*=}";;
--namespace=*) NAMESPACE="${1#*=}";;
--namespace) NAMESPACE="$2"; shift;;
*) echo_error "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
if [ -z "${LOCAL_CONTAINER_ID}" ]; then
if ! oc whoami > /dev/null; then
echo "You need to log into OpenShift first"
exit 1
fi
fi
if [ -z "${NEXUS_URL}" ]; then
configuredUrl="https://nexus.example.com"
if [ -f "${ODS_CORE_DIR}/../ods-configuration/ods-core.env" ]; then
echo_info "Configuration located"
configuredUrl=$(grep NEXUS_URL "${ODS_CORE_DIR}/../ods-configuration/ods-core.env" | cut -d "=" -f 2-)
fi
read -r -e -p "Enter Nexus URL [${configuredUrl}]: " input
if [ -z "${input}" ]; then
NEXUS_URL=${configuredUrl}
else
NEXUS_URL=${input:-""}
fi
fi
if [ -z "${ADMIN_PASSWORD}" ]; then
echo "Please enter Nexus admin password:"
read -r -e -s input
ADMIN_PASSWORD=${input:-""}
fi
if [ -z "${DEVELOPER_PASSWORD}" ]; then
if [ -f "${ODS_CORE_DIR}/../ods-configuration/ods-core.env" ]; then
echo_info "Configuration located, checking if password is changed from sample value"
samplePassword=$(grep NEXUS_PASSWORD_B64 "${ODS_CORE_DIR}/configuration-sample/ods-core.env.sample" | cut -d "=" -f 2-)
configuredPassword=$(grep NEXUS_PASSWORD_B64 "${ODS_CORE_DIR}/../ods-configuration/ods-core.env" | cut -d "=" -f 2- | base64 --decode)
if [ "${configuredPassword}" == "${samplePassword}" ]; then
echo_info "Developer password in ods-configuration/ods-core.env is the sample value"
else
echo_info "Setting developer password from ods-configuration/ods-core.env"
DEVELOPER_PASSWORD=${configuredPassword}
fi
fi
if [ -z "${DEVELOPER_PASSWORD}" ]; then
echo "Please enter Nexus developer password:"
read -r -e -s input
DEVELOPER_PASSWORD=${input:-""}
fi
fi
function waitForReady {
echo_info "Wait for Nexus to become responsive"
set +e
local n=0
local httpOk=
until [ $n -ge 20 ]; do
httpOk=$(curl ${INSECURE} -sS -o /dev/null -w "%{http_code}" "${NEXUS_URL}/service/rest/v1/status/writable")
if [ "${httpOk}" == "200" ]; then
echo_info "Nexus is up"
break
else
echo_info "Nexus is not up yet, waiting 10s ..."
sleep 10s
n=$((n+1))
fi
done
set -e
if [ "${httpOk}" != "200" ]; then
echo_error "Nexus did not start, got HTTP code ${httpOk}."
exit 1
fi
}
function runJsonScript {
local jsonScriptName=$1
shift 1
# shellcheck disable=SC2124
local runParams="$@"
echo "uploading ${jsonScriptName}.json"
curl ${INSECURE} -X POST -sSf \
--user "${ADMIN_USER}:${ADMIN_PASSWORD}" \
--header 'Content-Type: application/json' \
"${NEXUS_URL}/service/rest/v1/script" -d @json/"${jsonScriptName}".json
echo "running ${jsonScriptName}"
curl ${INSECURE} -X POST -sSf \
--user "${ADMIN_USER}:${ADMIN_PASSWORD}" \
--header 'Content-Type: text/plain' \
"${NEXUS_URL}/service/rest/v1/script/${jsonScriptName}/run" ${runParams} > /dev/null
echo "deleting ${jsonScriptName}"
curl ${INSECURE} -X DELETE -sSf \
--user "${ADMIN_USER}:${ADMIN_PASSWORD}" \
"${NEXUS_URL}/service/rest/v1/script/${jsonScriptName}"
}
function changeScriptSetting {
local allowCreation=$1
echo_info "Changing nexus.scripts.allowCreation to '${allowCreation}'"
local cmd="echo 'nexus.scripts.allowCreation=${allowCreation}' >> /nexus-data/etc/nexus.properties"
if [ -z "${LOCAL_CONTAINER_ID}" ]; then
oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" sh -c "${cmd}"
echo_info "Rollout new Nexus deployment to apply changes"
oc -n "${NAMESPACE}" rollout latest "dc/${NEXUS_DC}"
oc -n "${NAMESPACE}" rollout status "dc/${NEXUS_DC}" --watch=true
else
if ! docker exec -t "${LOCAL_CONTAINER_ID}" sh -c "${cmd}"; then
echo_error "Cannot exec in local container"
docker logs "${LOCAL_CONTAINER_ID}"
exit 1
fi
echo_info "Restart local container to apply changes"
docker stop "${LOCAL_CONTAINER_ID}" &> /dev/null
docker start "${LOCAL_CONTAINER_ID}" &> /dev/null
fi
}
waitForReady
changeScriptSetting "true"
waitForReady
# If default password exists and can be used, update it with ADMIN_PASSWORD.
DEFAULT_ADMIN_PASSWORD_FILE="/nexus-data/admin.password"
if [ -z "${LOCAL_CONTAINER_ID}" ]; then
ADMIN_DEFAULT_PASSWORD=$(oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" sh -c "cat ${DEFAULT_ADMIN_PASSWORD_FILE} 2> /dev/null || true")
HTTP_PROXY=$(oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" sh -c "echo -n \$HTTP_PROXY")
HTTPS_PROXY=$(oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" sh -c "echo -n \$HTTPS_PROXY")
NO_PROXY=$(oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" sh -c "echo -n \$NO_PROXY")
else
ADMIN_DEFAULT_PASSWORD=$(docker exec -t "${LOCAL_CONTAINER_ID}" sh -c "cat ${DEFAULT_ADMIN_PASSWORD_FILE} 2> /dev/null || true")
# shellcheck disable=SC2046,SC2005
HTTP_PROXY=$(echo $(docker exec -t "${LOCAL_CONTAINER_ID}" printenv HTTP_PROXY) | tr -d '"\r\n')
# shellcheck disable=SC2046,SC2005
HTTPS_PROXY=$(echo $(docker exec -t "${LOCAL_CONTAINER_ID}" printenv HTTPS_PROXY) | tr -d '"\r\n')
# shellcheck disable=SC2046,SC2005
NO_PROXY=$(echo $(docker exec -t "${LOCAL_CONTAINER_ID}" printenv NO_PROXY) | tr -d '"\r\n')
fi
if [ -n "${ADMIN_DEFAULT_PASSWORD}" ]; then
pong=$(curl ${INSECURE} -sS --user "${ADMIN_USER}:${ADMIN_DEFAULT_PASSWORD}" \
"${NEXUS_URL}/service/metrics/ping")
if [ "${pong}" == "pong" ]; then
echo_info "Change admin password"
curl ${INSECURE} -X POST -sSf \
--user "${ADMIN_USER}:${ADMIN_DEFAULT_PASSWORD}" \
--header 'Content-Type: application/json' \
"${NEXUS_URL}/service/rest/v1/script" -d @json/changeAdminPassword.json
curl ${INSECURE} -X POST -sSf \
--user "${ADMIN_USER}:${ADMIN_DEFAULT_PASSWORD}" \
--header 'Content-Type: text/plain' \
"${NEXUS_URL}/service/rest/v1/script/changeAdminPassword/run" -d "${ADMIN_PASSWORD}" > /dev/null
curl ${INSECURE} -X DELETE -sSf \
--user "${ADMIN_USER}:${ADMIN_PASSWORD}" \
"${NEXUS_URL}/service/rest/v1/script/changeAdminPassword"
fi
echo_info "Delete default admin password file"
if [ -z "${LOCAL_CONTAINER_ID}" ]; then
oc -n "${NAMESPACE}" rsh "dc/${NEXUS_DC}" bash -c "rm ${DEFAULT_ADMIN_PASSWORD_FILE} || true"
else
docker exec -t "${LOCAL_CONTAINER_ID}" rm "${DEFAULT_ADMIN_PASSWORD_FILE}"
fi
else
echo_info "File '${DEFAULT_ADMIN_PASSWORD_FILE}' does not exist - continuing with actual password"
fi
echo_info "Install Blob Stores"
runJsonScript "createBlobStores"
echo "{\"httpProxy\": \"${HTTP_PROXY}\", \"httpsProxy\": \"${HTTPS_PROXY}\", \"noProxy\": \"${NO_PROXY}\"}" > json/http-proxy-settings.json
echo_info "Configure HTTP proxy with: ${HTTP_PROXY}"
runJsonScript "createProxySettings" "-d @json/http-proxy-settings.json"
echo_info "Configure HTTPS proxy with: ${HTTPS_PROXY}"
runJsonScript "createProxySettingsHTTPS" "-d @json/http-proxy-settings.json"
echo_info "Configure no proxy exclusions with: ${NO_PROXY}"
runJsonScript "createNoProxySettings" "-d @json/http-proxy-settings.json"
rm json/http-proxy-settings.json
echo_info "Install Repositories"
runJsonScript "createRepos"
echo_info "Deactivate anonymous access"
runJsonScript "deactivateAnonymous"
echo_info "Setup developer role"
runJsonScript "createRole" "-d @json/developer-role.json"
echo_info "Setup developer user"
sed "s|@developer_password@|${DEVELOPER_PASSWORD}|g" json/developer-user.json > json/developer-user-with-password.json
runJsonScript "createUser" "-d @json/developer-user-with-password.json"
rm json/developer-user-with-password.json
if [ -z "${LOCAL_CONTAINER_ID}" ]; then
changeScriptSetting "false"
waitForReady
else
true # on local docker nexus don't care about security
fi