Bash script: Installs Docker, Skaffold, Minikube, and kubectl on a Linux system - bash

sims installation process is ok, but when i'm starting minikube i'm getting error:
Exiting due to DRV_NOT_HEALTHY: Found driver(s) but none were healthy. See above for suggestions how to fix installed drivers.
and then when i'm manually running command that i already have in script:
sudo usermod -aG docker $USER && newgrp docker
minikube starts...
any suggestions? here is whole script:
#!/bin/bash
function run_command {
local cmd="$1"
local spinner_text="$2"
echo -n "$spinner_text"
{
eval "$cmd" &>/dev/null
} &
local spinner=$!
local spin='-\|/'
local i=0
while kill -0 $spinner 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r$spinner_text ${spin:$i:1}"
sleep .1
done
wait $spinner
local exit_code=$?
if [[ $exit_code != 0 ]]; then
echo "Error: $cmd failed with exit code $exit_code."
exit $exit_code
fi
echo "Done."
}
# Update package lists
run_command "apt-get update" "Updating package lists... "
# Install Docker
run_command "apt-get install -y docker.io" "Installing Docker... "
# Install Skaffold
run_command "curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 && chmod +x skaffold && sudo mv skaffold /usr/local/bin" "Installing Skaffold... "
# Install Minikube
run_command "curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin" "Installing Minikube... "
# Install kubectl
run_command "curl -LO https://storage.googleapis.com/kubernetes-release/release/\`curl -L https://storage.googleapis.com/kubernetes-release/release/stable.txt\`/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/" "Installing kubectl... "
# Add the current user to the Docker group
run_command "sudo usermod -aG docker $USER && newgrp docker" "Adding user to Docker group... "
# Add virtual host to /etc/hosts
run_command "echo '192.168.49.2 hcms.dev' | sudo tee -a /etc/hosts" "Adding virtual host to /etc/hosts... "
echo "Installation complete."

Related

Install nvm inside Docker using bash as source for Docker's commands

When using bash to execute "docker exec" commands, I am trying to install nvm inside the Docker container. Those are the following commands inside the bash script:
Update:
I've added the whole script.
#!/bin/bash
ubuntu_version="ubuntu:$1"
node_version=$2
container_name="ubuntu_container"
green=`tput setaf 2`
reset=`tput sgr0`
red=`tput setaf 1`
echo "${green}Pulling Ubuntu version: $ubuntu_version ${reset}"
docker pull $ubuntu_version
echo "${green}Running Ubuntu in background...${reset}"
# Stop and remove an existing docker container
docker stop $container_name
docker rm $container_name
docker run -d --name $container_name --rm $ubuntu_version sleep inf
echo "${green}Updating Ubuntu...${reset}"
docker exec $container_name apt update
docker exec $container_name apt upgrade
echo "${green}Installing curl${reset}"
docker exec $container_name apt install -y curl
echo "${green}Installing nvm: ${red}$node_version${reset}"
docker exec -it $container_name bash -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash"
docker exec $container_name bash -c ". ~/.bashrc; nvm"
This leads to the following output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15037 100 15037 0 0 54481 0 --:--:-- --:--:-- --:--:-- 54481
=> Downloading nvm as script to '/root/.nvm'
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "nvm": executable file not found in $PATH: unknown
Seems like nvm is either not in the path or not installed. What am I missing here?
The problem is with the third docker exec.
You have two options
Run :
docker exec -it test_container bash
in bash session, you can run nvm
Or
Run :
docker exec test_container bash -c '. ~/.bashrc; nvm'
Update
Regarding 1), I thought(wrongly) you were running your docker exec manually.
Regarding 2), save follwoing in test.sh :
#!/bin/bash
docker run -d --name test_container2 --rm node sleep inf
docker exec test_container2 apt install -y curl
docker exec -it test_container2 bash -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash"
docker exec test_container2 bash -c '. ~/.bashrc; nvm'
and run bash test.sh
When I run it, I don't have nvm: command not found
Update 2
Your version of ~/.bashrc contains a test on $PS1, so a workaround, change the last line to :
docker exec $container_name bash -c "PS1=x; . ~/.bashrc; nvm"

Softether VPN Setup: Automatic Script executed from git

