fej/fejctl

128 lines
3.5 KiB
Plaintext
Raw Permalink 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'
web_dir='web'
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() {
2021-04-17 17:46:16 +02:00
local OPTIND c build_builder release
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 \
--file docker/docker-compose.override.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
}
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
# 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
# Building
b | build ) dcr build --bin "$bin" && dc -- logs -f app ;;
br | build-release ) dc -br build ;;
bf | build-frontend ) cd "$web_dir" && yarn run build ;;
# Running
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 ;;
rf | run-frontend ) dcr run --bin server && cd "$web_dir" && yarn run dev ;;
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 ;;
2021-04-24 09:47:28 +02:00
pb ) publish_builder ;;
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 "$@"