Big overhaul and bug fixes

- Fixes a bug that prevented getting input from the user in some terminals (https://stackoverflow.com/questions/6883363/read-user-input-inside-a-loop)
- No longer redirects stderr to /dev/null (Shows warnings and errors)
- Fixes checks about docker and debian
- Doesn't export color variables anymore (Idk why I did that in the first place?)
- Messages about successful operations only show if the operation was actually completed successfully
- Docker version, docker-compose version and user id now get printed in color
- Indentation fixes
...and more minor adjustments.
This commit is contained in:
Nicola
2023-02-14 12:31:05 +00:00
committed by GitHub
parent 874ca22f89
commit 1327a4f0e0
+32 -35
View File
@@ -1,92 +1,89 @@
#!/bin/bash #!/bin/bash
export err="\033[1;31m[-]\033[m" err="\033[1;31m[!]\033[m"
export msg="\033[1;32m[+]\033[m" msg="\033[1;32m[+]\033[m"
export info="\033[0;36m[:]\033[m" info="\033[0;36m[:]\033[m"
export ask="\033[1;35m[?]\033[m" ask="\033[0;35m[?]\033[m"
# Make sure the script is run as root # Make sure the script is run as root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "${err} This script must be run as root." 1>&2 echo -e "${err} This script must be run as root."
exit 1 exit 1
fi fi
# Check if Docker is already installed # Check if Docker is already installed
if [[ "$(command -v docker)" -ne "" ]]; then if command -v docker &>/dev/null; then
echo -e "${err} Docker is already installed." echo -e "${err} Docker is already installed."
exit 1; exit 1
fi fi
# Check if machine is Debian-based # Check if machine is Debian-based
if [[ "$(command -v apt)" == "" ]]; then if ! command -v apt-get &>/dev/null; then
echo -e "${err} This script only works on Debian-based machines, sorry!" echo -e "${err} This script only works on Debian-based machines, sorry!"
exit 1; exit 1
fi fi
# Install dependencies # Install dependencies
echo -e "${info} Updating repositories..." echo -e "${info} Updating repositories..."
apt-get update &> /dev/null apt-get update >/dev/null
echo -e "${info} Installing dependencies (this might take a while)..." echo -e "${info} Installing dependencies (this might take a while)..."
apt-get install -y \ apt-get install -y ca-certificates curl gnupg lsb-release >/dev/null
ca-certificates \
curl \
gnupg \
lsb-release &> /dev/null
# Add GPG key # Add GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg &&
echo -e "${msg} Added Docker's GPG key" echo -e "${msg} Added Docker's GPG key"
# Add repository # Add repository
echo \ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list >/dev/null &&
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list &> /dev/null
echo -e "${msg} Added Docker's stable repository" echo -e "${msg} Added Docker's stable repository"
# Install Docker # Install Docker
echo -e "${info} Updating repositories..." echo -e "${info} Updating repositories..."
apt-get update &> /dev/null apt-get update >/dev/null
echo -e "${info} Installing Docker (this might take a while)..." echo -e "${info} Installing Docker (this might take a while)..."
apt-get install -y docker-ce docker-ce-cli containerd.io &> /dev/null apt-get install -y docker-ce docker-ce-cli containerd.io >/dev/null &&
echo -e "${msg} Docker successfully installed:" $(docker --version) echo -e "${msg} Docker successfully installed: \033[0;32m$(docker --version)\033[m"
# Install Docker Compose # Install Docker Compose
while true; do while true; do
read -p "${ask} Do you want to install Docker-Compose? [y/n] " yn echo -e -n "${ask} "
read -p "Do you want to install Docker-Compose? [y/n] " yn </dev/tty
case $yn in case $yn in
[Yy]* ) echo -e "${info} Installing Docker-Compose (this might take a while)..."; [Yy] ) echo -e "${info} Installing Docker-Compose (this might take a while)...";
apt-get install -y docker-compose &> /dev/null; apt-get install -y docker-compose >/dev/null &&
echo -e "${msg} Docker Compose successfully installed:" $(docker-compose --version); echo -e "${msg} Docker Compose successfully installed: \033[0;32m$(docker-compose --version)\033[m"
break;; break;;
[Nn]* ) break;; [Nn] ) break;;
* ) ;; * ) ;;
esac esac
done done
# Create Docker user # Create Docker user
while true; do while true; do
read -p "${ask} Do you want to create a Docker user? [y/n] " yn echo -e -n "${ask} "
read -p "Do you want to create a Docker user? [y/n] " yn </dev/tty
case $yn in case $yn in
[Yy]* ) while :; do [Yy] ) while true; do
read -p "Please choose an ID for the new user/group: " id; echo -e -n "${ask} "
if [[ id "$id" &>/dev/null ]]; then read -p "Please choose an ID for the new user/group: " id </dev/tty;
if id $id &>/dev/null; then
echo -e "${err} An user with the same ID already exists."; echo -e "${err} An user with the same ID already exists.";
continue; continue;
else else
/usr/sbin/groupadd -g $id dockeruser && /usr/sbin/useradd dockeruser -u $id -g $id -m -s /bin/bash && echo -e "${add} Docker user created:" && id dockeruser; /usr/sbin/groupadd -g $id dockeruser && /usr/sbin/useradd dockeruser -u $id -g $id -m -s /bin/bash &&
echo -e "${msg} Docker user created: \033[0;32m$(id dockeruser)\033[m"
break; break;
fi fi
done done
break;; break;;
[Nn]* ) break;; [Nn] ) break;;
* ) ;; * ) ;;
esac esac
done done
# Enable Docker service at startup # Enable Docker service at startup
echo -e "${info} Starting Docker services..." echo -e "${info} Starting Docker services..."
systemctl start docker.service docker.socket containerd && systemctl enable docker.service docker.socket containerd &> /dev/null systemctl start docker.service docker.socket containerd && systemctl enable docker.service docker.socket containerd >/dev/null &&
echo -e "${msg} Docker services started and enabled." echo -e "${msg} Docker services started and enabled."
echo -e "${info} Process completed. Run '\033[0;36msystemctl status docker\033[m' to check Docker's status." echo -e "${info} Process completed. Run '\033[0;36msystemctl status docker\033[m' to check Docker's status."