diff --git a/discourse-setup b/discourse-setup index 5adfe1358..4a43bc767 100755 --- a/discourse-setup +++ b/discourse-setup @@ -27,6 +27,10 @@ while [ ${#} -gt 0 ]; do SKIP_CONNECTION_TEST="1" echo "skipping connection test" ;; + --skip-email-setup) + SKIP_EMAIL_SETUP="1" + echo "Skipping email configuration; Discourse ID authentication will be enabled." + ;; esac shift 1 @@ -541,67 +545,77 @@ ask_user_for_config() { done fi - if [ ! -z "$smtp_address" ] + if [ "$SKIP_EMAIL_SETUP" != "1" ] then - read -p "SMTP server address? [$smtp_address]: " new_value - if [ ! -z "$new_value" ] + if [ ! -z "$smtp_address" ] then - smtp_address="$new_value" + read -p "SMTP server address? [$smtp_address]: " new_value + if [ ! -z "$new_value" ] + then + smtp_address="$new_value" + fi fi - fi - if [ ! -z "$smtp_port" ] - then - read -p "SMTP port? [$smtp_port]: " new_value - if [ ! -z "$new_value" ] + if [ ! -z "$smtp_port" ] then - smtp_port="$new_value" + read -p "SMTP port? [$smtp_port]: " new_value + if [ ! -z "$new_value" ] + then + smtp_port="$new_value" + fi fi - fi - ## - ## automatically set correct user name based on common mail providers unless it's been set - ## - if [ "$smtp_user_name" == "user@example.com" ] - then - if [ "$smtp_address" == "smtp.sparkpostmail.com" ] - then - smtp_user_name="SMTP_Injection" - fi - if [ "$smtp_address" == "smtp.sendgrid.net" ] + ## + ## automatically set correct user name based on common mail providers unless it's been set + ## + if [ "$smtp_user_name" == "user@example.com" ] then - smtp_user_name="apikey" + if [ "$smtp_address" == "smtp.sparkpostmail.com" ] + then + smtp_user_name="SMTP_Injection" + fi + if [ "$smtp_address" == "smtp.sendgrid.net" ] + then + smtp_user_name="apikey" + fi + if [ "$smtp_address" == "smtp.mailgun.org" ] + then + smtp_user_name="postmaster@$hostname" + fi fi - if [ "$smtp_address" == "smtp.mailgun.org" ] + + if [ ! -z "$smtp_user_name" ] then - smtp_user_name="postmaster@$hostname" + read -p "SMTP user name? [$smtp_user_name]: " new_value + if [ ! -z "$new_value" ] + then + smtp_user_name="$new_value" + fi fi - fi - if [ ! -z "$smtp_user_name" ] - then - read -p "SMTP user name? [$smtp_user_name]: " new_value + read -p "SMTP password? [$smtp_password]: " new_value if [ ! -z "$new_value" ] then - smtp_user_name="$new_value" + smtp_password="$new_value" fi - fi - read -p "SMTP password? [$smtp_password]: " new_value - if [ ! -z "$new_value" ] - then - smtp_password="$new_value" - fi - - if [[ "$notification_email" == "noreply@discourse.example.com"* ]] - then - notification_email="noreply@$hostname" - 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" + read -p "notification email address? [$notification_email]: " new_value + if [ ! -z "$new_value" ] + then + notification_email="$new_value" + fi + else + echo "" + echo "Skipping SMTP configuration (--skip-email-setup enabled)" + if [[ "$notification_email" == "noreply@discourse.example.com"* ]] + then + notification_email="noreply@$hostname" + fi fi # set smtp_domain default value here rather than use Rails default of localhost @@ -644,10 +658,15 @@ ask_user_for_config() { 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" + if [ "$SKIP_EMAIL_SETUP" == "1" ] + then + echo "SMTP configuration: skipped (--skip-email-setup)" + else + echo "SMTP address : $smtp_address" + echo "SMTP port : $smtp_port" + echo "SMTP username : $smtp_user_name" + echo "SMTP password : $smtp_password" + fi echo "Notification email: $notification_email" if [ "$letsencrypt_status" == "Enter 'OFF' to disable." ] @@ -691,31 +710,87 @@ ask_user_for_config() { update_ok="n" fi - sed -i -e "s/^ DISCOURSE_SMTP_ADDRESS:.*/ DISCOURSE_SMTP_ADDRESS: $smtp_address/w $changelog" $web_file - if [ -s $changelog ] + if [ "$SKIP_EMAIL_SETUP" != "1" ] then - rm $changelog - else - echo "DISCOURSE_SMTP_ADDRESS change failed." - update_ok="n" - fi + sed -i -e "s/^ DISCOURSE_SMTP_ADDRESS:.*/ DISCOURSE_SMTP_ADDRESS: $smtp_address/w $changelog" $web_file + if [ -s $changelog ] + then + rm $changelog + else + echo "DISCOURSE_SMTP_ADDRESS change failed." + update_ok="n" + fi - sed -i -e "s/^ #\?DISCOURSE_SMTP_PORT:.*/ DISCOURSE_SMTP_PORT: $smtp_port/w $changelog" $web_file - if [ -s $changelog ] - then - rm $changelog - else - echo "DISCOURSE_SMTP_PORT change failed." - update_ok="n" - fi + sed -i -e "s/^ #\?DISCOURSE_SMTP_PORT:.*/ DISCOURSE_SMTP_PORT: $smtp_port/w $changelog" $web_file + if [ -s $changelog ] + then + rm $changelog + else + echo "DISCOURSE_SMTP_PORT change failed." + update_ok="n" + fi - sed -i -e "s/^ #\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog" $web_file - if [ -s $changelog ] - then - rm $changelog + sed -i -e "s/^ #\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog" $web_file + if [ -s $changelog ] + then + rm $changelog + else + echo "DISCOURSE_SMTP_USER_NAME 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" + echo "========================================" + echo "WARNING!!!" + echo "Your password contains a quote (\")" + echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it." + echo "========================================" + update_ok="n" + else + SLASH="|" + if [[ "$smtp_password" == *"$SLASH"* ]] + then SLASH="+" + if [[ "$smtp_password" == *"$SLASH"* ]] + then + SLASH="Q" + if [[ "$smtp_password" == *"$SLASH"* ]] + then + SLASH="BROKEN" + echo "========================================" + echo "WARNING!!!" + echo "Your password contains all available delimiters (+, |, and Q). " + echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it." + echo "========================================" + update_ok="n" + fi + fi + fi + fi + if [[ "$SLASH" != "BROKEN" ]] + then + sed -i -e "s${SLASH}^ #\?DISCOURSE_SMTP_PASSWORD:.*${SLASH} DISCOURSE_SMTP_PASSWORD: \"${smtp_password}\"${SLASH}w $changelog" $web_file + + if [ -s $changelog ] + then + rm $changelog + else + echo "DISCOURSE_SMTP_PASSWORD change failed." + update_ok="n" + fi + fi else - echo "DISCOURSE_SMTP_USER_NAME change failed." - update_ok="n" + echo "SMTP environment variables not updated (--skip-email-setup)." fi sed -i -e "s/^ #\?DISCOURSE_NOTIFICATION_EMAIL:.*/ DISCOURSE_NOTIFICATION_EMAIL: $notification_email/w $changelog" $web_file @@ -727,57 +802,6 @@ ask_user_for_config() { 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" - echo "========================================" - echo "WARNING!!!" - echo "Your password contains a quote (\")" - echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it." - echo "========================================" - update_ok="n" - else - SLASH="|" - if [[ "$smtp_password" == *"$SLASH"* ]] - then SLASH="+" - if [[ "$smtp_password" == *"$SLASH"* ]] - then - SLASH="Q" - if [[ "$smtp_password" == *"$SLASH"* ]] - then - SLASH="BROKEN" - echo "========================================" - echo "WARNING!!!" - echo "Your password contains all available delimiters (+, |, and Q). " - echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it." - echo "========================================" - update_ok="n" - fi - fi - fi - fi - if [[ "$SLASH" != "BROKEN" ]] - then - sed -i -e "s${SLASH}^ #\?DISCOURSE_SMTP_PASSWORD:.*${SLASH} DISCOURSE_SMTP_PASSWORD: \"${smtp_password}\"${SLASH}w $changelog" $web_file - - if [ -s $changelog ] - then - rm $changelog - else - echo "DISCOURSE_SMTP_PASSWORD change failed." - update_ok="n" - fi - fi - if ! [ -z $DEBUG ] then echo "Enabling Let's Encrypt" @@ -858,8 +882,14 @@ validate_config() { valid_config="y" - for x in DISCOURSE_SMTP_ADDRESS DISCOURSE_SMTP_USER_NAME DISCOURSE_SMTP_PASSWORD \ - DISCOURSE_DEVELOPER_EMAILS DISCOURSE_HOSTNAME + if [ "$SKIP_EMAIL_SETUP" == "1" ] + then + required_fields="DISCOURSE_DEVELOPER_EMAILS DISCOURSE_HOSTNAME" + else + required_fields="DISCOURSE_SMTP_ADDRESS DISCOURSE_SMTP_USER_NAME DISCOURSE_SMTP_PASSWORD DISCOURSE_DEVELOPER_EMAILS DISCOURSE_HOSTNAME" + fi + + for x in $required_fields do read_config $x local result=$read_config_result @@ -894,6 +924,59 @@ validate_config() { } +## +## Enable Discourse ID authentication when skipping email setup +## +enable_discourse_id() { + if [ "$SKIP_EMAIL_SETUP" != "1" ] + then + return + fi + + echo "" + echo "Configuring Discourse ID authentication..." + + local commands=( + " - exec: rails r \"SiteSetting.enable_discourse_id = true\"" + " - exec: rails r \"SiteSetting.enable_local_logins = false\"" + ) + + local added_any="0" + local failed_any="0" + + for cmd in "${commands[@]}" + do + if grep -qF "$cmd" "$web_file" + then + continue + fi + + local escaped_cmd=${cmd//\\/\\\\} + escaped_cmd=${escaped_cmd//&/\\&} + escaped_cmd=${escaped_cmd//\"/\\\"} + + if sed -i -e "/exec: echo \"End of custom commands\"/i\\$escaped_cmd" "$web_file" + then + echo "Added: $cmd" + added_any="1" + else + echo "Warning: could not add '$cmd' to $web_file" + failed_any="1" + fi + done + + if [ "$failed_any" == "1" ] + then + echo "Please ensure the commands above are present in $web_file before rebuilding." + elif [ "$added_any" != "1" ] + then + echo "Discourse ID commands already present in $web_file" + else + echo "Discourse ID will be enabled and local email logins disabled on first boot." + fi +} + + ## ## template file names ## @@ -982,6 +1065,7 @@ fi scale_ram_and_cpu ask_user_for_config validate_config +enable_discourse_id ## ## if we reach this point without exiting, OK to proceed