Skip to content

Commit

Permalink
Merge branch 'improvement/rotate-bash-logs' into q/125.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bert-e committed Mar 20, 2023
2 parents 9e87757 + 368104e commit b629d64
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
- Bump prometheus-operator and prometheus-config-reloader containers version to
[0.63.0](https://github.com/prometheus-operator/prometheus-operator/releases/tag/v0.63.0)

- Add log rotation to shell scripts
(PR[#4030](https://github.com/scality/metalk8s/pull/4030))

### Bug fixes

- Fix a bug in Workload Plane Ingress Virtual IPs that make the DaemonSet Pod
Expand Down
10 changes: 6 additions & 4 deletions scripts/backup.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ done
TMPFILES=$(mktemp -d)
BACKUP_DIR=$(mktemp -d)

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "$LOGFILE")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- Backup started on $(date -u -R) ---
EOF
Expand All @@ -67,10 +73,6 @@ cleanup() {

trap cleanup EXIT

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

_save_cp() {
local -r src="$(readlink -f "$1")"
local -r dst="$2"
Expand Down
11 changes: 6 additions & 5 deletions scripts/bootstrap.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ done

TMPFILES=$(mktemp -d)

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "${LOGFILE}")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- Bootstrap started on $(date -u -R) ---
EOF
Expand All @@ -51,8 +57,6 @@ cleanup() {

trap cleanup EXIT

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

SALT_CALL=${SALT_CALL:-salt-call}

declare -a PACKAGES=(
Expand All @@ -63,9 +67,6 @@ declare -a PACKAGES=(
genisoimage
)

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

orchestrate_bootstrap() {
local -r control_plane_ip=$(
$SALT_CALL --local grains.get metalk8s:control_plane_ip --out txt \
Expand Down
26 changes: 26 additions & 0 deletions scripts/common.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ RPM=${RPM:-$(command -v rpm || true)}
YUM=${YUM:-$(command -v yum || true)}
SYSTEMCTL=${SYSTEMCTL:-$(command -v systemctl)}
PYTHON=${PYTHON:-$(command -v python3 || command -v python || true)}
LOGFILE_MAX_ROTATIONS=10


determine_os() {
# We rely on /etc/os-release to discover the OS because its present on all
Expand Down Expand Up @@ -371,3 +373,27 @@ check_minion_id() {
return 1
fi
}
rotate_logfile() {
# because set -u, we pass an empty string as default value
_rotate_logfile ""
}
_rotate_logfile() {
# Rotate logfile if it exists
if [ -n "$1" ]; then
if [ "$1" -gt "$LOGFILE_MAX_ROTATIONS" ]; then
rm -f "$LOGFILE.$1"
return
fi
if [ -f "$LOGFILE.$1" ]; then
_rotate_logfile $(($1+1))
mv "$LOGFILE.$1" "$LOGFILE.$(($1+1))"
fi
else
if [ -f "$LOGFILE" ]; then
_rotate_logfile 1
mv "$LOGFILE" "$LOGFILE.1"
fi
fi
}
8 changes: 5 additions & 3 deletions scripts/downgrade.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ done

TMPFILES=$(mktemp -d)

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "${LOGFILE}")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- MetalK8s downgrade started on $(date -u -R) ---
EOF
Expand All @@ -83,9 +88,6 @@ cleanup() {

trap cleanup EXIT

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

precheck_downgrade () {
SALT_MASTER_CALL=(crictl exec -i "$(get_salt_container)")
"${SALT_MASTER_CALL[@]}" salt-run saltutil.sync_all \
Expand Down
11 changes: 6 additions & 5 deletions scripts/iso-manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ done

TMPFILES=$(mktemp -d)

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "${LOGFILE}")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- MetalK8s ISO manager started on $(date -u -R) ---
EOF
Expand All @@ -70,11 +76,6 @@ cleanup() {

trap cleanup EXIT

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

# helper function to set the current saltenv
_set_env() {
if [ -z "$SALTENV" ]; then
Expand Down
11 changes: 6 additions & 5 deletions scripts/restore.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ done
TMPFILES=$(mktemp -d)
BACKUP_DIR=$(mktemp -d)

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "$LOGFILE")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- Restore started on $(date -u -R) ---
EOF
Expand All @@ -65,8 +71,6 @@ cleanup() {

trap cleanup EXIT

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

SALT_CALL=${SALT_CALL:-salt-call}

declare -a PACKAGES=(
Expand All @@ -77,9 +81,6 @@ declare -a PACKAGES=(
genisoimage
)

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

_save_cp() {
local -r src="$(readlink -f "$1")"
local -r dst="$2"
Expand Down
10 changes: 6 additions & 4 deletions scripts/solutions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,14 @@ done

TMPFILES=$(mktemp -d)

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "$LOGFILE")"

rotate_logfile

exec > >(tee -ia "$LOGFILE") 2>&1

cleanup() {
Expand All @@ -205,10 +211,6 @@ cleanup() {

trap cleanup EXIT

BASE_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

check_command_mandatory_options() {
local -a missing_options=()
local -r command=$1
Expand Down
8 changes: 5 additions & 3 deletions scripts/upgrade.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ done

TMPFILES=$(mktemp -d)

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

mkdir -p "$(dirname "${LOGFILE}")"

rotate_logfile

cat << EOF >> "${LOGFILE}"
--- MetalK8s Upgrade started on $(date -u -R) ---
EOF
Expand All @@ -71,9 +76,6 @@ cleanup() {

trap cleanup EXIT

# shellcheck disable=SC1090,SC1091
. "$BASE_DIR"/common.sh

upgrade_bootstrap () {
"$SALT_CALL" saltutil.sync_all saltenv="$SALTENV"

Expand Down

0 comments on commit b629d64

Please sign in to comment.