Skip to content

Instantly share code, notes, and snippets.

@tolbrino
Last active March 1, 2022 16:01
Show Gist options
  • Save tolbrino/dcd6d940ad43a7e00f3e4502006660ed to your computer and use it in GitHub Desktop.
Save tolbrino/dcd6d940ad43a7e00f3e4502006660ed to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -Eeuo pipefail
msg() {
echo >&2 -e "${1-}"
}
usage() {
msg
msg "Usage: $0 [-h|--help] [-c|--count <count>] [-f|--folder] [-r|--release] [-p|--base-port]"
msg
msg "where <count>\t\tthe number of hoprd Docker containers to start, defaults to 1"
msg " <folder>\t\tthe base folder for all identity, database and log files, defaults to ./hoprd"
msg " <release>\t\tthe hoprd release which should be run \(mandatory\)"
msg " <base-port>\t\tthe base port which is used to assign hoprd ports, defaults to 11000"
msg
}
declare count="1"
declare folder="$(pwd)/hoprd"
declare release
declare token="^MYtoken4testing^"
declare password="summertime"
declare base_port=11000
while (( "$#" )); do
case "$1" in
-h|--help)
# return early with help info when requested
usage
exit 0
;;
-c|--count)
count="$2"
shift 2
;;
-f|--folder)
folder="$2"
shift 2
;;
-r|--release)
release="$2"
shift 2
;;
-p|--base-port)
base_port="$2"
shift 2
;;
*)
shift
;;
esac
done
# ensure release is set
: ${release?"<release> must be set"}
# ensure count > 0
[ ${count} -gt 0 ] || { msg "<count> must be greater than 0"; exit 1; }
# ensure base port > 1024
[ ${base_port} -gt 1024 ] || { msg "<base_port> must be greater than 1024"; exit 1; }
# ensure base folder exists
mkdir -p "${folder}"
# $1 = node number
start_node() {
local i="${1}"
local container_name="hoprd_${release//\//_}_${i}"
local container_image="gcr.io/hoprassociation/hoprd:${release}"
local host_folder="${folder}/${container_name}"
local admin_port=$((base_port+i*10+1))
local rest_port=$((base_port+i*10+2))
local p2p_port=$((base_port+i*10+3))
# ensure host folder exists
mkdir -p "${host_folder}"
# stop and remove previous container
docker inspect "${container_name}" >/dev/null && {
docker stop "${container_name}" >/dev/null || :
docker rm "${container_name}" >/dev/null
}
# we can remove the container, since the data is kept on the host and can be
# used later again
msg "Starting docker container ${container_name}"
docker run \
-tid \
--pull always \
--restart on-failure \
--name "${container_name}" \
-p 127.0.0.1:${admin_port}:3000/tcp \
-p 127.0.0.1:${rest_port}:3001/tcp \
-p 127.0.0.1:${p2p_port}:9091/tcp \
--mount type=bind,src=${host_folder},dst=/app/db \
${container_image} \
--admin \
--adminHost "0.0.0.0" \
--apiToken "${token}" \
--data "/app/db/data" \
--identity "/app/db/identity" \
--init \
--password "${password}" \
--rest \
--restHost "0.0.0.0"
msg
msg "Started node ${container_name}"
msg
msg "Admin UI: http://localhost:${admin_port}"
msg "Rest API: http://localhost:${rest_port}"
msg "Data Folder: ${host_folder}"
msg
}
while [ ${count} -gt 0 ]; do
# start node for counter value
start_node "${count}"
# decrement counter and repeat
let count--
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment