webapp-deployment-status-api/scripts/run.sh

195 lines
5.3 KiB
Bash
Raw Normal View History

2024-02-03 01:39:21 +00:00
#!/bin/bash
if [ "$RUN_AUCTIONS_HANDLER" = "true" ]; then
exec "./scripts/handle-auctions.sh"
exit
fi
2024-02-09 00:17:26 +00:00
function is_privileged {
ip link add dummy0 type dummy >/dev/null
if [[ $? -eq 0 ]]; then
echo "true"
ip link delete dummy0 >/dev/null
else
echo "false"
fi
}
2024-02-03 01:39:21 +00:00
if [ -z "$DEPLOYMENT_DNS_SUFFIX" ]; then
echo "DEPLOYMENT_DNS_SUFFIX is required."
exit 2
fi
if [ -z "$DEPLOYMENT_RECORD_NAMESPACE" ]; then
echo "DEPLOYMENT_RECORD_NAMESPACE is required."
exit 2
fi
if [ -z "$IMAGE_REGISTRY" ]; then
echo "IMAGE_REGISTRY is required."
exit 2
fi
if [ ! -f "/etc/config/laconic.yml" ]; then
echo "/etc/config/laconic.yml is required."
exit 2
fi
if [ ! -f "/etc/config/kube.yml" ]; then
echo "/etc/config/kube.yml is required."
exit 2
fi
AUCTION_OPTS=""
if [ "$HANDLE_AUCTION_REQUESTS" = "true" ]; then
AUCTION_OPTS="--auction-requests"
fi
2024-02-09 00:17:26 +00:00
STORAGE_ROOT="${STORAGE_ROOT:-/srv}"
DEPLOYMENTS_DIR="${DEPLOYMENTS_DIR:-$STORAGE_ROOT/deployments}"
LOG_DIR="${LOG_DIR:-$STORAGE_ROOT/logs}"
CONTAINERS_DIR="${CONTAINER_DIR:-$STORAGE_ROOT/containers}"
REGISTRY_LOCK_FILE="${REGISTRY_LOCK_FILE:-/srv/registry_mutex_lock_file}"
2024-02-09 00:17:26 +00:00
if [[ ! -d "${DEPLOYMENTS_DIR}" ]]; then
mkdir -p "${DEPLOYMENTS_DIR}"
2024-02-03 01:39:21 +00:00
fi
2024-02-09 00:17:26 +00:00
LOG_OPTS=""
if [[ -n "${LOG_DIR}" ]]; then
if [[ ! -d "${LOG_DIR}" ]]; then
mkdir -p "${LOG_DIR}"
2024-02-03 01:39:21 +00:00
fi
2024-02-09 00:17:26 +00:00
LOG_OPTS="--log-dir $LOG_DIR"
2024-02-03 01:39:21 +00:00
fi
2024-02-09 00:17:26 +00:00
if [[ ! -d "${CONTAINERS_DIR}" ]]; then
mkdir -p "${CONTAINERS_DIR}"
2024-02-03 01:39:21 +00:00
fi
2024-02-09 00:17:26 +00:00
if [[ "$CLEAN_DEPLOYMENTS" == "true" ]]; then
echo "Cleaning deployments directory..."
rm -rf ${DEPLOYMENTS_DIR}/*
2024-02-03 01:39:21 +00:00
fi
2024-02-09 00:17:26 +00:00
if [[ "$CLEAN_CONTAINERS" == "true" ]]; then
echo "Cleaning containers directory..."
rm -rf ${CONTAINERS_DIR}/*
fi
2024-02-07 21:58:19 +00:00
2024-02-09 00:17:26 +00:00
if [[ "$CLEAN_LOGS" == "true" ]] && [[ -n "$LOG_DIR" ]]; then
echo "Cleaning logs directory..."
rm -rf ${LOG_DIR}/*
2024-02-07 21:58:19 +00:00
fi
if [[ ! -d "${UPLOAD_DIRECTORY}" ]]; then
mkdir -p "${UPLOAD_DIRECTORY}"
fi
2024-02-09 00:17:26 +00:00
STORAGE_DRIVER="${STORAGE_DRIVER}"
if [[ -z "${STORAGE_DRIVER}" ]]; then
if [[ "true" == "`is_privileged`" ]]; then
STORAGE_DRIVER="overlay"
else
STORAGE_DRIVER="vfs"
fi
fi
echo "Using '$STORAGE_DRIVER' storage driver."
cat > /etc/containers/storage.conf <<EOF
[storage]
driver = "${STORAGE_DRIVER}"
runroot = "${CONTAINERS_DIR}/podman/run"
graphroot = "${CONTAINERS_DIR}/podman/storage"
[storage.options.overlay]
mount_program = "/usr/bin/fuse-overlayfs"
EOF
if [[ -n "$IMAGE_REGISTRY_CREDS" ]]; then
docker login --password "$IMAGE_REGISTRY_CREDS" --username ${IMAGE_REGISTRY_USER:-ANY} $IMAGE_REGISTRY
2024-02-07 21:58:19 +00:00
fi
2024-02-09 00:17:26 +00:00
cd /app/
2024-02-03 01:39:21 +00:00
while true; do
2024-02-09 00:22:47 +00:00
UPDATE_OPTS=""
if [[ "true" == "${ONLY_NEW_REQUESTS:-true}" ]] && [[ ! -f "${DEPLOYMENTS_DIR}/autodeploy.state" ]]; then
UPDATE_OPTS="--only-update-state"
fi
2024-02-03 01:39:21 +00:00
echo "=============================================================="
2024-03-25 16:50:40 +00:00
ps -ef | grep node | grep 'yarn start' >/dev/null
if [ $? -ne 0 ]; then
echo "########### STARTING API ############"
echo "Starting API"
yarn start &
fi
2024-02-03 01:39:21 +00:00
echo "########### UNDEPLOY ############"
laconic-so undeploy-webapp-from-registry \
--laconic-config /etc/config/laconic.yml \
--registry-lock-file "${REGISTRY_LOCK_FILE}" \
2024-02-09 00:17:26 +00:00
--deployment-parent-dir "${DEPLOYMENTS_DIR}" \
2024-02-03 01:39:21 +00:00
--delete-names \
--delete-volumes \
2024-02-09 00:17:26 +00:00
--state-file "${DEPLOYMENTS_DIR}/autoremove.state" \
2024-02-07 21:58:19 +00:00
--include-tags "$INCLUDE_TAGS" \
--exclude-tags "$EXCLUDE_TAGS" \
--lrn "$LRN" \
--min-required-payment 0 \
2024-02-07 21:58:19 +00:00
$EXTRA_UNDEPLOY_OPTS \
2024-02-09 00:22:47 +00:00
$UPDATE_OPTS \
2024-02-03 01:39:21 +00:00
--discover
rc=$?
if [ $rc -eq 0 ]; then
echo "############ UNDEPLOY SUCCESS #############"
else
echo "############ UNDEPLOY FAILURE STATUS $rc #############"
fi
2024-02-03 01:39:21 +00:00
echo "############ DEPLOY #############"
laconic-so deploy-webapp-from-registry \
--kube-config /etc/config/kube.yml \
--laconic-config /etc/config/laconic.yml \
--registry-lock-file "${REGISTRY_LOCK_FILE}" \
2024-02-03 01:39:21 +00:00
--image-registry ${IMAGE_REGISTRY} \
2024-02-09 00:17:26 +00:00
--deployment-parent-dir "${DEPLOYMENTS_DIR}" \
2024-02-03 01:39:21 +00:00
--dns-suffix ${DEPLOYMENT_DNS_SUFFIX} \
2024-07-24 00:40:14 +00:00
--record-namespace-dns lrn://${DEPLOYMENT_RECORD_NAMESPACE}/dns \
--record-namespace-deployments lrn://${DEPLOYMENT_RECORD_NAMESPACE}/deployments \
2024-02-09 00:17:26 +00:00
--state-file "${DEPLOYMENTS_DIR}/autodeploy.state" \
2024-02-07 21:58:19 +00:00
--include-tags "$INCLUDE_TAGS" \
--exclude-tags "$EXCLUDE_TAGS" \
2024-04-15 19:26:37 +00:00
--fqdn-policy "${FQDN_POLICY:-prohibit}" \
--lrn "$LRN" \
--min-required-payment ${MIN_REQUIRED_PAYMENT:-0} \
--config-upload-dir "$UPLOAD_DIRECTORY" \
2024-08-27 20:00:21 +00:00
--private-key-file "$OPENPGP_PRIVATE_KEY_FILE" \
--private-key-passphrase "$OPENPGP_PASSPHRASE" \
--recreate-on-deploy \
$AUCTION_OPTS \
2024-02-07 21:58:19 +00:00
$LOG_OPTS \
$EXTRA_DEPLOY_OPTS \
2024-02-09 00:22:47 +00:00
$UPDATE_OPTS \
2024-02-03 01:39:21 +00:00
--discover
rc=$?
if [ $rc -eq 0 ]; then
echo "############ DEPLOY SUCCESS #############"
else
echo "############ DEPLOY FAILURE STATUS $rc #############"
fi
2024-02-03 01:39:21 +00:00
# Cleanup any build leftovers
2024-02-27 23:59:16 +00:00
if [[ "${SYSTEM_PRUNE:-false}" == "true" ]]; then
docker system prune --all --force
fi
if [[ "${WEBAPP_IMAGE_PRUNE:-true}" == "true" ]]; then
2024-02-28 04:36:20 +00:00
APP_IMAGES="$(docker image ls --quiet --filter 'reference=laconic-webapp')"
DANGLING_IMAGES="$(docker image ls --quiet --filter 'dangling=true')"
if [[ -n "$APP_IMAGES" ]] || [[ -n "$DANGLING_IMAGES" ]]; then
echo "Pruning images: $APP_IMAGES $DANGLING_IMAGES"
docker image rm -f $APP_IMAGES $DANGLING_IMAGES
2024-02-28 00:35:30 +00:00
fi
2024-02-27 23:59:16 +00:00
fi
sleep ${CHECK_INTERVAL:-15}
2024-02-03 01:39:21 +00:00
done