DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
+if [ "$1" == "2container" ]
+then
+ TWO_CONTAINER="1"
+ echo "2container argument is deprecated. Use --two-container"
+ shift 1
+fi
+
+while [ ${#} -gt 0 ]; do
+ case "${1}" in
+ --debug)
+ DEBUG="1"
+ SKIP_REBUILD="1"
+ ;;
+ --skip-rebuild)
+ SKIP_REBUILD="1"
+ ;;
+ --two-container)
+ TWO_CONTAINER="1"
+ ;;
+ --skip-connection-test)
+ SKIP_CONNECTION_TEST="1"
+ echo "skipping connection test"
+ ;;
+ esac
+
+ shift 1
+done
+
##
## Make sure only root can run our script
##
check_IP_match() {
HOST="$1"
echo
- echo Checking your domain name . . .
- connect_to_port $HOST 443; ec=$?
+ if [ "$SKIP_CONNECTION_TEST" == 1 ]
+ then
+ echo "Setting EC to 2"
+ ec=2
+ else
+ echo Checking your domain name . . .
+ connect_to_port $HOST 443; ec=$?
+ fi
case $ec in
0)
echo "Connection to $HOST succeeded."
exit 1
;;
2)
- echo "Continuing without port check."
+ echo "Skipping port check."
;;
esac
}
##
check_port() {
- local valid=$(netstat -tln | awk '{print $4}' | grep ":${1}\$")
+ local valid=$(lsof -i:${1} | grep "LISTEN")
if [ -n "$valid" ]; then
echo "Port ${1} appears to already be in use."
fi
}
+assert_smtp_domain() {
+ if ! grep DISCOURSE_SMTP_DOMAIN $web_file >/dev/null 2>&1
+ then
+ echo "Adding SMTP_DOMAIN placeholder to $web_file"
+ sed -i '/^.*DISCOURSE_SMTP_PASSWORD.*/a \ \ #DISCOURSE_SMTP_DOMAIN: discourse.example.com # (required by some providers)' $web_file
+ fi
+ if ! grep DISCOURSE_SMTP_DOMAIN $web_file >/dev/null 2>&1
+ then
+ cat <<EOF
+
+ Adding DISCOURSE_SMTP_DOMAIN to $web_file has failed! This
+ indicates either that your $web_file is very old or otherwise not
+ what the script expects or that there is a bug in this script. The
+ best solution for a novice is to delete $web_file and start over.
+ An expert might prefer to edit $web_file by hand.
+
+EOF
+ read -p "Press return to continue or control-c to quit..."
+ fi
+}
+
+
+assert_notification_email() {
+ if ! grep DISCOURSE_NOTIFICATION_EMAIL $web_file >/dev/null 2>&1
+ then
+ echo "Adding DISCOURSE_NOTIFICATION_EMAIL placeholder to $web_file"
+ sed -i '/^.*DISCOURSE_SMTP_PASSWORD.*/a \ \ #DISCOURSE_NOTIFICATION_EMAIL: nobody@discourse.example.com # (address to send notifications from)' $web_file
+ fi
+ if ! grep DISCOURSE_NOTIFICATION_EMAIL $web_file >/dev/null 2>&1
+ then
+ cat <<EOF
+
+ Adding DISCOURSE_NOTIFICATION_EMAIL to $web_file has failed! This
+ indicates either that your $web_file is very old or otherwise not
+ what the script expects or that there is a bug in this script. The
+ best solution for a novice is to delete $web_file and start over.
+ An expert might prefer to edit $web_file by hand.
+
+EOF
+ read -p "Press return to continue or control-c to quit..."
+ fi
+}
+
+
##
## prompt user for typical Discourse config file values
##
# NOTE: Defaults now come from standalone.yml
+ read_config "DISCOURSE_HOSTNAME"
+ hostname=$read_config_result
local changelog=/tmp/changelog.$PPID
read_config "DISCOURSE_SMTP_ADDRESS"
local smtp_address=$read_config_result
then
smtp_password = ""
fi
+ read_config "DISCOURSE_NOTIFICATION_EMAIL"
+ local notification_email=$read_config_result
+ read_config "DISCOURSE_SMTP_DOMAIN"
+ local discourse_smtp_DOMAIN=$read_config_result
+
read_config "LETSENCRYPT_ACCOUNT_EMAIL"
local letsencrypt_account_email=$read_config_result
if [ -z $letsencrypt_account_email ]
local maxmind_status="ENTER to continue without MAXMIND GeoLite2 geolocation database"
fi
- read_config "DISCOURSE_HOSTNAME"
- hostname=$read_config_result
-
local new_value=""
local config_ok="n"
local update_ok="y"
fi
if [ "$smtp_address" == "smtp.sendgrid.net" ]
then
- smtp_user_name="apikey"
+ smtp_user_name="apikey"
fi
if [ "$smtp_address" == "smtp.mailgun.org" ]
then
- smtp_user_name="postmaster@$hostname"
+ smtp_user_name="postmaster@$hostname"
fi
fi
smtp_password="$new_value"
fi
+ if [[ "$notification_email" == "noreply@discourse.example.com"* ]]
+ then
+ notification_email="noreply@$hostname"
+ fi
+
+ read -p "notification email address? [$notification_email]: " new_value
+ if [ ! -z "$new_value" ]
+ then
+ notification_email="$new_value"
+ fi
+
+ # set smtp_domain default value here rather than use Rails default of localhost
+ smtp_domain=$(echo $notification_email | sed -e "s/.*@//")
+
if [ ! -z $letsencrypt_account_email ]
then
read -p "Optional email address for Let's Encrypt warnings? ($letsencrypt_status) [$letsencrypt_account_email]: " new_value
fi
echo -e "\nDoes this look right?\n"
- echo "Hostname : $hostname"
- echo "Email : $developer_emails"
- echo "SMTP address : $smtp_address"
- echo "SMTP port : $smtp_port"
- echo "SMTP username : $smtp_user_name"
- echo "SMTP password : $smtp_password"
+ echo "Hostname : $hostname"
+ echo "Email : $developer_emails"
+ echo "SMTP address : $smtp_address"
+ echo "SMTP port : $smtp_port"
+ echo "SMTP username : $smtp_user_name"
+ echo "SMTP password : $smtp_password"
+ echo "Notification email: $notification_email"
if [ "$letsencrypt_status" == "Enter 'OFF' to disable." ]
then
update_ok="n"
fi
+ sed -i -e "s/^ #\?DISCOURSE_NOTIFICATION_EMAIL:.*/ DISCOURSE_NOTIFICATION_EMAIL: $notification_email/w $changelog" $web_file
+ if [ -s $changelog ]
+ then
+ rm $changelog
+ else
+ echo "DISCOURSE_NOTIFICATION_EMAIL change failed."
+ update_ok="n"
+ fi
+
+ sed -i -e "s/^ #\?DISCOURSE_SMTP_DOMAIN:.*/ DISCOURSE_SMTP_DOMAIN: $smtp_domain/w $changelog" $web_file
+ if [ -s $changelog ]
+ then
+ rm $changelog
+ else
+ echo "DISCOURSE_SMTP_DOMAIN change failed."
+ update_ok="n"
+ fi
+
if [[ "$smtp_password" == *"\""* ]]
then
SLASH="BROKEN"
fi
fi
- echo "Enabling Let's Encrypt"
+ if ! [ -z $DEBUG ]
+ then
+ echo "Enabling Let's Encrypt"
+ fi
sed -i -e "s/^ #\?LETSENCRYPT_ACCOUNT_EMAIL:.*/ LETSENCRYPT_ACCOUNT_EMAIL: $letsencrypt_account_email/w $changelog" $web_file
if [ -s $changelog ]
then
sed -i -e "s/$src/$dst/w $changelog" $web_file
if [ -s $changelog ]
then
- echo "web.ssl.template.yml enabled"
+ if ! [ -z $DEBUG ]
+ then
+ echo "web.ssl.template.yml enabled"
+ fi
else
update_ok="n"
echo "web.ssl.template.yml NOT ENABLED--was it on already?"
echo
if [ $maxmind_license_key != "1234567890123456" ]
then
- echo "Setting MAXMIND key to $maxmind_license_key in $web_file"
sed -i -e "s/^.*DISCOURSE_MAXMIND_LICENSE_KEY:.*/ DISCOURSE_MAXMIND_LICENSE_KEY: $maxmind_license_key/w $changelog" $web_file
if [ -s $changelog ]
then
if [ "$update_ok" == "y" ]
then
- echo -e "\nConfiguration file at $config_file updated successfully!\n"
+ echo -e "\nConfiguration file at $web_file updated successfully!\n"
else
- echo -e "\nUnfortunately, there was an error changing $config_file\n"
- echo -d "This may happen if you have made unexpected changes."
+ echo -e "\nUnfortunately, there was an error changing $web_file\n"
+ echo -e "This may happen if you have made unexpected changes."
exit 1
fi
}
## template file names
##
-if [ "$1" == "2container" ] || [ -f containers/web_only.yml ]
+if [ "$TWO_CONTAINER" ] || [ -f containers/web_only.yml ]
then
app_name=web_only
data_name=data
echo Saving old file as $BACKUP
cp $web_file containers/$BACKUP
echo "Stopping existing container in 5 seconds or Control-C to cancel."
- sleep 5
+ #sleep 5
assert_maxmind_license_key
- ./launcher stop $app_name
+ assert_notification_email
+ assert_smtp_domain
+ #./launcher stop $app_name
echo
else
- check_ports
+ if [ "$SKIP_CONNECTION_TEST" != 1 ]
+ then
+ check_ports
+ fi
cp -v $web_template $web_file
if [ "$data_name" == "data" ]
then
## if we reach this point without exiting, OK to proceed
## rebuild won't fail if there's nothing to rebuild and does the restart
##
+if [ "$SKIP_REBUILD" ]
+then
+ echo "Updates successful. --skip-rebuild requested. Exiting."
+ exit
+fi
+
echo "Updates successful. Rebuilding in 5 seconds."
sleep 5 # Just a chance to ^C in case they were too fast on the draw
+
if [ "$data_name" == "$app_name" ]
then
echo Building $app_name
DISCOURSE_SMTP_PASSWORD: pa$$word
#DISCOURSE_SMTP_ENABLE_START_TLS: true # (optional, default true)
#DISCOURSE_SMTP_DOMAIN: discourse.example.com # (required by some providers)
+ #DISCOURSE_NOTIFICATION_EMAIL: noreply@discourse.example.com # (address to send notifications from)
## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
#LETSENCRYPT_ACCOUNT_EMAIL: me@example.com
DISCOURSE_SMTP_USER_NAME: user@example.com
DISCOURSE_SMTP_PASSWORD: pa$$word
#DISCOURSE_SMTP_ENABLE_START_TLS: true # (optional, default true)
+ #DISCOURSE_SMTP_DOMAIN: discourse.example.com # (required by some providers)
+ #DISCOURSE_NOTIFICATION_EMAIL: noreply@discourse.example.com # (address to send notifications from)
## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
#LETSENCRYPT_ACCOUNT_EMAIL: me@example.com
--- /dev/null
+## Smoke tests for `discourse-setup`
+
+These are not **real** tests, but do test that `discourse-setup` can produce or modify
+the YML file as expected.
+
+Tests will not run if yml files exist already. If the tests succeed, the container files are deleted.
+
+### `standalone` tests
+
+- run the first time to do an initial creation of `app.yml` and that the values get set as expected.
+- run again to change the values
+
+### `two-container` tests
+
+- run with `--two-container` switch to create separate data and web containers
+- run again (not requiring the `--two-container` switch) and update values as expected
+
+### `update-old-templates` tests
+
+- updates a very old (Sep 6, 2016) standalone.yml
+- updates a pretty old (Apr 13, 2018) web_only.yml
+
+The tests won't run if `app.yml` or `web_only.yml` exist.
+
+### `run-all-tests`
+
+Runs all three of the above tests and prints an error if `app.yml` or `web_only.yml` exist.
+
--- /dev/null
+#!/usr/bin/env bash
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+$DIR/standard
+$DIR/two-container
+$DIR/update-old-templates
+
+if [ -f $DIR/../containers/app.yml ] || [ -f $DIR/../containers/web_only.yml ]
+then
+ echo Some test failed. Sad.
+fi
\ No newline at end of file
--- /dev/null
+#!/usr/bin/env bash
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+YML=$DIR/../containers/app.yml
+if [ -f $YML ]
+then
+ echo "cannot run test if $YML exists."
+ exit
+fi
+
+check_value () {
+ VAR=$1
+ VAL=$2
+ YML=$3
+ if ! [[ $(grep $VAR $YML |sed -e "s/ $VAR: //") == "$VAL" ]]
+ then
+ echo $VAR is NOT $VAL
+ echo TEST FAILED. Aborting.
+ exit 1
+ fi
+}
+
+hostname='test.myhost.com'
+developer='admin@mail.myhost.com'
+smtp_address='smtp.myhostn.com'
+smtp_port=''
+smtp_user='smtpuser'
+smtp_pass='smtp-pw'
+notification=''
+letsencrypt='le@myhost.com'
+maxmind=''
+$DIR/../discourse-setup --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT 587 $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $hostname $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL noreply@$hostname $YML
+echo "################################ Initial install succeeded. ###################################"
+echo "Now running edit test."
+hostname='new.myhost.com'
+developer='new@mail.myhost.com'
+smtp_address='new.myhostn.com'
+smtp_port='2525'
+smtp_user='newuser'
+smtp_pass='new-smtp-pw'
+notification='somuser@otherhost.com'
+smtp_domain=otherhost.com # NOTE: script uses notification hostnme
+letsencrypt='le-new@myhost.com'
+maxmind='maxthisone'
+$DIR/../discourse-setup --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT $smtp_port $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $smtp_domain $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL $notification $YML
+echo "################################ Update of values succeeded. ###################################"
+echo "Removing $YML*"
+rm $YML*
+
+echo "Test succeeded. Removing $YML"
+rm $YML
--- /dev/null
+#!/usr/bin/env bash
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+YML=$DIR/../containers/web_only.yml
+DATA_YML=$DIR/../containers/data.yml
+if [ -f $YML ]
+then
+ echo "cannot run test if $YML exists."
+ exit
+fi
+
+check_value () {
+ VAR=$1
+ VAL=$2
+ YML=$3
+ if ! [[ $(grep $VAR $YML |sed -e "s/ $VAR: //") == "$VAL" ]]
+ then
+ echo $VAR is NOT $VAL
+ echo TEST FAILED. Aborting.
+ exit 1
+ fi
+}
+
+hostname='test.myhost.com'
+developer='admin@mail.myhost.com'
+smtp_address='smtp.myhostn.com'
+smtp_port=''
+smtp_user='smtpuser'
+smtp_pass='smtp-pw'
+notification=''
+letsencrypt='le@myhost.com'
+maxmind=''
+$DIR/../discourse-setup --two-container --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT 587 $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $hostname $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL noreply@$hostname $YML
+echo "################################ Initial install succeeded. ###################################"
+echo "Now running edit test."
+hostname='new.myhost.com'
+developer='new@mail.myhost.com'
+smtp_address='new.myhostn.com'
+smtp_port='2525'
+smtp_user='newuser'
+smtp_pass='new-smtp-pw'
+notification='somuser@otherhost.com'
+smtp_domain=otherhost.com # NOTE: script uses notification hostnme
+letsencrypt='le-new@myhost.com'
+maxmind='maxthisone'
+$DIR/../discourse-setup --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT $smtp_port $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $smtp_domain $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL $notification $YML
+check_value DISCOURSE_MAXMIND_LICENSE_KEY $maxmind $YML
+echo "################################ Update of values succeeded. ###################################"
+echo "Update values Test succeeded. Removing $YML"
+rm $YML* $DATA_YML
--- /dev/null
+#!/usr/bin/env bash
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+YML=$DIR/../containers/app.yml
+if [ -f $YML ]
+then
+ echo "cannot run test if $YML exists."
+ exit
+fi
+
+check_value () {
+ VAR=$1
+ VAL=$2
+ YML=$3
+ if ! [[ $(grep $VAR $YML |sed -e "s/ $VAR: //") == "$VAL" ]]
+ then
+ echo $VAR is NOT $VAL
+ echo TEST FAILED. Aborting.
+ exit 1
+ fi
+}
+
+# get old (Sep 6, 2016) standalone.yml that's missing MAXMIND and other values
+git show 7cf781fc0cf2542040f35e40cf79a1ab079c59f0:samples/standalone.yml>containers/app.yml > $YML
+
+hostname='new.myhost.com'
+developer='new@mail.myhost.com'
+smtp_address='new.myhostn.com'
+smtp_port='2525'
+smtp_user='newuser'
+smtp_pass='new-smtp-pw'
+notification='somuser@otherhost.com'
+smtp_domain=otherhost.com # NOTE: script uses notification hostnme
+letsencrypt='le-new@myhost.com'
+maxmind='maxthisone'
+$DIR/../discourse-setup --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT $smtp_port $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $smtp_domain $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL $notification $YML
+check_value DISCOURSE_MAXMIND_LICENSE_KEY $maxmind $YML
+echo "Update values Test succeeded. Removing $YML"
+rm $YML*
+
+# get old (Apr 13, 2018) web_only.yml that's missing MAXMIND and other values
+YML=$DIR/../containers/web_only.yml
+git show 04a06dd05ee5aaec8082c503c8b8429e51f239e0:samples/web_only.yml> $YML
+hostname='new.myhost.com'
+developer='new-admin@mail.myhost.com'
+smtp_address='new.myhostn.com'
+smtp_port='2525'
+smtp_user='newuser'
+smtp_pass='new-smtp-pw'
+notification='somuser@otherhost.com'
+smtp_domain=otherhost.com # NOTE: script uses notification hostnme
+letsencrypt='le-new@myhost.com'
+maxmind='maxthisone'
+$DIR/../discourse-setup --skip-connection-test --skip-rebuild <<EOF
+$hostname
+$developer
+$smtp_address
+$smtp_port
+$smtp_user
+$smtp_pass
+$notification
+$letsencrypt
+$maxmind
+
+
+EOF
+check_value DISCOURSE_HOSTNAME $hostname $YML
+check_value DISCOURSE_SMTP_ADDRESS $smtp_address $YML
+check_value DISCOURSE_SMTP_PORT $smtp_port $YML
+check_value DISCOURSE_SMTP_USER_NAME $smtp_user $YML
+check_value DISCOURSE_SMTP_PASSWORD \"$smtp_pass\" $YML
+check_value DISCOURSE_SMTP_DOMAIN $smtp_domain $YML
+check_value DISCOURSE_NOTIFICATION_EMAIL $notification $YML
+check_value DISCOURSE_MAXMIND_LICENSE_KEY $maxmind $YML
+echo "Update values Test succeeded. Removing $YML"
+rm $YML*