67 lines
3.4 KiB
Bash
Executable File
67 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
[[ ${WSL_DISTRO_NAME:-} == WebUp-Local && $EUID == 0 && -e /etc/webup-local/environment ]] || { echo 'Dedicated local environment required.' >&2; exit 1; }
|
|
project_root=$(realpath "$1")
|
|
k3s kubectl apply -f "$project_root/infra/k8s/review-rbac.yaml"
|
|
k3s kubectl apply -f "$project_root/infra/k8s/runtime-rbac.yaml"
|
|
if k3s kubectl -n platform-sandboxes get configmap webup-runtime >/dev/null 2>&1; then
|
|
k3s kubectl -n platform-sandboxes patch configmap webup-runtime --type merge -p '{"data":{"SOURCE_IMAGE":"docker.io/library/webup-development-sandbox:local-v1"}}'
|
|
else
|
|
k3s kubectl -n platform-sandboxes create configmap webup-runtime --from-literal=SOURCE_IMAGE=docker.io/library/webup-development-sandbox:local-v1
|
|
fi
|
|
install -d -m 0700 /etc/webup-local
|
|
if [[ ! -e /etc/webup-local/gateway.env ]]; then
|
|
python3 - <<'PY'
|
|
import os,secrets
|
|
fd=os.open('/etc/webup-local/gateway.env',os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600)
|
|
with os.fdopen(fd,'w') as stream:
|
|
stream.write('SANDBOX_TOKEN='+secrets.token_urlsafe(32)+'\nRUNTIME_TOKEN='+secrets.token_urlsafe(32)+'\n')
|
|
PY
|
|
fi
|
|
docker image inspect webup-sandbox-gateway:local-v1 webup-development-sandbox:local-v1 >/dev/null
|
|
docker save webup-sandbox-gateway:local-v1 webup-development-sandbox:local-v1 | k3s ctr --namespace k8s.io images import -
|
|
k3s kubectl --namespace platform-sandboxes create secret generic sandbox-gateway --from-env-file=/etc/webup-local/gateway.env --dry-run=client -o yaml | k3s kubectl apply -f -
|
|
python3 - "$project_root/infra/k8s/sandbox-gateway.yaml" <<'PY' | k3s kubectl apply -f -
|
|
import pathlib,sys
|
|
text=pathlib.Path(sys.argv[1]).read_text()
|
|
text=text.replace('registry.example.invalid/platform/sandbox-gateway:0.2.0','docker.io/library/webup-sandbox-gateway:local-v1')
|
|
text=text.replace('registry.example.invalid/platform/development-sandbox:0.2.0','docker.io/library/webup-development-sandbox:local-v1')
|
|
print(text)
|
|
PY
|
|
k3s kubectl --namespace platform-sandboxes rollout restart deployment/sandbox-gateway
|
|
k3s kubectl --namespace platform-sandboxes rollout status deployment/sandbox-gateway --timeout=120s
|
|
cat > /etc/systemd/system/webup-gateway-forward.service <<'UNIT'
|
|
[Unit]
|
|
Description=WebUp private local sandbox gateway forwarding
|
|
After=k3s.service
|
|
Requires=k3s.service
|
|
[Service]
|
|
Type=simple
|
|
Environment=KUBECONFIG=/etc/rancher/k3s/k3s.yaml
|
|
ExecStart=/usr/local/bin/k3s kubectl --namespace platform-sandboxes port-forward --address 127.0.0.1 service/sandbox-gateway 8090:8090
|
|
Restart=always
|
|
RestartSec=2
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
UNIT
|
|
systemctl daemon-reload
|
|
systemctl enable --now webup-gateway-forward
|
|
systemctl restart webup-gateway-forward
|
|
install -d -m 0700 "$project_root/.runtime/local-cluster"
|
|
cp /etc/webup-local/gateway.env "$project_root/.runtime/local-cluster/gateway.env"
|
|
python3 - <<'PY'
|
|
import json,time,urllib.request
|
|
values=dict(line.strip().split('=',1) for line in open('/etc/webup-local/gateway.env') if '=' in line)
|
|
for attempt in range(30):
|
|
try:
|
|
request=urllib.request.Request('http://127.0.0.1:8090/health/ready',headers={'Authorization':'Bearer '+values['SANDBOX_TOKEN']})
|
|
with urllib.request.urlopen(request,timeout=5) as response:
|
|
if json.load(response).get('ready'):
|
|
print('Private gateway authentication and Kubernetes health check passed.')
|
|
break
|
|
except Exception:
|
|
time.sleep(2)
|
|
else:
|
|
raise SystemExit('Gateway readiness did not pass; AI execution remains disabled.')
|
|
PY
|