From 54897057d92857f2f42ec28d66440f6892680933 Mon Sep 17 00:00:00 2001 From: Torben Brenner Date: Tue, 17 May 2022 15:55:25 +0200 Subject: [PATCH] refactor: Ensured Usage of Log Function This should ensure a more consistent script output --- lib/add_bc_user.sh | 6 ++---- lib/functions.sh | 12 ++++++------ lib/remove-bridgehead-units.sh | 8 +++++--- lib/setup-bridgehead-units.sh | 14 ++++++-------- lib/update-bridgehead.sh | 1 - 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/add_bc_user.sh b/lib/add_bc_user.sh index 1f758aa..8185658 100755 --- a/lib/add_bc_user.sh +++ b/lib/add_bc_user.sh @@ -1,12 +1,10 @@ #!/bin/bash -e source lib/functions.sh -log "This script add's a user with password to the bridghead" +log "INFO" "This script add's a user with password to the bridghead" read -p 'Username: ' bc_user read -sp 'Password: ' bc_password -echo - -log "Please export the line in the your environment. Please replace the dollar signs with with \\\$" +log "INFO" "\nPlease export the line in the your environment. Please replace the dollar signs with with \\\$" docker run --rm -it httpd:latest htpasswd -nb $bc_user $bc_password diff --git a/lib/functions.sh b/lib/functions.sh index e6c5dec..55781d8 100755 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -2,13 +2,13 @@ exitIfNotRoot() { if [ "$EUID" -ne 0 ]; then - echo "Please run as root" + log "ERROR" "Please run as root" exit 1 fi } log() { - echo "$(date +'%Y-%m-%d %T')" "$1:" "$2" + echo -e "$(date +'%Y-%m-%d %T')" "$1:" "$2" } printUsage() { @@ -18,7 +18,7 @@ printUsage() { checkRequirements() { if ! lib/prerequisites.sh; then - log ERROR "Validating Prerequisites failed, please fix the error(s) above this line." + log "ERROR" "Validating Prerequisites failed, please fix the error(s) above this line." exit 1 else return 0 @@ -38,12 +38,12 @@ fetchVarsFromVault() { return 0 fi - log INFO "Fetching secrets from vault ..." + log "INFO" "Fetching secrets from vault ..." [ -e /etc/bridgehead/vault.conf ] && source /etc/bridgehead/vault.conf if [ -z "$BW_MASTERPASS" ] || [ -z "$BW_CLIENTID" ] || [ -z "$BW_CLIENTSECRET" ]; then - log ERROR "Please supply correct credentials in /etc/bridgehead/vault.conf." + log "ERROR" "Please supply correct credentials in /etc/bridgehead/vault.conf." return 1 fi @@ -67,4 +67,4 @@ fetchVarsFromVault() { ##Setting Network properties export HOSTIP=$(MSYS_NO_PATHCONV=1 docker run --rm --add-host=host.docker.internal:host-gateway ubuntu cat /etc/hosts | grep 'host.docker.internal' | awk '{print $1}'); -export HOST=$(hostname) \ No newline at end of file +export HOST=$(hostname) diff --git a/lib/remove-bridgehead-units.sh b/lib/remove-bridgehead-units.sh index 7de201f..b81b042 100755 --- a/lib/remove-bridgehead-units.sh +++ b/lib/remove-bridgehead-units.sh @@ -3,12 +3,12 @@ source lib/functions.sh if [ $# -eq 0 ]; then - log "Please provide a Project as argument" + log "ERROR" "Please provide a Project as argument" exit 1 fi if [ $1 != "ccp" ] && [ $1 != "nngm" ] && [ $1 != "gbn" ]; then - log "Please provide a supported project like ccp, gbn or nngm" + log "ERROR" "Please provide a supported project like ccp, gbn or nngm" exit 1 fi @@ -16,8 +16,10 @@ export PROJECT=$1 #checkRequirements // not needed when uninstalling -log "Stopping systemd services and removing bridgehead ..." +log "INFO" "Stopping system units and removing bridgehead for ${PROJECT} ..." systemctl disable --now bridgehead@${PROJECT}.service bridgehead-update@${PROJECT}.timer bridgehead-update@${PROJECT}.service rm -v /etc/systemd/system/{bridgehead\@.service,bridgehead-update\@.timer,bridgehead-update\@.service} + +log "INFO" "Successfully removed bridgehead for ${PROJECT} from your system" diff --git a/lib/setup-bridgehead-units.sh b/lib/setup-bridgehead-units.sh index e6d93f0..dc3f9fb 100755 --- a/lib/setup-bridgehead-units.sh +++ b/lib/setup-bridgehead-units.sh @@ -5,12 +5,12 @@ source lib/functions.sh exitIfNotRoot if [ $# -eq 0 ]; then - log "Please provide a Project as argument" + log "ERROR" "Please provide a Project as argument" exit 1 fi if [ $1 != "ccp" ] && [ $1 != "nngm" ] && [ $1 != "gbn" ]; then - log "Please provide a supported project like ccp, gbn or nngm" + log "ERROR" "Please provide a supported project like ccp, gbn or nngm" exit 1 fi @@ -18,7 +18,7 @@ export PROJECT=$1 checkRequirements -echo -e "\nInstalling systemd units ..." +log "INFO" "Register system units for bridgehead and bridgehead-update" cp -v \ lib/systemd/bridgehead\@.service \ lib/systemd/bridgehead-update\@.service \ @@ -27,13 +27,11 @@ cp -v \ systemctl daemon-reload -echo - if ! systemctl is-active --quiet bridgehead@"${PROJECT}"; then - log "Enabling autostart of bridgehead@${PROJECT}.service" + log "INFO" "Enabling autostart of bridgehead@${PROJECT}.service" systemctl enable bridgehead@"${PROJECT}" - log "Enabling nightly updates for bridgehead@${PROJECT}.service ..." + log "INFO" "Enabling nightly updates for bridgehead@${PROJECT}.service ..." systemctl enable --now bridgehead-update@"${PROJECT}".timer fi -echo -e "\nDone - now start your bridgehead by running\n\tsystemctl start bridgehead@${PROJECT}.service\nor by rebooting your machine." +log "INFO" "\nDone - now start your bridgehead by running\n\tsystemctl start bridgehead@${PROJECT}.service\nor by rebooting your machine." diff --git a/lib/update-bridgehead.sh b/lib/update-bridgehead.sh index 4f9bc7a..8241048 100755 --- a/lib/update-bridgehead.sh +++ b/lib/update-bridgehead.sh @@ -8,7 +8,6 @@ for DIR in /etc/bridgehead $(pwd); do old_git_hash="$(git -C $DIR rev-parse --verify HEAD)" git -C $DIR fetch 2>&1 git -C $DIR pull 2>&1 - git -C $DIR remote -v new_git_hash="$(git -C $DIR rev-parse --verify HEAD)" git_updated="false" if [ "$old_git_hash" != "$new_git_hash" ]; then