add isolated live development deployments
This commit is contained in:
124
dev/start.sh
Executable file
124
dev/start.sh
Executable file
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
|
||||
REPO_ROOT=$(cd -- "$SCRIPT_DIR/.." && pwd)
|
||||
|
||||
validate_instance() {
|
||||
local instance=$1
|
||||
|
||||
if [[ ${#instance} -gt 42 || ! $instance =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]]; then
|
||||
echo "instance must be 1-42 lowercase letters, digits, or internal hyphens: $instance" >&2
|
||||
exit 2
|
||||
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:]-')
|
||||
instance=${1:-$default_instance}
|
||||
validate_instance "$instance"
|
||||
|
||||
namespace=${DEV_NAMESPACE:-vibes}
|
||||
pvc=${DEV_PVC:-control-vibes-control-data}
|
||||
shared_root=${DEV_SHARED_ROOT:-/data/live}
|
||||
pvc_subpath_root=${DEV_PVC_SUBPATH_ROOT:-live}
|
||||
domain=${DEV_DOMAIN:-control.k.mancave.link}
|
||||
host=${2:-$instance.$domain}
|
||||
release="hello-dev-$instance"
|
||||
source_dir="$REPO_ROOT/src"
|
||||
destination="$shared_root/$instance"
|
||||
state_dir="$shared_root/.hello-dev-state"
|
||||
pid_file="$state_dir/$instance.pid"
|
||||
log_file="$state_dir/$instance.log"
|
||||
|
||||
if [[ ! -d $source_dir ]]; then
|
||||
echo "source directory does not exist: $source_dir" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ $shared_root != /* || $shared_root == / ]]; then
|
||||
echo "DEV_SHARED_ROOT must be an absolute directory below /: $shared_root" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [[ ! $host =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$ ]]; then
|
||||
echo "invalid hostname: $host" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
mkdir -p -- "$state_dir"
|
||||
|
||||
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" \
|
||||
--namespace "$namespace" \
|
||||
--set-string "instance=$instance" \
|
||||
--set-string "ingress.host=$host" \
|
||||
--set-string "sharedVolume.claimName=$pvc" \
|
||||
--set-string "sharedVolume.subPath=$pvc_subpath_root/$instance" \
|
||||
| kubectl apply --namespace "$namespace" -f -
|
||||
|
||||
kubectl rollout status "deployment/$release" --namespace "$namespace" --timeout=120s
|
||||
trap - ERR
|
||||
|
||||
echo "Development instance '$instance' is live at http://$host"
|
||||
Reference in New Issue
Block a user