#!/bin/bash # Rocky 9 compatible . /usr/src/m2/framework/settings.sh . /usr/src/m2/framework/bash_functions.sh VERSION="1.1.0" SCRIPT_NAME="Go modules install" k_start k_config_details k_detect_os # Script variables GO_SRC_BIN_DIR=/usr/src/m2/scripts/go/bin GO_SRC_CRON_DIR=/usr/src/m2/scripts/go/cron GO_INSTALL_DIR=/usr/local/m2 GO_LOG_DIR=/var/log/m2 DEBUG_LOG=/dev/null CRON_MODIFIED=0 text_blue() { echo -e "\033[0;34m$1\033[0m" } # Check if server is marked as DB is_db_server() { if [[ "$DB_PRESENT" == "1" ]] && [[ "$MAIN_DB" == "1" || "$MAIN_DB" == "-1" ]]; then return 0 # In bash, 0 is considered as 'true' when evaluating IF else return 1 fi } # Install Go module install_module() { local module_name=$1 report "Installing module $(text_blue "$module_name")" 3 # Check if module exists if [ ! -e $GO_SRC_BIN_DIR/"$module_name" ]; then report "Module $module_name not found in $GO_SRC_BIN_DIR/$module_name" 1 return fi # Copy module to install dir report "Installing $module_name to $GO_INSTALL_DIR/$module_name" 3 &> $DEBUG_LOG cp -f $GO_SRC_BIN_DIR/"$module_name" $GO_INSTALL_DIR/"$module_name" # Create log if missing if [ ! -e $GO_LOG_DIR/"$module_name".log ]; then touch $GO_LOG_DIR/"$module_name".log fi chmod 777 $GO_LOG_DIR/"$module_name".log &> /dev/null } # Remove Go module remove_module() { local module_name=$1 if [ -e $GO_INSTALL_DIR/"$module_name" ]; then report "Removing module $(text_blue "$module_name")" fix rm -f $GO_INSTALL_DIR/"$module_name" &> /dev/null fi } # Remove cron remove_cron() { local cron_name=$1 if [ -e /etc/cron.d/"$cron_name" ]; then report "Removing cron $(text_blue "$cron_name")" fix rm -f /etc/cron.d/"$cron_name" &> /dev/null CRON_MODIFIED=1 fi } # Install Go module on main DB server install_main_db_module() { local module_name=$1 if is_db_server; then install_module "$module_name" else # Make sure that Go module is not present if it does not belong to DB server remove_module "$module_name" fi } # Install Go module on main DB server with cron install_main_db_module_with_cron() { local module_name=$1 install_main_db_module "$module_name" # Install cron if is_db_server; then if [ -e $GO_SRC_CRON_DIR/"$module_name" ]; then cp -f $GO_SRC_CRON_DIR/"$module_name" /etc/cron.d/"$module_name" CRON_MODIFIED=1 else report "Cron $GO_SRC_CRON_DIR/$module_name not found" 2 fi else # Make sure that Go module cron is not present if it does not belong to DB server remove_cron "$module_name" fi } # Check arguments check_arguments() { # Iterate each argument, make it to lowercase for arg in "$@" do arg_lower=$(echo "$arg" | awk '{print tolower($0)}') # Should we output debug to stdout? if [[ "$arg_lower" == "debug" ]]; then DEBUG_LOG="/dev/stdout" fi # Install specific module? if [[ "$arg_lower" =~ ^m4_.* ]]; then install_module "$arg_lower" exit 0 fi done } # MAIN # Check arguments check_arguments "$@" # List of modules to install install_main_db_module_with_cron "m4_subscriptions" install_main_db_module_with_cron "m4_subscription_actions" install_main_db_module_with_cron "m4_flat_rate_data" install_main_db_module_with_cron "m4_flat_rate_notifications" if [ "$CRON_MODIFIED" == "1" ]; then report "Restarting crond" 3 service crond restart &> $DEBUG_LOG fi /usr/src/m2/scripts/go/go_modules_check.sh k_exit "$EXIT_CODE"