Skip to content
Browse files

core: Make configurable whether we run with -name or with -sname

This fixes the fact that Zotonic always required a FQDN and the
"zotonic shell" command was not working reliably.

Zotonic now defaults to running with "-sname". To use "-name", set the
envirnoment variable ZOTONIC_DISTRIBUTED=true. Distributed
mode *really* requires a proper FQDN as returned by "hostname -f".

A better way to determine if Zotonic is running has also been added,
using "erl_call", and we determine more reliably where the "erl_call"
binary is located.
  • Loading branch information...
1 parent b42042f commit 62b80da1d712ce1b91802450a5642870ad5d7f40 @arjan arjan committed Oct 30, 2012
View
41 src/scripts/helpers/zotonic_setup
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
#
# Copyright 2011 Lloyd R. Prentice
+# Copyright 2012 Arjan Scherpenisse
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,6 +15,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+function require_zotonic_running {
+ PONG=$($ZOTONIC_CALL -a "zotonic ping" 2>&1)
+ if [ "$PONG" != "pong" ]; then
+ echo "Zotonic is not running. You need to start Zotonic first to use this command." 1>&2
+ echo 1>&2
+ exit 1
+ fi
+}
+export -f require_zotonic_running
+
export ZOTONIC_BIN=${ZOTONIC_BIN:=$ZOTONIC/bin}
export SITES=${SITES:=$ZOTONIC/priv/sites}
@@ -44,16 +55,40 @@ export KERNEL_POLL=${KERNEL_POLL:=true}
# The include path for the erlang vm, add when needed for your application.
export PA=${PA:="$ZOTONIC/ebin $ZOTONIC/deps/*/ebin $ZOTONIC/modules/*/deps/*/ebin $ZOTONIC/priv/modules/*/deps/*/ebin $ZOTONIC/priv/sites/*/modules/*/deps/*/ebin"}
-# The name of the Erlang node, this must be unique on your host.
-export SNAME=${SNAME:=zotonic001}
+# For zotonic-addsite et. al. we require Zotonic to be already
+# running. In that case, check if Zotonic is already running with the
+# -name parameter.
+DISTRIBUTED_DETECTED=$(ps u |grep beam.smp|grep $NODENAME@$HOSTNAME|grep \\-name>/dev/null&&echo true||echo false)
+export ZOTONIC_DISTRIBUTED=${ZOTONIC_DISTRIBUTED:=$DISTRIBUTED_DETECTED}
+
+# The ZOTONIC_DISTRIBUTED flag tells Zotonic to start a (potentially)
+# distributed node. In that case, we require the hostname to be a FQDN.
+if [ "$ZOTONIC_DISTRIBUTED" = "true" ]; then
+ HOSTNAME=$(hostname -f)
+ if [ "`echo $HOSTNAME|grep '\.'`" != "$HOSTNAME" ]; then
+ echo Zotonic is configured to run as distributed node, but your hostname is 1>&2
+ echo not configured to be a fully qualified domain name. Please configure 1>&2
+ echo your system so the output of \"hostname -f\" returns a FQDN. 1>&2
+ echo 1>&2
+ exit 1
+ fi
+ export NAME_ARG=-name
+else
+ export NAME_ARG=-sname
+fi
+
+# The name of the Erlang node, this must be unique on your host. ($SNAME is for b.c.)
+export NODENAME=${NODENAME:=${SNAME:=zotonic001}}
# Hostname to the fully qualified domain name of your host, or leave it as localhost.
export HOSTNAME=${HOSTNAME:=localhost}
# The filename where zotonic writes its unix process Id to, for monitoring applications.
export ZOTONIC_PIDFILE=$ZOTONIC/zotonic.pid
-export ZOTONIC_CALL="erl_call -name $SNAME@$HOSTNAME"
+# Locate the path to the erl_call binary
+ERL_CALL=$($ERL -noshell -noinput -eval "io:format(code:priv_dir(erl_interface))" -s erlang halt|sed 's~priv$~bin/erl_call~')
+export ZOTONIC_CALL="$ERL_CALL $NAME_ARG $NODENAME@$HOSTNAME"
# Make sure the erlang.config file exists
if [ ! -e "$ZOTONIC/priv/erlang.config" -a -e "$ZOTONIC/priv/erlang.config.in" ]; then
View
8 src/scripts/zotonic-addsite
@@ -106,12 +106,8 @@ function check_options {
source "$HOME/.zotonic-defaults"
fi
- $ZOTONIC_CALL -a "zotonic status" &> /dev/null
- if [ "$?" != "0" ]; then
- echo "Zotonic is not running. You need to start zotonic first to use this command."
- echo
- exit 1
- fi
+ # make sure Zotonic is up
+ require_zotonic_running
# The defaults
SKEL=${SKEL:=blog}
View
2 src/scripts/zotonic-compilefile
@@ -40,5 +40,5 @@ for FILE in $*; do
exit 1
fi
echo "Compiling $F"
- (cd $ZOTONIC; $ERL -noshell -pa $PA -name ${SNAME}_compile@$HOSTNAME -run make files "$F" -s erlang halt)
+ (cd $ZOTONIC; $ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_compile@$HOSTNAME -run make files "$F" -s erlang halt)
done
View
2 src/scripts/zotonic-debug
@@ -19,4 +19,4 @@
#
cd $ZOTONIC
-$ERL -env ERL_MAX_PORTS $MAX_PORTS +P $MAX_PROCESSES +K $KERNEL_POLL -pa $PA -name $SNAME@$HOSTNAME -boot start_sasl -config $ZOTONIC/priv/erlang.config -sasl errlog_type error -s zotonic
+$ERL -env ERL_MAX_PORTS $MAX_PORTS +P $MAX_PROCESSES +K $KERNEL_POLL -pa $PA $NAME_ARG $NODENAME@$HOSTNAME -boot start_sasl -config $ZOTONIC/priv/erlang.config -sasl errlog_type error -s zotonic
View
4 src/scripts/zotonic-disablesite
@@ -25,8 +25,8 @@ function disablesite {
$ZOTONIC/bin/zotonic status>/dev/null
if [ "$?" -eq 0 ]; then
# update & start site
- NODE=$SNAME@$HOSTNAME
- $ERL -noshell -pa $PA -name ${SNAME}_enable@$HOSTNAME -s z_sites_manager stop $NODE $1 -s init stop
+ NODE=$NODENAME@$HOSTNAME
+ $ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_enable@$HOSTNAME -s z_sites_manager stop $NODE $1 -s init stop
fi
}
View
4 src/scripts/zotonic-enablesite
@@ -24,8 +24,8 @@ function enablesite {
$ZOTONIC/bin/zotonic status>/dev/null
if [ "$?" -eq 0 ]; then
# update & start site
- NODE=$SNAME@$HOSTNAME
- $ERL -noshell -pa $PA -name ${SNAME}_enable@$HOSTNAME-s zotonic update $NODE -s z_sites_manager start $NODE $1 -s init stop
+ NODE=$NODENAME@$HOSTNAME
+ $ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_enable@$HOSTNAME-s zotonic update $NODE -s z_sites_manager start $NODE $1 -s init stop
fi
}
View
2 src/scripts/zotonic-etop
@@ -22,4 +22,4 @@
CONSOLE_LINES=`tput lines`
LINES=$(($CONSOLE_LINES - 11))
-$ERL -name zotonic_etop@$HOSTNAME -hidden -s etop -s erlang halt -lines $LINES -tracing off -output text -node $SNAME@$HOSTNAME $@
+$ERL $NAME_ARG ${NODENAME}_etop@$HOSTNAME -hidden -s etop -s erlang halt -lines $LINES -tracing off -output text -node $NODENAME@$HOSTNAME $@
View
3 src/scripts/zotonic-runtests
@@ -18,7 +18,6 @@ cd $ZOTONIC
set -e
-export SNAME="testsandbox"
export ZOTONIC_PORT=8040
export ZOTONIC_PORT_SSL=8043
export ZOTONIC_SMTP_BOUNCE_PORT=2535
@@ -41,7 +40,7 @@ erlydtl)
for MODULE in $MODULES; do ALL="$ALL,$MODULE"; done
ALL=`echo $ALL|sed 's/^,//'`
- $ERL +P 10000000 +K true -pa $PA -name $SNAME@$HOSTNAME -boot start_sasl -sasl errlog_type error -s zotonic -eval "timer:sleep(3000),init:stop(case eunit:test([$ALL],[]) of error -> 1; ok -> 0 end)"
+ $ERL +P 10000000 +K true -pa $PA $NAME_ARG ${NODENAME}_testsandbox@$HOSTNAME -boot start_sasl -sasl errlog_type error -s zotonic -eval "timer:sleep(3000),init:stop(case eunit:test([$ALL],[]) of error -> 1; ok -> 0 end)"
;;
esac
View
4 src/scripts/zotonic-shell
@@ -18,4 +18,6 @@
# usage zotonic-shell
#
-$ERL -name zotonic_shell_$$@$HOSTNAME -remsh $SNAME@$HOSTNAME
+require_zotonic_running
+
+$ERL $NAME_ARG ${NODENAME}_shell_$$@$HOSTNAME -remsh $NODENAME@$HOSTNAME
View
4 src/scripts/zotonic-start
@@ -53,12 +53,12 @@ else
starttime=$now
# Only build zotonic the first time, not for restarts
- echo "Starting zotonic $SNAME@$HOSTNAME"
+ echo "Starting Zotonic $NODENAME@$HOSTNAME"
${MAKE} >/dev/null
fi
ENV=`which env`
HEART_COMMAND="${ENV} HEART=true ZOTONIC_HEART_RESTARTS=$restarts ZOTONIC_HEART_START=$starttime $ZOTONIC_BIN/zotonic start"
export HEART_COMMAND
-$ERL -env ERL_MAX_PORTS $MAX_PORTS +P $MAX_PROCESSES +K $KERNEL_POLL -pa $PA -name $SNAME@$HOSTNAME -boot start_sasl -config $ZOTONIC/priv/erlang.config -heart -detached -s zotonic
+$ERL -env ERL_MAX_PORTS $MAX_PORTS +P $MAX_PROCESSES +K $KERNEL_POLL -pa $PA $NAME_ARG $NODENAME@$HOSTNAME -boot start_sasl -config $ZOTONIC/priv/erlang.config -heart -detached -s zotonic
View
10 src/scripts/zotonic-status
@@ -18,21 +18,19 @@
# usage zotonic-start
#
+require_zotonic_running
+
cd $ZOTONIC
-if [ ! -f $ZOTONIC_PIDFILE ]; then
- echo Not Running
- exit 1
-fi
PID=`cat $ZOTONIC_PIDFILE`
if ps -p $PID >&-
then
echo "\
-Running: $SNAME@$HOSTNAME
+Running: $NODENAME@$HOSTNAME
Sites Status:
============="
- $ERL -noshell -pa $PA -name ${SNAME}_stop@$HOSTNAME -s zotonic status $SNAME@$HOSTNAME -s init stop
+ $ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_stop@$HOSTNAME -s zotonic status $NODENAME@$HOSTNAME -s init stop
echo
exit 0
else
View
4 src/scripts/zotonic-stop
@@ -18,5 +18,5 @@
# usage zotonic-stop
#
-echo "Stopping zotonic $SNAME@$HOSTNAME"
-$ERL -noshell -pa $PA -name ${SNAME}_stop@$HOSTNAME -s zotonic stop $SNAME@$HOSTNAME
+echo "Stopping Zotonic $NODENAME@$HOSTNAME"
+$ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_stop@$HOSTNAME -s zotonic stop $NODENAME@$HOSTNAME
View
6 src/scripts/zotonic-update
@@ -18,5 +18,7 @@
# usage zotonic-update
#
-echo "Updating zotonic $SNAME"
-$ERL -noshell -pa $PA -name ${SNAME}_update@$HOSTNAME -s make all load -s zotonic update $SNAME@$HOSTNAME
+require_zotonic_running
+
+echo "Updating Zotonic $NODENAME"
+$ERL -noshell -pa $PA $NAME_ARG ${NODENAME}_update@$HOSTNAME -s make all load -s zotonic update $NODENAME@$HOSTNAME
View
5 src/zotonic.erl
@@ -20,7 +20,7 @@
-module(zotonic).
-author('Marc Worrell <marc@worrell.nl>').
--export([start/0, start/1, stop/0, stop/1, status/0, status/1, update/0, update/1, run_tests/0, ensure_started/1]).
+-export([start/0, start/1, stop/0, stop/1, ping/0, status/0, status/1, update/0, update/1, run_tests/0, ensure_started/1]).
-compile([{parse_transform, lager_transform}]).
@@ -71,6 +71,9 @@ stop([Node]) ->
end,
init:stop().
+%% @doc Just returns 'pong'; used by shell scripts to determine if node is alive.
+ping() ->
+ pong.
%% @spec status() -> ok
%% @doc Print the status of the current node.

0 comments on commit 62b80da

Please sign in to comment.
Something went wrong with that request. Please try again.