Last active
April 11, 2018 12:29
-
-
Save iuridiniz/1d445495d7acf2950767 to your computer and use it in GitHub Desktop.
script to start local databases instances under /tmp (ubuntu)
This file contains hidden or 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
#!/bin/sh | |
################################## | |
# CONFIG | |
COUCHDB_SERVER=${COUCHDB_SERVER:=$( which couchdb )} | |
# if couchdb was not found under PATH, try a search on ~/.local/opt and /opt | |
COUCHDB_SERVER=${COUCHDB_SERVER:=$( find ~/.local/opt /opt -name 'couchdb' -type f -executable -print -quit )} | |
COUCHDB_BASEDIR=${COUCHDB_BASEDIR:="${COUCHDB_SERVER%/*}/.."} | |
COUCHDB_HOME=${COUCHDB_HOME:="/tmp/couchdb"} | |
COUCHDB_DB_DATA=${COUCHDB_DB_DATA:="${COUCHDB_HOME}/data"} | |
################################# | |
# Default command | |
CMD=${1:-"start"} | |
# PID FILE | |
PID="${COUCHDB_HOME}/couchdb.pid" | |
LOG="${COUCHDB_HOME}/couchdb.log" | |
INI="${COUCHDB_HOME}/couchdb.ini" | |
URI="${COUCHDB_HOME}/couchdb.uri" | |
DEFAULT_INI="${COUCHDB_BASEDIR}/etc/default.ini" | |
LOCAL_INI="${COUCHDB_BASEDIR}/etc/local.ini" | |
# XXX: avoid epmd to listen on all address | |
# see: http://erlang.org/doc/man/epmd.html#environment_variables | |
export ERL_EPMD_ADDRESS="127.0.0.1" | |
if [ "$CMD" = "start" ]; then | |
which crudini >/dev/null || exit 1 | |
[ -x "$COUCHDB_SERVER" ] || exit 1 | |
if [ ! -d "$COUCHDB_HOME" ]; then | |
mkdir -p "${COUCHDB_HOME}" | |
fi | |
if [ ! -d "$COUCHDB_DB_DATA" ]; then | |
mkdir -p "${COUCHDB_DB_DATA}" | |
fi | |
if [ ! -e "$INI" ]; then | |
cp $COUCHDB_BASEDIR/etc/default.ini "$INI" | |
uuid=$(echo $RANDOM | md5sum | awk '{print $1}') | |
crudini --set "$INI" couchdb database_dir $COUCHDB_DB_DATA | |
crudini --set "$INI" couchdb view_index_dir $COUCHDB_DB_DATA | |
crudini --set "$INI" couchdb uri_file $URI | |
crudini --set "$INI" log file $LOG | |
crudini --set "$INI" log writer file | |
fi | |
_ip_cluster=$( crudini --get "$INI" chttpd bind_address 2>/dev/null || echo 127.0.0.1 ) | |
[ "$_ip_cluster" = "0.0.0.0" ] && _ip_cluster="127.0.0.1" | |
_port_cluster=$( crudini --get "$INI" chttpd port 2>/dev/null || echo 5984 ) | |
_ip_node=$( crudini --get "$INI" httpd bind_address 2>/dev/null || echo 127.0.0.1 ) | |
[ "$_ip_node" = "0.0.0.0" ] && _ip_node="127.0.0.1" | |
_port_node=$( crudini --get "$INI" httpd port 2>/dev/null || echo 5986 ) | |
echo "Starting: '$COUCHDB_SERVER'" | |
echo | |
echo " CLUSTER:" | |
echo " # --> Access the cluster at http://$_ip_cluster:$_port_cluster/_utils/index.html" | |
echo " # --> The offline documenation could be reached at http://$_ip_cluster:$_port_cluster/_utils/docs/" | |
echo | |
echo " NODE:" | |
echo " # --> Access this node at http://$_ip_node:$_port_node/_utils/index.html" | |
echo " # --> Get config of this node by: " | |
echo " # $ curl -X GET http://$_ip_node:$_port_node/_config/" | |
exec start-stop-daemon \ | |
--start \ | |
--oknodo \ | |
--background \ | |
--pidfile "$PID" \ | |
--exec "$COUCHDB_SERVER" \ | |
-- \ | |
-pidfile "$PID" \ | |
-couch_ini "$INI" | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping '$COUCHDB_SERVER'" | |
exec start-stop-daemon \ | |
--stop \ | |
--oknodo \ | |
--pidfile "$PID" | |
elif [ "$CMD" = "status" ]; then | |
exec start-stop-daemon \ | |
--status \ | |
--oknodo \ | |
--pidfile "$PID" | |
fi |
This file contains hidden or 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
#!/bin/sh | |
################################## | |
# CONFIG | |
MONGO_SERVER=${MONGO_SERVER:=$( which mongod )} | |
# if mongod was not found under PATH, try a search on ~/.local/opt and /opt | |
MONGO_SERVER=${MONGO_SERVER:=$( find ~/.local/opt /opt -name 'mongod' -print -quit )} | |
MONGO_HOME=${MONGO_HOME:="/tmp/mongod"} | |
MONGO_PORT=${MONGO_PORT:="27017"} | |
MONGO_IP=${MONGO_IP:="127.0.0.1"} | |
################################# | |
# Default command | |
CMD=${1:-"start"} | |
BASENAME=$(basename "$0") | |
MONGO_DBNAME="${BASENAME}.${MONGO_PORT}" | |
MONGO_DBPATH="${MONGO_HOME}/${MONGO_DBNAME}.db" | |
MONGO_PID="${MONGO_HOME}/${MONGO_DBNAME}.pid" | |
MONGO_LOG="${MONGO_HOME}/${MONGO_DBNAME}.log" | |
if [ "$CMD" = "start" ]; then | |
[ -x "$MONGO_SERVER" ] || exit 1 | |
if [ ! -d "${MONGO_DBPATH}" ]; then | |
mkdir -p "${MONGO_DBPATH}" | |
fi | |
echo "Starting: '${MONGO_SERVER}' [$MONGO_DBNAME]" | |
exec start-stop-daemon \ | |
--start \ | |
--oknodo \ | |
--pidfile "${MONGO_PID}" \ | |
--exec "${MONGO_SERVER}" \ | |
-- \ | |
--dbpath "${MONGO_DBPATH}" \ | |
--unixSocketPrefix "${MONGO_HOME}" \ | |
--port "${MONGO_PORT}" \ | |
--bind_ip "${MONGO_IP}" \ | |
--pidfilepath "${MONGO_PID}" \ | |
--logpath "${MONGO_LOG}" \ | |
--noauth \ | |
--verbose \ | |
--cpu \ | |
--fork | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping '$MONGO_SERVER' [$MONGO_DBNAME]" | |
exec start-stop-daemon \ | |
--stop \ | |
--oknodo \ | |
--pidfile "$MONGO_PID" | |
elif [ "$CMD" = "status" ]; then | |
exec start-stop-daemon \ | |
--status \ | |
--oknodo \ | |
--pidfile "$MONGO_PID" | |
fi |
This file contains hidden or 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
-- MASTER: create an user for replication | |
CREATE USER 'repl'@'%' IDENTIFIED BY 'Zkf5GIy-d47BTXt1-KR_.'; | |
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; | |
-- MASTER: flush binlog and lock tables | |
FLUSH TABLES WITH READ LOCK; | |
-- SHELL: CREATE DATABASE DUMP (or use rsync to master node) | |
-- MASTER (in another connection): show status | |
SHOW MASTER STATUS; | |
-- Write down the file and posistion id | |
-- MASTER: exit all remaing connections (this will unlock tables) | |
-- | |
-- ---------------------------------------------------------- | |
-- SLAVE: restore database [if rsync was used, you do not need to restone anything ] | |
-- SLAVE: set master (got info from SHOW MASTER STATUS); | |
CHANGE MASTER TO | |
MASTER_HOST='localhost', | |
MASTER_USER='repl', | |
MASTER_PORT=13306, | |
MASTER_PASSWORD='slavepass', | |
MASTER_LOG_FILE='log-1-bin.000001', | |
MASTER_LOG_POS=595; | |
-- SLAVE: start replication [cmd: start slave] | |
-- ############################################################################ | |
------------------------------------------------------------------------------- | |
-- ############################################################################ | |
-- ----- SHOW SLAVE STATUS\G | |
-- -rsync -a --exclude='cel.*' 172.16.7.66:/var/lib/mysql/ /var/lib/mysql_remote/ | |
-- see autossh [https://superuser.com/questions/37738/how-to-reliably-keep-an-ssh-tunnel-open] | |
Tue Mar 21 22:30:40 BRT 2017 | |
-- MariaDB [(none)]> SHOW MASTER STATUS; | |
+------------------+----------+--------------+------------------+ | |
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | | |
+------------------+----------+--------------+------------------+ | |
| mysql-bin.000001 | 71682 | | | | |
+------------------+----------+--------------+------------------+ | |
1 row in set (0.00 sec) | |
============================= | |
CHANGE MASTER TO | |
MASTER_HOST='172.16.7.66', | |
MASTER_USER='repl', | |
MASTER_PORT=63306, | |
MASTER_PASSWORD='Zkf5GIy-d47BTXt1-KR_.', | |
MASTER_LOG_FILE='mysql-bin.000001', | |
MASTER_LOG_POS=71682; | |
172.16.7.66 | |
iptables -t nat -A PREROUTING -p tcp -s 172.16.7.69 -d 172.16.7.66 --dport 63306 -j DNAT --to-destination 127.0.0.1:3306 | |
sysctl -w net.ipv4.conf.eth0.route_localnet=1 | |
-- http://turbogears.readthedocs.io/en/latest/cookbook/master-slave.html | |
-- http://unix.stackexchange.com/questions/111433/iptables-redirect-outside-requests-to-127-0-0-1 | |
-- CREATE USER 'repl'@'%' IDENTIFIED BY 'GQhASL2WPr8y'; | |
mysql> show master status; | |
+------------------+----------+--------------+------------------+ | |
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | | |
+------------------+----------+--------------+------------------+ | |
| mysql-bin.000002 | 23843838 | | | | |
+------------------+----------+--------------+------------------+ | |
1 row in set (0.01 sec) | |
mysql> | |
------------ | |
RESET SLAVE; | |
CHANGE MASTER TO | |
MASTER_HOST='192.168.88.1', | |
MASTER_USER='repl', | |
MASTER_PORT=3306, | |
MASTER_PASSWORD='GQhASL2WPr8y', | |
MASTER_LOG_FILE='mysql-bin.000002', | |
MASTER_LOG_POS=23843838; | |
This file contains hidden or 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
#!/bin/sh | |
################################## | |
# CONFIG | |
MYSQL_HOME=${MYSQL_HOME:=""} | |
MYSQLD_SAFE=${MYSQLD_SAFE:=$( which mysqld_safe )} | |
# MYSQLD=${MYSQLD:=$( which mysqld )} | |
MYSQL_INSTALL=${MYSQL_INSTALL:=$( which mysqld )} | |
# REPLICATION | |
MYSQL_MASTER=${MYSQL_MASTER:-""} | |
MYSQL_BIND_PORT=${MYSQL_BIND_PORT:-""} | |
MYSQL_SERVER_ID=${MYSQL_SERVER_ID:-""} | |
################################# | |
default_port=3306 | |
default_home="/tmp/mysql" | |
# run on slave mode master id is set | |
if [ -n "$MYSQL_MASTER_ID" ]; then | |
# slave node | |
MYSQL_SERVER_ID=${MYSQL_SERVER_ID:-$(( $MYSQL_MASTER_ID + 1 ))} | |
MYSQL_BIND_PORT=${MYSQL_BIND_PORT:-$(( $default_port + $MYSQL_SERVER_ID))} | |
else | |
# master node | |
MYSQL_SERVER_ID=${MYSQL_SERVER_ID:-"1"} | |
MYSQL_BIND_PORT=${MYSQL_BIND_PORT:-"$default_port"} | |
fi | |
MYSQL_HOME=${MYSQL_HOME:="$default_home/$MYSQL_SERVER_ID"} | |
MYSQL_DATA=${MYSQL_DATA:="${MYSQL_HOME}/data"} | |
# logbin? | |
if [ -z "$MYSQL_MASTER_ID" ]; then | |
# master node | |
REPLICATION_OPTS=" --log_bin=${MYSQL_HOME}/log-${MYSQL_SERVER_ID}-bin.log" | |
fi | |
REPLICATION_OPTS=" $REPLICATION_OPTS --server-id=$MYSQL_SERVER_ID --max_binlog_size=100M" | |
# how to start | |
# mysqld_safe --no-defaults --datadir="$MYSQL_HOME"/data --log-error="$MYSQL_HOME"/error.log --socket="$MYSQL_HOME"/socket | |
# Default command | |
CMD=${1:-"start"} | |
# PID FILE | |
PID="${MYSQL_HOME}/mysql.pid" | |
################ | |
# resolve symlinks | |
MYSQLD_SAFE=$(readlink -f "$MYSQLD_SAFE") | |
MYSQL_INSTALL=$(readlink -f "$MYSQL_INSTALL") | |
if [ "$CMD" = "start" ]; then | |
[ -x "$MYSQLD_SAFE" ] || exit 1 | |
if [ ! -d "$MYSQL_DATA" ]; then | |
[ -x "$MYSQL_INSTALL" ] || exit 1 | |
echo "Installing MYSQL on '$MYSQL_DATA' using '$MYSQL_INSTALL'" | |
# install db | |
mkdir -p "$MYSQL_DATA" | |
"$MYSQL_INSTALL" --no-defaults --log-error="$MYSQL_HOME"/error.log --initialize-insecure --datadir="${MYSQL_DATA}" || exit 1 | |
ln -s "$MYSQL_HOME"/"socket" "$MYSQL_HOME"/../"socket.$MYSQL_SERVER_ID" | |
if [ "$MYSQL_SERVER_ID" = "1" ]; then | |
ln -s "$MYSQL_HOME"/../"socket.$MYSQL_SERVER_ID" "$MYSQL_HOME"/../"socket" | |
fi | |
fi | |
echo "Starting '$MYSQLD_SAFE'" | |
# split MYSQLD_SAFE | |
CHDIR=$(dirname "$MYSQLD_SAFE") | |
CHDIR="$CHDIR/../" | |
MYSQLD_SAFE=$(basename "$MYSQLD_SAFE") | |
MYSQLD_SAFE="bin/$MYSQLD_SAFE" | |
# without password: --skip-grant-tables | |
exec start-stop-daemon \ | |
--start \ | |
--chdir "$CHDIR" \ | |
--background \ | |
--oknodo \ | |
--pidfile "$PID" \ | |
--exec "$MYSQLD_SAFE" \ | |
-- \ | |
--no-defaults \ | |
--pid-file="$PID" \ | |
--datadir="$MYSQL_DATA" \ | |
--log-error="$MYSQL_HOME"/error.log \ | |
--general-log=0 \ | |
--general-log-file="$MYSQL_HOME"/mysql.log \ | |
--socket="$MYSQL_HOME"/socket \ | |
--bind-address=127.0.0.1 \ | |
--port=$MYSQL_BIND_PORT \ | |
--default-storage-engine=MyISAM \ | |
--sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES \ | |
$REPLICATION_OPTS | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping '$MYSQLD_SAFE'" | |
exec start-stop-daemon \ | |
--stop \ | |
--oknodo \ | |
--pidfile "$PID" | |
elif [ "$CMD" = "status" ]; then | |
exec start-stop-daemon \ | |
--status \ | |
--oknodo \ | |
--pidfile "$PID" | |
fi |
This file contains hidden or 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
#!/bin/sh | |
################################## | |
# CONFIG | |
PGHOME=${PGHOME:=""} | |
PGDATA=${PGDATA:=""} | |
PGPORT=${PGPORT:=""} | |
PGCTL=${PGCTL:=$( which pg_ctl )} | |
PGBASEBACKUP=${PGBASEBACKUP:=$( which pg_basebackup )} | |
# debian location for pg_ctl (which is not on $PATH) is /usr/lib/postgresql/$VERSION/bin/pg_ctl | |
PGCTL=${PGCTL:=$( find /usr/lib/postgresql/ -name 'pg_ctl' -type f -executable -print -quit)} | |
# same for pg_basebackup | |
PGBASEBACKUP=${PGBASEBACKUP:=$( find /usr/lib/postgresql/ -name 'pg_basebackup' -type f -executable -print -quit)} | |
PGNODE=${PGNODE:="master"} | |
PGMASTERHOME=${PGMASTERHOME:=""} | |
################################# | |
# REPLICATION | |
# https://wiki.postgresql.org/wiki/Hot_Standby | |
# https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-on-postgresql-on-an-ubuntu-12-04-vps | |
# https://wiki.postgresql.org/wiki/Streaming_Replication | |
# https://wiki.postgresql.org/wiki/Binary_Replication_Tutorial | |
# https://www.niwi.nz/2013/02/16/replication-status-in-postgresql/ | |
# how to start | |
# /usr/lib/postgresql/9.4/bin/postgres -D /tmp/pg -c unix_socket_directories=/tmp | |
freeport() { | |
#__port=$(sysctl -b net.ipv4.ip_local_port_range | awk "{print \$1"}) | |
__port=5432 | |
while netstat -atn | grep -q :$__port; do | |
__port=$(expr $__port + 1); | |
done; | |
echo $__port; | |
unset -v __port | |
} | |
PGHOME=${PGHOME:="/tmp/pg/${PGNODE}"} | |
PGDATA=${PGDATA:="${PGHOME}/data"} | |
PGMASTERHOME=${PGMASTERHOME:="$PGHOME/../master"} | |
# get a port | |
PGPORT=${PGPORT:="$(freeport)"} | |
# Default command | |
CMD=${1:-"start"} | |
STDERR () { | |
cat - 1>&2 | |
} | |
[ -x "$PGCTL" ] || exit 1 | |
if [ "$CMD" = "start" ]; then | |
if [ ! -d "$PGHOME" ]; then | |
mkdir -p "$PGHOME" | |
fi | |
if [ ! -d "$PGDATA" ]; then | |
# configure as master or as slave | |
if [ "$PGNODE" = "master" ]; then | |
$PGCTL -D "$PGDATA" initdb || exit 1 | |
mkdir -p "$PGMASTERHOME/archive" | |
# configure to allow replication and logical decoding | |
cat << END >>"$PGDATA/postgresql.conf" | |
cluster_name = $PGNODE | |
#wal_level = replica # replica is only fot PG >= 9.6 | |
wal_level = logical # allow logical decoding | |
archive_mode = off | |
#archive_command = 'cp -i "%p" "$PGMASTERHOME/archive/%f"' | |
archive_command = 'cd .' | |
max_wal_senders = 5 | |
max_replication_slots = 5 | |
hot_standby = on | |
wal_keep_segments = 10 | |
max_wal_size = 50MB | |
END | |
# create a user for replication | |
cat << END >>"$PGDATA/pg_hba.conf" | |
# Allow replication connections from localhost, by a user with the | |
# replication privilege. | |
local replication $USER trust | |
host replication $USER 127.0.0.1/32 trust | |
host replication $USER ::1/128 trust | |
END | |
else | |
# slaves | |
[ -d "$PGMASTERHOME" ] || exit 1 | |
PGHOST=${PGMASTERHOME} $PGBASEBACKUP -D "$PGDATA" -w -R --xlog-method=stream --dbname="host=localhost user=$USER" | |
[ "$?" != "0" ] && rm -rf "$PGDATA" && exit 1 | |
cat << END >>"$PGDATA/postgresql.conf" | |
cluster_name = $PGNODE | |
#hot_standby = on | |
port = $PGPORT | |
END | |
cat << END >>"$PGDATA/recovery.conf" | |
#standby_mode = 'on' | |
#restore_command = 'cp -i "$PGMASTERHOME/archive/%f" "%p"' | |
#restore_command = 'cd .' | |
END | |
fi | |
if [ "$PGPORT" != "5432" ]; then | |
ln -sf "$PGHOME/.s.PGSQL.$PGPORT" "$PGHOME/.s.PGSQL.5432" | |
fi | |
if [ "$PGNODE" = "master" ]; then | |
ln -s "$PGHOME/.s.PGSQL.$PGPORT" "$PGHOME/../.s.PGSQL.5432" | |
fi | |
fi | |
echo "Starting by using '$PGCTL' on '$PGNODE'" | STDERR | |
$PGCTL -D "$PGDATA" -l "${PGHOME}/postgres.log" -o "-c unix_socket_directories=\"${PGHOME}\"" start | STDERR | |
cat << END | |
# ===================================== | |
# Use this setting in order to connect: | |
export PGHOST='$PGHOME' | |
export PGDATA='$PGDATA' | |
END | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping by using '$PGCTL' on '$PGNODE' at '$PGHOME'" | |
$PGCTL -D "$PGDATA" -l "${PGHOME}/postgres.log" -o "-c unix_socket_directories=\"${PGHOME}\"" stop | |
elif [ "$CMD" = "status" ]; then | |
exec "$PGCTL" -D "$PGDATA" status | |
elif [ "$CMD" = "restart" ]; then | |
$0 status && $0 stop | |
exec $0 start | |
fi |
This file contains hidden or 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
#!/bin/sh | |
################################## | |
# CONFIG | |
REDIS_SERVER=${REDIS_SERVER:=$( which redis-server )} | |
REDIS_HOME=${REDIS_HOME:="/tmp/redis"} | |
################################# | |
# Default command | |
CMD=${1:-"start"} | |
# PID FILE | |
PID="${REDIS_HOME}/redis.pid" | |
if [ "$CMD" = "start" ]; then | |
[ -x "$REDIS_SERVER" ] || exit 1 | |
if [ ! -d "$REDIS_HOME" ]; then | |
mkdir "${REDIS_HOME}" | |
fi | |
echo "Starting: '$REDIS_SERVER'" | |
exec start-stop-daemon \ | |
--start \ | |
--oknodo \ | |
--pidfile "$PID" \ | |
--exec "$REDIS_SERVER" \ | |
-- \ | |
--pidfile "$PID" \ | |
--daemonize "yes" \ | |
--dir "$REDIS_HOME" \ | |
--logfile "$REDIS_HOME"/redis.log \ | |
--unixsocket "$REDIS_HOME"/socket \ | |
--bind 127.0.0.1 | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping '$REDIS_SERVER'" | |
exec start-stop-daemon \ | |
--stop \ | |
--oknodo \ | |
--pidfile "$PID" | |
elif [ "$CMD" = "status" ]; then | |
exec start-stop-daemon \ | |
--status \ | |
--oknodo \ | |
--pidfile "$PID" | |
fi |
This file contains hidden or 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
#!/bin/sh | |
MARK="xxxx" | |
################################## | |
# CONFIG | |
RETHINKDB_SERVER=${RETHINKDB_SERVER:=$( which rethinkdb )} | |
RETHINKDB_HOME=${RETHINKDB_HOME:="/tmp/rethinkdb"} | |
RETHINKDB_CLUSTER_PORT=${RETHINKDB_CLUSTER_PORT:="$MARK"} | |
RETHINKDB_CLIENT_PORT=${RETHINKDB_CLIENT_PORT:="$MARK"} | |
RETHINKDB_HTTP_PORT=${RETHINKDB_HTTP_PORT:="$MARK"} | |
RETHINKDB_IP=${RETHINKDB_IP:="127.0.0.1"} | |
RETHINKDB_JOIN_IP=${RETHINKDB_JOIN_IP:="$RETHINKDB_IP"} | |
RETHINKDB_JOIN_PORT=${RETHINKDB_JOIN_PORT:="$MARK"} | |
################################# | |
_freeport() { | |
local port; | |
if [ -z "$1" ]; then | |
port=$(sysctl -b net.ipv4.ip_local_port_range | awk "{print \$1"}); | |
else | |
port="$1" | |
fi | |
while netstat -atn | grep -q :$port; do | |
port=$(expr $port + 1); | |
done; | |
echo $port | |
} | |
STDERR () { | |
cat - 1>&2 | |
} | |
# DEFAULT PORTS | |
RETHINKDB_DEFAULT_CLUSTER_PORT="29015" | |
RETHINKDB_DEFAULT_CLIENT_PORT="28015" | |
RETHINKDB_DEFAULT_HTTP_PORT="30015" | |
# DEFAULT CMD from ARGV1 or (start|join) | |
CMD=${1:-"start"} | |
# has a join port variable and no command, I think the user wants to join | |
if [ -z "$1" -a "$RETHINKDB_JOIN_PORT" != "$MARK" ]; then | |
CMD="join" | |
fi | |
# RETHINKDB_CLIENT_PORT ? | |
# if ARGV2 override CLIENT_PORT | |
if [ -n "$2" ]; then | |
RETHINKDB_CLIENT_PORT=$2 | |
fi | |
# RETHINKDB_JOIN_PORT ? | |
# if ARGV3 override JOIN_PORT | |
if [ -n "$3" ]; then | |
RETHINKDB_JOIN_PORT=$3 | |
fi | |
# SERVER PORTS | |
if [ "$CMD" = "join" ]; then | |
[ "$RETHINKDB_CLIENT_PORT" = "$MARK" ] && \ | |
RETHINKDB_CLIENT_PORT=$(_freeport $RETHINKDB_DEFAULT_CLIENT_PORT) | |
[ "$RETHINKDB_HTTP_PORT" = "$MARK" ] && \ | |
RETHINKDB_HTTP_PORT=$(_freeport $RETHINKDB_DEFAULT_HTTP_PORT) | |
[ "$RETHINKDB_CLUSTER_PORT" = "$MARK" ] && \ | |
RETHINKDB_CLUSTER_PORT=$(_freeport $RETHINKDB_DEFAULT_CLUSTER_PORT) | |
# connect to default instance | |
[ "$RETHINKDB_JOIN_PORT" = "$MARK" ] && \ | |
RETHINKDB_JOIN_PORT=$RETHINKDB_DEFAULT_CLUSTER_PORT | |
else | |
[ "$RETHINKDB_CLIENT_PORT" = "$MARK" ] && \ | |
RETHINKDB_CLIENT_PORT=$RETHINKDB_DEFAULT_CLIENT_PORT | |
[ "$RETHINKDB_HTTP_PORT" = "$MARK" ] && \ | |
RETHINKDB_HTTP_PORT=$(_freeport $RETHINKDB_DEFAULT_HTTP_PORT) | |
[ "$RETHINKDB_CLUSTER_PORT" = "$MARK" ] && \ | |
RETHINKDB_CLUSTER_PORT=$(_freeport $RETHINKDB_DEFAULT_CLUSTER_PORT) | |
# others must connect to me | |
RETHINKDB_JOIN_PORT=$RETHINKDB_CLUSTER_PORT | |
RETHINKDB_JOIN_IP=$RETHINKDB_IP | |
fi | |
BASENAME=$(basename "$0") | |
RETHINKDB_DBNAME="${BASENAME}.${RETHINKDB_CLIENT_PORT}" | |
RETHINKDB_DBPATH="${RETHINKDB_HOME}/${RETHINKDB_DBNAME}.db" | |
RETHINKDB_PID="${RETHINKDB_HOME}/${RETHINKDB_DBNAME}.pid" | |
RETHINKDB_LOG="${RETHINKDB_HOME}/${RETHINKDB_DBNAME}.log" | |
if [ "$CMD" = "start" -o "$CMD" = "join" ]; then | |
[ -x "$RETHINKDB_SERVER" ] || exit 1 | |
if [ ! -d "${RETHINKDB_DBPATH}" ]; then | |
mkdir -p "${RETHINKDB_DBPATH}" | |
fi | |
RETHINKDB_EXTRA_OPTS="" | |
if [ "$CMD" = "join" ]; then | |
RETHINKDB_EXTRA_OPTS="--join $RETHINKDB_JOIN_IP:$RETHINKDB_JOIN_PORT" | |
fi | |
echo "Starting: '${RETHINKDB_SERVER}' [$RETHINKDB_DBNAME]" | STDERR | |
echo " export RETHINKDB_CLIENT_PORT=$RETHINKDB_CLIENT_PORT" | |
echo " export RETHINKDB_HTTP_PORT=$RETHINKDB_HTTP_PORT" | |
echo " export RETHINKDB_CLUSTER_PORT=$RETHINKDB_CLUSTER_PORT" | |
echo " # connect to this cluter by:" | |
echo " export RETHINKDB_JOIN_IP=$RETHINKDB_JOIN_IP" | |
echo " export RETHINKDB_JOIN_PORT=$RETHINKDB_JOIN_PORT" | |
echo " # access this server on http://$RETHINKDB_IP:$RETHINKDB_HTTP_PORT/" | |
exec start-stop-daemon \ | |
--start \ | |
--oknodo \ | |
--pidfile "${RETHINKDB_PID}" \ | |
--exec "${RETHINKDB_SERVER}" \ | |
-- \ | |
--directory "${RETHINKDB_DBPATH}" \ | |
--bind "${RETHINKDB_IP}" \ | |
--driver-port "${RETHINKDB_CLIENT_PORT}" \ | |
--cluster-port "${RETHINKDB_CLUSTER_PORT}" \ | |
--http-port "${RETHINKDB_HTTP_PORT}" \ | |
--pid-file "${RETHINKDB_PID}" \ | |
--log-file "${RETHINKDB_LOG}" \ | |
$RETHINKDB_EXTRA_OPTS \ | |
--daemon | |
elif [ "$CMD" = "stop" ]; then | |
echo "Stopping '$RETHINKDB_SERVER' [$RETHINKDB_DBNAME]" | STDERR | |
exec start-stop-daemon \ | |
--stop \ | |
--oknodo \ | |
--pidfile "$RETHINKDB_PID" | |
elif [ "$CMD" = "status" ]; then | |
exec start-stop-daemon \ | |
--status \ | |
--oknodo \ | |
--pidfile "$RETHINKDB_PID" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment