LoMeS/dev_env_setup.sh

242 lines
7.9 KiB
Bash
Raw Normal View History

2025-10-08 17:46:15 +02:00
#!/bin/bash
2025-10-12 18:01:06 +02:00
trap "exit" INT
2025-10-08 17:46:15 +02:00
2025-10-12 17:31:48 +02:00
mkdir -p /tmp/LoMeS
2025-10-09 17:57:13 +02:00
source assets/shell/colors
source assets/config/deps/dependencies
### DEPENDENCY CHECK & INSTALLER
2025-10-08 17:46:15 +02:00
2025-10-09 20:07:44 +02:00
echo -e "\n ${LCY}Dependency and Privilege Check running...${CRS}\n"
2025-10-08 17:46:15 +02:00
2025-10-08 19:59:38 +02:00
### PRIVILEGES
2025-10-11 17:36:02 +02:00
if (( $(id -u) == 0 )); then ### AM I ROOT ?
2025-10-12 17:47:16 +02:00
echo -e " ${GRN}Privilege check passed...${CRS}\n"
2025-10-08 17:46:15 +02:00
2025-10-11 18:14:43 +02:00
else
2025-10-09 20:07:44 +02:00
echo -e " ${RED}Privilege check failed... Please run script with sudo or as root!${CRS}\n"
2025-10-08 17:46:15 +02:00
exit 1
2025-10-11 17:16:38 +02:00
2025-10-08 17:46:15 +02:00
fi
2025-10-08 19:59:38 +02:00
### DEPENDENCIES
2025-10-11 18:14:43 +02:00
2025-10-11 17:36:02 +02:00
dep_check() { ### MISSING PKGS ?
2025-10-11 17:16:38 +02:00
local pkg="$1"
if dpkg -s "$pkg" >/dev/null 2>&1; then
2025-10-12 17:23:44 +02:00
echo "$pkg" >>/tmp/LoMeS/installed_dev
2025-10-11 17:16:38 +02:00
else
2025-10-12 17:23:44 +02:00
echo "$pkg" >>/tmp/LoMeS/missing_dev
2025-10-11 17:16:38 +02:00
fi
}
for pkg in "${deps[@]}"; do
dep_check "$pkg"
2025-10-11 18:14:43 +02:00
done
2025-10-11 17:16:38 +02:00
2025-10-12 17:23:44 +02:00
installed="$(cat /tmp/LoMeS/installed_dev 2>/dev/null)"
missing="$(cat /tmp/LoMeS/missing_dev 2>/dev/null)"
2025-10-11 17:16:38 +02:00
if ! [ "$missing" ]; then
2025-10-12 17:46:01 +02:00
echo -e " ${GRN}Dependencies met. Proceeding...${CRS}\n"
2025-10-08 19:59:38 +02:00
2025-10-12 09:49:35 +02:00
else ### INSTALLING PKGS !
2025-10-12 17:46:01 +02:00
echo -e " ${RED}Following dependencies are missing :\n\n$missing${CRS}"
2025-10-11 17:16:38 +02:00
2025-10-08 17:46:15 +02:00
while true; do
2025-10-11 17:16:38 +02:00
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
2025-10-12 17:33:34 +02:00
sudo apt update > /dev/null 2>&1 && sudo apt install -y $missing > /dev/null 2>&1
2025-10-12 17:46:01 +02:00
echo -e " ${GRN}Dependencies installed. Proceeding...${CRS}\n"
2025-10-12 17:23:44 +02:00
sudo rm /tmp/LoMeS/installed_dev /tmp/LoMeS/missing_dev 2>/dev/null
2025-10-11 17:16:38 +02:00
break
2025-10-08 17:46:15 +02:00
2025-10-11 17:16:38 +02:00
elif [[ "$install_dep" == "n" || "$install_dep" = "N" ]]; then
2025-10-12 17:46:01 +02:00
echo -e " ${RED}Missing dependencies... Exiting!${CRS}\n"
2025-10-12 17:23:44 +02:00
sudo rm /tmp/LoMeS/installed_dev /tmp/LoMeS/missing_dev 2>/dev/null
2025-10-08 17:46:15 +02:00
exit 1
else
2025-10-12 17:46:01 +02:00
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 "
2025-10-11 17:16:38 +02:00
2025-10-08 17:46:15 +02:00
fi
done
fi
2025-10-08 19:59:38 +02:00
### NGINX SETUP & CONFIG
2025-10-09 17:57:13 +02:00
###### HOST
2025-10-12 09:49:35 +02:00
onif=$(/sbin/ip route get 162.249.72.1 | awk '{print $5}' | cut -d/ -f1) ### GET ACTIVE NETWORK INTERFACE !
2025-10-09 20:07:44 +02:00
while true; do
2025-10-12 17:46:01 +02:00
echo -e " ${YEL}What network interface will nginx be using?"
2025-10-09 20:07:44 +02:00
read -p " current = "$onif" --> " nif
2025-10-12 17:46:01 +02:00
echo -e "${CRS}"
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
if ! [ "$nif" ]; then
nif="$onif"
break
2025-10-11 17:16:38 +02:00
2025-10-10 13:28:40 +02:00
elif [ -d "/sys/class/net/$nif" ]; then
2025-10-09 20:07:44 +02:00
break
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
else
echo -e "\n ${LRD}Interface not found... Try again!${CRS}\n"
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
fi
done
2025-10-10 13:28:40 +02:00
2025-10-12 09:49:35 +02:00
ip4=$(/sbin/ip -o -4 addr list "$nif" | awk '{print $4}' | cut -d/ -f1) ### GET IP FOR CHOSEN INTERFACE !
2025-10-10 20:38:19 +02:00
echo -e "\n ${YEL}Current hostname : ${LCY}$(hostname)${CRS}"
2025-10-10 13:28:40 +02:00
echo -e " ${YEL}Current ip address : ${LCY}$ip4 ${YEL}@ ${LCY}$nif${CRS}"
2025-10-12 17:46:01 +02:00
echo -e "\n ${YEL}This information will be used to configure ${LCY}nginx.conf ${YEL}during the next steps."
2025-10-09 17:57:13 +02:00
2025-10-09 20:07:44 +02:00
while true; do
2025-10-12 17:23:44 +02:00
read -p " Would you like to change the hostname? (y/N) --> " conf_hostname ### HOST NAME CHANGE ?
2025-10-12 17:46:01 +02:00
echo -e "${CRS}"
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
if [[ "$conf_hostname" = "" || "$conf_hostname" = "n" || "$conf_hostname" = "N" ]]; then
2025-10-10 20:38:19 +02:00
new_hostname=$(hostname)
2025-10-09 20:07:44 +02:00
break
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
elif [[ "$conf_hostname" = "y" || "$conf_hostname" = "Y" ]]; then
read -p " Enter new hostname --> " new_hostname
2025-10-10 20:38:19 +02:00
sudo sed -i.backup "s/$(hostname)/$new_hostname/g" /etc/hosts
2025-10-10 20:30:49 +02:00
sudo echo "$new_hostname" > /etc/hostname
2025-10-11 19:10:48 +02:00
sudo systemctl restart NetworkManager 2>/dev/null
hostname -F /etc/hostname 2>/dev/null
2025-10-10 20:11:17 +02:00
echo -e "\n ${GRN}Host name changed to ${LCY}$(hostname)${CRS}"
2025-10-12 17:23:44 +02:00
echo "HC" > /tmp/LoMeS/honacha
2025-10-09 20:07:44 +02:00
break
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
else
echo -e "\n ${YEL}Invalid response... Try again...\n\n Y = (Yes, set new hostname)\n N = (No, leave as is)${CRS}\n "
2025-10-11 17:16:38 +02:00
2025-10-09 20:07:44 +02:00
fi
done
2025-10-09 17:57:13 +02:00
2025-10-10 15:12:52 +02:00
###### CERTIFICATION & CONFIGURATION
2025-10-08 18:44:04 +02:00
2025-10-08 17:46:15 +02:00
while true; do
2025-10-12 17:23:44 +02:00
echo -e "\n ${YEL}Configure SSL and create a self signed cetrificate?"
2025-10-09 17:57:13 +02:00
read -p " (Y/n) --> " installSSL
2025-10-12 17:23:44 +02:00
echo -e "${CRS}"
2025-10-09 20:07:44 +02:00
2025-10-08 17:46:15 +02:00
if [[ "$installSSL" = "" || "$installSSL" = "y" || "$installSSL" = "Y" ]]; then
2025-10-12 17:46:01 +02:00
echo -e " ${YEL}Enter path to certificates folder"
2025-10-09 15:42:38 +02:00
read -p " default = /etc/nginx/ssl --> " cert_path
2025-10-12 17:46:01 +02:00
echo -e "${CRS}"
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
if ! [ "$cert_path" ]; then
cert_path=/etc/nginx/ssl
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
elif [[ "$cert_path" = "." ]]; then
cert_path=$PWD
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
else
:
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
fi
2025-10-12 17:23:44 +02:00
echo -e " ${YEL}Enter file name for certificate and key"
2025-10-10 20:11:17 +02:00
read -p " default = $(hostname) --> " cert_name
2025-10-12 17:23:44 +02:00
echo -e "${CRS}"
2025-10-11 18:14:43 +02:00
2025-10-09 15:42:38 +02:00
if ! [ "$cert_name" ]; then
2025-10-10 20:11:17 +02:00
cert_name=$(hostname)
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
else
:
2025-10-11 17:16:38 +02:00
2025-10-09 15:42:38 +02:00
fi
2025-10-10 15:12:52 +02:00
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
2025-10-09 20:07:44 +02:00
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}\n"
2025-10-10 15:12:52 +02:00
sudo chmod 644 "$cert_path"/"$cert_name".crt
sudo chmod 600 "$cert_path"/"$cert_name".key
2025-10-10 20:38:19 +02:00
sudo cp assets/config/nginx/nginx_SSL.conf /etc/nginx/sites-enabled/$(hostname).conf
2025-10-10 20:42:05 +02:00
sudo sed -i "s/DOMAIN/$(hostname).local/g" /etc/nginx/sites-enabled/$(hostname).conf
2025-10-10 20:38:19 +02:00
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
2025-10-08 18:44:04 +02:00
break
2025-10-11 17:16:38 +02:00
2025-10-08 18:44:04 +02:00
elif [[ "$installSSL" == "n" || "$installSSL" = "N" ]]; then
2025-10-09 20:07:44 +02:00
echo -e "\n ${RED}Encryption disabled${CRS}\n"
2025-10-08 18:50:02 +02:00
break
2025-10-11 17:16:38 +02:00
2025-10-08 17:46:15 +02:00
else
2025-10-09 20:07:44 +02:00
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 "
2025-10-11 17:16:38 +02:00
fi
2025-10-09 20:07:44 +02:00
done
2025-10-12 17:23:44 +02:00
###### NGINX MAINTENANCE & LANDING PAGE
2025-10-10 15:12:52 +02:00
2025-10-12 15:22:41 +02:00
sudo rm -rf /var/www/html
2025-10-12 15:24:05 +02:00
sudo cp -R assets/html /var/www
2025-10-10 15:12:52 +02:00
2025-10-12 17:23:44 +02:00
if ! [ "$(sudo nginx -t > /dev/null 2>&1)" ]; then ### NGINX CONF CHECK
echo -e "\n ${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 ${LGN}Nginx reloaded...${CRS}"
2025-10-10 15:12:52 +02:00
2025-10-12 17:23:44 +02:00
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
2025-10-12 18:01:06 +02:00
echo -e "\n ${LGN}No errors while setting up...${CRS}\n"
2025-10-12 17:23:44 +02:00
else
2025-10-12 18:01:06 +02:00
echo -e "\n ${LRD}Error occured during setup! Please check ${LCY}/tmp/LoMeS/install_log ${LRD}for details...${CRS}"
2025-10-12 17:23:44 +02:00
fi
if [ -f "/tmp/LoMeS/honacha" ]; then ### HOST NAME CHANGE !?
2025-10-12 18:01:06 +02:00
echo -e "\n ${YEL}During setup you changed this machines host name. For the change to take effect, you should reboot! "
2025-10-12 17:23:44 +02:00
while true; do
read -p " Would you like to reboot? (Y/n) --> " reboot
echo -e "${CRS}"
2025-10-09 17:57:13 +02:00
2025-10-12 17:23:44 +02:00
if [[ "$reboot" = "" || "$reboot" = "y" || "$reboot" = "Y" ]]; then
sudo reboot
break
elif [[ "$reboot" = "n" || "$reboot" = "N" ]]; then
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
:
fi
###### STATUS
2025-10-09 17:57:13 +02:00
2025-10-11 17:36:02 +02:00
2025-10-12 09:49:35 +02:00
###### JEFF ######
2025-10-10 15:12:52 +02:00
#
2025-10-12 09:49:35 +02:00
#
#
2025-10-10 15:12:52 +02:00
#
2025-10-12 09:49:35 +02:00
###### JEFF END ######
2025-10-10 15:12:52 +02:00