mirror of
https://github.com/guezoloic/serverconfig.git
synced 2026-03-28 18:03:49 +00:00
feature: update install.sh and docker-compose file
This commit is contained in:
145
install.sh
145
install.sh
@@ -1,105 +1,76 @@
|
||||
#!/bin/bash
|
||||
|
||||
source ./libs/common.sh
|
||||
set -euo pipefail
|
||||
|
||||
mkdir -p $ETC_DIR
|
||||
rm -f $LOG
|
||||
readonly PROJECT_NAME="serverconfig"
|
||||
readonly PROJECT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
|
||||
ISERROR=false
|
||||
INSTALLED_DEP=( $(grep -v '^#' $(pwd)/requirements.txt) )
|
||||
readonly REQ=("curl" "docker")
|
||||
|
||||
readonly ENV_FILE="${PROJECT_DIR}/.env"
|
||||
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "The script needs to run as root."
|
||||
exit 1
|
||||
fi
|
||||
readonly GREEN='\033[0;32m'
|
||||
readonly RED='\033[0;31m'
|
||||
readonly YELLOW='\033[1;33m'
|
||||
readonly BLUE='\033[0;34m'
|
||||
readonly NC='\033[0m'
|
||||
|
||||
touch "$LOG" || ISERROR=true
|
||||
if $ISERROR; then
|
||||
info_print "Failed to Create $LOG" $ERROR_FLAG false; exit 1
|
||||
fi
|
||||
DATETIME_FORMAT="%d-%m-%Y %H:%M:%S"
|
||||
|
||||
chmod 644 "$LOG"
|
||||
function log() {
|
||||
local type="${1}"
|
||||
local color="${2}"
|
||||
local message="${3}"
|
||||
echo -e "${color}[$(date +"$DATETIME_FORMAT")] [${type}]${NC} ${message}"
|
||||
}
|
||||
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
ServerConfig Installation v1.0.0\n\
|
||||
--------------------------------------------------"
|
||||
info_print "License : MIT"
|
||||
info_print "Repository : https://github.com/guezoloic/serverconfig"
|
||||
info_print "Date : Installation $(date '+%Y-%m-%d %H:%M:%S')"
|
||||
function log_info() { log "INFO" "$BLUE" "$1"; }
|
||||
function log_success() { log "OK " "$GREEN" "$1"; }
|
||||
function log_error() { log "ERR " "$RED" "$1" >&2; }
|
||||
|
||||
|
||||
if $ISERROR; then
|
||||
info_print "Failed to move some scripts to $SCRIPT_FILE, See log $LOG" $ERROR_FLAG false; exit 1
|
||||
fi
|
||||
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
Installing config files to $ETC_DIR\n\
|
||||
--------------------------------------------------" -- false
|
||||
|
||||
for config in config/*; do
|
||||
filename=$(basename "$config")
|
||||
info_print "Moving $filename to $SCRIPT_FILE"
|
||||
|
||||
if [ -d "$config" ]; then
|
||||
cp -r "$config" "$ETC_DIR/" \
|
||||
&& { info_print "$ETC_DIR/$filename installed (directory)." $SUCCESS_FLAG; } \
|
||||
|| { info_print "$ETC_DIR/$filename failed (directory)." $ERROR_FLAG; ISERROR=true; }
|
||||
else
|
||||
install -Dm755 "$config" "$ETC_DIR/$filename" \
|
||||
&& { info_print "$ETC_DIR/$filename installed." $SUCCESS_FLAG; } \
|
||||
|| { info_print "$ETC_DIR/$filename failed." $ERROR_FLAG; ISERROR=true; }
|
||||
function check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
log_error "The script needs to run as root."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
if $ISERROR; then
|
||||
info_print "Failed to move some scripts to $ENV_FILE, See log $LOG" 3 false; exit 1
|
||||
fi
|
||||
function check_dependencies() {
|
||||
log_info "Checking system dependencies..."
|
||||
for cmd in "${REQ[@]}"; do
|
||||
if ! command -pv "$cmd" &>/dev/null; then
|
||||
log_error "${cmd} is not installed."
|
||||
exit 1
|
||||
else
|
||||
log_success "${cmd} is installed."
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
Checking dependencies \n\
|
||||
--------------------------------------------------" -- false
|
||||
function install_scripts() {
|
||||
log_info "Installing scripts..."
|
||||
for script in "$SCRIPT_FILE"/*.sh; do
|
||||
[ -e "$script" ] || continue
|
||||
log_info "Configuring $(basename "$script")..."
|
||||
if ! bash "$script" --install; then
|
||||
log_error "Hook failed for $script"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
for dep in ${INSTALLED_DEP[@]}; do
|
||||
if command -v "$dep" &>/dev/null; then
|
||||
info_print "$dep is installed." $SUCCESS_FLAG
|
||||
else
|
||||
info_print "$dep is not installed." $ERROR_FLAG
|
||||
ISERROR=true
|
||||
fi
|
||||
done
|
||||
function main() {
|
||||
clear
|
||||
echo -e "${YELLOW}${PROJECT_NAME} Installation${NC}"
|
||||
|
||||
if $ISERROR; then
|
||||
info_print "Some Dependencies are missing. Please check requirements.txt." $ERROR_FLAG false; exit 1
|
||||
fi
|
||||
check_root
|
||||
check_dependencies
|
||||
|
||||
log_info "Creating directories and files..."
|
||||
touch "$ENV_FILE"
|
||||
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
Installing scripts to $SCRIPT_FILE \n\
|
||||
--------------------------------------------------" -- false
|
||||
install_scripts
|
||||
|
||||
for scripts in libs/*.sh scripts/*.sh; do
|
||||
info_print "Moving $scripts to $SCRIPT_FILE"
|
||||
output="$SCRIPT_FILE/$scripts"
|
||||
log_success "Installation Complete"
|
||||
}
|
||||
|
||||
install $argument "$scripts" $output -Dm755 \
|
||||
&& { info_print "$output installed." $SUCCESS_FLAG; } \
|
||||
|| { info_print "$output failed." $ERROR_FLAG; ISERROR=true; }
|
||||
done
|
||||
|
||||
touch $ENV_FILE
|
||||
for element in $SCRIPT_FILE/*/*.sh; do
|
||||
bash "$element" --install
|
||||
done
|
||||
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
Installation Complete\n\
|
||||
--------------------------------------------------"
|
||||
info_print "All config files are in $ETC_DIR"
|
||||
info_print "All scripts are in $SCRIPT_FILE"
|
||||
|
||||
echo "Log file written at: $LOG"
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user