Merge pull request #222 from tgxworld/port_in_use_warning
authorSam <sam.saffron@gmail.com>
Sun, 21 Feb 2016 23:36:06 +0000 (10:36 +1100)
committerSam <sam.saffron@gmail.com>
Sun, 21 Feb 2016 23:36:06 +0000 (10:36 +1100)
FEATURE: Exit launcher and warn if ports to expose are in use.

launcher

index a8c3416d8b2de1bbfc682f9a079759cde5cdaf5c..7558a64e8390a3557586630216de442be97c52a3 100755 (executable)
--- a/launcher
+++ b/launcher
@@ -200,6 +200,18 @@ check_resources() {
   fi
 }
 
+check_ports() {
+  local valid=$(netstat -tln | awk '{print $4}' | grep ":${1}\$")
+
+  if [ -n "$valid" ]; then
+    echo "Launcher has detected that port ${1} is in use."
+    echo ""
+    echo "If you are trying to run Discourse simultaneously with another web server like Apache or nginx, you will need to bind to a different port."
+    echo "See https://meta.discourse.org/t/17247 for help."
+    exit 1
+  fi
+}
+
 if [ "$opt" != "--skip-prereqs" ] ; then
   prereqs
 fi
@@ -470,7 +482,18 @@ run_start(){
 
    host_run
    ports=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
-          "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
+          "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| \"-p #{p}\"}.join(' ')"`
+
+   IFS='-p ' read -a array <<< "$ports"
+   for element in "${array[@]}"
+   do
+     IFS=':' read -a args <<< "$element"
+     if [ "${#args[@]}" == "2" ]; then
+       check_ports "${args[0]}"
+     elif [ "${#args[@]}" == "3" ]; then
+       check_ports "${args[1]}"
+     fi
+   done
 
    docker_args=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
           "require 'yaml'; puts YAML.load(STDIN.readlines.join)['docker_args']"`