Skip to content

Commit

Permalink
wip - container setup cli and fixed some issues with docker compose y…
Browse files Browse the repository at this point in the history
…ml (still buggy)
  • Loading branch information
pushrbx committed Sep 24, 2023
1 parent d1f9a76 commit 94d0753
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 51 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
32 changes: 10 additions & 22 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
];

/**
Expand Down
101 changes: 83 additions & 18 deletions container-setup.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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!"#$%&'\''()*+,-./:;<=>?@[\]^_{|}~' </dev/urandom | head -c 16 ; echo)
fi
echo "$secret_name.txt not found, please provide a $secret_name [default is $generated_secret]:"
# prompt for secret and save it in file
read -r secret_value
if [ -z "$secret_value" ]; then
secret_value=$generated_secret
fi
echo "$secret_value" > "$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
Expand All @@ -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!"
;;
*)
Expand Down
21 changes: 13 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -64,7 +67,8 @@ services:
retries: 5

redis:
image: redis:7-alpine
image: docker.io/redis:7-alpine
hostname: redis
secrets:
- redis_password
environment:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions docker-entrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
$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;
}
Expand All @@ -51,6 +52,7 @@

if ($itemsWritten > 0) {
$envWriter->write();
echo "Secrets loaded successfully.";
}

$dotenv = Dotenv::createImmutable(__DIR__);
Expand Down
2 changes: 2 additions & 0 deletions storage/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
failovers.json
source_failover.lock
jikan_model_classes.json
container_compose_runtime
container_runtime

0 comments on commit 94d0753

Please sign in to comment.