Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade script grep sed bash fixes #647

Merged
merged 8 commits into from
May 20, 2020
48 changes: 43 additions & 5 deletions deploy/helm/sumologic/upgrade-1.0.0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ readonly PREVIOUS_VERSION=0.17

readonly TEMP_FILE=upgrade-1.0.0-temp-file

readonly MIN_BASH_VERSION=4.4
readonly MIN_YQ_VERSION=3.2.1

readonly KEY_MAPPINGS="
eventsDeployment.nodeSelector:fluentd.events.statefulset.nodeSelector
eventsDeployment.resources.limits.cpu:fluentd.events.statefulset.resources.limits.cpu
Expand Down Expand Up @@ -136,7 +139,7 @@ This script will automatically take the configurations of your existing values.y
and return one that is compatible with v1.0.0.

Requirements:
yq (3.2.1) https://github.com/mikefarah/yq/releases/tag/3.2.1
yq (>=3.2.1) https://github.com/mikefarah/yq/releases/tag/3.2.1
grep
sed

Expand Down Expand Up @@ -168,8 +171,37 @@ function check_required_command() {
command -v "${command_to_check}" >/dev/null 2>&1 || { error "Required command is missing: ${command_to_check}"; fatal "Please consult --help and install missing commands before continue. Aborting."; }
}

function compare_versions() {
local no_lower_than="${1}"
local app_version="${2}"

if [[ "$(printf '%s\n' "${app_version}" "$no_lower_than" | sort -V | head -n 1)" == "${no_lower_than}" ]]; then
echo "pass"
else
echo "fail"
fi
}

function check_app_version() {
local app_name="${1}"
local no_lower_than="${2}"
local app_version="${3}"

if [[ -z ${app_version} ]] || [[ $(compare_versions "${no_lower_than}" "${app_version}") == "fail" ]]; then
error "${app_name} version is invalid - it should be no lower than ${no_lower_than}"
fatal "Please update your ${app_name} version and retry."
fi
}

function check_yq_version() {
yq --version | grep 3.2.1 >/dev/null 2>&1 || { error "yq version is invalid. It should be exactly 3.2.1"; fatal "Please install it from: https://github.com/mikefarah/yq/releases/tag/3.2.1"; }
local yq_version
yq_version=$(yq --version | grep -oE '[^[:space:]]+$')

check_app_version "grep" "${MIN_YQ_VERSION}" "${yq_version}"
}

function check_bash_version() {
check_app_version "bash" "${MIN_BASH_VERSION}" "${BASH_VERSION}"
}

function create_temp_file() {
Expand Down Expand Up @@ -228,7 +260,7 @@ function migrate_customer_keys() {
info "Casting ${key} to str"
# As yq doesn't cast `on` and `off` from bool to cast, we use sed based casts
yq w -i ${TEMP_FILE} -- "${key}" "$(yq r "${OLD_VALUES_YAML}" "${key}")__YQ_REPLACEMENT_CAST"
sed -i '' 's/\(^.*: \)\(.*\)__YQ_REPLACEMENT_CAST/\1"\2"/g' ${TEMP_FILE}
sed -i.bak 's/\(^.*: \)\(.*\)__YQ_REPLACEMENT_CAST/\1"\2"/g' ${TEMP_FILE}
fi
done
echo
Expand Down Expand Up @@ -431,24 +463,29 @@ function migrate_fluentbit_db_path() {
info 'Replacing tail-db/tail-containers-state.db to tail-db/tail-containers-state-sumo.db'
warning 'Please ensure that new fluent-bit configuration is correct'

sed -i '' 's?tail-db/tail-containers-state.db?tail-db/tail-containers-state-sumo.db?g' ${TEMP_FILE}
sed -i.bak 's?tail-db/tail-containers-state.db?tail-db/tail-containers-state-sumo.db?g' ${TEMP_FILE}
}

function fix_yq() {
# account for yq bug that stringifies empty maps
sed -i '' "s/'{}'/{}/g" ${TEMP_FILE}
sed -i.bak "s/'{}'/{}/g" ${TEMP_FILE}
}

function rename_temp_file() {
mv ${TEMP_FILE} new_values.yaml
}

function cleanup_bak_file() {
rm ${TEMP_FILE}.bak
}

function echo_footer() {
DONE="\nThank you for upgrading to v1.0.0 of the Sumo Logic Kubernetes Collection Helm chart.\nA new yaml file has been generated for you. Please check the current directory for new_values.yaml."
echo -e "$DONE"
}

check_if_print_help_and_exit "${OLD_VALUES_YAML}"
check_bash_version
check_required_command yq
check_yq_version
check_required_command grep
Expand All @@ -473,6 +510,7 @@ migrate_fluentbit_db_path

fix_yq
rename_temp_file
cleanup_bak_file

echo_footer

Expand Down