-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoresvc_registry.sh
executable file
·381 lines (290 loc) · 15.4 KB
/
coresvc_registry.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
#
# Starts and stops coresvc-registry, which is used during staging, running via k3s, and running in docker. Will deploy to k3s if available, or fallback to docker
#
# Example Usage:
#
# "bash ./scripts/coresvc_registry.sh"
# Load the modules and pass all the same parameters that we're passing here
# shellcheck disable=SC1091
# shellcheck disable=SC2068
source "$(dirname "$(realpath "$0")")/../modules/load_modules.sh" $@
############################################################
# Script variables
############################################################
START_REGISTRY=false
STOP_REGISTRY=false
IS_RUNNING=false
HAS_DOCKER=false
HAS_K3S=false
DESTINATION_HOST=""
REGISTRY_REPO=""
############################################################
# Help #
############################################################
function show_help() {
# Display Help
echo "Starts and stops coresvc-registry, which is used during staging, running via k3s, and running in docker. Will deploy to k3s if available, or fallback to docker."
echo
echo "Syntax: bash ./scripts/coresvc_registry.sh [--dev-environment]"
echo "options:"
echo "--start [OPTIONAL] Start the registry. Will be in docker when paired with --dev-environment. Otherwise will start in kubernetes"
echo "--stop [OPTIONAL] Stops the registry. Will be in docker when paired with --dev-environment. Otherwise will stop in kubernetes"
echo "--help | -h [OPTIONAL] Help script (this screen)"
echo
exit 1
}
############################################################
# Process the input options.
############################################################
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) show_help ;;
--start)
START_REGISTRY=true
;;
--stop)
STOP_REGISTRY=true
;;
*) echo "Unknown parameter '$1'"; show_help ;;
esac
shift
done
############################################################
# Check to make sure we're good to go for the registry in development
############################################################
function check_prerequisites(){
info_log "START: ${FUNCNAME[0]}"
is_cmd_available "docker" HAS_DOCKER
is_cmd_available "kubectl" HAS_K3S
if [[ "${HAS_K3S}" == true ]]; then
# if we have kubectl, then check if we have k3s
is_cmd_available "k3s" HAS_K3S
if [[ "${HAS_K3S}" == true ]]; then
# We have k3s, so we need to check if it's running
run_a_script "pgrep \"k3s\"" k3s_status --ignore_error
if [[ -z "${k3s_status}" ]]; then
# k3s is installed but not running
HAS_K3S=false
fi
fi
fi
# shellcheck disable=SC2154
if [[ "${HAS_DOCKER}" == true ]]; then
debug_log "Docker found."
DESTINATION_HOST="docker"
fi
if [[ "${HAS_K3S}" == true ]]; then
debug_log "K3s found."
DESTINATION_HOST="k3s"
fi
[[ ! -d "${SPACEFX_DIR}/registry/data" ]] && create_directory "${SPACEFX_DIR}/registry/data"
[[ ! -d "${SPACEFX_DIR}/registry/pypiserver" ]] && create_directory "${SPACEFX_DIR}/registry/pypiserver"
[[ ! -d "${SPACEFX_DIR}/certs/registry" ]] && create_directory "${SPACEFX_DIR}/certs/registry"
debug_log "Querying for registry values..."
run_a_script "yq '.services.core.registry.repository' ${SPACEFX_DIR}/chart/values.yaml" REGISTRY_REPO
run_a_script "yq '.global.containerRegistry' ${SPACEFX_DIR}/chart/values.yaml" REGISTRY
run_a_script "yq '.services.core.registry.serviceNamespace' ${SPACEFX_DIR}/chart/values.yaml" NAMESPACE
calculate_tag_from_channel --tag "${SPACEFX_VERSION}" --result REGISTRY_TAG
debug_log "...REGISTRY_REPO calculated as '${REGISTRY_REPO}'"
debug_log "...REGISTRY calculated as '${REGISTRY}'"
debug_log "...NAMESPACE calculated as '${NAMESPACE}'"
debug_log "...REGISTRY_TAG calculated as '${REGISTRY_TAG}'"
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Check if the registry is already up and running
############################################################
function check_if_registry_is_already_running(){
info_log "START: ${FUNCNAME[0]}"
if [[ "${HAS_K3S}" == true ]]; then
info_log "Checking if '${REGISTRY_REPO}' is deployed to K3s..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} get deployments -A -o jsonpath=\"{.items[?(@.metadata.name=='${REGISTRY_REPO}')].metadata.name}\"" _previous_deployment --ignore_error
if [[ -n "${_previous_deployment}" ]]; then
info_log "...found '${REGISTRY_REPO}' running in K3s."
IS_RUNNING=true
fi
fi
# shellcheck disable=SC2154
if [[ "${HAS_DOCKER}" == true ]]; then
info_log "Checking if '${REGISTRY_REPO}' is already running in Docker..."
run_a_script "docker container ls -a --format '{{json .}}' | jq -r 'if any(.Names; .== \"${REGISTRY_REPO}\") then .State else empty end'" container_status --disable_log
if [[ "${container_status}" == "running" ]]; then
info_log "...found previous instance of '${REGISTRY_REPO}' in running in Docker"
IS_RUNNING=true
else
# Container status is not empty, but not "running" either. There's a stopped container that we need to remove
if [[ -n "${container_status}" ]]; then
info_log "...found non-running instance of '${REGISTRY_REPO}' in Docker. Removing..."
run_a_script "docker container rm ${REGISTRY_REPO} -f"
info_log "...successfully removed ${REGISTRY_REPO} in Docker"
fi
fi
fi
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Stop the registry
############################################################
function stop_registry(){
info_log "START: ${FUNCNAME[0]}"
if [[ "${IS_RUNNING}" == false ]]; then
info_log "No previous instance of '${REGISTRY_REPO}' found. Nothing to do"
return
fi
if [[ "${HAS_DOCKER}" == true ]]; then
info_log "Checking for ${REGISTRY_REPO} in Docker..."
run_a_script "docker container ls -a --format json | jq '. | select(.Names == \"${REGISTRY_REPO}\")'" docker_container --disable_log
if [[ -n "${docker_container}" ]]; then
info_log "...found ${REGISTRY_REPO} in Docker. Stopping..."
run_a_script "docker remove --force ${REGISTRY_REPO}"
info_log "...successfully stopped ${REGISTRY_REPO} in Docker"
else
info_log "...${REGISTRY_REPO} is not running in Docker. Nothing to do"
fi
fi
if [[ "${HAS_K3S}" == true ]]; then
info_log "Checking for ${REGISTRY_REPO} in K3s..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} get deployments -A -o jsonpath=\"{.items[?(@.metadata.name=='${REGISTRY_REPO}')].metadata.name}\"" _previous_deployment --ignore_error
if [[ -n "${_previous_deployment}" ]]; then
info_log "...found '${REGISTRY_REPO}' running in K3s. Stopping..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} delete deployment/${REGISTRY_REPO} -n ${NAMESPACE}"
info_log "...successfully stopped ${REGISTRY_REPO} in K3s"
fi
fi
info_log "Stopping registry processes (if still running)"
run_a_script "pgrep '^registry'" pids --ignore_error
for pid in $pids; do
debug_log "...terminating process id '${pid}'"
run_a_script "kill -9 ${pid}" --disable_log --ignore_error
done
info_log "...successfully stopped registry processes."
info_log "Stopping pypiserver processes (if still running)"
run_a_script "pgrep '^pypiserver'" pids --ignore_error
for pid in $pids; do
debug_log "...terminating process id '${pid}'"
run_a_script "kill -9 ${pid}" --disable_log --ignore_error
done
info_log "...successfully stopped pypiserver processes."
IS_RUNNING=false
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Start the registry in k3s
############################################################
function start_registry_k3s(){
info_log "START: ${FUNCNAME[0]}"
info_log "Checking for namespace '${NAMESPACE}'..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} get namespaces/${NAMESPACE}" has_namespace --ignore_error
if [[ -z "${has_namespace}" ]]; then
info_log "...not found. Deploying..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} create namespace ${NAMESPACE}"
info_log "...successfully deployed"
fi
info_log "Checking if '${REGISTRY_REPO}' container image has been imported..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} get nodes -o json | jq '.items[0].status.nodeInfo.containerRuntimeVersion' -r" k3sContainerRunTime
if [[ "${k3sContainerRunTime}" == *"docker"* ]]; then
# We're running in docker - pull the cache using the docker images command
run_a_script "docker images" k3s_images_in_cache
else
run_a_script "ctr --address /run/k3s/containerd/containerd.sock images list --quiet" k3s_images_in_cache
fi
if [[ "${k3s_images_in_cache}" != *"${REGISTRY}/${REGISTRY_REPO}"* ]]; then
info_log "...'${REGISTRY_REPO}' not found in image cache. Importing..."
if [[ ! -f "${SPACEFX_DIR}/images/${HOST_ARCHITECTURE}/coresvc-registry_${SPACEFX_VERSION}.tar" ]]; then
exit_with_error "Unable to find '${SPACEFX_DIR}/images/${HOST_ARCHITECTURE}/coresvc-registry_${SPACEFX_VERSION}.tar'"
fi
if [[ "${k3sContainerRunTime}" == *"docker"* ]]; then
run_a_script "docker load --quiet --input ${SPACEFX_DIR}/images/${HOST_ARCHITECTURE}/coresvc-registry_${SPACEFX_VERSION}.tar" image_hash
# Remove the return value we get from docker load
image_hash=${image_hash#"Loaded image: "}
# Tag don't match - this'll update it to match what we have in helm
run_a_script "docker tag ${image_hash} ${REGISTRY}/${REGISTRY_REPO}:${SPACEFX_VERSION}"
else
run_a_script "ctr --address /run/k3s/containerd/containerd.sock images import ${SPACEFX_DIR}/images/${HOST_ARCHITECTURE}/coresvc-registry_${SPACEFX_VERSION}.tar"
# Tag doesn't match - this'll update it to match what we have in helm
run_a_script "ctr --address /run/k3s/containerd/containerd.sock images list --quiet | grep '${REGISTRY_REPO}:${REGISTRY_TAG}'" current_tag
run_a_script "ctr --address /run/k3s/containerd/containerd.sock images tag ${current_tag} ${REGISTRY}/${REGISTRY_REPO}:${SPACEFX_VERSION}"
fi
fi
# Run a helm dependency update so we can
if [[ ! -f "${SPACEFX_DIR}/chart/Chart.lock" ]]; then
run_a_script "helm --kubeconfig ${KUBECONFIG} dependency update ${SPACEFX_DIR}/chart"
fi
run_a_script "helm --kubeconfig ${KUBECONFIG} template ${SPACEFX_DIR}/chart --set services.core.registry.enabled=true" registry_yaml
debug_log "...deploying core-registry..."
run_a_script "kubectl --kubeconfig ${KUBECONFIG} apply -f - <<SPACEFX_UPDATE_END
${registry_yaml}
SPACEFX_UPDATE_END" --disable_log
wait_for_deployment --namespace "${NAMESPACE}" --deployment "${REGISTRY_REPO}"
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Start the registry in docker
############################################################
function start_registry_docker(){
info_log "START: ${FUNCNAME[0]}"
# Calculate the image tag based on the channel and then check the registries to find it
info_log "Locating parent registry and calculating tags for '${REGISTRY_REPO}'..."
calculate_tag_from_channel --tag "${SPACEFX_VERSION}" --result spacefx_version_tag
find_registry_for_image "${REGISTRY_REPO}:${spacefx_version_tag}" coresvc_registry_parent
if [[ -z "${coresvc_registry_parent}" ]]; then
exit_with_error "${REGISTRY_REPO}:${spacefx_version_tag} was not found in any configured containers with pull_enabled. Please check your access"
fi
# We have our parent container registry. Check to see if it needs a repo suffix
check_for_repo_prefix --registry "${container_registry}" --repo "${REGISTRY_REPO}" --result _repo_name
# Check to see if the image is already in docker
run_a_script "docker images --format '{{json .}}' --no-trunc | jq -r '. | select(.Repository == \"${coresvc_registry_parent}/${_repo_name}\" and .Tag == \"${spacefx_version_tag}\") | any'" has_docker_image --ignore_error
if [[ "${has_docker_image}" == "true" ]]; then
info_log "...image ${coresvc_registry_parent}/${_repo_name}:${spacefx_version_tag} already exists in Docker. Nothing to do"
else
info_log "...image ${coresvc_registry_parent}/${_repo_name}:${spacefx_version_tag} not found in Docker. Pulling..."
run_a_script "docker pull ${coresvc_registry_parent}/${_repo_name}:${spacefx_version_tag}"
info_log "...successfully pulled ${coresvc_registry_parent}/${_repo_name}:${spacefx_version_tag} to Docker."
fi
info_log "Starting '${REGISTRY_REPO}'..."
run_a_script "docker run -d \
-p 5000:5000 \
-p 8080:8080 \
-v ${SPACEFX_DIR}/registry/data:/var/lib/registry \
-v ${SPACEFX_DIR}/certs/registry:/certs \
-v ${SPACEFX_DIR}/certs/ca/ca.spacefx.local.pem:/etc/pki/ca-trust/source/anchors/ca.spacefx.local.pem:ro \
-v ${SPACEFX_DIR}/registry/pypiserver/packages:/data/packages \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry.spacefx.local.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/registry.spacefx.local.key \
--name=${REGISTRY_REPO} ${coresvc_registry_parent}/${_repo_name}:${spacefx_version_tag}"
info_log "...successfully started core-registry."
info_log "END: ${FUNCNAME[0]}"
}
function main() {
write_parameter_to_log START_REGISTRY
write_parameter_to_log STOP_REGISTRY
check_prerequisites
check_if_registry_is_already_running
if [[ "${STOP_REGISTRY}" == true ]]; then
stop_registry
info_log "------------------------------------------"
info_log "END: ${SCRIPT_NAME}"
return
fi
if [[ "${START_REGISTRY}" == true ]]; then
write_parameter_to_log DESTINATION_HOST
info_log "Starting ${REGISTRY_REPO}"
if [[ ! -f "${SPACEFX_DIR}/certs/registry/registry.spacefx.local.crt" ]]; then
info_log "Missing certificates detected. Generating certificates and restarting ${REGISTRY_REPO} (if applicable)"
# We have to stop the registry if we have to regen the certificates
stop_registry
# Generate the new certificates for SSL/TLS
generate_certificate --profile "${SPACEFX_DIR}/certs/registry/registry.spacefx.local.ssl.json" --config "${SPACEFX_DIR}/certs/registry/registry.spacefx.local.ssl-config.json" --output "${SPACEFX_DIR}/certs/registry"
fi
if [[ "${IS_RUNNING}" == true ]]; then
info_log "Registry is already running. Nothing to do"
else
[[ "${DESTINATION_HOST}" == "docker" ]] && start_registry_docker
[[ "${DESTINATION_HOST}" == "k3s" ]] && start_registry_k3s
fi
info_log "------------------------------------------"
info_log "END: ${SCRIPT_NAME}"
fi
}
main