You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.1 KiB

#!/usr/bin/env bash
set -euo pipefail
show_help() {
cat << EOF
umbrel-dev 0.0.0
Automatically initialize and manage an Umbrel development environment.
Usage: umbrel-dev <command> [options]
Commands:
help Show this help message
init Initialize an Umbrel development environment in the working directory
boot Boot the development VM
halt Halt the development VM
destroy Destroy the development VM
containers List container services
rebuild <container> Rebuild a container service
logs Stream Umbrel logs
run <command> Run a command inside the development VM
ssh Get an SSH session inside the development VM
EOF
}
# Check required dependencies are installed
# If not, fail with instructions on how to fix
check_dependencies() {
# TODO: Only warn about gnu-sed on macos and properly handle both sed/gsed in $PATH
for cmd in "gsed" "git" "vagrant" "vboxmanage"; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "This script requires gnu-sed Git, VirtualBox and Vagrant to be installed."
echo
echo "You can install them with brew:"
echo
echo " brew install gnu-sed git"
echo " brew cask install vagrant virtualbox"
exit 1
fi
done
}
# Run a command inside the development VM
run_in_vm() {
vagrant ssh -c "cd /vagrant/getumbrel/umbrel && $1"
}
# Get script location and correctly handle any symlinks
get_script_location() {
source="${BASH_SOURCE[0]}"
# Resolve $source until the file is no longer a symlink
while [ -h "$source" ]; do
dir="$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
source="$(readlink "$source")"
# If $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $source != /* ]] && source="$dir/$source"
done
dir="$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
echo $dir
}
check_umbrel_dev_environment() {
filename=".umbrel-dev"
dir=$PWD
while [ ! -e "$dir/$filename" ]; do
dir=${dir%/*}
if [[ "$dir" = "" ]]; then
echo "Error: This doesn't seem to be an umbrel-dev environment."
exit 1
fi
done
}
# Check deps before running any commands
check_dependencies
if [ -z ${1+x} ]; then
command=""
else
command="$1"
fi
# Initialize an Umbrel development environment in the working directory
if [[ "$command" = "init" ]]; then
# List of tuples
# github_repo docker_image
repos=(
"getumbrel/umbrel getumbrel/umbrel"
"getumbrel/umbrel-dashboard getumbrel/dashboard"
"getumbrel/umbrel-manager getumbrel/manager"
"getumbrel/umbrel-middleware getumbrel/middleware"
)
if [[ "$(ls -A)" ]]; then
echo "Working directory must be empty!"
exit 1
fi
echo "Creating Vagrantfile..."
cp "$(get_script_location)/Vagrantfile" .
echo "Cloning container repositories..."
for ((i = 0; i < ${#repos[@]}; i++)); do
repo="${repos[$i]}"
github_repo="$(echo $repo | cut -d' ' -f1)"
docker_repo="$(echo $repo | cut -d' ' -f2)"
echo
git clone "https://github.com/$github_repo.git" "$github_repo"
if [[ "$github_repo" != "getumbrel/umbrel" ]]; then
echo
echo "Patching docker-compose.yml to build $github_repo container from source..."
gsed -i "s#image: $docker_repo\:.*#build: \.\.\/\.\.\/$github_repo#g" getumbrel/umbrel/docker-compose.yml
fi
done
touch .umbrel-dev
echo
echo "Your development environment is now setup"
echo "You can boot your development VM with:"
echo
echo " umbrel-dev boot"
exit
fi
# Boot the development VM
if [[ "$command" = "boot" ]]; then
check_umbrel_dev_environment
vagrant up
exit
fi
# Halt the development VM
if [[ "$command" = "halt" ]]; then
check_umbrel_dev_environment
vagrant halt
exit
fi
# Destroy the development VM
if [[ "$command" = "destroy" ]]; then
check_umbrel_dev_environment
vagrant destroy
exit
fi
# List container services
if [[ "$command" = "containers" ]]; then
check_umbrel_dev_environment
run_in_vm "docker-compose config --services"
exit
fi
# Rebuild a container service
if [[ "$command" = "rebuild" ]]; then
check_umbrel_dev_environment
if [ -z ${2+x} ]; then
echo "A second argument is required!"
exit 1
fi
container="$2"
run_in_vm " \
docker-compose build $container \
&& docker-compose stop $container \
&& docker-compose rm -f $container \
&& docker-compose up -d $container"
exit
fi
# Stream Umbrel logs
if [[ "$command" = "logs" ]]; then
check_umbrel_dev_environment
run_in_vm "docker-compose logs -f"
exit
fi
# Run a command inside the development VM
if [[ "$command" = "run" ]]; then
check_umbrel_dev_environment
if [ -z ${2+x} ]; then
echo "A second argument is required!"
exit 1
fi
run_in_vm "$2"
exit
fi
# Get an SSH session inside the development VM
if [[ "$command" = "ssh" ]]; then
check_umbrel_dev_environment
run_in_vm bash
exit
fi
# If we get here it means no valid command was supplied
# Show help and exit
show_help
exit 1