2019-09-10 22:13:27 +00:00
|
|
|
import
|
2020-06-22 19:51:21 +00:00
|
|
|
strformat, os, confutils, algorithm, sequtils
|
2019-10-29 13:41:10 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Command = enum
|
|
|
|
restart_nodes
|
2019-11-05 13:00:11 +00:00
|
|
|
reset_network
|
2019-10-29 13:41:10 +00:00
|
|
|
|
|
|
|
CliConfig = object
|
|
|
|
network: string
|
|
|
|
|
2020-08-01 18:32:41 +00:00
|
|
|
validatorsDir {.
|
|
|
|
defaultValue: "validators"
|
|
|
|
name: "validators-dir" }: string
|
2020-06-22 19:51:21 +00:00
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
case cmd {.command.}: Command
|
|
|
|
of restart_nodes:
|
|
|
|
discard
|
|
|
|
|
2019-11-05 13:00:11 +00:00
|
|
|
of reset_network:
|
2020-06-03 09:07:43 +00:00
|
|
|
secretsDir {.
|
|
|
|
defaultValue: "secrets"
|
|
|
|
name: "secrets-dir" }: string
|
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
networkDataDir {.
|
2019-11-05 13:00:11 +00:00
|
|
|
defaultValue: "data"
|
2019-11-11 14:43:12 +00:00
|
|
|
name: "network-data-dir" }: string
|
2019-11-05 13:00:11 +00:00
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
totalUserValidators {.
|
2019-11-05 13:00:11 +00:00
|
|
|
defaultValue: 0
|
2019-11-11 14:43:12 +00:00
|
|
|
name: "user-validators" }: int
|
2019-10-29 13:41:10 +00:00
|
|
|
|
2019-11-12 12:14:55 +00:00
|
|
|
Node = object
|
|
|
|
id: int
|
|
|
|
server: string
|
|
|
|
container: string
|
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
var conf = load CliConfig
|
2019-09-10 22:13:27 +00:00
|
|
|
|
|
|
|
var
|
2020-06-22 19:51:21 +00:00
|
|
|
serverCount = 6
|
|
|
|
instancesCount = 1
|
2020-08-01 18:32:41 +00:00
|
|
|
validators = listDirs(conf.validatorsDir).mapIt(splitPath(it)[1])
|
2020-06-02 19:59:51 +00:00
|
|
|
|
|
|
|
sort(validators)
|
2019-11-05 13:00:11 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
proc findOrDefault[K, V](tupleList: openArray[(K, V)], key: K, default: V): V =
|
2019-11-05 13:00:11 +00:00
|
|
|
for t in tupleList:
|
|
|
|
if t[0] == key:
|
|
|
|
return t[1]
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
2019-11-12 12:14:55 +00:00
|
|
|
iterator nodes: Node =
|
2019-09-10 22:13:27 +00:00
|
|
|
for i in 0 ..< serverCount:
|
|
|
|
let
|
2019-11-12 12:14:55 +00:00
|
|
|
serverShortName = if i == 0: "master-01" else: &"node-0{i}"
|
2020-01-28 20:28:52 +00:00
|
|
|
server = &"{serverShortName}.aws-eu-central-1a.nimbus.test.statusim.net"
|
2019-09-10 22:13:27 +00:00
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
for j in 0 ..< instancesCount:
|
2019-11-12 12:14:55 +00:00
|
|
|
yield Node(id: i*instancesCount + j,
|
|
|
|
server: server,
|
|
|
|
container: &"beacon-node-{conf.network}-{j+1}")
|
|
|
|
|
|
|
|
iterator validatorAssignments: tuple[node: Node; firstValidator, lastValidator: int] =
|
|
|
|
let
|
2020-06-02 19:59:51 +00:00
|
|
|
systemValidators = validators.len - conf.totalUserValidators
|
2019-11-12 12:14:55 +00:00
|
|
|
|
|
|
|
defaultValidatorAssignment = proc (nodeIdx: int): int =
|
|
|
|
(systemValidators div serverCount) div instancesCount
|
|
|
|
|
|
|
|
customValidatorAssignments = {
|
|
|
|
# This is used just to force the correct type of the table
|
|
|
|
"default": defaultValidatorAssignment
|
|
|
|
,
|
|
|
|
"testnet0": proc (nodeIdx: int): int =
|
|
|
|
if nodeidx < 4:
|
|
|
|
systemValidators div 4
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
,
|
|
|
|
"testnet1": proc (nodeIdx: int): int =
|
2019-11-14 14:10:14 +00:00
|
|
|
if nodeidx < 4:
|
|
|
|
systemValidators div 4
|
|
|
|
else:
|
|
|
|
0
|
2019-11-12 12:14:55 +00:00
|
|
|
}
|
2019-11-05 13:00:11 +00:00
|
|
|
|
2019-11-12 12:14:55 +00:00
|
|
|
var nextValidatorIdx = conf.totalUserValidators
|
|
|
|
for node in nodes():
|
|
|
|
let
|
|
|
|
validatorAssignmentFn = customValidatorAssignments.findOrDefault(
|
|
|
|
conf.network, defaultValidatorAssignment)
|
|
|
|
nodeValidatorCount = validatorAssignmentFn(node.id)
|
2019-11-05 13:00:11 +00:00
|
|
|
|
2019-11-12 12:14:55 +00:00
|
|
|
yield (node,
|
|
|
|
nextValidatorIdx,
|
|
|
|
nextValidatorIdx + nodeValidatorCount)
|
|
|
|
|
|
|
|
inc nextValidatorIdx, nodeValidatorCount
|
2019-09-10 22:13:27 +00:00
|
|
|
|
2019-10-29 13:41:10 +00:00
|
|
|
case conf.cmd
|
|
|
|
of restart_nodes:
|
2019-09-10 22:13:27 +00:00
|
|
|
for n in nodes():
|
2020-02-11 16:45:52 +00:00
|
|
|
if n.id mod 2 == 0:
|
2020-06-03 00:18:25 +00:00
|
|
|
echo &"echo Pulling container image on {n.server} ..."
|
2020-02-11 16:45:52 +00:00
|
|
|
# This will only print one line: "docker.io/statusteam/nimbus_beacon_node:testnet1".
|
|
|
|
echo &"ssh {n.server} docker pull -q statusteam/nimbus_beacon_node:{conf.network}"
|
2020-06-03 00:18:25 +00:00
|
|
|
echo &"echo Starting container {n.container}@{n.server} ..."
|
2020-02-11 16:45:52 +00:00
|
|
|
# docker-compose will rebuild the container if it detects a newer image.
|
|
|
|
# Prints: "Recreating beacon-node-testnet1-1 ... done".
|
2020-03-26 18:07:19 +00:00
|
|
|
echo &"ssh {n.server} 'cd /docker/{n.container} && docker-compose --compatibility up -d'"
|
2019-10-29 03:58:31 +00:00
|
|
|
|
2019-11-05 13:00:11 +00:00
|
|
|
of reset_network:
|
2019-11-12 12:14:55 +00:00
|
|
|
for n, firstValidator, lastValidator in validatorAssignments():
|
2019-10-29 13:41:10 +00:00
|
|
|
var
|
2020-06-02 19:59:51 +00:00
|
|
|
validatorDirs = ""
|
2020-06-22 19:51:21 +00:00
|
|
|
secretFiles = ""
|
2019-10-29 13:41:10 +00:00
|
|
|
networkDataFiles = conf.networkDataDir & "/{genesis.ssz,bootstrap_nodes.txt}"
|
|
|
|
|
2019-11-12 12:14:55 +00:00
|
|
|
for i in firstValidator ..< lastValidator:
|
2020-06-02 19:59:51 +00:00
|
|
|
validatorDirs.add " "
|
2020-08-01 18:32:41 +00:00
|
|
|
validatorDirs.add conf.validatorsDir / validators[i]
|
2020-06-03 09:07:43 +00:00
|
|
|
secretFiles.add " "
|
|
|
|
secretFiles.add conf.secretsDir / validators[i]
|
2019-10-29 03:58:31 +00:00
|
|
|
|
2019-10-29 16:35:21 +00:00
|
|
|
let dockerPath = &"/docker/{n.container}/data/BeaconNode"
|
2019-11-12 12:14:55 +00:00
|
|
|
echo &"echo Syncing {lastValidator - firstValidator} keys starting from {firstValidator} to container {n.container}@{n.server} ... && \\"
|
2020-06-22 19:51:21 +00:00
|
|
|
echo &" ssh {n.server} 'sudo rm -rf /tmp/nimbus && mkdir -p /tmp/nimbus/{{net-data,validators,secrets}}' && \\"
|
2020-02-11 16:45:52 +00:00
|
|
|
echo &" rsync -a -zz {networkDataFiles} {n.server}:/tmp/nimbus/net-data/ && \\"
|
2020-06-22 19:51:21 +00:00
|
|
|
if validators.len > 0:
|
2020-06-03 09:07:43 +00:00
|
|
|
echo &" rsync -a -zz {validatorDirs} {n.server}:/tmp/nimbus/validators/ && \\"
|
|
|
|
echo &" rsync -a -zz {secretFiles} {n.server}:/tmp/nimbus/secrets/ && \\"
|
2019-11-05 13:00:11 +00:00
|
|
|
|
2020-02-26 16:59:31 +00:00
|
|
|
echo &" ssh {n.server} 'sudo docker container stop {n.container}; " &
|
2020-06-22 19:51:21 +00:00
|
|
|
&"sudo rm -rf {dockerPath}/{{db,validators,secrets,net-data}}* && " &
|
|
|
|
&"sudo mv /tmp/nimbus/* {dockerPath}/ && " &
|
2019-09-10 22:13:27 +00:00
|
|
|
&"sudo chown dockremap:docker -R {dockerPath}'"
|
2019-10-29 13:41:10 +00:00
|
|
|
|