Am kind of new to bash scripting but i was wondering if it is possible to create automatic installations script for softether vpn server on raspberry pi to be executed from git and if it is possible how do you create one as I cant find any of the tutorial online to implement what i want, I tried to create mine and I failed miserably. any help will be appreciated.Thanks
sudo su
cd /root/
wget https://www.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/Linux/SoftEther_VPN_Server/32bit_-_ARM_EABI/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-arm_eabi-32bit.tar.gz
tar xzf softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-arm_eabi-32bit.tar.gz && rm softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-arm_eabi-32bit.tar.gz
cd vpnserver && sudo make
cd ..
sudo mv vpnserver /usr/local && cd /usr/local/vpnserver/
sudo chmod 600 *
sudo chmod 700 vpnserver vpncmd
sudo ./vpnserver start
sudo ./vpncmd
ServerPasswordSet
echo net.ipv4.ip_forward = 1 | ${SUDO} tee -a /etc/sysctl.conf
echo net.ipv6.ip_forward = 1 | ${SUDO} tee -a /etc/sysctl.conf
sudo cat >> /etc/init.d/vpnserver << EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: vpnserver
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: SoftEther VPN 1.0 RC2
# Description: Start vpnserver daemon SoftEther VPN 1.0 Server
### END INIT INFO
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/vpnserver
. /lib/lsb/init-functions
test -x $DAEMON || exit 0
case "$1" in
start)
sleep 3
log_daemon_msg "Starting SoftEther VPN 1.0 Server" "vpnserver"
$DAEMON start >/dev/null 2>&1
touch $LOCK
log_end_msg 0
sleep 3
;;
stop)
log_daemon_msg "Stopping SoftEther VPN 1.0 Server" "vpnserver"
$DAEMON stop >/dev/null 2>&1
rm $LOCK
log_end_msg 0
sleep 2
;;
restart)
$DAEMON stop
sleep 2
$DAEMON start
sleep 5
;;
status)
if [ -e $LOCK ]
then
echo "vpnserver is running."
else
echo "vpnserver is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
EOF
sudo chmod 755 /etc/init.d/vpnserver
sudo update-rc.d vpnserver defaults
systemctl enable vpnserver
sudo /etc/init.d/vpnserver start
sudo /etc/init.d/vpnserver stop
sudo /etc/init.d/vpnserver restart
wget -qO- https://ipecho.net/plain ; echo

Bash Script with Several Nested elif Statements Not Working

