Commit 64960ae7 authored by Rémy Coutable's avatar Rémy Coutable

Conform scripts/review_apps/review-apps.sh to shellcheck

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 6dadd965
...@@ -38,13 +38,13 @@ function previousDeployFailed() { ...@@ -38,13 +38,13 @@ function previousDeployFailed() {
local deploy="${1}" local deploy="${1}"
echoinfo "Checking for previous deployment of ${deploy}" true echoinfo "Checking for previous deployment of ${deploy}" true
helm status ${deploy} >/dev/null 2>&1 helm status "${deploy}" >/dev/null 2>&1
local status=$? local status=$?
# if `status` is `0`, deployment exists, has a status # if `status` is `0`, deployment exists, has a status
if [ $status -eq 0 ]; then if [ $status -eq 0 ]; then
echoinfo "Previous deployment found, checking status..." echoinfo "Previous deployment found, checking status..."
deployment_status=$(helm status ${deploy} | grep ^STATUS | cut -d' ' -f2) deployment_status=$(helm status "${deploy}" | grep ^STATUS | cut -d' ' -f2)
echoinfo "Previous deployment state: ${deployment_status}" echoinfo "Previous deployment state: ${deployment_status}"
if [[ "$deployment_status" == "FAILED" || "$deployment_status" == "PENDING_UPGRADE" || "$deployment_status" == "PENDING_INSTALL" ]]; then if [[ "$deployment_status" == "FAILED" || "$deployment_status" == "PENDING_UPGRADE" || "$deployment_status" == "PENDING_INSTALL" ]]; then
status=0; status=0;
...@@ -64,13 +64,8 @@ function delete() { ...@@ -64,13 +64,8 @@ function delete() {
return return
fi fi
local track="${1-stable}"
local name="$CI_ENVIRONMENT_SLUG" local name="$CI_ENVIRONMENT_SLUG"
if [[ "$track" != "stable" ]]; then
name="$name-$track"
fi
echoinfo "Deleting release '$name'..." true echoinfo "Deleting release '$name'..." true
helm delete --purge "$name" || true helm delete --purge "$name" || true
...@@ -97,7 +92,8 @@ function get_pod() { ...@@ -97,7 +92,8 @@ function get_pod() {
echoinfo "Running '${get_pod_cmd}'" true echoinfo "Running '${get_pod_cmd}'" true
while true; do while true; do
local pod_name="$(eval $get_pod_cmd)" local pod_name
pod_name="$(eval "${get_pod_cmd}")"
[[ "${pod_name}" == "" ]] || break [[ "${pod_name}" == "" ]] || break
echoinfo "Waiting till '${app_name}' pod is ready"; echoinfo "Waiting till '${app_name}' pod is ready";
...@@ -113,7 +109,7 @@ function perform_review_app_deployment() { ...@@ -113,7 +109,7 @@ function perform_review_app_deployment() {
ensure_namespace ensure_namespace
install_tiller install_tiller
install_external_dns install_external_dns
time deploy time deploy || true
wait_for_review_app_to_be_accessible wait_for_review_app_to_be_accessible
add_license add_license
} }
...@@ -157,7 +153,8 @@ function install_tiller() { ...@@ -157,7 +153,8 @@ function install_tiller() {
function install_external_dns() { function install_external_dns() {
local release_name="dns-gitlab-review-app" local release_name="dns-gitlab-review-app"
local domain=$(echo "${REVIEW_APPS_DOMAIN}" | awk -F. '{printf "%s.%s", $(NF-1), $NF}') local domain
domain=$(echo "${REVIEW_APPS_DOMAIN}" | awk -F. '{printf "%s.%s", $(NF-1), $NF}')
echoinfo "Installing external DNS for domain ${domain}..." true echoinfo "Installing external DNS for domain ${domain}..." true
if ! deployExists "${KUBE_NAMESPACE}" "${release_name}" || previousDeployFailed "${release_name}" ; then if ! deployExists "${KUBE_NAMESPACE}" "${release_name}" || previousDeployFailed "${release_name}" ; then
...@@ -183,17 +180,17 @@ function create_secret() { ...@@ -183,17 +180,17 @@ function create_secret() {
echoinfo "Creating the ${CI_ENVIRONMENT_SLUG}-gitlab-initial-root-password secret in the ${KUBE_NAMESPACE} namespace..." true echoinfo "Creating the ${CI_ENVIRONMENT_SLUG}-gitlab-initial-root-password secret in the ${KUBE_NAMESPACE} namespace..." true
kubectl create secret generic -n "$KUBE_NAMESPACE" \ kubectl create secret generic -n "$KUBE_NAMESPACE" \
$CI_ENVIRONMENT_SLUG-gitlab-initial-root-password \ "${CI_ENVIRONMENT_SLUG}-gitlab-initial-root-password" \
--from-literal=password=$REVIEW_APPS_ROOT_PASSWORD \ --from-literal="password=${REVIEW_APPS_ROOT_PASSWORD}" \
--dry-run -o json | kubectl apply -f - --dry-run -o json | kubectl apply -f -
} }
function download_gitlab_chart() { function download_gitlab_chart() {
echoinfo "Downloading the GitLab chart..." true echoinfo "Downloading the GitLab chart..." true
curl -o gitlab.tar.bz2 https://gitlab.com/charts/gitlab/-/archive/$GITLAB_HELM_CHART_REF/gitlab-$GITLAB_HELM_CHART_REF.tar.bz2 curl -o gitlab.tar.bz2 "https://gitlab.com/charts/gitlab/-/archive/${GITLAB_HELM_CHART_REF}/gitlab-${GITLAB_HELM_CHART_REF}.tar.bz2"
tar -xjf gitlab.tar.bz2 tar -xjf gitlab.tar.bz2
cd gitlab-$GITLAB_HELM_CHART_REF cd "gitlab-${GITLAB_HELM_CHART_REF}"
echoinfo "Adding the gitlab repo to Helm..." echoinfo "Adding the gitlab repo to Helm..."
helm repo add gitlab https://charts.gitlab.io helm repo add gitlab https://charts.gitlab.io
...@@ -203,19 +200,9 @@ function download_gitlab_chart() { ...@@ -203,19 +200,9 @@ function download_gitlab_chart() {
} }
function deploy() { function deploy() {
set +e
local track="${1-stable}"
local name="$CI_ENVIRONMENT_SLUG" local name="$CI_ENVIRONMENT_SLUG"
if [[ "$track" != "stable" ]]; then
name="$name-$track"
fi
echoinfo "Deploying ${name}..." true echoinfo "Deploying ${name}..." true
replicas="1"
service_enabled="false"
postgres_enabled="$POSTGRES_ENABLED"
IMAGE_REPOSITORY="registry.gitlab.com/gitlab-org/build/cng-mirror" IMAGE_REPOSITORY="registry.gitlab.com/gitlab-org/build/cng-mirror"
IMAGE_VERSION="${CI_PROJECT_NAME#gitlab-}" IMAGE_VERSION="${CI_PROJECT_NAME#gitlab-}"
gitlab_migrations_image_repository="${IMAGE_REPOSITORY}/gitlab-rails-${IMAGE_VERSION}" gitlab_migrations_image_repository="${IMAGE_REPOSITORY}/gitlab-rails-${IMAGE_VERSION}"
...@@ -226,24 +213,6 @@ function deploy() { ...@@ -226,24 +213,6 @@ function deploy() {
gitlab_shell_image_repository="${IMAGE_REPOSITORY}/gitlab-shell" gitlab_shell_image_repository="${IMAGE_REPOSITORY}/gitlab-shell"
gitlab_workhorse_image_repository="${IMAGE_REPOSITORY}/gitlab-workhorse-${IMAGE_VERSION}" gitlab_workhorse_image_repository="${IMAGE_REPOSITORY}/gitlab-workhorse-${IMAGE_VERSION}"
# canary uses stable db
[[ "$track" == "canary" ]] && postgres_enabled="false"
env_track=$( echo $track | tr -s '[:lower:]' '[:upper:]' )
env_slug=$( echo ${CI_ENVIRONMENT_SLUG//-/_} | tr -s '[:lower:]' '[:upper:]' )
if [[ "$track" == "stable" ]]; then
# for stable track get number of replicas from `PRODUCTION_REPLICAS`
eval new_replicas=\$${env_slug}_REPLICAS
service_enabled="true"
else
# for all tracks get number of replicas from `CANARY_PRODUCTION_REPLICAS`
eval new_replicas=\$${env_track}_${env_slug}_REPLICAS
fi
if [[ -n "$new_replicas" ]]; then
replicas="$new_replicas"
fi
# Cleanup and previous installs, as FAILED and PENDING_UPGRADE will cause errors with `upgrade` # Cleanup and previous installs, as FAILED and PENDING_UPGRADE will cause errors with `upgrade`
if [ "$CI_ENVIRONMENT_SLUG" != "production" ] && previousDeployFailed "$CI_ENVIRONMENT_SLUG" ; then if [ "$CI_ENVIRONMENT_SLUG" != "production" ] && previousDeployFailed "$CI_ENVIRONMENT_SLUG" ; then
echo "Deployment in bad state, cleaning up $CI_ENVIRONMENT_SLUG" echo "Deployment in bad state, cleaning up $CI_ENVIRONMENT_SLUG"
...@@ -284,9 +253,9 @@ HELM_CMD=$(cat << EOF ...@@ -284,9 +253,9 @@ HELM_CMD=$(cat << EOF
--set gitlab.unicorn.image.tag="$CI_COMMIT_REF_SLUG" \ --set gitlab.unicorn.image.tag="$CI_COMMIT_REF_SLUG" \
--set gitlab.task-runner.image.repository="$gitlab_task_runner_image_repository" \ --set gitlab.task-runner.image.repository="$gitlab_task_runner_image_repository" \
--set gitlab.task-runner.image.tag="$CI_COMMIT_REF_SLUG" \ --set gitlab.task-runner.image.tag="$CI_COMMIT_REF_SLUG" \
--set gitlab.gitaly.image.repository="registry.gitlab.com/gitlab-org/build/cng-mirror/gitaly" \ --set gitlab.gitaly.image.repository="$gitlab_gitaly_image_repository" \
--set gitlab.gitaly.image.tag="v$GITALY_VERSION" \ --set gitlab.gitaly.image.tag="v$GITALY_VERSION" \
--set gitlab.gitlab-shell.image.repository="registry.gitlab.com/gitlab-org/build/cng-mirror/gitlab-shell" \ --set gitlab.gitlab-shell.image.repository="$gitlab_shell_image_repository" \
--set gitlab.gitlab-shell.image.tag="v$GITLAB_SHELL_VERSION" \ --set gitlab.gitlab-shell.image.tag="v$GITLAB_SHELL_VERSION" \
--set gitlab.unicorn.workhorse.image="$gitlab_workhorse_image_repository" \ --set gitlab.unicorn.workhorse.image="$gitlab_workhorse_image_repository" \
--set gitlab.unicorn.workhorse.tag="$CI_COMMIT_REF_SLUG" \ --set gitlab.unicorn.workhorse.tag="$CI_COMMIT_REF_SLUG" \
...@@ -302,17 +271,19 @@ EOF ...@@ -302,17 +271,19 @@ EOF
echoinfo "${HELM_CMD}" echoinfo "${HELM_CMD}"
eval $HELM_CMD eval $HELM_CMD
set -e
} }
function wait_for_review_app_to_be_accessible() { function wait_for_review_app_to_be_accessible() {
# In case the Review App isn't completely available yet. Keep trying for 5 minutes. # In case the Review App isn't completely available yet. Keep trying for 5 minutes.
local interval=5 local interval=5
local elapsed_seconds=0 local elapsed_seconds=0
local max_seconds=5*60 local max_seconds=$((5 * 60))
while true; do while true; do
local review_app_http_code=$(curl --silent --output /dev/null --max-time 5 --write-out "%{http_code}" "${CI_ENVIRONMENT_URL}/users/sign_in") local review_app_http_code
([[ "${review_app_http_code}" != "200" ]] && [[ "${elapsed_seconds}" -lt "${max_seconds}" ]]) || break review_app_http_code=$(curl --silent --output /dev/null --max-time 5 --write-out "%{http_code}" "${CI_ENVIRONMENT_URL}/users/sign_in")
if [[ "${review_app_http_code}" -eq "200" ]] || [[ "${elapsed_seconds}" -gt "${max_seconds}" ]]; then
break
fi
printf "." printf "."
let "elapsed_seconds+=interval" let "elapsed_seconds+=interval"
...@@ -336,10 +307,10 @@ function add_license() { ...@@ -336,10 +307,10 @@ function add_license() {
echoinfo "Installing license..." true echoinfo "Installing license..." true
echo "${REVIEW_APPS_EE_LICENSE}" > /tmp/license.gitlab echo "${REVIEW_APPS_EE_LICENSE}" > /tmp/license.gitlab
kubectl -n "$KUBE_NAMESPACE" cp /tmp/license.gitlab ${task_runner_pod}:/tmp/license.gitlab kubectl -n "$KUBE_NAMESPACE" cp /tmp/license.gitlab "${task_runner_pod}":/tmp/license.gitlab
rm /tmp/license.gitlab rm /tmp/license.gitlab
kubectl -n "$KUBE_NAMESPACE" exec -it ${task_runner_pod} -- /srv/gitlab/bin/rails runner -e production \ kubectl -n "$KUBE_NAMESPACE" exec -it "${task_runner_pod}" -- /srv/gitlab/bin/rails runner -e production \
' '
content = File.read("/tmp/license.gitlab").strip; content = File.read("/tmp/license.gitlab").strip;
FileUtils.rm_f("/tmp/license.gitlab"); FileUtils.rm_f("/tmp/license.gitlab");
...@@ -369,7 +340,8 @@ function get_job_id() { ...@@ -369,7 +340,8 @@ function get_job_id() {
local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs?per_page=100&page=${page}${query_string}" local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs?per_page=100&page=${page}${query_string}"
echoinfo "GET ${url}" echoinfo "GET ${url}"
local job_id=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq "map(select(.name == \"${job_name}\")) | map(.id) | last") local job_id
job_id=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq "map(select(.name == \"${job_name}\")) | map(.id) | last")
[[ "${job_id}" == "null" && "${page}" -lt "$max_page" ]] || break [[ "${job_id}" == "null" && "${page}" -lt "$max_page" ]] || break
let "page++" let "page++"
...@@ -385,20 +357,23 @@ function get_job_id() { ...@@ -385,20 +357,23 @@ function get_job_id() {
function play_job() { function play_job() {
local job_name="${1}" local job_name="${1}"
local job_id=$(get_job_id "${job_name}" "scope=manual"); local job_id
job_id=$(get_job_id "${job_name}" "scope=manual");
if [ -z "${job_id}" ]; then return; fi if [ -z "${job_id}" ]; then return; fi
local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/jobs/${job_id}/play" local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/jobs/${job_id}/play"
echoinfo "POST ${url}" echoinfo "POST ${url}"
local job_url=$(curl --silent --show-error --request POST --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".web_url") local job_url
job_url=$(curl --silent --show-error --request POST --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".web_url")
echoinfo "Manual job '${job_name}' started at: ${job_url}" echoinfo "Manual job '${job_name}' started at: ${job_url}"
} }
function wait_for_job_to_be_done() { function wait_for_job_to_be_done() {
local job_name="${1}" local job_name="${1}"
local query_string="${2}" local query_string="${2}"
local job_id=$(get_job_id "${job_name}" "${query_string}"); local job_id
job_id=$(get_job_id "${job_name}" "${query_string}")
if [ -z "${job_id}" ]; then return; fi if [ -z "${job_id}" ]; then return; fi
echoinfo "Waiting for the '${job_name}' job to finish..." echoinfo "Waiting for the '${job_name}' job to finish..."
...@@ -410,7 +385,8 @@ function wait_for_job_to_be_done() { ...@@ -410,7 +385,8 @@ function wait_for_job_to_be_done() {
local interval=30 local interval=30
local elapsed_seconds=0 local elapsed_seconds=0
while true; do while true; do
local job_status=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".status" | sed -e s/\"//g) local job_status
job_status=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".status" | sed -e s/\"//g)
[[ "${job_status}" == "pending" || "${job_status}" == "running" ]] || break [[ "${job_status}" == "pending" || "${job_status}" == "running" ]] || break
printf "." printf "."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment