2021-04-15 22:46:23 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
image='chewingbever/fej'
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
# Creates the needed images
|
|
|
|
#
|
|
|
|
# $1: wether to build the debug or the release image (default debug)
|
|
|
|
function create_images() {
|
|
|
|
# First, we build the builder
|
|
|
|
DOCKER_BUILDKIT=1 docker build \
|
|
|
|
-f docker/Dockerfile.builder \
|
2021-04-17 13:46:38 +02:00
|
|
|
-t "$image-builder:latest" . || {
|
|
|
|
>&2 echo "Failed to build builder.";
|
|
|
|
exit 1;
|
|
|
|
}
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
if [[ "$1" = "rel" ]]; then
|
|
|
|
DOCKER_BUILDKIT=1 docker build \
|
|
|
|
-t "$image:latest" \
|
2021-04-17 13:46:38 +02:00
|
|
|
-f docker/Dockerfile.rel . || {
|
|
|
|
>&2 echo "Failed to build release image.";
|
|
|
|
exit 1;
|
|
|
|
}
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
else
|
|
|
|
# Then, we create the debug image
|
|
|
|
DOCKER_BUILDKIT=1 docker build \
|
|
|
|
-t "$image:dev" \
|
2021-04-17 13:46:38 +02:00
|
|
|
-f docker/Dockerfile.dev . || {
|
|
|
|
>&2 echo "Failed to build debug image.";
|
|
|
|
exit 1;
|
|
|
|
}
|
2021-04-15 23:59:31 +02:00
|
|
|
fi
|
|
|
|
}
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
# Execute the debug image (must be built first)
|
|
|
|
#
|
|
|
|
# $@: the arguments to pass to the image (passed as arguments to cargo)
|
|
|
|
function run_image() {
|
2021-04-17 13:46:38 +02:00
|
|
|
docker volume create fej_build-cache > /dev/null
|
|
|
|
docker volume create fej_registry-cache > /dev/null
|
|
|
|
docker volume create fej_db-data > /dev/null
|
2021-04-16 23:20:22 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
# Run the database image
|
|
|
|
docker run --rm \
|
|
|
|
--detach \
|
|
|
|
--name fej_db \
|
|
|
|
--network fej \
|
|
|
|
-p 5432:5432 \
|
|
|
|
-e 'POSTGRES_DB=fej' \
|
|
|
|
-e 'POSTGRES_USER=fej' \
|
|
|
|
-e 'POSTGRES_PASSWORD=fej' \
|
|
|
|
-v 'fej_db-data:/var/lib/postgresql/data' \
|
|
|
|
postgres:13-alpine
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
# Run the binary image
|
|
|
|
docker run \
|
|
|
|
--detach \
|
|
|
|
--rm \
|
|
|
|
--interactive \
|
|
|
|
--tty \
|
|
|
|
--publish 8000:8000 \
|
|
|
|
--name fej \
|
|
|
|
--env-file .env.container \
|
|
|
|
--network fej \
|
2021-04-17 13:46:38 +02:00
|
|
|
-v 'fej_build-cache:/app/target' \
|
|
|
|
-v 'fej_registry-cache:/app/.cargo/registry' \
|
2021-04-15 23:59:31 +02:00
|
|
|
"$image:dev" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Attach to the fej container
|
|
|
|
function logs() {
|
|
|
|
docker logs -f fej
|
|
|
|
}
|
|
|
|
|
|
|
|
# Builds the given binary
|
|
|
|
#
|
|
|
|
# $1: the binary to build
|
|
|
|
function build() {
|
|
|
|
create_images
|
|
|
|
run_image build --bin "$1"
|
|
|
|
logs
|
|
|
|
}
|
|
|
|
|
|
|
|
# Runs the given binary
|
|
|
|
#
|
|
|
|
# $1: the binary to run
|
|
|
|
function run() {
|
|
|
|
create_images
|
|
|
|
run_image run --bin "$1"
|
|
|
|
logs
|
|
|
|
}
|
|
|
|
|
|
|
|
# Runs the tests
|
|
|
|
function tests() {
|
|
|
|
create_images
|
|
|
|
run_image test --no-fail-fast
|
|
|
|
logs
|
|
|
|
}
|
|
|
|
|
|
|
|
# Stops both containers
|
|
|
|
function stop() {
|
|
|
|
docker stop fej_db
|
|
|
|
docker stop -t 0 fej
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_release() {
|
2021-04-17 15:10:42 +02:00
|
|
|
docker-compose \
|
|
|
|
--file docker/docker-compose.yml \
|
|
|
|
--project-name fej-release \
|
|
|
|
up \
|
|
|
|
--detach
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_release() {
|
|
|
|
docker-compose \
|
|
|
|
--file docker/docker-compose.yml \
|
|
|
|
--project-name fej-release \
|
|
|
|
down
|
2021-04-15 23:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Tags & pushes the release version to Docker Hub
|
|
|
|
function publish() {
|
|
|
|
branch=`git rev-parse --abbrev-ref HEAD`
|
|
|
|
|
|
|
|
if [[ "$branch" != master ]]; then
|
|
|
|
>&2 echo "You can only publish from master."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2021-04-17 13:46:38 +02:00
|
|
|
create_images rel
|
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1`
|
|
|
|
major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'`
|
|
|
|
minor_version=`echo "$patch_version" | sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/'`
|
|
|
|
tags=("latest" "$patch_version" "$minor_version" "$major_version")
|
|
|
|
|
|
|
|
for tag in "${tags[@]}"; do
|
|
|
|
# Create the tag
|
|
|
|
docker tag "$image:$tags" "$image:$tag"
|
|
|
|
|
|
|
|
# Push the tag
|
|
|
|
docker push "$image:$tag"
|
|
|
|
|
|
|
|
# Remove the tag again, if it's not the main tag
|
|
|
|
[[ "$tag" != "$tags" ]] && docker rmi "$image:$tag"
|
2021-04-15 22:46:23 +02:00
|
|
|
done
|
2021-04-15 23:59:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entrypoint to the script
|
|
|
|
#
|
|
|
|
# $1: action to perform, defaults to 'build'
|
|
|
|
# $2: binary to use, defaults to 'server'
|
|
|
|
function main() {
|
|
|
|
# Default values
|
|
|
|
cmd="${1:-build}"
|
|
|
|
bin="${2:-server}"
|
|
|
|
|
|
|
|
case $cmd in
|
2021-04-16 00:06:48 +02:00
|
|
|
# Building
|
2021-04-15 23:59:31 +02:00
|
|
|
b | build ) build "$bin" ;;
|
|
|
|
br | build-release ) create_images rel ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
|
|
|
|
# Running
|
2021-04-15 23:59:31 +02:00
|
|
|
r | run ) run "$bin" ;;
|
|
|
|
rr | run-release ) run_release ;;
|
|
|
|
s | stop ) stop ;;
|
2021-04-17 15:10:42 +02:00
|
|
|
sr | stop-release ) stop_release ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
|
|
|
|
# Ease of life
|
|
|
|
psql ) docker exec -it fej_db psql -U fej -d fej ;;
|
|
|
|
sh ) docker exec -it fej sh ;;
|
|
|
|
|
|
|
|
# Misc
|
|
|
|
docs ) cargo doc --no-deps ;;
|
|
|
|
format ) cargo fmt ;;
|
|
|
|
l | logs ) logs ;;
|
|
|
|
lint ) cargo fmt -- --check ;;
|
2021-04-15 23:59:31 +02:00
|
|
|
p | push | publish ) publish ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
t | test ) tests ;;
|
2021-04-15 23:59:31 +02:00
|
|
|
* ) >&2 echo "Invalid command."; exit 1 ;;
|
|
|
|
esac
|
2021-04-15 22:46:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|