diff --git a/setup.sh b/setup.sh index b9c8aa39d..7b7ded427 100755 --- a/setup.sh +++ b/setup.sh @@ -1,33 +1,72 @@ #!/bin/bash -new_key_base=`openssl rand -hex 64` -s3_key=`openssl rand -hex 40` -timezone=`curl https://ipapi.co/timezone` ENV_FILE=.env -if [[ -f "$ENV_FILE" ]]; then - if grep -Fq "SECRET_KEY_BASE=" $ENV_FILE; then - sed -i '' "s/^SECRET_KEY_BASE=.*/SECRET_KEY_BASE=$new_key_base/g" $ENV_FILE +_has_variable() { + variable=$1 + grep -q "^$variable" $ENV_FILE + if [[ $? -eq 0 ]]; then + return 0 else - echo 'SECRET_KEY_BASE='$new_key_base >> $ENV_FILE + return 1 fi -else - echo 'DB_NAME=production' >> $ENV_FILE - echo 'DB_USER=aquarium' >> $ENV_FILE - echo 'DB_PASSWORD=aSecretAquarium' >> $ENV_FILE - echo 'S3_SERVICE=minio' >> $ENV_FILE - echo 'S3_ID=aquarium_minio' >> $ENV_FILE - echo 'S3_SECRET_ACCESS_KEY='$s3_key >> $ENV_FILE - echo 'S3_REGION=us-west-1' >> $ENV_FILE - echo 'TIMEZONE='$timezone >> $ENV_FILE - echo 'SECRET_KEY_BASE='$new_key_base >> $ENV_FILE +} + +_set_value() { + variable=$1 + value=$2 + echo $variable=$value >> $ENV_FILE +} + +_set_variable() { + variable=$1 + value=$2 + _has_variable $variable + if [[ $? -gt 0 ]]; then + _set_value $variable $value + fi +} + +_set_random() { + variable=$1 + length=$2 + _has_variable $variable + if [[ $? -gt 0 ]]; then + value=`openssl rand -hex $length` + _set_value $variable $value + fi +} + +_set_timezone() { + _has_variable 'TIMEZONE' + if [[ $? -gt 0 ]]; then + timezone=`curl https://ipapi.co/timezone` 2> /dev/null + _set_variable 'TIMEZONE' $timezone + fi +} + +if [[ ! -f "$ENV_FILE" ]]; then + echo "Initializing configuration file" + touch $ENV_FILE fi -DATA_DIR=./docker -DB_INIT_DIR=$DATA_DIR/mysql_init +_set_variable 'APP_PUBLIC_PORT' '80' +_set_variable 'S3_PUBLIC_PORT' '9000' +_set_variable 'DB_NAME' 'production' +_set_variable 'DB_USER' 'aquarium' +_set_variable 'DB_PASSWORD' 'aSecretAquarium' +_set_variable 'S3_SERVICE' 'minio' +_set_variable 'S3_ID' 'aquarium_minio' +_set_variable 'S3_REGION' 'us-west-1' +_set_random 'S3_SECRET_ACCESS_KEY' '40' +_set_random 'SECRET_KEY_BASE' '64' +_set_timezone + +DB_INIT_DIR=./docker/mysql_init DB_FILE=$DB_INIT_DIR/dump.sql if [[ ! -f "$DB_FILE" ]]; then cp $DB_INIT_DIR/default.sql $DB_INIT_DIR/dump.sql fi + # TODO: allow user to set other values # TODO: make this a git post-checkout hook, though don't replace secret_key_base \ No newline at end of file