#!/bin/bash #*************************# # RELEASER:check # #*************************# # Designed & Developed by # # Adrien Marquès # # # #*************************# # git.xdrm.io # #*************************# S="20" inline_progress(){ # 1. init arguments text="" size="$S" sep=" " # 2. extract text test ! -z "$1" && text="$1"; # 3. extract size test ! -z "$2" && test "`expr $2 + 0 2>/dev/null`" = "$2" && size="$2"; # 4. Write text len="`echo -n \"$text\" | wc -m`"; echo -n "$text"; for i in `seq $len $size`; do echo -n "$sep"; done; return; } # (1) Check if requirements are installed #--------------------------------------------------------# echo -e "\e[33m>\e[0m (1) required programs \e[33m<\e[0m" requirements="curl git docker docker-compose make" for r in $requirements; do inline_progress " . $r"; if which $r >/dev/null 2>&1; then echo "ok"; else echo "missing"; echo -e "\n\e[31m/!\\\\\e[0m you must install \e[33m$r\e[0m."; echo -e " this program is required to install the project"; exit 1; fi; done; # (2) Check docker permissions #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (2) docker \e[33m<\e[0m"; # (1) docker permissions # inline_progress " . group"; if groups | grep 'docker' >/dev/null 2>&1; then echo "ok"; else echo "missing"; echo; echo -e " you might add the current user to the \e[33mdocker\e[0m group !"; echo -e "\e[31m/!\\\\\e[0m it is safer for administrative tasks. You might want"; echo -e " to avoid using 'root' for docker."; exit 1; fi; # (3) find out http installer #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (3) http installer \e[33m<\e[0m"; inline_progress " * http server"; echo; # 1. Check for installed candidates existing="apache2 httpd nginx" candidates=""; ncandidates="0" i="0" for current in $existing; do inline_progress " . $current"; if which $current >/dev/null 2>&1; then echo "ok"; test -z "$candidates" && candidates="$current" || candidates="$candidates $current"; ncandidates="`expr $ncandidates + 1`"; else echo ".."; fi; i="`expr $i + 1`"; done; # 2. fail if no candidate if [ "$ncandidates" -lt 1 ]; then echo; echo -e " no http server has been found, you might install one."; echo -e "\e[31m/!\\\\\e[0m the installer supports the most common ones: \e[33mnginx\e[0m" echo -e " and \e[33mapache\e[0m (httpd)"; exit 1; # 3. only one candidate elif [ "$ncandidates" -eq 1 ]; then echo; echo -e " \e[33m$candidates\e[0m http server has been found. a reverse proxy will be" echo -e "\e[34m(!)\e[0m installed in order for the project to be remotely accessible."; echo -e " do you want to use $candidates ? \e[33my\e[0m/\e[33mn\e[0m"; read -p " > " -n1 install; echo; case "$install" in Y|y) ;; *) echo -e "\n\e[31m/!\\\\\e[0m aborting installation"; exit 1;; esac; http_installer="$candidates"; # 4. multiple candidates else echo; echo -e " multiple http server candidates have been found. a reverse proxy will be" echo -e "\e[34m(!)\e[0m installed in order for the project to be remotely accessible."; echo -e " please choose the http server one you want to use:" echo -e " (0) \e[33mabort\e[0m" i="1"; for c in $candidates; do echo -e " ($i) \e[33m$c\e[0m"; i="`expr $i + 1`"; done; read -p " > " -n1 install; echo; # 4.1. Abort (or invalid) if [ "`expr $install + 0 2>/dev/null`" != "$install" -o "$install" = "0" ]; then echo -e "\n\e[31m/!\\\\\e[0m aborting installation"; exit 1; elif [ "$install" -gt "$ncandidates" ]; then echo -e "\n\e[31m/!\\\\\e[0m aborting installation"; exit 1; fi; http_installer=$(echo $candidates | awk "{print \$$install}") fi; echo; # 5. 'httpd' is service name, but config dir is apache2 http_root="/etc/$http_installer"; test "$http_installer" = "httpd" && http_root="/etc/apache2"; # 6. configuration root dir inline_progress " . config root"; if [ -d "$http_root" ]; then echo "ok"; else echo "missing"; echo -e "\n\e[31m/!\\\\\e[0m cannot find configuration folder \e[33m$http_root\e[0m. aborting installation"; exit 1; fi; # 7. configuration structure inline_progress " . config tree"; if [ -d "$http_root/sites-available" -a -d "$http_root/sites-enabled" ]; then echo "ok"; else echo "invalid"; echo -e "\n invalid configuration structure. You might have \e[33m$http_root/sites-available\e[0m containing"; echo -e "\e[31m/!\\\\\e[0m your configurations and \e[33m$http_root/sites-enabled\e[0m containing symbolic links"; echo -e " only for activated configurations. aborting installation"; exit 1; fi; # (4) information disclaimer #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (4) information \e[33m<\e[0m"; echo; echo -e " \e[34m(!)\e[0m (1) the project will run inside docker containers (3 containers)."; echo; echo -e " \e[34m(!)\e[0m (2) the only links it has with the host machine (this machine) are:"; echo; echo -e " \e[34m(!)\e[0m - \e[34mweb files\e[0m (that will be shared between multiple containers)"; echo -e " \e[34m(!)\e[0m - \e[34menvironment files\e[0m (for continuous integration)"; echo -e " \e[34m(!)\e[0m - \e[34ma listening socket\e[0m (to accept incoming http requests)"; echo; echo -e " \e[34m(!)\e[0m (3) in order for incoming requests to follow the path:"; echo -e " \e[34m(!)\e[0m [remote] => [http server] => [docker]"; echo; echo -e " \e[34m(!)\e[0m we will install a reverse proxy on the http server ($http_installer) to"; echo -e " \e[34m(!)\e[0m automatically redirect requests from a specific \e[34m\e[0m to docker's"; echo -e " \e[34m(!)\e[0m listening \e[34m\e[0m."; echo; echo -e " \e[34m(!)\e[0m WARNING: The \e[34m\e[0m must be available for your server and be a sub-domain"; echo -e " \e[34m(!)\e[0m in order for your DNS to be valid."; echo; echo -e " \e[34m(!)\e[0m EXAMPLE: If your server has \e[33mexample.net\e[0m as a DNS."; echo -e " \e[34m(!)\e[0m the \e[34m\e[0m \e[33mtest.example.net\e[0m is valid"; echo -e " \e[34m(!)\e[0m the \e[34m\e[0m \e[33mtest.google.com\e[0m is not valid"; # (5) prompt server variables #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (5) redirection variables \e[33m<\e[0m"; # 1. Port minport="1024" maxport="49151" echo -e " * what port do you want docker to use ? (between $minport and $maxport)"; echo -e " ( values other than \e[33m8080\e[0m may be unstable )"; echo -en " \e[34m\e[0m = "; read port; if [ -z "$port" -o "`expr $port + 0 2>/dev/null`" != "$port" ]; then echo; echo -e "\e[31m/!\\\\\e[0m invalid port number. aborting installation" exit 1; elif [ "$port" -lt $minport -o "$port" -gt $maxport ]; then echo; echo -e "\e[31m/!\\\\\e[0m port out of range. aborting installation" exit 1; fi; # 2. Host echo -e " * what host do you want to redirect to docker ?"; echo -en " \e[34m\e[0m = "; read host; if [ -z "$host" ]; then echo; echo -e "\e[31m/!\\\\\e[0m invalid host. aborting installation" exit 1; fi; # 3. Unlock permissions echo -n " * sudo access "; if sudo echo "granted"; then echo -n ""; else echo; echo -e "\e[31m/!\\\\\e[0m permission issue. you must launch the installer with a sudoer. aborting installation" exit 1; fi; # (6) pre-install: reverse-proxy #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (6) reverse proxy \e[33m<\e[0m"; # (1) LOCAL DNS # inline_progress " . local dns"; # 1.1. remove previously generated + append new line cat /etc/hosts | grep -vP "# generated by: ptut installer" > /tmp/installer_hosts; echo -e "127.0.0.1\t$host\t# generated by: ptut installer" | sudo tee -a /tmp/installer_hosts >/dev/null; sudo cp /etc/hosts /etc/hosts.backup; sudo mv /tmp/installer_hosts /etc/hosts; c2="$?"; # 1.2. manage errors if [ "$c2" = "0" ]; then echo "ok"; else echo; echo -e "\e[31m/!\\\\\e[0m cannot write to /etc/hosts. skipping this step." fi; inline_progress " * $http_installer"; echo; # 1. Copy configuration echo -n " - create proxy configuration "; if [ "$http_installer" = "apache2" -o "$http_installer" = "httpd" ]; then echo """ ServerName $host ServerAlias www.$host ProxyPreserveHost On ProxyRequests off ProxyPass / http://127.0.0.1:$port/ ProxyPassReverse / http://127.0.0.1:$port/ """ | sudo tee $http_root/sites-available/$host.reverse-proxy > /dev/null; elif [ "$http_installer" = "nginx" ]; then echo """server { listen 80; server_name $host www.$host; location / { proxy_set_header Host \$host; proxy_pass http://127.0.0.1:$port; } }""" | sudo tee $http_root/sites-available/$host.reverse-proxy > /dev/null; else echo "error"; echo -e "\n\e[31m/!\\\\\e[0m no installer matches '$http_installer'. aborting installation"; exit 1; fi; # manage error if [ "$?" = "0" ]; then echo "ok"; else echo "error"; echo -e "\n\e[31m/!\\\\\e[0m cannot write to $http_root/sites-availables/. aborting installation"; exit 1; fi; echo -n " - enable proxy "; sudo ln -fs ../sites-available/$host.reverse-proxy $http_root/sites-enabled/$host.reverse-proxy; echo "ok"; echo -n " - reload server "; sudo systemctl is-active $http_installer || sudo systemctl start $http_installer; sudo systemctl reload $http_installer; echo "ok"; # (7) clone sources #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (7) fetching \e[33m<\e[0m"; # 1. Clone inline_progress " . cloning"; git clone --recursive https://git.xdrm.io/ptut/virtenv.git $host; if [ "$?" = "0" ]; then echo "ok"; else echo "error"; echo -e "\n\e[31m/!\\\\\e[0m cannot clone sources to ./$host. aborting installation"; exit 1; fi; # 2. Fetch database inline_progress " . fetch data"; mkdir -p ./$host/metactl/persistent \ && curl https://xdrm.io/script/ptut-database > ./$host/metactl/persistent/mariadb.sql; if [ "$?" = "0" ]; then echo "ok"; else echo "error"; echo -e "\n\e[31m/!\\\\\e[0m cannot fetch data. aborting installation"; exit 1; fi; # 3. Set custom port inline_progress " . port $port"; cat ./$host/docker-compose.yml | sed "s/8080:80/$port:80/" > /tmp/docker-compose-new-port; mv /tmp/docker-compose-new-port ./$host/docker-compose.yml; echo "ok"; # (8) make installer #--------------------------------------------------------# echo; echo -e "\e[33m>\e[0m (8) installer \e[33m<\e[0m"; make -C ./$host start;