Moved development stuff to docker-compose

master^2
Jef Roosens 2021-04-17 16:04:04 +02:00
parent 00bf8501dd
commit 43e26191cc
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
5 changed files with 112 additions and 118 deletions

32
Cargo.lock generated
View File

@ -345,6 +345,16 @@ dependencies = [
"syn 1.0.69", "syn 1.0.69",
] ]
[[package]]
name = "diesel_migrations"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c"
dependencies = [
"migrations_internals",
"migrations_macros",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.9.0" version = "0.9.0"
@ -376,6 +386,7 @@ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
"diesel", "diesel",
"diesel_migrations",
"regex", "regex",
"reqwest", "reqwest",
"rocket", "rocket",
@ -854,6 +865,27 @@ version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "migrations_internals"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860"
dependencies = [
"diesel",
]
[[package]]
name = "migrations_macros"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c"
dependencies = [
"migrations_internals",
"proc-macro2 1.0.26",
"quote 1.0.9",
"syn 1.0.69",
]
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.2.6" version = "0.2.6"

View File

@ -7,4 +7,3 @@ COPY --chown=builder:builder ./docker/entrypoint_dev.sh /entrypoint.sh
COPY --chown=builder:builder ./Rocket.toml /app/Rocket.toml COPY --chown=builder:builder ./Rocket.toml /app/Rocket.toml
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]
CMD ["run", "--bin", "server"]

View File

@ -0,0 +1,31 @@
version: '2.4'
services:
app:
build:
# Make sure the build context is one directory up
context: '..'
dockerfile: './docker/Dockerfile.dev'
image: 'chewingbever/fej:dev'
restart: 'no'
container_name: 'fej_app'
volumes:
- 'build-cache:/app/target'
- 'registry-cache:/app/.cargo/registry'
ports:
- '8000:8000'
command: "${CMD}"
db:
container_name: 'fej_db'
restart: 'no'
# the devop environment exposes the database so we can use the Diesel cli
ports:
- '5432:5432'
volumes:
build-cache:
registry-cache:

View File

@ -2,13 +2,13 @@ version: '2.4'
services: services:
app: app:
build:
context: '..'
dockerfile: 'docker/Dockerfile.rel'
image: 'chewingbever/fej:latest' image: 'chewingbever/fej:latest'
restart: 'always' restart: 'always'
depends_on:
db:
condition: 'service_healthy'
environment: environment:
- 'DATABASE_URL=postgres://fej:fej@db:5432/fej' - 'DATABASE_URL=postgres://fej:fej@db:5432/fej'

158
fejctl
View File

@ -2,121 +2,52 @@
image='chewingbever/fej' image='chewingbever/fej'
# Creates the needed images # Small wrapper around the docker-compose command
# #
# $1: wether to build the debug or the release image (default debug) # Flags:
function create_images() { # -b: build the builder
# First, we build the builder # -r: use the release image instead
DOCKER_BUILDKIT=1 docker build \ function dc() {
-f docker/Dockerfile.builder \ while getopts ":br" c; do
-t "$image-builder:latest" . || { case $c in
>&2 echo "Failed to build builder."; b ) build_builder=1 ;;
exit 1; r ) release=1 ;;
} esac
done
shift $((OPTIND-1))
if [[ "$1" = "rel" ]]; then if [[ "$build_builder" -eq 1 ]]; then
# We always rebuild the builder before we run any compose command
DOCKER_BUILDKIT=1 docker build \ DOCKER_BUILDKIT=1 docker build \
-t "$image:latest" \ -f docker/Dockerfile.builder \
-f docker/Dockerfile.rel . || { -t "$image-builder:latest" . || {
>&2 echo "Failed to build release image."; >&2 echo "Failed to build builder.";
exit 1; 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 \
"$@"
else else
# Then, we create the debug image DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose \
DOCKER_BUILDKIT=1 docker build \ --file docker/docker-compose.yml \
-t "$image:dev" \ --file docker/docker-compose.dev.yml \
-f docker/Dockerfile.dev . || { --project-name fej-dev \
>&2 echo "Failed to build debug image."; "$@"
exit 1;
}
fi fi
} }
# Execute the debug image (must be built first) # Execute the debug image (must be built first)
# #
# $@: the arguments to pass to the image (passed as arguments to cargo) # $@: the arguments to pass to the image (passed as arguments to cargo)
function run_image() { function dcr() {
docker volume create fej_build-cache > /dev/null CMD="$@" dc -b -- up \
docker volume create fej_registry-cache > /dev/null --build \
docker volume create fej_db-data > /dev/null --detach
# 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
# Run the binary image
docker run \
--detach \
--rm \
--interactive \
--tty \
--publish 8000:8000 \
--name fej \
--env-file .env.container \
--network fej \
-v 'fej_build-cache:/app/target' \
-v 'fej_registry-cache:/app/.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() {
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
} }
# Tags & pushes the release version to Docker Hub # Tags & pushes the release version to Docker Hub
@ -128,7 +59,8 @@ function publish() {
exit 2 exit 2
fi fi
create_images rel # Build the release images
dc -br build
patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1` patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1`
major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'` major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'`
@ -159,26 +91,26 @@ function main() {
case $cmd in case $cmd in
# Building # Building
b | build ) build "$bin" ;; b | build ) dcr build --bin "$bin" && dc -- logs -f app ;;
br | build-release ) create_images rel ;; br | build-release ) dc -br build ;;
# Running # Running
r | run ) run "$bin" ;; r | run ) dcr run --bin "$bin" && dc -- logs -f app ;;
rr | run-release ) run_release ;; rr | run-release ) dc -br -- run --build --detach ;;
s | stop ) stop ;; s | stop ) dc down ;;
sr | stop-release ) stop_release ;; sr | stop-release ) dc -r stop ;;
# Ease of life # Ease of life
psql ) docker exec -it fej_db psql -U fej -d fej ;; psql ) dc -- exec db psql -U fej -d fej ;;
sh ) docker exec -it fej sh ;; sh ) dc -- exec app sh ;;
# Misc # Misc
docs ) cargo doc --no-deps ;; docs ) cargo doc --no-deps ;;
format ) cargo fmt ;; format ) cargo fmt ;;
l | logs ) logs ;; l | logs ) dc -- logs -f app ;;
lint ) cargo fmt -- --check ;; lint ) cargo fmt -- --check ;;
p | push | publish ) publish ;; p | push | publish ) publish ;;
t | test ) tests ;; t | test ) dc test --no-fail-fast ;;
* ) >&2 echo "Invalid command."; exit 1 ;; * ) >&2 echo "Invalid command."; exit 1 ;;
esac esac
} }