fej/fejctl

119 lines
3.2 KiB
Plaintext
Raw Normal View History

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
# Small wrapper around the docker-compose command
2021-04-15 23:59:31 +02:00
#
# Flags:
# -b: build the builder
# -r: use the release image instead
function dc() {
while getopts ":br" c; do
case $c in
b ) build_builder=1 ;;
r ) release=1 ;;
esac
done
shift $((OPTIND-1))
if [[ "$build_builder" -eq 1 ]]; then
# We always rebuild the builder before we run any compose command
2021-04-15 23:59:31 +02:00
DOCKER_BUILDKIT=1 docker build \
-f docker/Dockerfile.builder \
-t "$image-builder:latest" . || {
>&2 echo "Failed to build builder.";
2021-04-17 13:46:38 +02:00
exit 1;
}
fi
if [[ "$release" -eq 1 ]]; then
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose \
--file docker/docker-compose.yml \
--project-name fej \
"$@"
2021-04-15 22:46:23 +02:00
2021-04-15 23:59:31 +02:00
else
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)
function dcr() {
CMD="$@" dc -b -- up \
--build \
--detach
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
# Build the release images
dc -br build
2021-04-17 13:46:38 +02:00
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
# Building
b | build ) dcr build --bin "$bin" && dc -- logs -f app ;;
br | build-release ) dc -br build ;;
# Running
r | run ) dcr run --bin "$bin" && dc -- logs -f app ;;
rr | run-release ) dc -br -- run --build --detach ;;
s | stop ) dc down ;;
sr | stop-release ) dc -r stop ;;
# Ease of life
psql ) dc -- exec db psql -U fej -d fej ;;
sh ) dc -- exec app sh ;;
# Misc
docs ) cargo doc --no-deps ;;
format ) cargo fmt ;;
l | logs ) dc -- logs -f app ;;
lint ) cargo fmt -- --check ;;
2021-04-15 23:59:31 +02:00
p | push | publish ) publish ;;
t | test ) dc test --no-fail-fast ;;
2021-04-15 23:59:31 +02:00
* ) >&2 echo "Invalid command."; exit 1 ;;
esac
2021-04-15 22:46:23 +02:00
}
main "$@"