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-17 16:04:04 +02:00
|
|
|
# Small wrapper around the docker-compose command
|
2021-04-15 23:59:31 +02:00
|
|
|
#
|
2021-04-17 16:04:04 +02:00
|
|
|
# Flags:
|
|
|
|
# -b: build the builder
|
|
|
|
# -r: use the release image instead
|
|
|
|
function dc() {
|
2021-04-17 17:46:16 +02:00
|
|
|
local OPTIND c build_builder release
|
|
|
|
|
2021-04-17 16:04:04 +02:00
|
|
|
while getopts ":br" c; do
|
|
|
|
case $c in
|
|
|
|
b ) build_builder=1 ;;
|
|
|
|
r ) release=1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
|
|
if [[ "$release" -eq 1 ]]; then
|
|
|
|
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose \
|
|
|
|
--file docker/docker-compose.yml \
|
2021-04-17 21:39:29 +02:00
|
|
|
--file docker/docker-compose.override.yml \
|
2021-04-17 16:04:04 +02:00
|
|
|
--project-name fej \
|
|
|
|
"$@"
|
2021-04-15 22:46:23 +02:00
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
else
|
2021-04-17 16:04:04 +02:00
|
|
|
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose \
|
|
|
|
--file docker/docker-compose.yml \
|
|
|
|
--file docker/docker-compose.dev.yml \
|
|
|
|
--project-name fej-dev \
|
|
|
|
"$@"
|
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)
|
2021-04-17 16:04:04 +02:00
|
|
|
function dcr() {
|
|
|
|
CMD="$@" dc -b -- up \
|
|
|
|
--build \
|
|
|
|
--detach
|
2021-04-15 23:59:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 09:47:28 +02:00
|
|
|
# Publish the builder image
|
|
|
|
function publish_builder() {
|
|
|
|
DOCKER_BUILDKIT=1 docker build \
|
|
|
|
-f docker/Dockerfile.builder \
|
|
|
|
-t "$image-builder:latest" . || {
|
|
|
|
>&2 echo "Failed to build builder.";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
docker push "$image-builder:latest"
|
|
|
|
}
|
|
|
|
|
2021-04-15 23:59:31 +02:00
|
|
|
# Tags & pushes the release version to Docker Hub
|
|
|
|
function publish() {
|
2021-04-17 17:46:16 +02:00
|
|
|
local branch=`git rev-parse --abbrev-ref HEAD`
|
2021-04-15 23:59:31 +02:00
|
|
|
|
|
|
|
if [[ "$branch" != master ]]; then
|
|
|
|
>&2 echo "You can only publish from master."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2021-04-17 16:04:04 +02:00
|
|
|
# Build the release images
|
|
|
|
dc -br build
|
2021-04-17 13:46:38 +02:00
|
|
|
|
2021-04-17 17:46:16 +02:00
|
|
|
local patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1`
|
|
|
|
local major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'`
|
|
|
|
local minor_version=`echo "$patch_version" | sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/'`
|
|
|
|
local tags=("latest" "$patch_version" "$minor_version" "$major_version")
|
2021-04-15 23:59:31 +02:00
|
|
|
|
|
|
|
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
|
2021-04-17 17:46:16 +02:00
|
|
|
local cmd="${1:-build}"
|
|
|
|
local bin="${2:-server}"
|
2021-04-15 23:59:31 +02:00
|
|
|
|
|
|
|
case $cmd in
|
2021-04-16 00:06:48 +02:00
|
|
|
# Building
|
2021-04-17 16:04:04 +02:00
|
|
|
b | build ) dcr build --bin "$bin" && dc -- logs -f app ;;
|
|
|
|
br | build-release ) dc -br build ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
|
|
|
|
# Running
|
2021-04-17 16:04:04 +02:00
|
|
|
r | run ) dcr run --bin "$bin" && dc -- logs -f app ;;
|
2021-04-17 18:20:59 +02:00
|
|
|
rr | run-release ) dc -br -- up --build --detach && dc -r -- logs -f app ;;
|
2021-04-17 16:04:04 +02:00
|
|
|
s | stop ) dc down ;;
|
|
|
|
sr | stop-release ) dc -r stop ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
|
|
|
|
# Ease of life
|
2021-04-17 16:04:04 +02:00
|
|
|
psql ) dc -- exec db psql -U fej -d fej ;;
|
|
|
|
sh ) dc -- exec app sh ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
|
|
|
|
# Misc
|
|
|
|
docs ) cargo doc --no-deps ;;
|
|
|
|
format ) cargo fmt ;;
|
2021-04-17 16:04:04 +02:00
|
|
|
l | logs ) dc -- logs -f app ;;
|
2021-04-16 00:06:48 +02:00
|
|
|
lint ) cargo fmt -- --check ;;
|
2021-04-15 23:59:31 +02:00
|
|
|
p | push | publish ) publish ;;
|
2021-04-24 09:47:28 +02:00
|
|
|
pb ) publish_builder ;;
|
2021-04-17 21:39:29 +02:00
|
|
|
t | test ) dcr -- test --no-fail-fast && dc -- logs -f app ;;
|
2021-04-15 23:59:31 +02:00
|
|
|
* ) >&2 echo "Invalid command."; exit 1 ;;
|
|
|
|
esac
|
2021-04-15 22:46:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|