LoMeS/lomes-setup.sh
2025-10-20 12:25:49 +02:00

276 lines
10 KiB
Bash
Executable file

#!/bin/bash
trap "exit" INT
chmod +x lomes-app.py
mkdir -p /tmp/LoMeS
source assets/shell/colors
source assets/config/deps/dependencies
### DEPENDENCY CHECK & INSTALLER
echo -e "\n ${LCY}Dependency and Privilege Check running...${CRS}\n"
### PRIVILEGES
if (( $(id -u) == 0 )); then ### AM I ROOT ?
echo -e " ${LCY}- ${GRN}Privilege check passed...${CRS}\n"
else
echo -e " ${LCY}- ${RED}Privilege check failed... Please run script with sudo or as root!${CRS}\n"
exit 1
fi
### DEPENDENCIES
dep_check() { ### MISSING PKGS ?
local pkg="$1"
if dpkg -s "$pkg" >/dev/null 2>&1; then
echo "$pkg" >>/tmp/LoMeS/installed_dev
else
echo "$pkg" >>/tmp/LoMeS/missing_dev
fi
}
for pkg in "${deps[@]}"; do
dep_check "$pkg"
done
installed="$(cat /tmp/LoMeS/installed_dev 2>/dev/null)"
missing="$(cat /tmp/LoMeS/missing_dev 2>/dev/null)"
if ! [ "$missing" ]; then
echo -e " ${LCY}- ${GRN}Dependencies met. Proceeding...${CRS}\n"
else ### INSTALLING PKGS !
echo -e " ${LCY}- ${RED}Following dependencies are missing :\n\n$missing${CRS}"
while true; do
echo -e "\n ${YEL}Do you wish to install via APT?"
read -p " (Y/n) --> " install_dep
echo -e "${CRS}"
if [[ "$install_dep" = "" || "$install_dep" = "y" || "$install_dep" = "Y" ]]; then
sudo apt update > /dev/null 2>&1 && sudo apt install -y $missing --simulate > /dev/null 2>&1
echo -e " ${LCY}- ${GRN}Dependencies installed. Proceeding...${CRS}\n"
sudo rm /tmp/LoMeS/installed_dev /tmp/LoMeS/missing_dev 2>/dev/null
break
elif [[ "$install_dep" == "n" || "$install_dep" = "N" ]]; then
echo -e " ${RED}Missing dependencies... Exiting!${CRS}\n"
sudo rm /tmp/LoMeS/installed_dev /tmp/LoMeS/missing_dev 2>/dev/null
exit 1
else
echo -e " ${YEL}Invalid response... Try again...\n\n ${GRN}Y ${YEL}= (Yes, install dependencies and continue)\n ${RED}N ${YEL}= (No, don't install dependencies and exit)${CRS}\n "
fi
done
fi
### NGINX SETUP & CONFIG
###### CONNECTION & INTERFACE
onif=$(/sbin/ip route get 162.249.72.1 | awk '{print $5}' | cut -d/ -f1)
while true; do ### SELECT NETWORK INTERFACE !
echo -e " ${YEL}What network interface will nginx be using?"
read -p " current = "$onif" --> " nif
echo -e "${CRS}"
if ! [ "$nif" ]; then
nif="$onif"
break
elif [ -d "/sys/class/net/$nif" ]; then
break
else
echo -e "\n ${LRD}Interface not found... Try again!${CRS}\n"
fi
done
ip4=$(/sbin/ip -o -4 addr list "$nif" | awk '{print $4}' | cut -d/ -f1) ### GET IP FOR CHOSEN INTERFACE !
echo -e "\n ${YEL}Current hostname : ${LCY}$(hostname)${CRS}"
echo -e " ${YEL}Current ip address : ${LCY}$ip4 ${YEL}@ ${LCY}$nif${CRS}"
echo -e "\n ${YEL}This information will be used to configure ${LCY}nginx.conf ${YEL}during the next steps."
while true; do ### HOST NAME CHANGE ?
read -p " Would you like to change the hostname? (y/N) --> " conf_hostname
echo -e "${CRS}"
if [[ "$conf_hostname" = "" || "$conf_hostname" = "n" || "$conf_hostname" = "N" ]]; then
new_hostname=$(hostname)
break
elif [[ "$conf_hostname" = "y" || "$conf_hostname" = "Y" ]]; then
read -p " Enter new hostname --> " new_hostname
sudo sed -i.backup "s/$(hostname)/$new_hostname/g" /etc/hosts
sudo echo "$new_hostname" > /etc/hostname
sudo systemctl restart NetworkManager 2>/dev/null
hostname -F /etc/hostname 2>/dev/null
echo -e "\n ${GRN}Host name changed to ${LCY}$(hostname)${CRS}"
echo "HC" > /tmp/LoMeS/honacha
break
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n Y = (Yes, set new hostname)\n N = (No, leave as is)${CRS}\n "
fi
done
while true; do ### CERTIFICATION & CONFIGURATION HTTPS
echo -e "\n ${YEL}Configure nginx with SSL and create a self signed cetrificate?"
read -p " (Y/n) --> " nginxSSL
echo -e "${CRS}"
if [[ "$nginxSSL" = "" || "$nginxSSL" = "y" || "$nginxSSL" = "Y" ]]; then
echo -e " ${YEL}Enter path to certificates folder"
read -p " default = /etc/nginx/ssl --> " cert_path
echo -e "${CRS}"
if ! [ "$cert_path" ]; then
cert_path=/etc/nginx/ssl
elif [[ "$cert_path" = "." ]]; then
cert_path=$PWD
else
:
fi
echo -e " ${YEL}Enter file name for certificate and key"
read -p " default = $(hostname) --> " cert_name
echo -e "${CRS}"
if ! [ "$cert_name" ]; then
cert_name=$(hostname)
else
:
fi
sudo mkdir -p "$cert_path"
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout "$cert_path"/"$cert_name".key -out "$cert_path"/"$cert_name".crt
echo -e "\n ${GRN}SSL certificate files ${LCY}$cert_name.crt ${GRN}and ${LCY}$cert_name.key ${GRN}created and stored in ${LCY}$cert_path${CRS}"
sudo chmod 644 "$cert_path"/"$cert_name".crt
sudo chmod 600 "$cert_path"/"$cert_name".key
sudo cp assets/config/nginx/nginx_SSL.conf /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s/DOMAIN/$(hostname).local/g" /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s/IPADDR/$ip4/g" /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s|CERTPATH|$cert_path/$cert_name|" /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s|KEYPATH|$cert_path/$cert_name|" /etc/nginx/sites-enabled/$(hostname).conf
break
elif [[ "$nginxSSL" == "n" || "$nginxSSL" = "N" ]]; then
while true; do ### CONFIGURATION HTTP
echo -e " ${YEL}Configure nginx with HTTP?"
read -p " (Y/n) --> " nginxHTTP
echo -e "${CRS}"
if [[ "$nginxHTTP" = "" || "$nginxHTTP" = "y" || "$nginxHTTP" = "Y" ]]; then
sudo cp assets/config/nginx/nginx_HTTP.conf /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s/DOMAIN/$(hostname).local/g" /etc/nginx/sites-enabled/$(hostname).conf
sudo sed -i "s/IPADDR/$ip4/g" /etc/nginx/sites-enabled/$(hostname).conf
break
elif [[ "$nginxHTTP" = "n" || "$nginxHTTP" = "N" ]]; then
echo -e " ${LCY}- ${LRD}Keeping nginx default configuration!${CRS}"
break
else
echo -e " ${YEL}Invalid response... Try again...\n\n Y = (Yes, configure nginx)\n N = (No, skip nginx configuration)${CRS}\n "
break
fi
done
break
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n Y = (Yes, configure SSL certificate and continue)\n N = (No, leave unencrypted and continue)${CRS}\n "
fi
done
###### NGINX MAINTENANCE
sudo rm -rf /var/www/html
if ! [ "$(sudo nginx -t > /dev/null 2>&1)" ]; then ### NGINX CONF CHECK
echo -e "\n ${LCY}- ${LGN}Nginx configuration checks out...${CRS}"
else
echo -e "\n ${LRD}Nginx configuration is malformed!${CRS}"
echo "nginx -t --> failed" >> /tmp/LoMeS/install_log
fi
if ! [ "$(sudo nginx -s reload > /dev/null 2>&1)" ]; then ### RELOAD NGINX
echo -e "\n ${LCY}- ${LGN}Nginx reloaded...${CRS}"
else
echo -e "\n ${LRD}Nginx couldn't reload!${CRS}"
echo "nginx -s reload --> failed" >> /tmp/LoMeS/install_log
fi
if ! [ -f "/tmp/LoMeS/install_log" ]; then ### LOG CHECK
echo -e "\n ${LCY}- ${LGN}No errors while setting up...${CRS}"
else
echo -e "\n ${LRD}Error occured during setup! Please check ${LCY}/tmp/LoMeS/install_log ${LRD}for details...${CRS}"
fi
if [ -f "/tmp/LoMeS/honacha" ]; then ### REBOOT DUE TO HOST NAME CHANGE
echo -e "\n ${YEL}During setup you changed this machines host name. For the change to take effect, you should reboot! "
while true; do
read -p " Would you like to reboot? (Y/n) --> " reboot
echo -e "${CRS}"
if [[ "$reboot" = "" || "$reboot" = "y" || "$reboot" = "Y" ]]; then
echo -e " ${YEL}Rebooting in 10sec..."
echo -e " ${YEL}to start flask manually, run ${LCY}python3 lomes-app.py ${YEL}from LoMeS directory${CRS}"
sleep 10s
sudo reboot
elif [[ "$reboot" = "n" || "$reboot" = "N" ]]; then
echo -e " ${YEL}to start flask manually, run ${LCY}python3 lomes-app.py ${YEL}from LoMeS directory${CRS}"
break
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n Y = (Yes, reboot)\n N = (No, don't reboot)${CRS}\n "
fi
done
else
while true; do
echo -e "${YEL}" ### START FLASK ?
read -p " Would you like to start flask? (Y/n) --> " flask_start
echo -e "${CRS}"
if [[ "$flask_start" = "" || "$flask_start" = "y" || "$flask_start" = "Y" ]]; then
echo -e " ${YEL}Starting flask in 10sec..."
echo -e " ${YEL}to start flask manually, run ${LCY}python3 lomes-app.py ${YEL}from LoMeS directory${CRS}\n"
sleep 10s
python3 lomes-app.py
break
elif [[ "$flask_start" = "n" || "$flask_start" = "N" ]]; then
echo -e " ${YEL}to start flask manually, run ${LCY}python3 lomes-app.py ${YEL}from LoMeS directory${CRS}\n"
break
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n Y = (Yes, to start flask)\n N = (No, start flask later)${CRS}\n "
fi
done
fi