Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request scaffold-eth#65 from austintgriffith/graph-on-main
Browse files Browse the repository at this point in the history
Add a subgraph to the main scaffold
  • Loading branch information
austintgriffith authored Oct 19, 2020
2 parents 9a55618 + 9dfba61 commit 695c243
Show file tree
Hide file tree
Showing 31 changed files with 924 additions and 178 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ packages/buidler/artifacts
packages/react-app/src/contracts
packages/buidler/cache

docker/**/data

# dependencies
/node_modules
/.pnp
Expand Down
75 changes: 75 additions & 0 deletions docker/graph-node/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Full build with debuginfo for graph-node
#
# The expectation if that the docker build uses the parent directory as PWD
# by running something like the following
# docker build --target STAGE -f docker/Dockerfile .

FROM rust:latest as graph-node-build

ARG COMMIT_SHA=unknown
ARG REPO_NAME=unknown
ARG BRANCH_NAME=unknown
ARG TAG_NAME=unknown

ADD . /graph-node

RUN cd /graph-node \
&& RUSTFLAGS="-g" cargo install --locked --path node \
&& cargo clean \
&& objcopy --only-keep-debug /usr/local/cargo/bin/graph-node /usr/local/cargo/bin/graph-node.debug \
&& strip -g /usr/local/cargo/bin/graph-node \
&& cd /usr/local/cargo/bin \
&& objcopy --add-gnu-debuglink=graph-node.debug graph-node \
&& echo "REPO_NAME='$REPO_NAME'" > /etc/image-info \
&& echo "TAG_NAME='$TAG_NAME'" >> /etc/image-info \
&& echo "BRANCH_NAME='$BRANCH_NAME'" >> /etc/image-info \
&& echo "COMMIT_SHA='$COMMIT_SHA'" >> /etc/image-info \
&& echo "CARGO_VERSION='$(cargo --version)'" >> /etc/image-info \
&& echo "RUST_VERSION='$(rustc --version)'" >> /etc/image-info

# The graph-node runtime image with only the executable
FROM debian:buster-slim as graph-node
ENV RUST_LOG ""
ENV GRAPH_LOG ""
ENV EARLY_LOG_CHUNK_SIZE ""
ENV ETHEREUM_RPC_PARALLEL_REQUESTS ""
ENV ETHEREUM_BLOCK_CHUNK_SIZE ""

ENV postgres_host ""
ENV postgres_user ""
ENV postgres_pass ""
ENV postgres_db ""
# The full URL to the IPFS node
ENV ipfs ""
# The etherum network(s) to connect to. Set this to a space-separated
# list of the networks where each entry has the form NAME:URL
ENV ethereum ""
# The role the node should have, one of index-node, query-node, or
# combined-node
ENV node_role "combined-node"
# The name of this node
ENV node_id "default"

# HTTP port
EXPOSE 8000
# WebSocket port
EXPOSE 8001
# JSON-RPC port
EXPOSE 8020

RUN apt-get update \
&& apt-get install -y libpq-dev ca-certificates netcat

ADD docker/wait_for docker/start /usr/local/bin/
COPY --from=graph-node-build /usr/local/cargo/bin/graph-node /usr/local/bin
COPY --from=graph-node-build /etc/image-info /etc/image-info
COPY docker/Dockerfile /Dockerfile
CMD start

# Debug image to access core dumps
FROM graph-node-build as graph-node-debug
RUN apt-get update \
&& apt-get install -y curl gdb postgresql-client

