From ef4b24a20066523532f3b182e1647cb4a222d69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Thu, 19 Jun 2025 22:32:20 +0200 Subject: [PATCH] feat(install.sh): dependancies finder --- install.sh | 136 ++++++++++++++++++++++++------------------ scripts/aws-backup.sh | 2 +- scripts/common.sh | 2 +- scripts/sshd-login.sh | 18 ++++++ 4 files changed, 98 insertions(+), 60 deletions(-) diff --git a/install.sh b/install.sh index 53b962c..2be39e6 100644 --- a/install.sh +++ b/install.sh @@ -4,61 +4,6 @@ source "./scripts/common.sh" SCRIPT_FILE="/usr/local/bin" -AUTO_CONFIRM=false -[[ "$1" == "--true" ]] && AUTO_CONFIRM=true - -if [[ $EUID -ne 0 ]]; then - echo "The script needs to run as root." - exit 1 -fi - -touch "$LOG" || { echo "Cannot create log file $LOG"; exit 1; } -chmod 644 "$LOG" - -datetime_print "-- Starting ServerConfig installation v(1.0.0) --" - -mkdir -p "$ETC_DIR" && \ -datetime_print "$ETC_DIR ensured." - -datetime_print "Installing scripts to $SCRIPT_FILE..." - -install_file() { - local filename="$1" - local pathname="$2" - local argument="${3:--Dm755}" - - datetime_print "Installing $filename to $pathname..." - - if [[ ! -f "scripts/$filename" ]]; then - datetime_print "Source file scripts/$filename not found." "ERROR" - exit 1 - fi - - if [[ -e "$pathname/$filename" ]]; then - if $AUTO_CONFIRM; then - yn="y" - else - read -p "$pathname/$filename already set, overwrite? (y/N): " yn - fi - case "$yn" in - [Yy]*) - ;; - *) - datetime_print "$pathname/$filename not changed." - return - ;; - esac - fi - - install $argument "scripts/$filename" "$pathname/$filename" && \ - datetime_print "$filename installed." || \ - { datetime_print "Error while installing $filename" "ERROR"; exit 1; } -} -echo -datetime_print "------------- install files -------------" -for element in scripts/*.sh; do install_file "$(basename $element)" "$SCRIPT_FILE" -Dm755; done -install_file "docker-compose.yml" "$ETC_DIR" -Dm644 - create_env_variable() { local key="$1" local value="$2" @@ -90,15 +35,83 @@ create_env_variable() { esac else echo "$key=$value" >> "$ENV_FILE" - datetime_print "$key set." + datetime_print "$key \e[32mset\e[0m." fi } +install_file() { + local filename="$1" + local pathname="$2" + local argument="${3:--Dm755}" + + datetime_print "Installing $filename to $pathname..." + + if [[ ! -f "scripts/$filename" ]]; then + datetime_print "Source file scripts/$filename not found." "\e[31mERROR\e[0m" + exit 1 + fi + + if [[ -e "$pathname/$filename" ]]; then + if $AUTO_CONFIRM; then + yn="y" + else + read -p "$pathname/$filename already set, overwrite? (y/N): " yn + fi + case "$yn" in + [Yy]*) + ;; + *) + datetime_print "$pathname/$filename not changed." + return + ;; + esac + fi + + install $argument "scripts/$filename" "$pathname/$filename" && \ + datetime_print "$filename \e[32minstalled.\e[0m" || \ + { datetime_print "Error while installing $filename" "\e[31mERROR\e[0m"; exit 1; } +} + +AUTO_CONFIRM=false +[[ "$1" == "--true" ]] && AUTO_CONFIRM=true + +INSTALLED_PROG=("curl" "aws") +for prog in ${INSTALLED_PROG[@]}; do + echo -n "Verifing $prog... " + if ! command -v $prog &> "/dev/null"; then + echo -e "\e[31mError: not installed\e[0m" + exit 1 + fi + echo -e "\e[32mDone\e[0m" +done + +if [[ $EUID -ne 0 ]]; then + echo "The script needs to run as root." + exit 1 +fi + +touch "$LOG" || { echo "\e[31mCannot create log file $LOG\e[0m"; exit 1; } +chmod 644 "$LOG" + +datetime_print "Starting ServerConfig Installation v1.0.0" + +mkdir -p "$ETC_DIR" && \ +datetime_print "$ETC_DIR ensured." + +datetime_print "Installing scripts to $SCRIPT_FILE..." + echo -datetime_print "--------- define .env variables ---------" +datetime_print "------------- Install Files -------------" +for element in scripts/*.sh; do install_file "$(basename $element)" "$SCRIPT_FILE" -Dm755; done +install_file "docker-compose.yml" "$ETC_DIR" -Dm644 + + +echo +datetime_print "--------- Define .env variables ---------" ENV_LIST=("AWS" "TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN" "EMAIL" "WG_HOSTNAME_VPN") +touch "$ENV_FILE" for env in "${ENV_LIST[@]}"; do read -p "Enter value for $env: " value create_env_variable "$env" "$value" @@ -112,7 +125,14 @@ while true; do done echo -datetime_print "--------- Installation complete ---------" +datetime_print "---------- Script Installation ----------" +for element in /usr/local/bin/*.sh; do + bash "$element" --install +done + +echo +datetime_print "--------- Installation Complete ---------" datetime_print "All config files are in $ETC_DIR" datetime_print "All scripts are in $SCRIPT_FILE" + echo "Log file written at: $LOG" \ No newline at end of file diff --git a/scripts/aws-backup.sh b/scripts/aws-backup.sh index fc12a76..ad6adb2 100644 --- a/scripts/aws-backup.sh +++ b/scripts/aws-backup.sh @@ -2,7 +2,7 @@ source /etc/serverconfig/.env -DIR="$(cd "$(dirname "$0")" && pwd)" +DIR="$(cd "$(dirname "$0")" && pwd)" BACKUP="$DIR/aws-bakup.bak" LOG="/var/log/aws-bak.log" diff --git a/scripts/common.sh b/scripts/common.sh index 639cbda..def8590 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -13,5 +13,5 @@ datetime_print() { local level="${2:-INFO}" local timestamp="[$(date +"$DATETIME_FORMAT")]" - echo "$timestamp - $level: $message" | tee -a "$LOG" + echo -e "$timestamp - $level: $message" | tee -a "$LOG" } \ No newline at end of file diff --git a/scripts/sshd-login.sh b/scripts/sshd-login.sh index bae3b78..dcd914a 100644 --- a/scripts/sshd-login.sh +++ b/scripts/sshd-login.sh @@ -2,6 +2,24 @@ source /usr/local/bin/notifications.sh +# initialize_config() { +# local isInstalling="$1" +# local target_file="$2" +# local crontab_configuration="$3" +# local link_path="$4" + +# if [[ $isInstalling != "--install" ]]; then +# return; +# fi + +# echo "$crontab_configuration $0" | crontab - +# } + +# install_para="$1" +# if [[ $install_para == "--install" ]]; then + +# fi + case "$PAM_TYPE" in open_session) PAYLOAD=" { \"text\": \"$PAM_USER logged in (remote host: $PAM_RHOST) at $(date).\" }"