Skip to content

Instantly share code, notes, and snippets.

@lilianmoraru
Last active May 20, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilianmoraru/85f687e4167c2385b1ed to your computer and use it in GitHub Desktop.
Save lilianmoraru/85f687e4167c2385b1ed to your computer and use it in GitHub Desktop.
Script to setup vagga on Debian based systems
#! /bin/sh
red_color="\033[1;31m"
green_color="\033[1;32m"
no_color="\033[0m"
throw_error() {
if [ ! -z "$1" ]; then
printf "${red_color}$1${no_color}\n"
fi
printf "\n${red_color}Vagga installation failed!${no_color}\n"
exit 1
}
## Commodity function to ensure that an error is thrown if a command fails to execute
## Usage: run (command) [error message if it fails]
run() {
eval "$1" || throw_error "$2"
}
ensure_tool_is_avaiable() {
if [ -z "$(which $1)" ]; then
echo "Installing $1:"
run \
"sudo apt-get install $1 -y" \
"\
Failed to install $1: check that you have an internet connection or\n\
that you have the rights to run 'sudo apt-get install $1'"
fi
}
ensure_tools_are_available() {
for tool in "$@"; do
ensure_tool_is_avaiable "$1"
done
}
## Installation steps @ref: http://vagga.readthedocs.org/en/latest/installation.html
install_uidmap() {
echo "Copying uidmap(vagga dependency) neccessary to run containers without root rights"
## Copying and installing uidmap
local uidmap_filename="uidmap_4.1.5.1-1ubuntu9_amd64.deb"
rm -f "$uidmap_filename"
run \
"wget -c \"http://gr.archive.ubuntu.com/ubuntu/pool/main/s/shadow/$uidmap_filename\"" \
"\
Failed to download uidmap:\n\
check that (1)you have internet connection, (2)the link is valid"
run \
"sudo dpkg -i \"$uidmap_filename\"" \
"Failed to install uidmap"
rm -f "$uidmap_filename"
## Registering current user
local output="$(id -un):100000:65536"
echo "$output" | sudo tee /etc/subuid /etc/subgid
assert_content() {
if [ -z "$(cat $1 | grep "$output")" ]; then
throw_error "Failed to setup uidmap(vagga dependency): $1 does not contain the expected data"
fi
}
assert_content "/etc/subuid"
assert_content "/etc/subgid"
printf "${green_color}Finished installing uidmap(vagga dependency)${no_color}\n"
}
install_vagga() {
echo "Installing vagga:"
## Downloading the latest stable version of vagga
run \
"vagga_archive_name=\"$(basename $(curl -s http://files.zerogw.com/vagga/latest.html | cut -d '"' -f 2))\"" \
"\
Failed to find out the latest version of vagga:\n\
check that you have internet connection or that the link is correct"
run \
"wget \"http://files.zerogw.com/vagga/$vagga_archive_name\"" \
"Failed to download vagga: check if you have internet connection or that the link is correct"
## Setting up vagga settings file
## Settings file path @ref: http://vagga.readthedocs.org/en/latest/settings.html#global-settings
mkdir -p $HOME/.vagga/.cache
## Chaging the default location of the .vagga folder, from project path -> to home path
echo "storage-dir: $HOME/.vagga" > $HOME/.vagga/settings.yaml
echo "cache-dir: $HOME/.vagga/.cache" >> $HOME/.vagga/settings.yaml
if [ ! -f "$HOME/.vagga/settings.yaml" ]; then
throw_error "Failed to create vagga's settings file"
fi
## Installing vagga
run "tar xf \"$vagga_archive_name\""
rm -f "$vagga_archive_name"
run "cd vagga"
run "sudo ./install.sh"
cd ..
rm -rf vagga
if [ ! -z "$(which vagga)" ]; then
printf "\n${green_color}Finished installing vagga${no_color}\n"
else
throw_error
fi
}
main() {
## Ensuring wget, curl and tar are available
## wget - to copy uidmap and vagga
## curl - to extract the latest stable vagga
## tar - to extract vagga archive
ensure_tools_are_available wget curl tar
## Installing uidmap - a vagga dependency needed to run containers without root rights
install_uidmap
## Installing vagga
install_vagga
}
## Start of the script
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment