diff --git a/.env b/.env new file mode 100644 index 0000000..9ce68e2 --- /dev/null +++ b/.env @@ -0,0 +1,12 @@ +POSTGRES_DB_FOLDER= +POSTGRES_DB_HOST= +POSTGRES_DB_PORT= +POSTGRES_DB_NAME= +POSTGRES_USER_NAME= +POSTGRES_DB_PASSWORD= +HASURA_PORT= +HASURA_ADMIN_SECRET= +JUNO_SSL_MODE= +JUNO_WORKERS_NUMBER= +RPC_URL= +CLIENT_URL= diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c78f333 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:latest + +ARG JUNO_WORKERS_NUMBER=1 + +ENV JUNO_WORKERS_NUMBER=${JUNO_WORKERS_NUMBER} + +WORKDIR /app + +COPY . . + +RUN make install + +CMD juno config.toml --workers $JUNO_WORKERS_NUMBER diff --git a/Makefile b/Makefile index 2a1372a..72305ba 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,13 @@ install: go.sum @echo "installing juno binary..." @go install -mod=readonly $(BUILD_FLAGS) . +############################################################################### +# Build / Run in Docker +############################################################################### + +docker: + @sh scripts/start-docker.sh + ############################################################################### # Tools ############################################################################### diff --git a/README.md b/README.md index 9490d5d..dcfd85c 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ ## Table of Contents - - [Background](#background) - - [Install](#install) - - [Usage](#usage) - - [Schemas](#schemas) - - [Future Improvements](#future-improvements) - - [Contributing](#contributing) - - [License](#license) +- [Background](#background) +- [Install](#install) +- [Usage](#usage) +- [Schemas](#schemas) +- [Future Improvements](#future-improvements) +- [Contributing](#contributing) +- [License](#license) ## Background @@ -54,6 +54,32 @@ To install the binary run `make install`. **Note**: Requires [Go 1.13+](https://golang.org/dl/) +## Running in docker + +- Open and fill `.env` file with all necessary data +- To install in docker run `make docker`. + +Juno, Hasura and Postgres would be deployed in docker containers. + +**Note** Not necessary to create `config.toml` for docker installation, it would be generated automatically from `.env` file. + +Example of .env file: + +```toml +POSTGRES_DB_FOLDER=/home/user/postgres +POSTGRES_DB_HOST=localhost +POSTGRES_DB_PORT=5235 +POSTGRES_DB_NAME=juno-postgres +POSTGRES_USER_NAME=juno +POSTGRES_DB_PASSWORD=1234juno4321 +HASURA_PORT=8080 +HASURA_ADMIN_SECRET=1234goodpasscode4321 +JUNO_SSL_MODE=disable +JUNO_WORKERS_NUMBER=4 +RPC_URL=http://my.gaia-node.ai/rpc/ +CLIENT_URL=http://client.my.gaia-node.ai/ +``` + ## Usage Juno internally runs a single worker that consumes from a single queue. The diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..6fe2ac8 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,44 @@ +version: '3' + +services: + postgres: + image: postgres:latest + restart: always + container_name: juno_postgres + volumes: + - ./postgres:/var/lib/postgresql/data + - ./schema:/root/schema + environment: + - POSTGRES_DB=${POSTGRES_DB_NAME} + - POSTGRES_USER=${POSTGRES_USER_NAME} + - POSTGRES_PASSWORD=${POSTGRES_DB_PASSWORD} + networks: + juno-net: + ipv4_address: 172.28.1.2 + ports: + - 127.0.0.1:${POSTGRES_DB_PORT}:5432 + graphql-engine: + image: hasura/graphql-engine:latest + restart: always + container_name: juno_hasura + depends_on: + - "postgres" + environment: + HASURA_GRAPHQL_DATABASE_URL: postgres://${POSTGRES_USER_NAME}:${POSTGRES_DB_PASSWORD}@172.28.1.2:5432/${POSTGRES_DB_NAME} + HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console + HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log + HASURA_GRAPHQL_ADMIN_SECRET: $HASURA_ADMIN_SECRET + networks: + juno-net: + ipv4_address: 172.28.1.3 + ports: + - 127.0.0.1:${HASURA_PORT}:8080 + + +networks: + juno-net: + driver: bridge + ipam: + driver: default + config: + - subnet: 172.28.1.0/24 \ No newline at end of file diff --git a/scripts/start-docker.sh b/scripts/start-docker.sh new file mode 100644 index 0000000..9e6a4df --- /dev/null +++ b/scripts/start-docker.sh @@ -0,0 +1,33 @@ +#! /bin/bash + +# temporeraly import variables +export $(cat .env) + +# run postgres and hasura in containers, wait 2 seconds to start postgres +docker-compose up -d +sleep 2 + +# init database with basic tables +docker exec -ti juno_postgres psql -f /root/schema/validator.sql -d $POSTGRES_DB_NAME -U $POSTGRES_USER_NAME +docker exec -ti juno_postgres psql -f /root/schema/pre_commit.sql -d $POSTGRES_DB_NAME -U $POSTGRES_USER_NAME +docker exec -ti juno_postgres psql -f /root/schema/block.sql -d $POSTGRES_DB_NAME -U $POSTGRES_USER_NAME +docker exec -ti juno_postgres psql -f /root/schema/transaction.sql -d $POSTGRES_DB_NAME -U $POSTGRES_USER_NAME + +# create docker.config.toml, put values from .env file to config.toml +echo -n 'rpc_node="' >> config.toml && echo -n $RPC_URL >> config.toml && echo -n '"' >> config.toml +sed -i "/rpc/a client_node=\"$CLIENT_URL\"" config.toml +echo >> config.toml +echo '[database]' >> config.toml +sed -i "/database/a host=\"$POSTGRES_DB_HOST\"" config.toml +sed -i "/host=/a port=$POSTGRES_DB_PORT" config.toml +sed -i "/port/a name=\"$POSTGRES_DB_NAME\"" config.toml +sed -i "/name/a user=\"$POSTGRES_USER_NAME\"" config.toml +sed -i "/user/a password=\"$POSTGRES_DB_PASSWORD\"" config.toml +sed -i "/password/a ssl_mode=\"$JUNO_SSL_MODE\"" config.toml + +# build juno and run it in container +docker build -t juno:latest --build-arg JUNO_WORKERS_NUMBER=$JUNO_WORKERS_NUMBER . +docker run -d --name juno --network="host" juno:latest + +# remove config toml, as far as it copyed to container +rm config.toml