mount dev sources directly from workspace PVC
This commit is contained in:
12
README.md
12
README.md
@@ -31,14 +31,14 @@ instance can be supplied explicitly:
|
|||||||
./dev/start.sh feature-one feature-one.control.k.mancave.link
|
./dev/start.sh feature-one feature-one.control.k.mancave.link
|
||||||
```
|
```
|
||||||
|
|
||||||
Edits anywhere under `src/` are mirrored into the running NGINX pod. Stop an
|
Each subtree must live below the PVC mount at `/home/node`. Its `src/` directory
|
||||||
instance and permanently remove its Kubernetes resources, sync process, and
|
is mounted directly into NGINX, so edits are visible immediately. Stop an
|
||||||
shared files with:
|
instance and permanently remove all of its labeled Kubernetes resources with:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./dev/stop.sh hello
|
./dev/stop.sh hello
|
||||||
```
|
```
|
||||||
|
|
||||||
The scripts use namespace `vibes`, PVC `control-vibes-control-data`, and domain
|
The scripts use namespace `vibes`, PVC `control-vibes-control-data`, PVC mount
|
||||||
`control.k.mancave.link` by default. Override them with `DEV_NAMESPACE`,
|
root `/home/node`, and domain `control.k.mancave.link` by default. Override them
|
||||||
`DEV_PVC`, `DEV_SHARED_ROOT`, `DEV_PVC_SUBPATH_ROOT`, or `DEV_DOMAIN`.
|
with `DEV_NAMESPACE`, `DEV_PVC`, `DEV_PVC_MOUNT_ROOT`, or `DEV_DOMAIN`.
|
||||||
|
|||||||
84
dev/start.sh
84
dev/start.sh
@@ -13,73 +13,27 @@ validate_instance() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
sync_tree() {
|
|
||||||
local source_dir=$1
|
|
||||||
local destination=$2
|
|
||||||
|
|
||||||
mkdir -p -- "$destination"
|
|
||||||
find "$destination" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +
|
|
||||||
cp -a -- "$source_dir/." "$destination/"
|
|
||||||
}
|
|
||||||
|
|
||||||
source_signature() {
|
|
||||||
local source_dir=$1
|
|
||||||
|
|
||||||
find "$source_dir" -printf '%P\0%y\0%s\0%T@\0%l\0' | sort -z | sha256sum
|
|
||||||
}
|
|
||||||
|
|
||||||
is_sync_process() {
|
|
||||||
local pid=$1
|
|
||||||
local instance=$2
|
|
||||||
local command
|
|
||||||
|
|
||||||
[[ $pid =~ ^[0-9]+$ && -r /proc/$pid/cmdline ]] || return 1
|
|
||||||
command=$(tr '\0' ' ' <"/proc/$pid/cmdline")
|
|
||||||
[[ $command == *"start.sh __sync $instance "* ]]
|
|
||||||
}
|
|
||||||
|
|
||||||
if [[ ${1:-} == "__sync" ]]; then
|
|
||||||
instance=${2:?missing instance}
|
|
||||||
source_dir=${3:?missing source directory}
|
|
||||||
destination=${4:?missing destination}
|
|
||||||
validate_instance "$instance"
|
|
||||||
|
|
||||||
previous_signature=$(source_signature "$source_dir")
|
|
||||||
while true; do
|
|
||||||
sleep 1
|
|
||||||
current_signature=$(source_signature "$source_dir")
|
|
||||||
if [[ $current_signature != "$previous_signature" ]]; then
|
|
||||||
sync_tree "$source_dir" "$destination"
|
|
||||||
previous_signature=$current_signature
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
default_instance=$(basename -- "$REPO_ROOT" | tr '[:upper:]_' '[:lower:]-')
|
default_instance=$(basename -- "$REPO_ROOT" | tr '[:upper:]_' '[:lower:]-')
|
||||||
instance=${1:-$default_instance}
|
instance=${1:-$default_instance}
|
||||||
validate_instance "$instance"
|
validate_instance "$instance"
|
||||||
|
|
||||||
namespace=${DEV_NAMESPACE:-vibes}
|
namespace=${DEV_NAMESPACE:-vibes}
|
||||||
pvc=${DEV_PVC:-control-vibes-control-data}
|
pvc=${DEV_PVC:-control-vibes-control-data}
|
||||||
shared_root=${DEV_SHARED_ROOT:-/data/live}
|
pvc_mount_root=${DEV_PVC_MOUNT_ROOT:-/home/node}
|
||||||
pvc_subpath_root=${DEV_PVC_SUBPATH_ROOT:-live}
|
|
||||||
domain=${DEV_DOMAIN:-control.k.mancave.link}
|
domain=${DEV_DOMAIN:-control.k.mancave.link}
|
||||||
host=${2:-$instance.$domain}
|
host=${2:-$instance.$domain}
|
||||||
release="hello-dev-$instance"
|
release="hello-dev-$instance"
|
||||||
source_dir="$REPO_ROOT/src"
|
source_dir=$(realpath -- "$REPO_ROOT/src")
|
||||||
destination="$shared_root/$instance"
|
pvc_mount_root=$(realpath -- "$pvc_mount_root")
|
||||||
state_dir="$shared_root/.hello-dev-state"
|
|
||||||
pid_file="$state_dir/$instance.pid"
|
|
||||||
log_file="$state_dir/$instance.log"
|
|
||||||
|
|
||||||
if [[ ! -d $source_dir ]]; then
|
if [[ ! -d $source_dir ]]; then
|
||||||
echo "source directory does not exist: $source_dir" >&2
|
echo "source directory does not exist: $source_dir" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $shared_root != /* || $shared_root == / ]]; then
|
if [[ $source_dir != "$pvc_mount_root/"* ]]; then
|
||||||
echo "DEV_SHARED_ROOT must be an absolute directory below /: $shared_root" >&2
|
echo "source directory is outside the PVC mount at $pvc_mount_root: $source_dir" >&2
|
||||||
exit 2
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! $host =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$ ]]; then
|
if [[ ! $host =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$ ]]; then
|
||||||
@@ -87,38 +41,16 @@ if [[ ! $host =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$ ]]; then
|
|||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p -- "$state_dir"
|
pvc_subpath=${source_dir#"$pvc_mount_root/"}
|
||||||
|
|
||||||
if [[ -f $pid_file ]]; then
|
|
||||||
old_pid=$(<"$pid_file")
|
|
||||||
if is_sync_process "$old_pid" "$instance"; then
|
|
||||||
kill "$old_pid"
|
|
||||||
wait "$old_pid" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
rm -f -- "$pid_file"
|
|
||||||
fi
|
|
||||||
|
|
||||||
sync_tree "$source_dir" "$destination"
|
|
||||||
nohup "$SCRIPT_DIR/start.sh" __sync "$instance" "$source_dir" "$destination" \
|
|
||||||
>"$log_file" 2>&1 &
|
|
||||||
sync_pid=$!
|
|
||||||
printf '%s\n' "$sync_pid" >"$pid_file"
|
|
||||||
|
|
||||||
cleanup_failed_start() {
|
|
||||||
kill "$sync_pid" 2>/dev/null || true
|
|
||||||
rm -f -- "$pid_file"
|
|
||||||
}
|
|
||||||
trap cleanup_failed_start ERR
|
|
||||||
|
|
||||||
helm template "$release" "$SCRIPT_DIR/helm" \
|
helm template "$release" "$SCRIPT_DIR/helm" \
|
||||||
--namespace "$namespace" \
|
--namespace "$namespace" \
|
||||||
--set-string "instance=$instance" \
|
--set-string "instance=$instance" \
|
||||||
--set-string "ingress.host=$host" \
|
--set-string "ingress.host=$host" \
|
||||||
--set-string "sharedVolume.claimName=$pvc" \
|
--set-string "sharedVolume.claimName=$pvc" \
|
||||||
--set-string "sharedVolume.subPath=$pvc_subpath_root/$instance" \
|
--set-string "sharedVolume.subPath=$pvc_subpath" \
|
||||||
| kubectl apply --namespace "$namespace" -f -
|
| kubectl apply --namespace "$namespace" -f -
|
||||||
|
|
||||||
kubectl rollout status "deployment/$release" --namespace "$namespace" --timeout=120s
|
kubectl rollout status "deployment/$release" --namespace "$namespace" --timeout=120s
|
||||||
trap - ERR
|
|
||||||
|
|
||||||
echo "Development instance '$instance' is live at http://$host"
|
echo "Development instance '$instance' is live at http://$host"
|
||||||
|
|||||||
34
dev/stop.sh
34
dev/stop.sh
@@ -12,44 +12,12 @@ if [[ ${#instance} -gt 42 || ! $instance =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]];
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
namespace=${DEV_NAMESPACE:-vibes}
|
namespace=${DEV_NAMESPACE:-vibes}
|
||||||
shared_root=${DEV_SHARED_ROOT:-/data/live}
|
|
||||||
destination="$shared_root/$instance"
|
|
||||||
state_dir="$shared_root/.hello-dev-state"
|
|
||||||
pid_file="$state_dir/$instance.pid"
|
|
||||||
log_file="$state_dir/$instance.log"
|
|
||||||
label="dev.mancave.link/instance=$instance"
|
label="dev.mancave.link/instance=$instance"
|
||||||
|
|
||||||
if [[ $shared_root != /* || $shared_root == / ]]; then
|
kubectl delete deployment,service,ingress \
|
||||||
echo "DEV_SHARED_ROOT must be an absolute directory below /: $shared_root" >&2
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -f $pid_file ]]; then
|
|
||||||
sync_pid=$(<"$pid_file")
|
|
||||||
process_command=""
|
|
||||||
if [[ $sync_pid =~ ^[0-9]+$ && -r /proc/$sync_pid/cmdline ]]; then
|
|
||||||
process_command=$(tr '\0' ' ' <"/proc/$sync_pid/cmdline")
|
|
||||||
fi
|
|
||||||
if [[ $process_command == *"start.sh __sync $instance "* ]]; then
|
|
||||||
kill "$sync_pid"
|
|
||||||
for _ in {1..20}; do
|
|
||||||
kill -0 "$sync_pid" 2>/dev/null || break
|
|
||||||
sleep 0.1
|
|
||||||
done
|
|
||||||
kill -9 "$sync_pid" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
kubectl delete all,ingress,configmap,secret,pvc \
|
|
||||||
--namespace "$namespace" \
|
--namespace "$namespace" \
|
||||||
--selector "$label" \
|
--selector "$label" \
|
||||||
--ignore-not-found=true \
|
--ignore-not-found=true \
|
||||||
--wait=true
|
--wait=true
|
||||||
|
|
||||||
if [[ -d $destination ]]; then
|
|
||||||
find "$destination" -depth -delete
|
|
||||||
fi
|
|
||||||
rm -f -- "$pid_file" "$log_file"
|
|
||||||
rmdir -- "$state_dir" 2>/dev/null || true
|
|
||||||
|
|
||||||
echo "Development instance '$instance' was permanently removed"
|
echo "Development instance '$instance' was permanently removed"
|
||||||
|
|||||||
@@ -7,6 +7,6 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello, world!!</h1>
|
<h1>Hello, world!!</h1>
|
||||||
<p>Served by NGINX on Minikube from the shared src mount.</p>
|
<p>Served by NGINX on Minikube from the direct PVC src mount.</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user