Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testnet with two nodes in docker compose #27

Merged
5 commits merged into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM elixir:alpine AS uniris-ci

ARG skip_tests=0

# CI
# - compile
# - release
Expand Down Expand Up @@ -44,7 +46,7 @@ RUN mix phx.digest \
&& mix distillery.release

# gen PLT
RUN mix git_hooks.run pre_push
RUN [ $skip_tests -eq 0 ] && mix git_hooks.run pre_push || true

# Install
RUN mkdir /opt/app \
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ config :uniris, Uniris.P2P.Endpoint,
# with webpack to recompile .js and .css sources.
config :uniris, UnirisWeb.Endpoint,
http: [port: 4000],
server: true,
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [
node: [
Expand Down
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: "3.9"

services:

node1:
build:
context: .
args:
skip_tests: 1
image: uniris-node:latest
environment:
- UNIRIS_CRYPTO_SEED=node1
- UNIRIS_MUT_DIR=/opt/data
volumes:
- ./data1:/opt/data
networks:
testnet:
ipv4_address: 172.16.16.101

node2:
build:
context: .
args:
skip_tests: 1
image: uniris-node:latest
depends_on:
- node1
environment:
- UNIRIS_CRYPTO_SEED=node2
- UNIRIS_MUT_DIR=/opt/data
- UNIRIS_P2P_SEEDS=172.16.16.101:3002:0008117DAD3A936B641106B53AF3B828940C3BC5A77F1C9BFB8AD214EF6897B000:tcp
volumes:
- ./scripts/wait-for-node.sh:/wait-for-node.sh:ro
- ./data2:/opt/data
command: ["/wait-for-node.sh", "http://node1:4000/up", "./bin/uniris_node", "foreground"]
networks:
testnet:
ipv4_address: 172.16.16.102

networks:
testnet:
ipam:
driver: default
config:
- subnet: "172.16.16.0/24"
2 changes: 2 additions & 0 deletions lib/uniris/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ defmodule Uniris.Bootstrap do
else
P2P.set_node_globally_available(Crypto.node_public_key(0))
end

:persistent_term.put(:uniris_up, :up)
end

defp should_bootstrap?(_ip, _port, _, nil), do: true
Expand Down
19 changes: 19 additions & 0 deletions lib/uniris_web/controllers/up_controler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule UnirisWeb.UpController do
@moduledoc false

use UnirisWeb, :controller

@doc """
Respond with 200 when node is ready otherwise with 503.
Used only to indicate that the first testnet node is bootrapped.
"""
def up(conn, _) do
:up = :persistent_term.get(:uniris_up)
rescue
_ ->
resp(conn, 503, "wait")
else
_ ->
resp(conn, 200, "up")
end
end
1 change: 1 addition & 0 deletions lib/uniris_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule UnirisWeb.Router do
pipe_through(:browser)

get("/", RootController, :index)
get("/up", UpController, :up)
live_dashboard("/dashboard")
end

Expand Down
2 changes: 1 addition & 1 deletion rel/pre_configure/clone-node-env.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#/usr/bin/env bash
#!/usr/bin/env bash

rpc='\
System.get_env() \
Expand Down
18 changes: 18 additions & 0 deletions scripts/wait-for-node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -e

curl="$1"
node="${curl#*//}"
node="${node%:*}"
shift
cmd="$@"
wget="wget -qO /dev/null"

until $wget "$curl" ; do
>&2 echo "Node $node is unavailable - sleeping"
sleep 1
done

>&2 echo "Node $node is up - executing command"
exec $cmd