Last active
June 10, 2020 14:46
-
-
Save wwerner/16bffe328cc6fe1c134f41de55e867bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Like this script? Generate boilerplate for similar ones at https://bashplate.now.sh. | |
set -o errexit # exit on error | |
set -o nounset # don't allow unset variables | |
# set -o xtrace # enable for debugging | |
usage() { | |
printf "Starts a CockroachDB cluster on the current node\n\nRequires cockroach binary on the path and the ports to be available.\n" | |
printf "Usage: $(basename "$0") " | |
printf -- "[-h] " | |
printf -- "[-v] " | |
printf -- "[-n=< nodes >] " | |
printf -- "[-p=< port >] " | |
printf -- "-d=< dir > " | |
printf -- "[-a=< admin-port >] " | |
printf "\n" | |
printf " -%s\t%s - %s%s\n" "h" "help" "Show this help message." "" | |
printf " -%s\t%s - %s%s\n" "v" "version" "Show version information." "" | |
printf " -%s\t%s - %s%s\n" "n" "nodes" "Number of nodes" " (default: 3)" | |
printf " -%s\t%s - %s%s\n" "p" "port" "Port of the first node, subsequent nodes will increment this port" " (default: 26257)" | |
printf " -%s\t%s - %s%s\n" "d" "dir" "Data root directory, each node will store its data in a sub folder named 'node-<n>'" "(default: .)" | |
printf " -%s\t%s - %s%s\n" "a" "admin-port" "Port for the admin interface, subsequent nodes will increment this port" " (default: 8080)" | |
} | |
version() { | |
printf "0.0.1\n" | |
} | |
# default values | |
opt_help="false" | |
opt_version="false" | |
opt_nodes="3" | |
opt_dir="." | |
opt_port="26257" | |
opt_admin_port="8080" | |
# option parsing | |
OPTSPEC=:hvn:p:d:a: | |
while getopts $OPTSPEC option; do | |
case "$option" in | |
h ) usage; opt_help="true"; exit 0 ;; | |
v ) version; opt_version="true"; exit 0 ;; | |
n ) opt_nodes=$OPTARG; ;; | |
p ) opt_port=$OPTARG; ;; | |
d ) opt_dir=$OPTARG; ;; | |
a ) opt_admin_port=$OPTARG; ;; | |
\? ) echo "Unknown option: -$OPTARG" >&2; exit 1;; | |
: ) echo "Missing option argument for -$OPTARG" >&2; exit 1;; | |
* ) echo "Unimplemented option: -$OPTARG" >&2; exit 1;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# convenience variables | |
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" | |
__base="$(basename ${__file} .sh)" | |
__root="$(cd "$(dirname "${__dir}")" && pwd)" # update this to make it point your project's root | |
# this would be a good place to start writing your actual script. | |
echo "$__base was called with..." | |
echo -h, --help=$opt_help | |
echo -v, --version=$opt_version | |
echo -n, --nodes=$opt_nodes | |
echo -p, --port=$opt_port | |
echo -d, --dir=$opt_dir | |
echo -a, --admin-port=$opt_admin_port | |
i=1 | |
nodelist="localhost:"$opt_port | |
while [[ $i -lt $opt_nodes ]] | |
do | |
port=$(($opt_port + $i)) | |
nodelist="$nodelist,localhost:$port" | |
((i = i + 1)) | |
done | |
echo $nodelist | |
j=0 | |
while [[ $j -lt $opt_nodes ]] | |
do | |
port=$(($opt_port + $j)) | |
adminport=$(($opt_admin_port + $j)) | |
dir="$opt_dir/node-$j" | |
cockroach start \ | |
--insecure \ | |
--listen-addr=localhost:$port \ | |
--join=$nodelist \ | |
--http-addr=localhost:$adminport \ | |
--store=$dir \ | |
--background | |
((j = j + 1)) | |
done | |
cockroach init --host localhost:$opt_port --insecure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment