diff --git a/Makefile b/Makefile index 886dbe7b..30ddcc29 100644 --- a/Makefile +++ b/Makefile @@ -82,3 +82,24 @@ migrate: $(MIGRATE) checkdbvars import: test -n "$(NAME)" # $$NAME psql $(NAME) < db/schema.sql + +#Rinkeby docker environment +RINKEBY_COMPOSE_FILE=dockerfiles/rinkeby/docker-compose.yml + +.PHONY: rinkeby_env_up +rinkeby_env_up: + docker-compose -f $(RINKEBY_COMPOSE_FILE) up -d geth + docker-compose -f $(RINKEBY_COMPOSE_FILE) up --build migrations + docker-compose -f $(RINKEBY_COMPOSE_FILE) up -d --build vulcanizedb + +.PHONY: rinkeby_env_deploy +rinkeby_env_deploy: + docker-compose -f $(RINKEBY_COMPOSE_FILE) up -d --build vulcanizedb + +.PHONY: dev_env_migrate +rinkeby_env_migrate: + docker-compose -f $(RINKEBY_COMPOSE_FILE) up --build migrations + +.PHONY: rinkeby_env_down +rinkeby_env_down: + docker-compose -f $(RINKEBY_COMPOSE_FILE) down diff --git a/README.md b/README.md index fe2d69bf..65b41dd3 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,29 @@ Sync VulcanizeDB from the LevelDB underlying a Geth node. - `--ending-block-number`/`-e`: block number to sync to - `--all`/`-a`: sync all missing blocks +## Start full environment in docker by single command + +### Geth Rinkeby + +make command | description +------------------- | ---------------- +rinkeby_env_up | start geth, postgres and rolling migrations, after migrations done starting vulcanizedb container +rinkeby_env_deploy | build and run vulcanizedb container in rinkeby environment +rinkeby_env_migrate | build and run rinkeby env migrations +rinkeby_env_down | stop and remove all rinkeby env containers + +Success run of the VulcanizeDB container require full geth state sync, +attach to geth console and check sync state: + +```bash +$ docker exec -it rinkeby_vulcanizedb_geth geth --rinkeby attach +... +> eth.syncing +false +``` + +If you have full rinkeby chaindata you can move it to `rinkeby_vulcanizedb_geth_data` docker volume to skip long wait of sync. + ## Running the Tests ### Unit Tests diff --git a/cmd/root.go b/cmd/root.go index 8ce83ff2..45a633ca 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,6 +39,8 @@ func database(cmd *cobra.Command, args []string) { Name: viper.GetString("database.name"), Hostname: viper.GetString("database.hostname"), Port: viper.GetInt("database.port"), + User: viper.GetString("database.user"), + Password: viper.GetString("database.password"), } viper.Set("database.config", databaseConfig) } @@ -50,12 +52,16 @@ func init() { rootCmd.PersistentFlags().String("database-name", "vulcanize_public", "database name") rootCmd.PersistentFlags().Int("database-port", 5432, "database port") rootCmd.PersistentFlags().String("database-hostname", "localhost", "database hostname") + rootCmd.PersistentFlags().String("database-user", "", "database user") + rootCmd.PersistentFlags().String("database-password", "", "database password") rootCmd.PersistentFlags().String("client-ipcPath", "", "location of geth.ipc file") rootCmd.PersistentFlags().String("client-levelDbPath", "", "location of levelDb chaindata") viper.BindPFlag("database.name", rootCmd.PersistentFlags().Lookup("database-name")) viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port")) viper.BindPFlag("database.hostname", rootCmd.PersistentFlags().Lookup("database-hostname")) + viper.BindPFlag("database.user", rootCmd.PersistentFlags().Lookup("database-user")) + viper.BindPFlag("database.password", rootCmd.PersistentFlags().Lookup("database-password")) viper.BindPFlag("client.ipcPath", rootCmd.PersistentFlags().Lookup("client-ipcPath")) viper.BindPFlag("client.levelDbPath", rootCmd.PersistentFlags().Lookup("client-levelDbPath")) } diff --git a/dockerfiles/rinkeby/Dockerfile b/dockerfiles/rinkeby/Dockerfile new file mode 100644 index 00000000..7f5d5a7b --- /dev/null +++ b/dockerfiles/rinkeby/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:1.10.3-alpine3.7 + +RUN apk add --no-cache make gcc musl-dev + +ADD . /go/src/github.com/vulcanize/vulcanizedb +WORKDIR /go/src/github.com/vulcanize/vulcanizedb +RUN go build -o /app main.go + +ENTRYPOINT ["/app"] diff --git a/dockerfiles/rinkeby/config.toml b/dockerfiles/rinkeby/config.toml new file mode 100644 index 00000000..7fa6796e --- /dev/null +++ b/dockerfiles/rinkeby/config.toml @@ -0,0 +1,9 @@ +[database] +name = "vulcanizedb" +hostname = "postgres" +port = 5432 +user = "postgres" +password = "postgres" + +[client] +ipcPath = "/geth/geth.ipc" diff --git a/dockerfiles/rinkeby/docker-compose.yml b/dockerfiles/rinkeby/docker-compose.yml new file mode 100644 index 00000000..4c3bd3aa --- /dev/null +++ b/dockerfiles/rinkeby/docker-compose.yml @@ -0,0 +1,63 @@ +version: '2.2' + +services: + + vulcanizedb: + build: + context: ./../../ + dockerfile: dockerfiles/rinkeby/Dockerfile + container_name: rinkeby_vulcanizedb + command: "sync --starting-block-number 0 --config /config.toml" + volumes: + - "./config.toml:/config.toml" + - "vulcanizedb_geth_data:/geth" + networks: + vulcanizedb_net: + + migrations: + image: migrate/migrate:v3.3.0 + container_name: rinkeby_vulcanizedb_migrations + depends_on: + postgres: + condition: service_healthy + command: -database postgresql://postgres:postgres@postgres:5432/vulcanizedb?sslmode=disable -path /migrations up + volumes: + - ./../../db/migrations:/migrations + networks: + vulcanizedb_net: + + postgres: + image: postgres:9.6.5-alpine + container_name: rinkeby_vulcanizedb_postgres + environment: + POSTGRES_USER: postgres + POSTGRES_DB: vulcanizedb + POSTGRES_PASSWORD: postgres + volumes: + - "vulcanizedb_db_data:/var/lib/postgresql/data" + networks: + vulcanizedb_net: + healthcheck: + test: ["CMD", "pg_isready"] + interval: 5s + timeout: 5s + retries: 30 + + geth: + image: ethereum/client-go:v1.8.11 + container_name: rinkeby_vulcanizedb_geth + cpus: 0.3 + hostname: eth + command: '--rinkeby --rpc --rpcaddr="0.0.0.0" --rpcvhosts="geth"' + volumes: + - "vulcanizedb_geth_data:/root/.ethereum/rinkeby" + networks: + vulcanizedb_net: + +volumes: + vulcanizedb_geth_data: + vulcanizedb_db_data: + +networks: + vulcanizedb_net: + driver: bridge diff --git a/pkg/config/database.go b/pkg/config/database.go index 7e82fb97..84f139fa 100644 --- a/pkg/config/database.go +++ b/pkg/config/database.go @@ -5,9 +5,15 @@ import "fmt" type Database struct { Hostname string Name string + User string + Password string Port int } func DbConnectionString(dbConfig Database) string { + if len(dbConfig.User) > 0 && len(dbConfig.Password) > 0 { + return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable", + dbConfig.User, dbConfig.Password, dbConfig.Hostname, dbConfig.Port, dbConfig.Name) + } return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", dbConfig.Hostname, dbConfig.Port, dbConfig.Name) }