I am trying to create a script I can provide to those who will use an academic game I am making. I am trying to do the following:
Verify that Apache2 is installed and Running
If Apache2 is installed, move a folder containing the website files to /var/www/html while backing up apache's original index.html
The code is as follows:
#!/bin/sh
acm=[]
cnn=[]
gnu=[]
ieee=[]
if [pgrep -x "apache2" > /dev/null]; then
echo "Apache 2 Installed and Running!"
if [ -d "$HOME/acmDL" ]; then
sudo mkdir /var/www/bak
sudo mv /var/www/html/index.html /var/www/bak
sudo mv acmDL /var/www/html/
cd /var/www/html/
sudo mv acmDL/* .
exit
elif [ -d "$HOME/cnnDL" ]; then
sudo mkdir /var/www/bak
sudo mv /var/www/html/index.html /var/www/bak
sudo mv cnnDL /var/www/html/
cd /var/www/html/
sudo mv cnnDL/* .
exit
elif [ -d "$HOME/gnuDL" ]; then
sudo mkdir /var/www/bak
sudo mv /var/www/html/index.html /var/www/bak
sudo mv gnuDL /var/www/html/
cd /var/www/html/
sudo mv gnuDL/* .
exit
elif [ -d "$HOME/ieeeDL" ]; then
sudo mkdir /var/www/bak
sudo mv /var/www/html/index.html /var/www/bak
sudo mv ieeeDL /var/www/html/
cd /var/www/html/
sudo mv ieeeDL/* .
exit
else
echo "Provided websites not found... Are you using a custom website?"
fi
else
echo "Please check apache2... It may not have installed correctly"
fi
The error I keep getting is syntax error near unexpected token `elif' on line 15.
As you can see, I even tried moving the boolean expression [ - d "$HOME/site" ] to their own variables, but then the error becomes : -d: command not found and the error on line 15.
Is what I am trying to do impossible, or am I missing something undocumented and yet completely obvious (like a handful of my previous posts)?
This is being run on a minimal installation of Ubuntu 18 on a Virtual Machine. The site directories are shared by Filezilla. Script written in Notepad++ on Windows 7 x64.
First of all, can you rewrite it like this?
Please tell me the execution result.
This is wrong.
if [pgrep -x "apache2" > /dev/null]; then
This is correct.
pgrep -x "apache2" > /dev/null
if [ $? -eq 0 ]; then

Is the Syntax for the Bash Script right for an if elif statement

I added another condition to my if, elseif condition for my bash shell script. I am new to shell scripting, can you guys review my code if my syntax for if conditions are right especially the "fi" implementation. Much appreciated.
if [ -f /etc/system-release ] && grep Amazon /etc/system-release > /dev/null; then
cd /tmp
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
else
# we're either RedHat or Ubuntu
DISTRIBUTOR=`lsb_release -is`
DISTRIBUTOR2=`lsb_release -cs`
if [ "trusty" == $DISTRIBUTOR2 ]; then
cd /tmp
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
sudo dpkg -i amazon-ssm-agent.deb
sudo start amazon-ssm-agent
elif [ "RedHatEnterpriseServer" == $DISTRIBUTOR ]; then
cd /tmp
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
elif [ "xenial" == $DISTRIBUTOR2 ]; then
cd /tmp
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
sudo dpkg -i amazon-ssm-agent.deb
sudo systemctl enable amazon-ssm-agent
fi
fi
sleep 10
Looks basically ok, but https://www.shellcheck.net/ has a couple of comments that you should probably address.

How do I uninstall docker-compose?

I installed docker-compose by following the official documentation: http://docs.docker.com/compose/install/
Now I want to uninstall docker-compose.
$ docker-compose -h
offers no command for uninstalling, nor does the official documentation offer any instructions.
I deleted my docker-compose.yml file and /usr/local/bin/docker-compose, but I want to make sure that's everything.
I'm using OSX Yosemite 10.10.3 on a MacbookPro.
EDIT: Regarding the installation instructions I followed, I didn't use pip. I used the documented curl command:
$ curl -L https://github.com/docker/compose/releases/download/1.3.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Documenation
Please note that this is now in the docs.
Coupled Installation and Removal
Note: on Mac Docker now installs Docker Compose. So the strategy for removal has changed a bit. If you uninstall Docker, and you want to uninstall both, then you also uninstall Docker Compose.
Individual Removal if Installed Using curl
It is commonly installed to /usr/local/bin/docker-compose on macs. However, you can run which docker-compose to find the exact location.
Run the following command (*nix systems) to remove:
rm $(which docker-compose)
If you get a permission denied error then you will need to prepend sudo:
sudo rm $(which docker-compose)
To verify that it was successful, run the following command which should return nothing:
which docker-compose
It should say that the command wasn't found.
Individual Removal if Installed Using PIP
If you've installed Docker Compose using PIP then you can run:
pip uninstall docker-compose
You may have to use sudo if you get a permission denied error:
sudo pip uninstall docker-compose
first get docker path by:
which docker-compose
then it will return path like:/usr/bin/docker-compose
then remove it:
sudo rm -rf /usr/bin/docker-compose
Nowadays docker-compose is part of the docker toolbox.
If you want to remove everything that comes with the Docker Toolbox (including Docker itself).
You can execute this shell script:
#!/bin/bash
# Uninstall Script
if [ "${USER}" != "root" ]; then
echo "$0 must be run as root!"
exit 2
fi
while true; do
read -p "Remove all Docker Machine VMs? (Y/N): " yn
case $yn in
[Yy]* ) docker-machine rm -f $(docker-machine ls -q); break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no."; exit 1;;
esac
done
echo "Removing Applications..."
rm -rf /Applications/Docker
echo "Removing docker binaries..."
rm -f /usr/local/bin/docker
rm -f /usr/local/bin/docker-machine
rm -r /usr/local/bin/docker-machine-driver*
rm -f /usr/local/bin/docker-compose
echo "Removing boot2docker.iso"
rm -rf /usr/local/share/boot2docker
echo "All Done!"
If you still have the depreciated Boot2docker and you want to get rid of it as well.
You can uninstall it by executing the following shell script:
#!/bin/bash
# Uninstall Script
if [ "$(which boot2docker)" == "" ]; then
echo "boot2docker does not exist on your machine!"
exit 1
fi
if [ "${USER}" != "root" ]; then
echo "$0 must be run as root!"
exit 2
fi
echo "Stopping boot2docker processes..."
boot2docker stop && boot2docker delete
echo "Removing boot2docker executable..."
rm -f /usr/local/bin/boot2docker
echo "Removing boot2docker ISO and socket files..."
rm -rf ~/.boot2docker
rm -rf /usr/local/share/boot2docker
echo "Removing boot2docker SSH keys..."
rm -f ~/.ssh/id_boot2docker*
echo "Removing boot2docker OSX files..."
rm -f /private/var/db/receipts/io.boot2docker.*
rm -f /private/var/db/receipts/io.boot2dockeriso.*
echo "Removing Docker executable..."
rm -f /usr/local/bin/docker
echo "All Done!"
You can also just:
sudo yum remove docker-compose-plugin
on RPM-based like Centos
according to: https://docs.docker.com/compose/install/uninstall/
I would use pip uninstall docker-compose

Resources