kitsune/k8s/commander.sh

93 строки
2.8 KiB
Bash
Исходник Обычный вид История

#!/bin/bash
set -exo pipefail
2018-03-15 14:51:59 +03:00
GREEN='\033[1;32m'
NC='\033[0m' # No Color
DOCKER_HUB="https://hub.docker.com/r/mozilla/kitsune/tags/"
PYENV_FILE='.python-version'
2018-03-15 14:51:59 +03:00
function whatsdeployed {
2021-06-09 13:18:48 +03:00
xdg-open "https://whatsdeployed.io/s-iiX"
2018-03-15 14:51:59 +03:00
}
function deploy {
2021-06-09 13:18:48 +03:00
REGION=${2}
REGION_ENV=${3}
COMMIT_HASH=${4}
DEPLOY_SECRETS=${5:-NO}
K8S_NAMESPACE="sumo-${REGION_ENV}"
if [[ "${DEPLOY_SECRETS}" == "secrets" ]]; then
echo "Applying secrets"
${KUBECTL_BIN} -n "${K8S_NAMESPACE}" apply -f "regions/${REGION}/${REGION_ENV}-secrets.yaml"
else
echo "Secrets will *NOT* be applied"
fi
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" deployments.create-celery --apply --tag "prod-${COMMIT_HASH}"
2021-06-09 13:18:48 +03:00
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" rollouts.status-celery
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" deployments.create-cron --apply --tag "prod-${COMMIT_HASH}"
2021-06-09 13:18:48 +03:00
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" rollouts.status-cron
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" deployments.create-web --apply --tag "prod-${COMMIT_HASH}"
2021-06-09 13:18:48 +03:00
invoke -f "regions/${REGION}/${REGION_ENV}.yaml" rollouts.status-web
post-deploy "$@"
echo ":tada: Successfully deployed <${DOCKER_HUB}|prod-${COMMIT_HASH}> to <https://${REGION_ENV}-${REGION}.sumo.mozit.cloud/|SUMO-${REGION_ENV} in ${REGION}>"
2021-06-09 13:18:48 +03:00
printf "${GREEN}OK${NC}\n"
}
function post-deploy {
2021-06-09 13:18:48 +03:00
REGION=${2}
REGION_ENV=${3}
K8S_NAMESPACE="sumo-${REGION_ENV}"
# run post-deployment tasks
echo "Running post-deployment tasks"
# Get the name of a running web pod on which we can run the post-deploy script
SUMO_POD=$(${KUBECTL_BIN} -n "${K8S_NAMESPACE}" get pods | egrep 'sumo-.*-web' | grep Running | head -1 | awk '{ print $1 }')
${KUBECTL_BIN} -n "${K8S_NAMESPACE}" exec "${SUMO_POD}" bin/run-post-deploy.sh
}
function initialize {
2021-06-09 13:18:48 +03:00
REGION=${2}
if [ -f "./regions/${REGION}/kubectl" ]; then
KUBECTL_BIN="./regions/${REGION}/kubectl"
else
KUBECTL_BIN=$(which kubectl)
fi
export KUBECTL_BIN
if [ -f "./regions/${REGION}/kubeconfig" ]; then
export KUBECONFIG="./regions/${REGION}/kubeconfig"
fi
${KUBECTL_BIN} version >/dev/null
if [ $? == 1 ]; then
echo "Can't connect to the Kubernetes server. Exiting here"
exit 1
fi
compare-client-server-versions
}
function compare-client-server-versions {
2021-06-09 13:18:48 +03:00
CLIENT_VERSION=$(${KUBECTL_BIN} version --short | grep Client | awk -F. '{print $2}')
SERVER_VERSION=$(${KUBECTL_BIN} version --short | grep Server | awk -F. '{print $2}')
# Calculate difference between versions
DIFFERENCE=$((CLIENT_VERSION - SERVER_VERSION))
if ((DIFFERENCE > 1)) || ((DIFFERENCE < -1)); then
echo "Version mismatch; Client and server versions must be equal or have 1 minor version step. Exiting here"
echo "Client version: $CLIENT_VERSION, Server version: $SERVER_VERSION "
exit 1
fi
2018-03-15 14:51:59 +03:00
}
if ![ -f "$PYENV_FILE" ]; then
source venv/bin/activate
fi
initialize "$@"
2018-03-15 14:51:59 +03:00
$1 $@