Added mirror support to avoid hitting remote constantly
This commit is contained in:
80
devtools/push-dep-pkg-mirror.sh
Executable file
80
devtools/push-dep-pkg-mirror.sh
Executable file
@@ -0,0 +1,80 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
NAMESPACE="${NAMESPACE:-default}"
|
||||
PACKAGES_DIR="$(realpath "$SCRIPT_DIR/../packages/")"
|
||||
APP_LABEL="${APP_LABEL:-app=dep-pkg-mirror}"
|
||||
CONTAINER="${CONTAINER:-nginx}"
|
||||
REMOTE_DIR="${REMOTE_DIR:-/usr/share/nginx/html/monok8s/packages}"
|
||||
|
||||
if [ ! -d "$PACKAGES_DIR" ]; then
|
||||
echo "error: package dir not found: $PACKAGES_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
need_cmd() {
|
||||
command -v "$1" >/dev/null 2>&1 || {
|
||||
echo "error: missing command: $1" >&2
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
need_cmd kubectl
|
||||
need_cmd mktemp
|
||||
need_cmd tar
|
||||
need_cmd cp
|
||||
need_cmd find
|
||||
|
||||
pod="$(
|
||||
kubectl -n "$NAMESPACE" get pod \
|
||||
-l "$APP_LABEL" \
|
||||
--field-selector=status.phase=Running \
|
||||
-o jsonpath='{.items[0].metadata.name}'
|
||||
)"
|
||||
|
||||
if [ -z "$pod" ]; then
|
||||
echo "error: no running pod found with label: $APP_LABEL in namespace: $NAMESPACE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "using pod: $pod"
|
||||
echo "remote dir: $REMOTE_DIR"
|
||||
|
||||
echo "checking remote dir is writable"
|
||||
kubectl -n "$NAMESPACE" exec "$pod" -c "$CONTAINER" -- sh -c "
|
||||
mkdir -p '$REMOTE_DIR' &&
|
||||
touch '$REMOTE_DIR/.write-test' &&
|
||||
rm -f '$REMOTE_DIR/.write-test'
|
||||
"
|
||||
|
||||
stage="$(mktemp -d)"
|
||||
cleanup() {
|
||||
rm -rf "$stage"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
mkdir -p "$stage/packages"
|
||||
|
||||
echo "staging $PACKAGES_DIR/ as packages/"
|
||||
tar \
|
||||
--exclude='.DS_Store' \
|
||||
--exclude='.stamp-*' \
|
||||
-C "$PACKAGES_DIR" \
|
||||
-cf - . | tar -C "$stage/packages" -xf -
|
||||
|
||||
echo "copying staged packages into mirror pod"
|
||||
kubectl -n "$NAMESPACE" cp \
|
||||
"$stage/packages/." \
|
||||
"$pod:$REMOTE_DIR" \
|
||||
-c "$CONTAINER"
|
||||
|
||||
echo "done"
|
||||
echo
|
||||
echo "Mirror base URL:"
|
||||
echo " DEP_PKG_MIRROR=http://dep-pkg-mirror.${NAMESPACE}.svc.cluster.local/monok8s"
|
||||
echo
|
||||
echo "Example:"
|
||||
echo " packages/kubernetes/kubelet-v1.35.3"
|
||||
echo " -> http://dep-pkg-mirror.${NAMESPACE}.svc.cluster.local/monok8s/packages/kubernetes/kubelet-v1.35.3"
|
||||
Reference in New Issue
Block a user