COPY docker/Dockerfile /Dockerfile
COPY docker/bin/* /usr/local/bin/
78 changes: 78 additions & 0 deletions docker/graph-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Graph Node Docker Image

Preconfigured Docker image for running a Graph Node.

## Usage

```sh
docker run -it \
-e postgres_host=<HOST>[:<PORT>] \
-e postgres_user=<USER> \
-e postgres_pass=<PASSWORD> \
-e postgres_db=<DBNAME> \
-e ipfs=<HOST>:<PORT> \
-e ethereum=<NETWORK_NAME>:<ETHEREUM_RPC_URL> \
graphprotocol/graph-node:latest
```

### Example usage

```sh
docker run -it \
-e postgres_host=host.docker.internal:5432
-e postgres_user=graph-node \
-e postgres_pass=oh-hello \
-e postgres_db=graph-node \
-e ipfs=host.docker.internal:5001 \
-e ethereum=mainnet:http://localhost:8545/ \
graphprotocol/graph-node:latest
```

## Docker Compose

The Docker Compose setup requires an Ethereum network name and node
to connect to. By default, it will use `mainnet:http://host.docker.internal:8545`
in order to connect to an Ethereum node running on your host machine.
You can replace this with anything else in `docker-compose.yaml`.

> **Note for Linux users:** On Linux, `host.docker.internal` is not
> currently supported. Instead, you will have to replace it with the
> IP address of your Docker host (from the perspective of the Graph
> Node container).
> To do this, run:
>
> ```
> CONTAINER_ID=$(docker container ls | grep graph-node | cut -d' ' -f1)
> docker exec $CONTAINER_ID /bin/bash -c 'ip route | awk \'/^default via /{print $3}\''
> ```
>
> This will print the host's IP address. Then, put it into `docker-compose.yml`:
>
> ```
> sed -i -e 's/host.docker.internal/<IP ADDRESS>/g' docker-compose.yml
> ```
After you have set up an Ethereum node—e.g. Ganache or Parity—simply
clone this repository and run
```sh
docker-compose up
```
This will start IPFS, Postgres and Graph Node in Docker and create persistent
data directories for IPFS and Postgres in `./data/ipfs` and `./data/postgres`. You
can access these via:

- Graph Node:
- GraphiQL: `http://localhost:8000/`
- HTTP: `http://localhost:8000/subgraphs/name/<subgraph-name>`
- WebSockets: `ws://localhost:8001/subgraphs/name/<subgraph-name>`
- Admin: `http://localhost:8020/`
- IPFS:
- `127.0.0.1:5001` or `/ip4/127.0.0.1/tcp/5001`
- Postgres:
- `postgresql://graph-node:let-me-in@localhost:5432/graph-node`

Once this is up and running, you can use
[`graph-cli`](https://github.com/graphprotocol/graph-cli) to create and
deploy your subgraph to the running Graph Node.
11 changes: 11 additions & 0 deletions docker/graph-node/bin/create
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/bash

if [ $# != 1 ]; then
echo "usage: create <name>"
exit 1
fi

api="http://index-node.default/"

data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_create", "params": {"name":"%s"}, "id":"1"}' "$1")
curl -s -H "content-type: application/json" --data "$data" "$api"
9 changes: 9 additions & 0 deletions docker/graph-node/bin/debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /bin/bash

if [ -f "$1" ]
then
exec rust-gdb -c "$1" /usr/local/cargo/bin/graph-node
else
echo "usage: debug <core-file>"
exit 1
fi
12 changes: 12 additions & 0 deletions docker/graph-node/bin/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /bin/bash

if [ $# != 3 ]; then
echo "usage: deploy <name> <ipfs_hash> <node>"
exit 1
fi

api="http://index-node.default/"

echo "Deploying $1 (deployment $2)"
data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_deploy", "params": {"name":"%s", "ipfs_hash":"%s", "node_id":"%s"}, "id":"1"}' "$1" "$2" "$3")
curl -s -H "content-type: application/json" --data "$data" "$api"
12 changes: 12 additions & 0 deletions docker/graph-node/bin/reassign
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /bin/bash

if [ $# -lt 3 ]; then
echo "usage: reassign <name> <ipfs_hash> <node>"
exit 1
fi

api="http://index-node.default/"

echo Assigning to "$3"
data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_reassign", "params": {"name":"%s", "ipfs_hash":"%s", "node_id":"%s"}, "id":"1"}' "$1" "$2" "$3")
curl -s -H "content-type: application/json" --data "$data" "$api"
11 changes: 11 additions & 0 deletions docker/graph-node/bin/remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/bash

if [ $# != 1 ]; then
echo "usage: create <name>"
exit 1
fi

api="http://index-node.default/"

data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_remove", "params": {"name":"%s"}, "id":"1"}' "$1")
curl -s -H "content-type: application/json" --data "$data" "$api"
26 changes: 26 additions & 0 deletions docker/graph-node/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /bin/bash

# This file is only here to ease testing/development. Official images are
# built using the 'cloudbuild.yaml' file

type -p podman > /dev/null && docker=podman || docker=docker

cd $(dirname $0)/..

if [ -d .git ]
then
COMMIT_SHA=$(git rev-parse HEAD)
TAG_NAME=$(git tag --points-at HEAD)
REPO_NAME="Checkout of $(git remote get-url origin) at $(git describe --dirty)"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
fi
for stage in graph-node-build graph-node graph-node-debug
do
$docker build --target $stage \
--build-arg "COMMIT_SHA=$COMMIT_SHA" \
--build-arg "REPO_NAME=$REPO_NAME" \
--build-arg "BRANCH_NAME=$BRANCH_NAME" \
--build-arg "TAG_NAME=$TAG_NAME" \
-t $stage \
-f docker/Dockerfile .
done
53 changes: 53 additions & 0 deletions docker/graph-node/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
options:
machineType: "N1_HIGHCPU_32"
timeout: 1800s
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '--target', 'graph-node-build',
'--build-arg', 'COMMIT_SHA=$COMMIT_SHA',
'--build-arg', 'REPO_NAME=$REPO_NAME',
'--build-arg', 'BRANCH_NAME=$BRANCH_NAME',
'--build-arg', 'TAG_NAME=$TAG_NAME',
'-t', 'gcr.io/$PROJECT_ID/graph-node-build:$SHORT_SHA',
'-f', 'docker/Dockerfile', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '--target', 'graph-node',
'--build-arg', 'COMMIT_SHA=$COMMIT_SHA',
'--build-arg', 'REPO_NAME=$REPO_NAME',
'--build-arg', 'BRANCH_NAME=$BRANCH_NAME',
'--build-arg', 'TAG_NAME=$TAG_NAME',
'-t', 'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA',
'-f', 'docker/Dockerfile', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '--target', 'graph-node-debug',
'--build-arg', 'COMMIT_SHA=$COMMIT_SHA',
'--build-arg', 'REPO_NAME=$REPO_NAME',
'--build-arg', 'BRANCH_NAME=$BRANCH_NAME',
'--build-arg', 'TAG_NAME=$TAG_NAME',
'-t', 'gcr.io/$PROJECT_ID/graph-node-debug:$SHORT_SHA',
'-f', 'docker/Dockerfile', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['tag',
'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA',
'lutter/graph-node:$SHORT_SHA']
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args: ['docker/tag.sh']
secretEnv: ['PASSWORD']
env:
- 'SHORT_SHA=$SHORT_SHA'
- 'TAG_NAME=$TAG_NAME'
- 'PROJECT_ID=$PROJECT_ID'
- 'DOCKER_HUB_USER=$_DOCKER_HUB_USER'
- 'BRANCH_NAME=$BRANCH_NAME'
images:
- 'gcr.io/$PROJECT_ID/graph-node-build:$SHORT_SHA'
- 'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA'
- 'gcr.io/$PROJECT_ID/graph-node-debug:$SHORT_SHA'
substitutions:
# The owner of the access token whose encrypted value is in PASSWORD
_DOCKER_HUB_USER: "lutter"
secrets:
- kmsKeyName: projects/the-graph-staging/locations/global/keyRings/docker/cryptoKeys/docker-hub-push
secretEnv:
PASSWORD: 'CiQAdfFldbmUiHgGP1lPq6bAOfd+VQ/dFwyohB1IQwiwQg03ZE8STQDvWKpv6eJHVUN1YoFC5FcooJrH+Stvx9oMD7jBjgxEH5ngIiAysWP3E4Pgxt/73xnaanbM1EQ94eVFKCiY0GaEKFNu0BJx22vCYmU4'
38 changes: 38 additions & 0 deletions docker/graph-node/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3'
services:
graph-node:
image: graphprotocol/graph-node
ports:
- '8000:8000'
- '8001:8001'
- '8020:8020'
- '8030:8030'
- '8040:8040'
depends_on:
- ipfs
- postgres
environment:
postgres_host: postgres:5432
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'localhost:http://host.docker.internal:8545'
RUST_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.4.23
ports:
- '5001:5001'
volumes:
- ./data/ipfs:/data/ipfs
postgres:
image: postgres
ports:
- '5432:5432'
command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
environment:
POSTGRES_USER: graph-node
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
volumes:
- ./data/postgres:/var/lib/postgresql/data
8 changes: 8 additions & 0 deletions docker/graph-node/hooks/post_checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e
set -x

echo "Setting SOURCE_BRANCH to ${SOURCE_BRANCH}"

sed -i "s@^ENV SOURCE_BRANCH \"master\"@ENV SOURCE_BRANCH \"${SOURCE_BRANCH}\"@g" Dockerfile
42 changes: 42 additions & 0 deletions docker/graph-node/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

set -e

if ! which docker 2>&1 > /dev/null; then
echo "Please install 'docker' first"
exit 1
fi

if ! which docker-compose 2>&1 > /dev/null; then
echo "Please install 'docker-compose' first"
exit 1
fi

if ! which jq 2>&1 > /dev/null; then
echo "Please install 'jq' first"
exit 1
fi

# Create the graph-node container
docker-compose up --no-start graph-node

# Start graph-node so we can inspect it
docker-compose start graph-node

# Identify the container ID
CONTAINER_ID=$(docker container ls | grep graph-node | cut -d' ' -f1)

# Inspect the container to identify the host IP address
HOST_IP=$(docker inspect "$CONTAINER_ID" | jq -r .[0].NetworkSettings.Networks[].Gateway)

echo "Host IP: $HOST_IP"

# Inject the host IP into docker-compose.yml
sed -i -e "s/host.docker.internal/$HOST_IP/g" docker-compose.yml

function stop_graph_node {
# Ensure graph-node is stopped
docker-compose stop graph-node
}

trap stop_graph_node EXIT
Loading

0 comments on commit 695c243

Please sign in to comment.