From 13b2a19b3d2dfb36918d0536016376178dd79ad7 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Tue, 16 Apr 2024 17:46:07 +0200
Subject: [PATCH] build prod dockerfile

---
 Dockerfile                          |  7 +--
 Dockerfile.Hasura                   |  5 ++
 Dockerfile.Kubo                     |  5 ++
 docker-compose.prod.yml             | 72 +++++++++++++++++++++++++++++
 docker-compose.yml                  |  9 ++--
 scripts/remove-default-bootstrap.sh |  3 ++
 src/kubo.ts                         |  3 +-
 7 files changed, 96 insertions(+), 8 deletions(-)
 create mode 100644 Dockerfile.Hasura
 create mode 100644 Dockerfile.Kubo
 create mode 100644 docker-compose.prod.yml
 create mode 100644 scripts/remove-default-bootstrap.sh

diff --git a/Dockerfile b/Dockerfile
index aecb3ac..38a57e6 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -19,7 +19,8 @@ CMD ["pnpm", "exec", "tsx", "src/indexer/start.ts"]
 # TODO build image only for indexer without app
 # TODO really build for node and not use tsx (is it necessary)
 
-# build and run for test
-# docker buildx build . -t test-datapod
-# docker run --rm -it test-datapod
+# build and run
+# docker buildx build . -t h30x/duniter-datapod
+# docker image push h30x/duniter-datapod
+# docker run --rm -it h30x/duniter-datapod
 # CMD ["pnpm", "exec", "tsx", "src/scripts/hello.ts"]
diff --git a/Dockerfile.Hasura b/Dockerfile.Hasura
new file mode 100644
index 0000000..ac2ec44
--- /dev/null
+++ b/Dockerfile.Hasura
@@ -0,0 +1,5 @@
+FROM hasura/graphql-engine:v2.38.1.cli-migrations-v3
+COPY ./hasura/migrations /hasura-migrations
+COPY ./hasura/metadata /hasura-metadata
+# docker buildx build -f Dockerfile.Hasura . -t h30x/datapod-hasura
+# docker image push h30x/datapod-hasura
\ No newline at end of file
diff --git a/Dockerfile.Kubo b/Dockerfile.Kubo
new file mode 100644
index 0000000..4cb3d2e
--- /dev/null
+++ b/Dockerfile.Kubo
@@ -0,0 +1,5 @@
+FROM ipfs/kubo:v0.28.0
+COPY ./scripts/remove-default-bootstrap.sh /container-init.d/001-remove-default-bootstrap.sh
+CMD ["daemon", "--enable-pubsub-experiment"]
+# docker buildx build -f Dockerfile.Kubo . -t h30x/datapod-kubo
+# docker image push h30x/datapod-kubo
\ No newline at end of file
diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml
new file mode 100644
index 0000000..fd55b8f
--- /dev/null
+++ b/docker-compose.prod.yml
@@ -0,0 +1,72 @@
+# prod compose file
+
+services:
+  # ------
+  postgres:
+    image: postgres:16
+    restart: always
+    volumes:
+      - db_data:/var/lib/postgresql/data
+    environment:
+      POSTGRES_USER: ${DB_USER:-postgres}
+      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgrespassword}
+      POSTGRES_DB: ${DB_DATABASE:-postgres}
+    healthcheck:
+      test: ['CMD-SHELL', 'pg_isready -U postgres -d postgres']
+      interval: 1s
+
+  # ------
+  hasura:
+    image: h30x/datapod-hasura
+    depends_on:
+      postgres:
+        condition: service_healthy
+    restart: always
+    ports:
+      - '${HASURA_LISTEN_PORT:-8765}:8080'
+    environment:
+      HASURA_GRAPHQL_DATABASE_URL: postgres://${DB_USER:-postgres}:${DB_PASSWORD:-postgrespassword}@postgres:5432/${DB_DATABASE:-postgres}
+      HASURA_GRAPHQL_ENABLE_CONSOLE: true
+      HASURA_GRAPHQL_DEV_MODE: false
+      HASURA_GRAPHQL_UNAUTHORIZED_ROLE: public
+      HASURA_GRAPHQL_ENABLE_TELEMETRY: false
+      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-my_hasura_secret}
+
+  # ------
+  kubo:
+    image: h30x/datapod-kubo
+    ports:
+      # p2p port public
+      - 4001:4001
+      - 4001:4001/udp
+      # public gateway
+      - 8080:8080
+      # expose RPC locally to allow access with ssh tunnel
+      - 127.0.0.1:5001:5001
+    volumes:
+      - kubo_data:/data/ipfs
+    healthcheck:
+      test: ['CMD', 'ipfs', 'diag', 'cmds']
+      interval: 1s
+    restart: always
+
+  # ------
+  datapod:
+    image: h30x/duniter-datapod
+    depends_on:
+      postgres:
+        condition: service_healthy
+      kubo:
+        condition: service_healthy
+    environment:
+      KUBO_RPC: "http://kubo:5001"
+      DB_HOST: postgres
+      DB_PORT: 5432
+      DB_USER: ${DB_USER:-postgres}
+      DB_PASSWORD: ${DB_PASSWORD:-postgrespassword}
+    restart: always
+
+
+volumes:
+  db_data:
+  kubo_data:
diff --git a/docker-compose.yml b/docker-compose.yml
index e9bfd32..f38f1ac 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,10 +1,11 @@
-version: '3.6'
+# development compose file
+
 services:
   postgres:
     image: postgres:16
     restart: always
     ports:
-      - '5432:5432'
+      - '5432:5432' # expose db for indexer
     volumes:
       - db_data:/var/lib/postgresql/data
     environment:
@@ -16,13 +17,13 @@ services:
       retries: 5
 
   hasura:
-    image: hasura/graphql-engine:v2.38.1.cli-migrations-v3
+    image: hasura/graphql-engine:v2.38.1.cli-migrations-v3 # dev image to apply migrations
     depends_on:
       postgres:
         condition: service_healthy
     restart: always
     ports:
-      - '${HASURA_LISTEN_PORT}:8080'
+      - '${HASURA_LISTEN_PORT}:8080' # because kubo gateway is already listening on 8080
     environment:
       HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:${DB_PASSWORD}@postgres:5432/postgres
       HASURA_GRAPHQL_ENABLE_CONSOLE: true
diff --git a/scripts/remove-default-bootstrap.sh b/scripts/remove-default-bootstrap.sh
new file mode 100644
index 0000000..bb0cc11
--- /dev/null
+++ b/scripts/remove-default-bootstrap.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+set -ex
+ipfs bootstrap rm all
diff --git a/src/kubo.ts b/src/kubo.ts
index 507985b..0854bf6 100644
--- a/src/kubo.ts
+++ b/src/kubo.ts
@@ -3,7 +3,8 @@ import type { IPFSHTTPClient } from 'kubo-rpc-client'
 import { CID } from 'multiformats'
 
 // env
-var process : NodeJS.Process | undefined = typeof(process) == "undefined" ? undefined : process
+// TODO clean way to declare KUBO_RPC for Vue App
+// var process : NodeJS.Process | undefined = typeof(process) == "undefined" ? undefined : process
 const KUBO_RPC = process?.env.KUBO_RPC || 'http://127.0.0.1:5001'
 const KUBO_GATEWAY = process?.env.KUBO_GATEWAY || 'http://127.0.0.1:8080'
 
-- 
GitLab