[#26] Started fejctl

master^2
Jef Roosens 2021-04-15 23:59:31 +02:00
parent 22e9dcceaf
commit 0ba31bd8ba
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
1 changed files with 136 additions and 24 deletions

160
fejctl
View File

@ -1,35 +1,147 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This script is a helpful utility for developing fej inside Docker. image='chewingbever/fej'
# Default values # Creates the needed images
image="chewingbever/fej" #
mode="dev" # Should be either 'rel' or 'dev' # $1: wether to build the debug or the release image (default debug)
action="" function create_images() {
attach="--detach" # First, we build the builder
cmd="b" DOCKER_BUILDKIT=1 docker build \
bin="server" -f docker/Dockerfile.builder \
-t "$image-builder:latest" .
# Calculated variables if [[ "$1" = "rel" ]]; then
patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1` DOCKER_BUILDKIT=1 docker build \
major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'` -t "$image:latest" \
minor_version=`echo "$patch_version" | sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/'` -f docker/Dockerfile.rel .
branch=`git rev-parse --abbrev-ref HEAD`
else
# Then, we create the debug image
DOCKER_BUILDKIT=1 docker build \
-t "$image:dev" \
-f docker/Dockerfile.dev .
fi
}
# Execute the debug image (must be built first)
#
# $@: the arguments to pass to the image (passed as arguments to cargo)
function run_image() {
# 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
function main() { # Run the binary image
# Parse the flags docker run \
while getopts ":i:m:a:l" c; do --detach \
case $c in --rm \
i ) image="$OPTARG" ;; --interactive \
m ) mode="$OPTARG" ;; --tty \
a ) action="$OPTARG" ;; --publish 8000:8000 \
l ) attach="" ;; --name fej \
? ) exit 1 ;; --env-file .env.container \
esac --network fej \
-v 'fej_build-cache:/usr/src/app/target' \
-v 'fej_registry-cache:/root/.cargo/registry' \
"$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() {
echo "Not implemented"
}
# 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
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"
done done
shift $((OPTIND-1))
}
# 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
b | build ) build "$bin" ;;
br | build-release ) create_images rel ;;
r | run ) run "$bin" ;;
rr | run-release ) run_release ;;
p | push | publish ) publish ;;
t | test ) tests ;;
s | stop ) stop ;;
p | push | publish ) publish ;;
* ) >&2 echo "Invalid command."; exit 1 ;;
esac
} }
main "$@" main "$@"