openreplay/scripts/helmcharts/openreplay-cli
rjshrjndrn 750d6378e4 chore(cli): Install partiticular version of OR
Signed-off-by: rjshrjndrn <rjshrjndrn@gmail.com>
2023-01-27 18:20:20 +01:00

307 lines
8.6 KiB
Bash
Executable file

#!/bin/bash
# vim: set ft=sh:
OR_DIR="/var/lib/openreplay"
APP_NS="${APP_NS:-app}"
DB_NS="${DB_NS:-db}"
OR_REPO="https://github.com/openreplay/openreplay"
tmp_dir=$(mktemp -d)
# For example HELM_OPTIONS="--set dbMigrationUpstreamBranch=dev"
#HELM_OPTIONS=""
# If you want to install the dev version. It can be any branch or tag.
#OR_VERSION="dev"
[[ -d $OR_DIR ]] || {
sudo mkdir $OR_DIR
}
export PATH=$PATH:/var/lib/openreplay
tools=(
zyedidia/eget
stern/stern
derailed/k9s
hidetatz/kubecolor
)
# Ref: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BWHITE='\033[1;37m'
NC='\033[0m' # No Color
# Checking whether the app exists or we do have to upgade.
function exists() {
which "${1}" &> /dev/null
return $?
}
function err_cd() {
if ! cd "$1" &> /dev/null ; then
log err not able to cd to "$1"
exit 100
fi
}
function log () {
case "$1" in
info)
shift
echo -e "${GREEN}[INFO]" "$@" "${NC}"
return
;;
debug)
shift
echo -e "${YELLOW}[DEBUG]" "$@" "${NC}"
return
;;
title)
shift
echo -e "\n${BWHITE}-" "$@" "${NC}"
return
;;
err)
shift
echo -e "${RED}[ERROR]" "$@" "${NC}"
exit 100
;;
*)
echo "Not supported log format"
;;
esac
echo "[Error]" "$@"
exit 100
}
function install_packages() {
[[ -e "$OR_DIR/eget" ]] || {
cd "$tmp_dir" || log err "Not able to cd to tmp dir $tmp_dir"
curl --version &> /dev/null || log err "curl not found. Please install"
curl https://zyedidia.github.io/eget.sh | sh
sudo mv eget $OR_DIR
err_cd -
}
for package in "${tools[@]}"; do
log info Installing "$(awk -F/ '{print $2}' <<< $package)"
sudo /var/lib/openreplay/eget -q --upgrade-only --to "${OR_DIR}" "$package"
done
log info Installing yq
sudo /var/lib/openreplay/eget -q --upgrade-only --to "$OR_DIR" mikefarah/yq --asset=^tar.gz
log info Installing helm
sudo /var/lib/openreplay/eget -q --upgrade-only --to "$OR_DIR" https://get.helm.sh/helm-v3.10.2-linux-amd64.tar.gz -f helm
log info Installing kubectl
sudo /var/lib/openreplay/eget -q --upgrade-only --to "$OR_DIR" https://dl.k8s.io/release/v1.20.0/bin/linux/amd64/kubectl
log info Installing Busybox
sudo /var/lib/openreplay/eget -q --upgrade-only --to "$OR_DIR" https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox
date | sudo tee $OR_DIR/packages.lock &> /dev/null
}
function help() {
echo -e ${BWHITE}
cat <<"EOF"
___ ____ _
/ _ \ _ __ ___ _ __ | _ \ ___ _ __ | | __ _ _ _
| | | | '_ \ / _ \ '_ \| |_) / _ \ '_ \| |/ _` | | | |
| |_| | |_) | __/ | | | _ < __/ |_) | | (_| | |_| |
\___/| .__/ \___|_| |_|_| \_\___| .__/|_|\__,_|\__, |
|_| |_| |___/
EOF
echo -e ${NC}
log info '
Usage: openreplay [ -h | --help ]
[ -s | --status ]
[ -u | --upgrade ]
[ -U | --deprecated-upgrade /path/to/old_vars.yaml]
[ -r | --restart ]
[ -R | --Reload ]
[ -p | --install-packages ]
[ -l | --logs SERVICE ]
Services: alerts assets assist chalice
db ender frontend heuristics
http integrations nginx-controller
peers sink sourcemapreader storage
'
return
}
function status() {
log info OpenReplay Version
# awk '(NR<2)' < "$OR_DIR/vars.yaml"
awk '/fromVersion/{print $2}' < "${OR_DIR}/vars.yaml"
log info Disk
df -h /var
log info Memory
free -mh
log info CPU
uname -a
# Print only the fist line.
awk '(NR<2)' < /etc/os-release
echo "CPU Count: $(nproc)"
log info Kubernetes
kubecolor version --short
log info Openreplay Component
kubecolor get po -n "${APP_NS}"
kubecolor get po -n "${DB_NS}"
return
}
# Function to upgrade helm openreplay app.
function or_helm_upgrade() {
set -o pipefail
log_file="${tmp_dir}/helm.log"
chart_names=(
toolings
openreplay
)
for chart in "${chart_names[@]}"; do
[[ -z $OR_VERSION ]] || HELM_OPTIONS="--set dbMigrationUpstreamBranch=${OR_VERSION}"
if ! helm upgrade --install "$chart" ./"$chart" -n "$APP_NS" --wait -f ./vars.yaml --atomic --debug $HELM_OPTIONS 2>&1 | tee -a "${log_file}"; then
log err "
Installation failed, run ${BWHITE}cat ${log_file}${RED} for more info
If logs aren't verbose, run ${BWHITE}openreplay --status${RED}
If pods are in failed state, run ${BWHITE}openreplay --logs <pod-name>${RED}
"
fi
done
set +o pipefail
return
}
function upgrade_old() {
old_vars_path="$1"
or_version=$(busybox awk '/fromVersion/{print $2}' < "${old_vars_path}")
sudo cp "${old_vars_path}" ${OR_DIR}/vars.yaml.backup."${or_version//\"}"_"$(date +%Y%m%d-%H%M%S)" || log err "Not able to copy old vars.yaml"
sudo cp "${old_vars_path}" ${OR_DIR}/vars.yaml || log err "Not able to copy old vars.yaml"
upgrade
}
function upgrade() {
# TODO:
# 1. store vars.yaml in central place.
# 3. In upgrade you'll have to clone the repo
# 3. How to update package. Because openreplay -u will be done from old update script
# 4. Update from Version
exists git || log err "Git not found. Please install"
log info "Working directory $tmp_dir"
err_cd "$tmp_dir"
or_version=$(busybox awk '/fromVersion/{print $2}' < "${OR_DIR}/vars.yaml")
# Creating backup dir of current installation
[[ -d "$OR_DIR/openreplay" ]] && sudo cp -rfb "$OR_DIR/openreplay" "$OR_DIR/openreplay_${or_version//\"}" && sudo rm -rf ${OR_DIR}/openreplay
git_options="-b ${OR_VERSION:-main}"
eval git clone "${OR_REPO}" --depth 1 $git_options
err_cd openreplay/scripts/helmcharts
install_packages
[[ -d /openreplay ]] && sudo chown -R 1001:1001 /openreplay
# Merge prefrerences
cp $OR_DIR/vars.yaml old_vars.yaml
or_new_version=$(awk '/fromVersion/{print $2}' < "vars.yaml")
yq '(load("old_vars.yaml") | .. | select(tag != "!!map" and tag != "!!seq")) as $i ireduce(.; setpath($i | path; $i))' vars.yaml > new_vars.yaml
mv new_vars.yaml vars.yaml
or_helm_upgrade
# Update the version
busybox sed -i "s/fromVersion.*/fromVersion: ${or_new_version}/" vars.yaml
sudo mv ./openreplay-cli /bin/
sudo mv ./vars.yaml "$OR_DIR"
sudo cp -rf ../../../openreplay $OR_DIR/
log info "Configuration file is saved in /var/lib/openreplay/vars.yaml"
log info "Run ${BWHITE}openreplay -h${GREEN} to see the cli information to manage OpenReplay."
err_cd -
return
}
function reload() {
err_cd $OR_DIR/openreplay/scripts/helmcharts
sudo cp -f $OR_DIR/vars.yaml .
or_helm_upgrade
return
}
function clean_tmp_dir() {
[[ -z $SKIP_DELETE_TMP_DIR ]] && rm -rf "${tmp_dir}"
}
[[ -f $OR_DIR/packages.lock ]] || {
log title Installing packages "${NC}"
install_packages
}
PARSED_ARGUMENTS=$(busybox getopt -a -n openreplay -o Rrvpiuhsl:U: --long reload,restart,verbose,install-packages,install,upgrade,help,status,logs,deprecated-upgrade: -- "$@")
VALID_ARGUMENTS=$?
if [[ "$VALID_ARGUMENTS" != "0" ]]; then
help
exit 100
fi
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-v | --verbose) VERBOSE=1; echo $VERBOSE; clean_tmp_dir ; shift ;;
-h | --help)
help
clean_tmp_dir
exit 0
;;
-u | --upgrade)
log title "Upgrading OpenReplay"
upgrade
clean_tmp_dir
exit 0
;;
-U | --deprecated-upgrade)
log title "[Deprected] Upgrading OpenReplay"
upgrade_old "$2"
clean_tmp_dir
exit 0
;;
-r | --restart)
log title "Restarting OpenReplay Components"
kubectl rollout restart deployment -n "${APP_NS}"
kubectl rollout status deployment -n "${APP_NS}"
clean_tmp_dir
exit 0
;;
-R | --reload)
log title "Reloading OpenReplay Components"
reload
clean_tmp_dir
exit 0
;;
-s | --status)
log title "Checking OpenReplay Components Status"
status
clean_tmp_dir
exit 0
;;
-l | --logs)
# Skipping double quotes because we want globbing. For example
# ./openreplay -l "chalice --tail 10"
stern -A --container-state=running,terminated $2
clean_tmp_dir
exit 0
;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift; break ;;
# If invalid options were passed, then getopt should have reported an error,
# which we checked as VALID_ARGUMENTS when getopt was called...
*)
echo "Unexpected option: $1 - this should not happen."
help
clean_tmp_dir
;;
esac
done
[ $# -eq 0 ] && help
clean_tmp_dir