Skip to content

Instantly share code, notes, and snippets.

@tristan
Created September 6, 2017 10:07
Show Gist options
  • Save tristan/56ac778fdd2df8178fc70bd7d0ec4696 to your computer and use it in GitHub Desktop.
Save tristan/56ac778fdd2df8178fc70bd7d0ec4696 to your computer and use it in GitHub Desktop.
run toshi bot without docker
# required
TOKEN_APP_USERNAME=
TOKEN_APP_SEED=
# optional
STAGE=development
DATABASE_URL=postgres://toshi@localhost/toshi-bot-$(echo $TOKEN_APP_SEED | $(command -v md5sum || command -v md5) | cut -b -32)?sslmode=disable
#TOKEN_APP_NAME="Robot #"
LOG_LEVEL=DEBUG
ENABLE_LOG_TIMESTAMPS=1
TOSHI_APP_IS_PUBLIC=false
#!/usr/bin/env bash
set -xeuo pipefail
IFS=$'\n\t'
# check dependencies
if ! command -v redis-server >/dev/null 2>&1; then
echo "ERROR: missing requirement: redis-server"
case $OSTYPE in
darwin*)
if command -v brew >/dev/null 2>&1; then
echo "Install with: brew install redis"
elif comman -v port >/dev/null 2>&1; then
echo "Install with: sudo port install redis"
else
echo "See https://redis.io/ for installation details"
fi
;;
linux*)
if command -v apt-get >/dev/null 2>&1; then
echo "Install with: sudo apt-get install redis-server"
elif command -v pacman >/dev/null 2>&1; then
echo "Install with: sudo pacman -S redis"
else
echo "See https://redis.io/ for installation details"
fi
;;
*)
echo "See https://redis.io/ for installation details"
;;
esac
exit 1
fi
if ! command -v yarn >/dev/null 2>&1; then
echo "ERROR: missing requirement: yarn"
case $OSTYPE in
darwin*)
if command -v brew >/dev/null 2>&1; then
echo "Install with: brew install yarn"
elif comman -v port >/dev/null 2>&1; then
echo "Install with: sudo port install yarn"
else
echo "See https://yarnpkg.com for installation details"
fi
;;
linux*)
if command -v apt-get >/dev/null 2>&1; then
echo "Install with: sudo apt-get install yarn"
elif command -v pacman >/dev/null 2>&1; then
echo "Install with: sudo pacman -S yarn"
else
echo "See https://yarnpkg.com for installation details"
fi
;;
*)
echo "See https://yarnpkg.com for installation details"
;;
esac
exit 1
fi
if ! command -v jrunscript >/dev/null 2>&1; then
echo "ERROR: missing requirement: java"
case $OSTYPE in
darwin*)
if command -v brew >/dev/null 2>&1; then
echo "Install with: brew cask install java"
else
echo "Google search for the best way to install this for your OS."
fi
;;
*)
echo "Google search for the best way to install this for your OS."
;;
esac
exit 1
fi
# check for
if ! jrunscript -e 'if (javax.crypto.Cipher.getMaxAllowedKeyLength("RC5") >= 256) { exit(0); } else { exit(1); }'; then
echo "ERROR: missing requirement: Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy"
case $OSTYPE in
darwin*)
if command -v brew >/dev/null 2>&1; then
echo "Install with: brew cask install jce-unlimited-strength-policy"
else
echo "Google search for the best way to install this for your OS."
fi
;;
*)
echo "Google search for the best way to install this for your OS."
;;
esac
exit 1
fi
# install javascript dependencies
yarn
# check for arguments in command line
for filename in "$@"; do
if [[ $filename == *.conf ]]; then
if [ ! -z ${CONFIG_FILE:-} ]; then
echo "ERROR: got multiple config files"
exit 1
fi
CONFIG_FILE=$filename
elif [[ $filename == *.js ]]; then
if [ ! -z ${BOT_JS:-} ]; then
echo "ERROR: got multiple javascript files"
exit 1
fi
BOT_JS=$filename
else
echo "ERROR: Unknown argument: $filename"
echo "Only *.conf and *.js files are allowed as arguments"
exit 1
fi
done
if [ -z ${CONFIG_FILE:-} ]; then
CONFIG_FILE="bot.conf"
fi
if [ ! -e $CONFIG_FILE ]; then
echo "ERROR: missing config file: $CONFIG_FILE"
exit 1
fi
if [ -z ${BOT_JS:-} ]; then
BOT_JS="src/bot.js"
fi
if [ ! -e $BOT_JS ]; then
echo "ERROR: missing bot entry file: $BOT_JS"
exit 1
fi
source $CONFIG_FILE
# export all the variables from the config file, excluding lines starting with # and empty lines
export $(cut -d= -f1 $CONFIG_FILE | grep -v -G '^\s*#.*$' | grep -v -G '^\s*$')
# default stage set to production
if [ -z ${STAGE:-} ]; then
STAGE=production
export STAGE
fi
# make sure database url is set
if [ -z ${DATABASE_URL:-} ]; then
# default to sqlite db
DATABASE_URL=sqlite:///tokenbot-$STAGE.$TOKEN_APP_USERNAME.sqlite.db
export DATABASE_URL
elif [[ $DATABASE_URL == postgres*://* ]]; then
IFS="," read DB_PROTO DB_USER DB_PASS DB_HOST DB_PORT DB_NAME <<< $(echo $DATABASE_URL | perl -lne 'print "$1,$2,$3,$4,$5,$6" if /^(postgres(?:ql)?|sqlite):\/\/([^:@]*)(?::([^@]*))?@([^:\/]*?)(?::(.*?))?\/([^\?]*?)(?:\?.*)$/')
# attempt to do the following based of DATABASE_URL
if ! psql $DATABASE_URL -c "SELECT 1" >/dev/null 2>&1; then
case $OSTYPE in
darwin*)
echo "Attempting to create database user and database..."
if [ ! -z $DB_USER ]; then
if [ ! -z $DB_PASS ]; then
psql -h ${DB_HOST:-localhost} -p ${DB_PORT:-5432} -c "CREATE ROLE $DB_NAME WITH ENCRYPTED PASSWORD '"$DB_PASS"'" || true
else
createuser -h ${DB_HOST:-localhost} -p ${DB_PORT:-5432} $DB_USER || true
fi
fi
createdb -h ${DB_HOST:-localhost} -p ${DB_PORT:-5432} -O $DB_USER $DB_NAME || true
if ! psql $DATABASE_URL -c "SELECT 1" >/dev/null 2>&1; then
echo "ERROR: Failed to create database user and database"
exit 1
fi
;;
linux*)
echo "ERROR: Cannot connect to database: $DATABASE_URL."
echo "Make sure database exists and access rights are correct"
exit 1
;;
esac
fi
elif [[ $DATABASE_URL == postgres*://* ]]; then
echo "Invalid DATABASE_URL"
exit 1
fi
# default timestamps to be on
if [ -z ${ENABLE_LOG_TIMESTAMPS:-} ]; then
ENABLE_LOG_TIMESTAMPS=1
export ENABLE_LOG_TIMESTAMPS
fi
# pull headless client
./gradlew stage
trap killgroup SIGINT
killgroup(){
# kill all tasks
kill 0
}
while :; do
for (( REDIS_PORT = 30000 ; REDIS_PORT <= 65000 ; REDIS_PORT++ )); do
#nc -l -p "$REDIS_PORT" 2>/dev/null && break 2
if ! lsof -i -n -P | grep LISTEN | grep -q ":${REDIS_PORT}"; then
break 2;
fi
done
done
REDIS_URL=redis://localhost:$REDIS_PORT
export REDIS_URL
YMLCONFIG=config/$STAGE.yml
#HEADLESS_JAR=$(ls -t build/libs/toshi-headless*.jar | head -1)
# localhost only
pushd ../token-headless-client
./gradlew ToshiHeadlessClientCapsule
popd
HEADLESS_JAR=$(ls -t ../token-headless-client/build/libs/toshi-headless*.jar | head -1)
redis-server --port $REDIS_PORT --loglevel warning && killgroup || killgroup &
java ${JAVA_OPTS+x} -jar $HEADLESS_JAR $YMLCONFIG && killgroup || killgroup &
node_modules/.bin/nodemon -L $BOT_JS $YMLCONFIG && killgroup || killgroup &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment