Advertisement
YaBoiSwayZ

service-setup.sh

Jul 27th, 2024
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.88 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4. set -u
  5. set -o pipefail
  6.  
  7. LOG_FILE="/var/log/install_script.log"
  8. ROLLBACK_FILE="/var/log/install_script_rollback.log"
  9.  
  10. log() {
  11.   printf "%s - %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" | tee -a "$LOG_FILE"
  12. }
  13.  
  14. rollback_log() {
  15.   printf "%s\n" "$1" | tee -a "$ROLLBACK_FILE"
  16. }
  17.  
  18. command_exists() {
  19.   command -v "$1" >/dev/null 2>&1
  20. }
  21.  
  22. package_installed() {
  23.   dpkg -l | grep -q "$1"
  24. }
  25.  
  26. commands=("ftp" "pftp" "sh" "apache2" "telnetd" "nginx" "mysql" "sshd" "postgresql" "redis-server" "docker")
  27.  
  28. pre_flight_checks() {
  29.   if ! command_exists "sudo"; then
  30.     log "Error: This script requires 'sudo' to run. Please install 'sudo' and try again." >&2
  31.     exit 1
  32.   fi
  33. }
  34.  
  35. detect_os() {
  36.   if [[ -f /etc/debian_version ]]; then
  37.     if grep -q "Ubuntu" /etc/os-release; then
  38.       printf "ubuntu\n"
  39.     else
  40.       printf "debian\n"
  41.     fi
  42.   elif [[ -f /etc/redhat-release ]]; then
  43.     if grep -q "CentOS" /etc/redhat-release; then
  44.       printf "centos\n"
  45.     elif grep -q "Fedora" /etc/redhat-release; then
  46.       printf "fedora\n"
  47.     else
  48.       printf "redhat\n"
  49.     fi
  50.   elif [[ -f /etc/arch-release ]]; then
  51.     printf "arch\n"
  52.   elif [[ -f /etc/SuSE-release ]]; then
  53.     printf "suse\n"
  54.   elif [[ -f /etc/freebsd-update.conf ]]; then
  55.     printf "freebsd\n"
  56.   elif [[ "$(uname)" == "Darwin" ]]; then
  57.     printf "darwin\n"
  58.   else
  59.     printf "unsupported\n"
  60.   fi
  61. }
  62.  
  63. prompt_user() {
  64.   read -p "$1 (y/n): " -n 1 -r
  65.   printf "\n"
  66.   if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  67.     log "Operation aborted by user." >&2
  68.     exit 1
  69.   fi
  70. }
  71.  
  72. install_packages() {
  73.   local os="$1"
  74.   case "$os" in
  75.     debian | ubuntu)
  76.       sudo apt-get update
  77.       for cmd in "${missing[@]}"; do
  78.         if ! package_installed "$cmd"; then
  79.           if sudo apt-get install -y "$cmd"; then
  80.             rollback_log "sudo apt-get remove -y $cmd"
  81.           else
  82.             log "Error: Failed to install $cmd." >&2
  83.             exit 1
  84.           fi
  85.         fi
  86.       done
  87.       ;;
  88.     redhat | centos)
  89.       for cmd in "${missing[@]}"; do
  90.         if sudo yum install -y "$cmd"; then
  91.           rollback_log "sudo yum remove -y $cmd"
  92.         else
  93.           log "Error: Failed to install $cmd." >&2
  94.           exit 1
  95.         fi
  96.       done
  97.       ;;
  98.     fedora)
  99.       for cmd in "${missing[@]}"; do
  100.         if sudo dnf install -y "$cmd"; then
  101.           rollback_log "sudo dnf remove -y $cmd"
  102.         else
  103.           log "Error: Failed to install $cmd." >&2
  104.           exit 1
  105.         fi
  106.       done
  107.       ;;
  108.     arch)
  109.       for cmd in "${missing[@]}"; do
  110.         if sudo pacman -S --noconfirm "$cmd"; then
  111.           rollback_log "sudo pacman -R --noconfirm $cmd"
  112.         else
  113.           log "Error: Failed to install $cmd." >&2
  114.           exit 1
  115.         fi
  116.       done
  117.       ;;
  118.     suse)
  119.       for cmd in "${missing[@]}"; do
  120.         if sudo zypper install -y "$cmd"; then
  121.           rollback_log "sudo zypper remove -y $cmd"
  122.         else
  123.           log "Error: Failed to install $cmd." >&2
  124.           exit 1
  125.         fi
  126.       done
  127.       ;;
  128.     freebsd)
  129.       for cmd in "${missing[@]}"; do
  130.         if sudo pkg install -y "$cmd"; then
  131.           rollback_log "sudo pkg delete -y $cmd"
  132.         else
  133.           log "Error: Failed to install $cmd." >&2
  134.           exit 1
  135.         fi
  136.       done
  137.       ;;
  138.     darwin)
  139.       for cmd in "${missing[@]}"]; do
  140.         if brew install "$cmd"; then
  141.           rollback_log "brew uninstall $cmd"
  142.         else
  143.           log "Error: Failed to install $cmd." >&2
  144.           exit 1
  145.         fi
  146.       done
  147.       ;;
  148.     *)
  149.       log "Error: Unsupported OS. Please install the missing packages manually." >&2
  150.       exit 1
  151.       ;;
  152.   esac
  153.   wait
  154. }
  155.  
  156. set_permissions() {
  157.   for cmd in "${commands[@]}"; do
  158.     local path; path=$(command -v "$cmd")
  159.     if [[ -n "$path" ]]; then
  160.       log "Setting correct permissions for $path"
  161.       if ! sudo chmod u+rwx "$path"; then
  162.         log "Error: Failed to set permissions for $path." >&2
  163.         exit 1
  164.       fi
  165.     fi
  166.   done
  167. }
  168.  
  169. configure_services() {
  170.   local os="$1"
  171.   case "$os" in
  172.     debian | ubuntu)
  173.       command_exists "apache2" && sudo systemctl enable apache2 && sudo systemctl start apache2 && rollback_log "sudo systemctl disable apache2 && sudo systemctl stop apache2"
  174.       command_exists "telnetd" && sudo systemctl enable inetd && sudo systemctl start inetd && rollback_log "sudo systemctl disable inetd && sudo systemctl stop inetd"
  175.       command_exists "nginx" && sudo systemctl enable nginx && sudo systemctl start nginx && rollback_log "sudo systemctl disable nginx && sudo systemctl stop nginx"
  176.       command_exists "mysql" && sudo systemctl enable mysql && sudo systemctl start mysql && rollback_log "sudo systemctl disable mysql && sudo systemctl stop mysql"
  177.       command_exists "sshd" && sudo systemctl enable ssh && sudo systemctl start ssh && rollback_log "sudo systemctl disable ssh && sudo systemctl stop ssh"
  178.       command_exists "postgresql" && sudo systemctl enable postgresql && sudo systemctl start postgresql && rollback_log "sudo systemctl disable postgresql && sudo systemctl stop postgresql"
  179.       command_exists "redis-server" && sudo systemctl enable redis-server && sudo systemctl start redis-server && rollback_log "sudo systemctl disable redis-server && sudo systemctl stop redis-server"
  180.       command_exists "docker" && sudo systemctl enable docker && sudo systemctl start docker && rollback_log "sudo systemctl disable docker && sudo systemctl stop docker"
  181.       ;;
  182.     redhat | centos | fedora)
  183.       command_exists "httpd" && sudo systemctl enable httpd && sudo systemctl start httpd && rollback_log "sudo systemctl disable httpd && sudo systemctl stop httpd"
  184.       command_exists "telnetd" && sudo systemctl enable telnet.socket && sudo systemctl start telnet.socket && rollback_log "sudo systemctl disable telnet.socket && sudo systemctl stop telnet.socket"
  185.       command_exists "nginx" && sudo systemctl enable nginx && sudo systemctl start nginx && rollback_log "sudo systemctl disable nginx && sudo systemctl stop nginx"
  186.       command_exists "mysqld" && sudo systemctl enable mysqld && sudo systemctl start mysqld && rollback_log "sudo systemctl disable mysqld && sudo systemctl stop mysqld"
  187.       command_exists "sshd" && sudo systemctl enable sshd && sudo systemctl start sshd && rollback_log "sudo systemctl disable sshd && sudo systemctl stop sshd"
  188.       command_exists "postgresql" && sudo systemctl enable postgresql && sudo systemctl start postgresql && rollback_log "sudo systemctl disable postgresql && sudo systemctl stop postgresql"
  189.       command_exists "redis" && sudo systemctl enable redis && sudo systemctl start redis && rollback_log "sudo systemctl disable redis && sudo systemctl stop redis"
  190.       command_exists "docker" && sudo systemctl enable docker && sudo systemctl start docker && rollback_log "sudo systemctl disable docker && sudo systemctl stop docker"
  191.       ;;
  192.     arch | suse)
  193.       command_exists "httpd" && sudo systemctl enable httpd && sudo systemctl start httpd && rollback_log "sudo systemctl disable httpd && sudo systemctl stop httpd"
  194.       command_exists "telnetd" && sudo systemctl enable telnetd && sudo systemctl start telnetd && rollback_log "sudo systemctl disable telnetd && sudo systemctl stop telnetd"
  195.       command_exists "nginx" && sudo systemctl enable nginx && sudo systemctl start nginx && rollback_log "sudo systemctl disable nginx && sudo systemctl stop nginx"
  196.       command_exists "mysqld" && sudo systemctl enable mysqld && sudo systemctl start mysqld && rollback_log "sudo systemctl disable mysqld && sudo systemctl stop mysqld"
  197.       command_exists "sshd" && sudo systemctl enable sshd && sudo systemctl start sshd && rollback_log "sudo systemctl disable sshd && sudo systemctl stop sshd"
  198.       command_exists "postgresql" && sudo systemctl enable postgresql && sudo systemctl start postgresql && rollback_log "sudo systemctl disable postgresql && sudo systemctl stop postgresql"
  199.       command_exists "redis" && sudo systemctl enable redis && sudo systemctl start redis && rollback_log "sudo systemctl disable redis && sudo systemctl stop redis"
  200.       command_exists "docker" && sudo systemctl enable docker && sudo systemctl start docker && rollback_log "sudo systemctl disable docker && sudo systemctl stop docker"
  201.       ;;
  202.     freebsd)
  203.       command_exists "apache24" && sudo sysrc apache24_enable="YES" && sudo service apache24 start && rollback_log "sudo sysrc apache24_enable=\"NO\" && sudo service apache24 stop"
  204.       command_exists "nginx" && sudo sysrc nginx_enable="YES" && sudo service nginx start && rollback_log "sudo sysrc nginx_enable=\"NO\" && sudo service nginx stop"
  205.       command_exists "mysql-server" && sudo sysrc mysql_enable="YES" && sudo service mysql-server start && rollback_log "sudo sysrc mysql_enable=\"NO\" && sudo service mysql-server stop"
  206.       command_exists "openssh" && sudo sysrc sshd_enable="YES" && sudo service sshd start && rollback_log "sudo sysrc sshd_enable=\"NO\" && sudo service sshd stop"
  207.       command_exists "postgresql" && sudo sysrc postgresql_enable="YES" && sudo service postgresql start && rollback_log "sudo sysrc postgresql_enable=\"NO\" && sudo service postgresql stop"
  208.       command_exists "redis" && sudo sysrc redis_enable="YES" && sudo service redis start && rollback_log "sudo sysrc redis_enable=\"NO\" && sudo service redis stop"
  209.       command_exists "docker" && sudo sysrc docker_enable="YES" && sudo service docker start && rollback_log "sudo sysrc docker_enable=\"NO\" && sudo service docker stop"
  210.       ;;
  211.   esac
  212. }
  213.  
  214. handle_edge_cases() {
  215.   local disk_usage; disk_usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
  216.   if [[ -n "$disk_usage" && "$disk_usage" -gt 90 ]]; then
  217.     log "Error: Insufficient disk space. Please free up some space and try again." >&2
  218.     exit 1
  219.   fi
  220.   if ! ping -c 1 google.com >/dev/null 2>&1; then
  221.     log "Error: Network is unreachable. Please check your internet connection and try again." >&2
  222.     exit 1
  223.   fi
  224. }
  225.  
  226. rollback() {
  227.   log "Rolling back changes..."
  228.   tac "$ROLLBACK_FILE" | while read -r line; do
  229.     eval "$line"
  230.   done
  231.   log "Rollback completed."
  232. }
  233.  
  234. trap rollback ERR
  235.  
  236. main() {
  237.   log "Starting script execution"
  238.  
  239.   pre_flight_checks
  240.   handle_edge_cases
  241.  
  242.   local os; os=$(detect_os)
  243.   if [[ "$os" == "unsupported" ]]; then
  244.     log "Error: Unsupported OS. Exiting." >&2
  245.     exit 1
  246.   fi
  247.  
  248.   log "Detected OS: $os"
  249.  
  250.   missing=()
  251.   for cmd in "${commands[@]}"; do
  252.     if ! command_exists "$cmd"; then
  253.       missing+=("$cmd")
  254.     fi
  255.   done
  256.  
  257.   if [[ ${#missing[@]} -ne 0 ]]; then
  258.     log "Missing commands: ${missing[*]}"
  259.     prompt_user "Do you want to attempt to install the missing packages?"
  260.     install_packages "$os"
  261.   else
  262.     log "All required commands are installed."
  263.   fi
  264.  
  265.   set_permissions
  266.  
  267.   prompt_user "Do you want to configure specific services?"
  268.   configure_services "$os"
  269.  
  270.   log "Script execution completed successfully."
  271. }
  272.  
  273. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement