From 0d176b30e8c77e2224f1cfb9eee9e608abe2501a Mon Sep 17 00:00:00 2001 From: jarvis8x7b <157810922+jarvis8x7b@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:07:59 +0800 Subject: [PATCH 1/2] build: add docker compose to bundle all services --- .env.standalone.example | 15 ++++ Dockerfile | 30 ++++--- docker-compose.yml | 190 ++++++++++++++++++++++++++++++++++++++-- schema.prisma | 3 +- utils/utils.go | 17 ++-- 5 files changed, 223 insertions(+), 32 deletions(-) create mode 100644 .env.standalone.example diff --git a/.env.standalone.example b/.env.standalone.example new file mode 100644 index 0000000..71318bc --- /dev/null +++ b/.env.standalone.example @@ -0,0 +1,15 @@ +REDIS_USERNAME= +REDIS_PASSWORD= + +DB_HOST=postgres-service:5432 +DB_NAME= +DB_USERNAME= +DB_PASSWORD= + +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_S3_BUCKET_NAME= +S3_PUBLIC_URL= + +JWT_SECRET= +ETHEREUM_NODE= diff --git a/Dockerfile b/Dockerfile index 74b215d..f6e9868 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,33 +5,37 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download +# prefetch the prisma binaries, so that they will be cached and not downloaded on each change +RUN go run github.com/steebchen/prisma-client-go prefetch COPY . . RUN go run github.com/steebchen/prisma-client-go generate -RUN go run github.com/playwright-community/playwright-go/cmd/playwright@latest install --with-deps +RUN go run github.com/playwright-community/playwright-go/cmd/playwright@latest install --with-deps firefox ARG PLATFORM=linux ARG ARCH=amd64 RUN CGO_ENABLED=0 GOARCH=${ARCH} GOOS=${PLATFORM} go build -a -installsuffix cgo -o service ./cmd/server/main.go -FROM ubuntu:22.04 +# FROM ubuntu:22.04 +FROM mcr.microsoft.com/playwright:v1.40.0-jammy -RUN apt-get update && \ - DEBIAN_FRONTEND=noninteractive \ - apt-get install -y --no-install-recommends \ - ca-certificates \ - build-essential \ - xorg \ - gnome-core \ - libgtk-3-dev && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +# RUN apt-get update && \ +# DEBIAN_FRONTEND=noninteractive \ +# apt-get install -y --no-install-recommends \ +# ca-certificates \ +# build-essential \ +# xorg \ +# gnome-core \ +# libgtk-3-dev && \ +# apt-get clean && \ +# rm -rf /var/lib/apt/lists/* WORKDIR /dojo-api COPY --from=builder /app/service . +COPY --from=builder /app/.env . EXPOSE 8080 -CMD ["./service"] \ No newline at end of file +CMD ["./service"] diff --git a/docker-compose.yml b/docker-compose.yml index 2466060..7eb88c1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,186 @@ -version: '3.8' +--- +volumes: + postgres-volume: + redis-volume: + testnet-lite-volume: + services: - postgres: - image: postgres:latest + redis-service: + env_file: + - .env + image: redis/redis-stack-server:7.4.0-v0 + environment: + # CHANGE THE PASSWORD TO A PASSWORD YOU WANT TO USE + REDIS_ARGS: "--requirepass ${REDIS_PASSWORD} --user ${REDIS_USERNAME} on >${REDIS_PASSWORD} ~* allcommands" + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + expose: + - 6379 + volumes: + - redis-volume:/data + + postgres-service: + env_file: + - .env + image: postgres:15.7 environment: - POSTGRES_DB: subnet_db - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres + POSTGRES_DB: ${DB_NAME} + POSTGRES_USER: ${DB_USERNAME} + POSTGRES_PASSWORD: ${DB_PASSWORD} + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME}"] + interval: 5s + timeout: 5s + retries: 5 ports: - "5432:5432" + expose: + - 5432 volumes: - - postgres_data:/var/lib/postgresql/data -volumes: - postgres_data: + - postgres-volume:/var/lib/postgresql/data + + prisma-setup: + platform: linux/amd64 + image: golang:1.22 + env_file: + - .env + environment: + DATABASE_URL: postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME} + working_dir: /app + volumes: + - ./go.mod:/app/go.mod + - ./go.sum:/app/go.sum + - ./schema.prisma:/app/schema.prisma + command: + - /bin/sh + - -c + - | + go install github.com/steebchen/prisma-client-go@latest + go run github.com/steebchen/prisma-client-go generate + go run github.com/steebchen/prisma-client-go db push + depends_on: + - postgres-service + + node-subtensor-testnet: + container_name: node-subtensor-testnet + image: ghcr.io/opentensor/subtensor:latest + platform: linux/amd64 + ports: + - "9944:9944" + - "30333:30333" + - "9933:9933" + expose: + - 9944 + - 30333 + - 9933 + environment: + - CARGO_HOME=/var/www/node-subtensor/.cargo + volumes: + - testnet-lite-volume:/tmp/blockchain + command: + - /bin/bash + - -c + - | + node-subtensor \ + --base-path /tmp/blockchain \ + --chain raw_spec_testfinney.json \ + --rpc-external --rpc-cors all \ + --no-mdns \ + --in-peers 500 --out-peers 500 \ + --bootnodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr \ + --sync warp + --reserved-nodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr \ + --reserved-only + healthcheck: + test: + # [ + # "CMD-SHELL", + # "docker logs node-subtensor-testnet 2>&1 | grep -q 'Imported #' && exit 0 || exit 1", + # ] + ["CMD-SHELL", "exit 0"] + interval: 12s + timeout: 24s + retries: 10 + start_period: 30s + + sidecar: + image: docker.io/parity/substrate-api-sidecar:latest + read_only: true + environment: + # for testnet: wss://test.finney.opentensor.ai:443 + # SAS_SUBSTRATE_URL: wss://test.finney.opentensor.ai:443 + SAS_SUBSTRATE_URL: ws://node-subtensor-testnet:9944 + expose: + - 8080 + healthcheck: + # jank as fuck since no curl + test: + [ + "CMD", + "node", + "-e", + "const http = require('http'); const options = { hostname: '127.0.0.1', port: 8080, path: '/blocks/head', method: 'GET' }; const req = http.request(options, (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }); req.on('error', (error) => { console.error(error); process.exit(1); }); req.end();", + ] + interval: 12s + timeout: 24s + retries: 10 + start_period: 30s + depends_on: + node-subtensor-testnet: + condition: service_healthy + + worker-api: + env_file: + - .env + platform: linux/amd64 + build: + context: . + dockerfile: Dockerfile + environment: + RUNTIME_ENV: local + SERVER_PORT: 8081 + SUBNET_UID: 98 + VALIDATOR_MIN_STAKE: 20000 + CORS_ALLOWED_ORIGINS: http://localhost* + SUBSTRATE_API_URL: sidecar:8080 + # authentication + TOKEN_EXPIRY: 24 + REDIS_HOST: redis-service + REDIS_PORT: 6379 + # these envs get sourced from .env so we dont need to rebuild + # # postgres + # ETHEREUM_NODE: ${ETHEREUM_NODE} + # JWT_SECRET: ${JWT_SECRET} + # DB_NAME: ${DB_NAME} + # DB_HOST: ${DB_HOST} + # DB_USERNAME: ${DB_USERNAME} + # DB_PASSWORD: ${DB_PASSWORD} + # DATABASE_URL: postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME} + # # aws s3 setup + # AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} + # AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} + # AWS_S3_BUCKET_NAME: ${AWS_S3_BUCKET_NAME} + # S3_PUBLIC_URL: ${S3_PUBLIC_URL} + # # redis + # REDIS_USERNAME: ${REDIS_USERNAME} + # REDIS_PASSWORD: ${REDIS_PASSWORD} + ports: + - "8080:8081" + depends_on: + redis-service: + condition: service_healthy + postgres-service: + condition: service_healthy + sidecar: + condition: service_healthy + prisma-setup: + condition: service_completed_successfully + command: ["./service", "--debug"] + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8081"] + interval: 10s + timeout: 10s + retries: 5 diff --git a/schema.prisma b/schema.prisma index 7484b4e..3c8abcc 100644 --- a/schema.prisma +++ b/schema.prisma @@ -4,7 +4,8 @@ datasource db { } generator db { - provider = "go run github.com/steebchen/prisma-client-go" + provider = "go run github.com/steebchen/prisma-client-go" + binaryTargets = ["darwin-arm64", "debian-openssl-3.0.x"] } model ExternalUser { diff --git a/utils/utils.go b/utils/utils.go index 1481193..74a45cb 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -16,7 +16,6 @@ import ( "github.com/aws/aws-sdk-go-v2/feature/s3/manager" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/joho/godotenv" - "github.com/playwright-community/playwright-go" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/rs/zerolog/pkgerrors" @@ -51,14 +50,14 @@ func init() { LoadDotEnv("AWS_S3_BUCKET_NAME") LoadDotEnv("S3_PUBLIC_URL") - err = playwright.Install( - &playwright.RunOptions{ - Browsers: []string{"firefox"}, - }, - ) - if err != nil { - log.Fatal().Err(err).Msg("Error installing playwright") - } + // err = playwright.Install( + // &playwright.RunOptions{ + // Browsers: []string{"firefox"}, + // }, + // ) + // if err != nil { + // log.Fatal().Err(err).Msg("Error installing playwright") + // } zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack zerolog.TimeFieldFormat = zerolog.TimeFormatUnix From 82b64256747061f3422ef6747e805c64b1d135a8 Mon Sep 17 00:00:00 2001 From: jarvis8x7b <157810922+jarvis8x7b@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:15:00 +0800 Subject: [PATCH 2/2] build: specify subtensor and sidecar versions --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7eb88c1..ba3d54f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -66,7 +66,7 @@ services: node-subtensor-testnet: container_name: node-subtensor-testnet - image: ghcr.io/opentensor/subtensor:latest + image: ghcr.io/opentensor/subtensor:pr-720 platform: linux/amd64 ports: - "9944:9944" @@ -107,7 +107,7 @@ services: start_period: 30s sidecar: - image: docker.io/parity/substrate-api-sidecar:latest + image: docker.io/parity/substrate-api-sidecar:v19.0.2 read_only: true environment: # for testnet: wss://test.finney.opentensor.ai:443