browsertrix/btrix
Tessa Walsh aa3f1ebf5f
Add down command to uninstall and delete data (#1285)
Small improvement to `btrix` helper. Adds `./btrix down` command to
uninstall and delete data without resetting the dev environment.
2023-10-13 17:16:12 -07:00

188 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# ./btrix: Browsertrix Cloud dev environment utility
#
# Note: btrix helper expects a local.yaml file to exist in
# the chart directory alongside values.yaml.
#
# Test commands require installing pytest first, e.g.:
# python3 -m pip install pytest
#
# Usage:
#
# $ ./btrix bootstrap
# Build frontend and backend and upgrade
# Optional args:
# -microk8s: Preface kubectl/helm commands with microk8s
# -wait: Wait until pods are ready
#
# $ ./btrix reset
# Uninstall, delete data, then bootstrap
# Optional args:
# -microk8s: Preface kubectl/helm commands with microk8s
# -wait: Wait until pods are ready
#
# $ ./btrix down
# Uninstall and delete data
# Optional args:
# -microk8s: Preface kubectl/helm commands with microk8s
#
# $ ./btrix test
# Run backend tests
#
# $ ./btrix nightly
# Run nightly backend tests
bootstrap(){
echo "Building backend..."
./scripts/build-backend.sh
echo "Building frontend..."
./scripts/build-frontend.sh
echo "Installing..."
helm upgrade --install -f ./chart/values.yaml -f ./chart/local.yaml btrix ./chart
}
bootstrapMicrok8s(){
echo "Building backend..."
./scripts/build-backend.sh
echo "Building frontend..."
./scripts/build-frontend.sh
echo "Installing..."
microk8s helm3 upgrade --install -f ./chart/values.yaml -f ./chart/local.yaml btrix ./chart
}
waitUntilReady(){
echo "Waiting until ready..."
kubectl wait --for=condition=ready pod --all --timeout=300s
}
waitUntilReadyMicrok8s(){
echo "Waiting until ready..."
microk8s kubectl wait --for=condition=ready pod --all --timeout=300s
}
reset(){
echo "Stopping all crawls & profile browsers"
kubectl delete cjs -n crawlers --all
kubectl delete pjs -n crawlers --all
echo "Uninstalling..."
helm uninstall btrix
echo "Deleting data..."
kubectl delete pvc --all
kubectl delete cronjob -n crawlers --all
kubectl delete configmap -n crawlers -l btrix.crawlconfig
}
resetMicrok8s(){
echo "Stopping all crawls & profile browsers"
microk8s kubectl delete cjs -n crawlers --all
microk8s kubectl delete pjs -n crawlers --all
echo "Uninstalling..."
microk8s helm uninstall btrix
echo "Deleting data..."
microk8s kubectl delete pvc --all
microk8s kubectl delete cronjob -n crawlers --all
microk8s kubectl delete configmap -n crawlers -l btrix.crawlconfig
}
runTests() {
echo "Running backend tests..."
python3 -m pytest backend/test/test_*.py
}
runNightlyTests() {
echo "Running nightly backend tests..."
python3 -m pytest backend/test_nightly/test_*.py
}
CONTEXT=$(cat ~/.kube/config | grep "current-context:" | sed "s/current-context: //")
MICROK8S="-microk8s"
WAIT="-wait"
# bootstrap: build frontend and backend, upgrade and wait until ready
if [[ $1 = "bootstrap" ]]; then
echo "Current context: $CONTEXT"
echo "Are you sure you want to update this context?"
if [[ "$(read -e -p '[y/N] > '; echo $REPLY)" == [Yy]* ]] ; then
echo Continuing
else
echo Stopping
exit 1
fi
if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
bootstrapMicrok8s
else
bootstrap
fi
if [[ $2 = "$WAIT" || $3 = "$WAIT" ]]; then
if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
waitUntilReadyMicrok8s
else
waitUntilReady
fi
fi
fi
# reset: uninstall, delete data, then bootstrap
if [[ $1 = "reset" ]]; then
echo "Current context: $CONTEXT"
echo "Resetting k8s cluster will delete the database. All running crawls will first be canceled. Are you sure you want to do this?"
if [[ "$(read -e -p '[y/N] > '; echo $REPLY)" == [Yy]* ]] ; then
echo Continuing
else
echo Stopping
exit 1
fi
if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
resetMicrok8s
bootstrapMicrok8s
else
reset
bootstrap
fi
if [[ $2 = "$WAIT" || $3 = "$WAIT" ]] ; then
if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
waitUntilReadyMicrok8s
else
waitUntilReady
fi
fi
fi
# test: run backend tests
if [[ $1 = "test" ]]; then
runTests
fi
# nightly: run nightly backend tests
if [[ $1 = "nightly" ]]; then
runNightlyTests
fi
# down: stop and uninstall browsertrix cloud
if [[ $1 = "down" ]]; then
if [[ $2 = "$MICROK8S" ]] ; then
resetMicrok8s
else
reset
fi
fi
echo "Done"