Repository navigation
Expand file tree
/
Copy pathlocal-image.sh
More file actions
executable file
·132 lines (119 loc) · 5.59 KB
/
Copy pathlocal-image.sh
File metadata and controls
executable file
·132 lines (119 loc) · 5.59 KB
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
#!/usr/bin/env bash
#
# Build the images a local cluster runs and deploy them to it.
#
# The flow this replaces, spelled out once:
#
# 1. the server/room image (ndmspc/ci/Dockerfile.base) - the C++ and the published page
# 2. that same image with the page from the ndmspc-ui working tree copied over it
# (ndmspc/ci/Dockerfile.ui-local) - so a page change is served without releasing anything
# 3. `ansible-playbook playbooks/local.yml --tags apply` with `-e ndmspc_image=<tag>`
#
# The workspace is the directory that holds the three checkouts (ndmspc, ndmspc-ui, devops),
# which is where `cmd` is run from; `--workspace` overrides it.
#
set -euo pipefail
CLUSTER="kind"
TAG="local"
REGISTRY="localhost:5001"
WORKSPACE="$PWD"
REBUILD_BASE=false
WITH_PAGE=true
APPLY=true
SKIP_TLS_VERIFY=true
usage() {
cat <<'EOF'
Usage: devops/scripts/local-image.sh [options]
-c, --cluster <name> cluster config to apply, from devops/configs (default: kind)
-t, --tag <name> name this run's image (default: local); the image is tagged
<name>-<timestamp> so a deployment always rolls, and <name> beside it
-r, --registry <host> registry to push to (default: localhost:5001)
-w, --workspace <dir> directory holding ndmspc/, ndmspc-ui/ and devops/ (default: cwd)
--base rebuild the base image as well (needed after C++ changes; it is
built automatically when the tag does not exist yet)
--no-page skip the ndmspc-ui page overlay (deploy the published page)
--no-apply build and push only, leave the cluster alone
--verify-tls verify the registry certificate when pushing
-h, --help this text
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--cluster) CLUSTER="${2:?}"; shift 2 ;;
-t|--tag) TAG="${2:?}"; shift 2 ;;
-r|--registry) REGISTRY="${2:?}"; shift 2 ;;
-w|--workspace) WORKSPACE="${2:?}"; shift 2 ;;
--base) REBUILD_BASE=true; shift ;;
--no-page) WITH_PAGE=false; shift ;;
--no-apply) APPLY=false; shift ;;
--verify-tls) SKIP_TLS_VERIFY=false; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown argument: $1" >&2; usage >&2; exit 1 ;;
esac
done
NDMSPC_DIR="${WORKSPACE}/ndmspc"
UI_DIR="${WORKSPACE}/ndmspc-ui"
DEVOPS_DIR="${WORKSPACE}/devops"
for dir in "${NDMSPC_DIR}" "${UI_DIR}" "${DEVOPS_DIR}"; do
[[ -d "$dir" ]] || { echo "Missing ${dir}: --workspace points at the directory holding ndmspc, ndmspc-ui and devops." >&2; exit 1; }
done
CONFIG="${DEVOPS_DIR}/configs/${CLUSTER}.yaml"
[[ -f "$CONFIG" ]] || { echo "No such cluster config: ${CONFIG}" >&2; exit 1; }
BASE_IMAGE="${REGISTRY}/ndmspc/base:base-${TAG}"
# A tag of its own for every run: a Knative Service is rolled when its pod template changes, and a
# tag that stays the same while the image under it does not is a template that never changes - the
# running pods would keep the image they were started with however often this script ran. `-t` names
# the run (`local-20260926-025500`), and a plain `local` is tagged alongside for convenience.
IMAGE="${REGISTRY}/ndmspc/base:${TAG}-$(date +%Y%m%d-%H%M%S)"
IMAGE_ALIAS="${REGISTRY}/ndmspc/base:${TAG}"
push() {
local image="$1"
if [[ "$SKIP_TLS_VERIFY" == true ]]; then
podman push --tls-verify=false "$image"
else
podman push "$image"
fi
}
# --- 1. The base image: the server, the room image and the published page ---------------
# Built when asked for, or when this tag has never been built (a fresh machine, or a tag
# bumped after a C++ change).
if [[ "$REBUILD_BASE" == true ]] || ! podman image exists "$BASE_IMAGE" >/dev/null 2>&1; then
echo "==> Building the base image ${BASE_IMAGE} (this takes a few minutes)"
podman build -f "${NDMSPC_DIR}/ci/Dockerfile.base" -t "$BASE_IMAGE" "$NDMSPC_DIR"
echo "==> Pushing ${BASE_IMAGE}"
push "$BASE_IMAGE"
else
echo "==> Reusing the base image ${BASE_IMAGE} (--base rebuilds it)"
fi
# --- 2. The page from the ndmspc-ui working tree, on top of it --------------------------
if [[ "$WITH_PAGE" == true ]]; then
# The library first: the page build bundles `@ndmspc/ndmspc-ui` from its `dist/`, so building
# the page alone would bake in whatever `dist/` was left there (and `build` type-checks it).
echo "==> Building the ndmspc-ui library (dist/) from the working tree"
(cd "$UI_DIR" && npm run build)
echo "==> Building the ndmspc-ui page (page-dist/) from the working tree"
(cd "$UI_DIR" && npm run build:page)
echo "==> Overlaying it onto ${BASE_IMAGE} as ${IMAGE}"
podman build -f "${NDMSPC_DIR}/ci/Dockerfile.ui-local" \
--build-arg "BASE_IMAGE=${BASE_IMAGE}" \
-t "$IMAGE" "${UI_DIR}/page-dist"
else
echo "==> Using the published page (--no-page): tagging it as ${IMAGE}"
podman tag "$BASE_IMAGE" "$IMAGE"
fi
echo "==> Pushing ${IMAGE}"
push "$IMAGE"
podman tag "$IMAGE" "$IMAGE_ALIAS"
# Push the alias too: it is the tag a room can be upgraded/reverted to (the skeleton's `imageTags`),
# so a registry that only ever held an older `:local` would serve that stale image to every roll.
echo "==> Pushing ${IMAGE_ALIAS}"
push "$IMAGE_ALIAS"
# --- 3. Point the cluster at it --------------------------------------------------------
if [[ "$APPLY" == true ]]; then
echo "==> Applying ${CONFIG} with ndmspc_image=${IMAGE}"
(cd "$DEVOPS_DIR" && ansible-playbook playbooks/local.yml --tags apply \
-e "@${CONFIG}" -e "ndmspc_image=${IMAGE}")
else
echo "==> Not applying (--no-apply). Point a cluster at it with:"
echo " cd ${DEVOPS_DIR} && ansible-playbook playbooks/local.yml --tags apply -e @${CONFIG} -e ndmspc_image=${IMAGE}"
fi