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
49 changes: 44 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,9 +139,10 @@ 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 (>= ${MIN_YQ_VERSION}) https://github.com/mikefarah/yq/releases/tag/3.2.1
grep
sed
bash (>= ${MIN_BASH_VERSION})

Usage:
# for default helm release name 'collection' and namespace 'sumologic'
Expand Down Expand Up @@ -168,8 +172,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: '${app_version}' is invalid - it should be no lower than ${no_lower_than}"
fatal "Please update your ${app_name} 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 +261,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 +464,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 +511,7 @@ migrate_fluentbit_db_path

fix_yq
rename_temp_file
cleanup_bak_file

echo_footer

Expand Down
8 changes: 6 additions & 2 deletions vagrant/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ sudo -H -u vagrant -i helm init --wait

usermod -a -G microk8s vagrant

# Install yq
sudo curl https://github.com/mikefarah/yq/releases/download/3.2.1/yq_linux_amd64 -L -o /usr/local/bin/yq && sudo chmod +x /usr/local/bin/yq
# install yq with access to file structure
curl https://github.com/mikefarah/yq/releases/download/3.2.1/yq_linux_amd64 -L -o /usr/local/bin/yq-3.2.1
chmod +x /usr/local/bin/yq-3.2.1
curl https://github.com/mikefarah/yq/releases/download/3.3.0/yq_linux_amd64 -L -o /usr/local/bin/yq-3.3.0
chmod +x /usr/local/bin/yq-3.3.0
ln -s /usr/local/bin/yq-3.3.0 /usr/local/bin/yq

set +x
echo Dashboard local in-vagrant IP:
Expand Down