From 309e1d6402dc51c02464898fae65b978ab7ce9ed Mon Sep 17 00:00:00 2001 From: pushrbx Date: Sun, 20 Aug 2023 22:35:02 +0100 Subject: [PATCH] wip - container setup cli and fixed some issues with docker compose yml (still buggy) --- .dockerignore | 4 ++ Dockerfile | 6 +-- app/Console/Kernel.php | 32 ++++--------- container-setup.sh | 101 +++++++++++++++++++++++++++++++++-------- docker-compose.yml | 21 +++++---- docker-entrypoint.php | 2 + storage/app/.gitignore | 2 + 7 files changed, 117 insertions(+), 51 deletions(-) mode change 100644 => 100755 container-setup.sh diff --git a/.dockerignore b/.dockerignore index 4222f42..2fb6556 100644 --- a/.dockerignore +++ b/.dockerignore @@ -23,3 +23,7 @@ db_username.txt db_password.txt redis_password.txt typesense_api_key.txt +.phpunit.result.cache +.env +.env.dist +docker-compose.yml diff --git a/Dockerfile b/Dockerfile index 642d831..9ec08f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM spiralscout/roadrunner:2.12.2 as roadrunner -FROM composer:2.5.1 as composer -FROM mlocati/php-extension-installer:1.5.52 as php-ext-installer +FROM docker.io/spiralscout/roadrunner:2.12.2 as roadrunner +FROM docker.io/composer:2.5.1 as composer +FROM docker.io/mlocati/php-extension-installer:1.5.52 as php-ext-installer FROM php:8.1.16-bullseye COPY --from=composer /usr/bin/composer /usr/bin/composer COPY --from=php-ext-installer /usr/bin/install-php-extensions /usr/local/bin/ diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 59f915a..22c9b19 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,20 +2,8 @@ namespace App\Console; -use App\Console\Commands\ClearQueuedJobs; use App\Console\Commands\CacheRemove; -use App\Console\Commands\Indexer\AnimeIndexer; -use App\Console\Commands\Indexer\AnimeSweepIndexer; -use App\Console\Commands\Indexer\AnimeScheduleIndexer; -use App\Console\Commands\Indexer\CommonIndexer; -use App\Console\Commands\Indexer\CurrentSeasonIndexer; -use App\Console\Commands\Indexer\GenreIndexer; -use App\Console\Commands\Indexer\MangaIndexer; -use App\Console\Commands\Indexer\MangaSweepIndexer; -use App\Console\Commands\Indexer\ProducersIndexer; -use App\Console\Commands\ManageMicrocaching; -use App\Console\Commands\ModifyCacheDriver; -use App\Console\Commands\ModifyCacheMethod; +use App\Console\Commands\Indexer; use Illuminate\Console\Scheduling\Schedule; use Laravel\Lumen\Console\Kernel as ConsoleKernel; @@ -28,15 +16,15 @@ class Kernel extends ConsoleKernel */ protected $commands = [ CacheRemove::class, - CommonIndexer::class, - AnimeScheduleIndexer::class, - CurrentSeasonIndexer::class, - AnimeIndexer::class, - MangaIndexer::class, - GenreIndexer::class, - ProducersIndexer::class, - AnimeSweepIndexer::class, - MangaSweepIndexer::class, + Indexer\CommonIndexer::class, + Indexer\AnimeScheduleIndexer::class, + Indexer\CurrentSeasonIndexer::class, + Indexer\AnimeIndexer::class, + Indexer\MangaIndexer::class, + Indexer\GenreIndexer::class, + Indexer\ProducersIndexer::class, + Indexer\AnimeSweepIndexer::class, + Indexer\MangaSweepIndexer::class ]; /** diff --git a/container-setup.sh b/container-setup.sh old mode 100644 new mode 100755 index 17d2376..03f52ca --- a/container-setup.sh +++ b/container-setup.sh @@ -1,14 +1,22 @@ #!/bin/bash -JIKAN_API_VERSION=v4.0.0-rc.11 -DOCKER_COMPOSE_PROJECT_NAME=jikan-api-$JIKAN_API_VERSION +_JIKAN_API_VERSION=v4.0.0 +SUBSTITUTE_VERSION=$_JIKAN_API_VERSION +if [ -x "$(command -v git)" ]; then + SUBSTITUTE_VERSION=$(git describe --tags | sed -e "s/-[a-z0-9]\{8\}/-$(git rev-parse --short HEAD)/g") +fi +export _JIKAN_API_VERSION=${JIKAN_API_VERSION:-$SUBSTITUTE_VERSION} + +DOCKER_COMPOSE_PROJECT_NAME=jikan-api-$_JIKAN_API_VERSION +DOCKER_CMD="docker" +DOCKER_COMPOSE_CMD="docker-compose" display_help() { echo "============================================================" echo "Jikan API Container Setup CLI" echo "============================================================" echo "Syntax: ./container-setup.sh [command]" - echo "Jikan API Version: $JIKAN_API_VERSION" + echo "Jikan API Version: $_JIKAN_API_VERSION" echo "---commands---" echo "help Print CLI help" echo "build-image Build Image Locally" @@ -19,27 +27,80 @@ display_help() { } validate_prereqs() { - docker -v >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo -e "'docker' is not installed or not runnable without sudo. \xE2\x9D\x8C" + docker_exists=$(command -v docker) + docker_compose_exists=$(command -v docker-compose) + podman_exists=$(command -v podman) + podman_compose_exists=$(command -v podman-compose) + + if [ -x "$docker_exists" ] && [ -x "$podman_exists" ]; then + echo -e "'docker' is not installed. \xE2\x9D\x8C" + exit 1 else echo -e "Docker is Installed. \xE2\x9C\x94" fi - docker-compose -v >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo -e "'docker-compose' is not installed. \xE2\x9D\x8C" - else - echo -e "Docker compose is Installed. \xE2\x9C\x94" + if [ -x "$docker_exists" ]; then + DOCKER_CMD="docker" + docker -v >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo -e "'docker' is not executable without sudo. \xE2\x9D\x8C" + exit 1 + fi + elif [ -n "$podman_exists" ]; then + DOCKER_CMD="podman" fi + + if [ -x "$docker_compose_exists" ] && [ -x "$docker_compose_exists" ]; then + echo -e "'docker-compose' is not installed. \xE2\x9D\x8C" + exit 1 + else + echo -e "Docker compose is Installed. \xE2\x9C\x94" + fi + + if [ -x "$docker_compose_exists" ]; then + DOCKER_COMPOSE_CMD="docker-compose" + elif [ -x "$podman_compose_exists" ]; then + DOCKER_COMPOSE_CMD="podman-compose" + else + echo "Error" + exit 1 + fi } build_image() { - docker build --rm --compress -t jikanme/jikan-rest:$JIKAN_API_VERSION . + validate_prereqs + $DOCKER_CMD build --rm --compress -t jikanme/jikan-rest:"$_JIKAN_API_VERSION" . + $DOCKER_CMD tag jikanme/jikan-rest:"$_JIKAN_API_VERSION" jikanme/jikan-rest:latest +} + +ensure_secrets() { + declare -a secrets=("db_password" "db_username" "redis_password" "typesense_api_key") + + for secret_name in "${secrets[@]}" + do + if [ ! -f "$secret_name.txt" ]; then + if [ "$secret_name" == "db_username" ]; then + generated_secret="jikan" + else + generated_secret=$(LC_ALL=c tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_{|}~' "$secret_name.txt" + else + echo -e "$secret_name.txt found, using it's value. \xE2\x9C\x94" + fi + done } start() { - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME up -d + validate_prereqs + ensure_secrets + exec $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" up -d } case "$1" in @@ -55,17 +116,21 @@ case "$1" in "start") start ;; + "stop") + validate_prereqs + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" down + ;; "execute-indexers") echo "Indexing anime..." - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:anime + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" exec jikan_rest php /app/artisan indexer:anime echo "Indexing manga..." - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:manga + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" exec jikan_rest php /app/artisan indexer:manga echo "Indexing characters and people..." - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:common + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" exec jikan_rest php /app/artisan indexer:common echo "Indexing genres..." - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:genres + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" exec jikan_rest php /app/artisan indexer:genres echo "Indexing producers..." - docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:producers + $DOCKER_COMPOSE_CMD -p "$DOCKER_COMPOSE_PROJECT_NAME" exec jikan_rest php /app/artisan indexer:producers echo "Indexing done!" ;; *) diff --git a/docker-compose.yml b/docker-compose.yml index 8ebe53c..96a2b9a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,7 +19,7 @@ secrets: services: jikan_rest: - image: jikanme/jikan-rest:v4.0.0-rc.11 + image: "jikanme/jikan-rest:${_JIKAN_API_VERSION:-latest}" user: "${APP_UID:-10001}:${APP_GID:-10001}" networks: - jikan_network @@ -34,6 +34,7 @@ services: - ./docker/config/.env.compose ports: - '8080:8080/tcp' + hostname: jikan-rest-api healthcheck: test: [ 'CMD-SHELL', 'wget --spider -q "http://127.0.0.1:2114/health?plugin=http"' ] interval: 2s @@ -44,18 +45,20 @@ services: typesense: { condition: service_healthy } mongodb: - image: mongo:focal + image: docker.io/mongo:focal + hostname: mongodb volumes: - mongo-data:/data/db ports: - '27017/tcp' - command: --wiredTigerCacheSizeGB ${MONGO_CACHE_SIZE_GB:1} + command: "--wiredTigerCacheSizeGB ${MONGO_CACHE_SIZE_GB:1}" networks: - jikan_network secrets: + - db_username - db_password environment: - MONGO_INITDB_ROOT_USERNAME: "${DB_USERNAME:-root}" + MONGO_INITDB_ROOT_USERNAME_FILE: /run/secrets/db_username MONGO_INITDB_ROOT_PASSWORD_FILE: /run/secrets/db_password healthcheck: test: echo 'db.runCommand("ping").ok' | mongo mongodb://localhost:27017 --quiet @@ -64,7 +67,8 @@ services: retries: 5 redis: - image: redis:7-alpine + image: docker.io/redis:7-alpine + hostname: redis secrets: - redis_password environment: @@ -74,7 +78,7 @@ services: command: - /bin/sh - -c - - redis-server --requirepass "$${cat /run/secrets/redis_password}" + - redis-server --requirepass "$$(cat /run/secrets/redis_password)" volumes: - redis-data:/data:rw ports: @@ -85,7 +89,8 @@ services: timeout: 1s typesense: - image: typesense/typesense:0.24.1 + image: docker.io/typesense/typesense:0.24.1 + hostname: typesense entrypoint: /bin/sh secrets: - typesense_api_key @@ -100,6 +105,6 @@ services: ports: - "8108/tcp" healthcheck: - test: [ 'CMD-SHELL', 'curl', '-s', '-f', 'http://localhost:8108/health' ] + test: [ 'CMD-SHELL', '{ ! [ -f "curl_created" ] && apt -qq update -y && apt -qq install -y curl && touch curl_created && curl -s -f http://localhost:8108/health; } || { curl -s -f http://localhost:8108/health; }' ] interval: 5s timeout: 2s diff --git a/docker-entrypoint.php b/docker-entrypoint.php index 97d41be..c80a0bf 100644 --- a/docker-entrypoint.php +++ b/docker-entrypoint.php @@ -37,6 +37,7 @@ if (!file_exists(".env")) { $envWriter = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env'); $itemsWritten = 0; foreach (array_keys($current_env) as $env_key) { + echo $env_key; if (!str_contains($env_key, "__FILE")) { continue; } @@ -51,6 +52,7 @@ foreach (array_keys($current_env) as $env_key) { if ($itemsWritten > 0) { $envWriter->write(); + echo "Secrets loaded successfully."; } $dotenv = Dotenv::createImmutable(__DIR__); diff --git a/storage/app/.gitignore b/storage/app/.gitignore index ce9fcc1..11df869 100644 --- a/storage/app/.gitignore +++ b/storage/app/.gitignore @@ -2,3 +2,5 @@ failovers.json source_failover.lock jikan_model_classes.json +container_compose_runtime +container_runtime