The changes look sane to me, based on very limited knowledge of
systemd,
but I trust you. :-)
Before it goes in though, can you summarize how you've tested it? Has
this been tested on systems both with and without systemd?
On 08/26/2011 09:26 AM, Zane Bitter wrote:
> * Read the broker and port options from the environment as well as from the
> CLI. This makes it easier to configure agents through systemd, which is
> replacing SysV init in Fedora.
>
> * Also set up the .service files to correctly launch daemons with the correct
> configuration.
>
> * Turn matahari-brokerd into a wrapper script which can interpret the
> matahari-broker sysconfig file for qpidd.
>
> * Pass only the correct args for the daemon type. Agents get
> MATAHARI_AGENT_ARGS; consoles get MATAHARI_CONSOLE_ARGS instead.
>
> Signed-off-by: Zane Bitter<zbitter(a)redhat.com>
> ---
> CMakeLists.txt | 18 +++++++
> matahari-broker.init.in | 25 +++++-----
> matahari-broker.service | 19 ++++++++
> matahari-brokerd.in | 8 +++
> matahari.init.in | 41 +++++++----------
> matahari.service.in | 10 ++--
> matahari.spec.in | 9 +++-
> src/CMakeLists.txt | 2 +
> src/cmake/modules/MatahariMacros.cmake | 2 -
> src/lib/mh_agent.cpp | 78 +++++++++++++++++++++++---------
> src/sysconfig/CMakeLists.txt | 10 ++--
> 11 files changed, 148 insertions(+), 74 deletions(-)
> create mode 100644 matahari-broker.service
> create mode 100644 matahari-brokerd.in
>
> diff --git a/CMakeLists.txt b/CMakeLists.txt
> index a3d34b2..815c73c 100644
> --- a/CMakeLists.txt
> +++ b/CMakeLists.txt
> @@ -109,6 +109,9 @@ else(WIN32)
>
> set (CPACK_GENERATOR "TGZ")
>
> + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/matahari-brokerd.in
> + ${CMAKE_CURRENT_BINARY_DIR}/matahari-brokerd)
> +
> configure_file(${CMAKE_CURRENT_SOURCE_DIR}/matahari.init.in
> ${CMAKE_CURRENT_BINARY_DIR}/matahari.init)
>
> @@ -118,6 +121,13 @@ else(WIN32)
> configure_file(${CMAKE_CURRENT_SOURCE_DIR}/matahari-broker.sysconf.in
> ${CMAKE_CURRENT_BINARY_DIR}/matahari-broker.sysconf)
>
> + install(DIRECTORY DESTINATION ${localstatedir}/lib/matahari
> + DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
> + GROUP_READ GROUP_WRITE GROUP_EXECUTE)
> + install(CODE "execute_process(COMMAND chown root:qpidd
${localstatedir}/lib/matahari)")
> +
> + install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/matahari-brokerd DESTINATION sbin)
> +
> install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/matahari-broker DESTINATION
${initdir})
>
> install(FILES ${CMAKE_CURRENT_BINARY_DIR}/matahari.sysconf DESTINATION
${sysconfdir}/sysconfig RENAME matahari)
> @@ -126,6 +136,14 @@ else(WIN32)
>
> install(FILES ${CMAKE_CURRENT_BINARY_DIR}/matahari-broker.conf DESTINATION
${sysconfdir})
>
> + # Check for systemd
> + include(FindPkgConfig)
> + pkg_check_modules(systemd systemd)
> +
> + if(systemd_FOUND)
> + set(systemdunitdir "/lib/systemd/system")
> + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/matahari-broker.service DESTINATION
${systemdunitdir})
> + endif(systemd_FOUND)
> endif(WIN32)
>
> INCLUDE(InstallRequiredSystemLibraries)
> diff --git a/matahari-broker.init.in b/matahari-broker.init.in
> index aee1d6d..e74926c 100755
> --- a/matahari-broker.init.in
> +++ b/matahari-broker.init.in
> @@ -35,27 +35,23 @@
> # description: Matahari Broker is an AMQP broker. It receives, stores, routes and
forwards messages using the AMQP protcol.
> # processname: qpidd
>
> -prog=matahari-brokerd
> +prog="matahari-brokerd --pid-dir @localstatedir@/run/matahari"
> +proc=qpidd
> lockfile=@localstatedir@/lock/subsys/matahari-broker
> pidfile=@localstatedir(a)/run/matahari-broker.pid
>
> # Turn off automatic systemd redirects
> -SYSTEMCTL_SKIP_REDIRECT=1
> +#SYSTEMCTL_SKIP_REDIRECT=1
>
> # Source function library.
> . @sysconfdir(a)/rc.d/init.d/functions
>
> -if [ -f @sysconfdir@/sysconfig/matahari-broker ] ; then
> - . @sysconfdir@/sysconfig/matahari-broker
> -fi
> -
> RETVAL=0
>
> start() {
> echo -n $"Starting Matahari broker daemon: "
> - install -o root -g qpidd -m 770 -d @localstatedir@/lib/matahari
> install -o root -g qpidd -m 775 -d @localstatedir@/run/matahari
> - daemon --pidfile $pidfile --check $prog --user qpidd /usr/sbin/$prog --daemon
--pid-dir @localstatedir@/run/matahari --data-dir @localstatedir@/lib/matahari
$QPIDD_OPTIONS
> + daemon --pidfile $pidfile --check $proc --user qpidd /usr/sbin/$prog --daemon
> RETVAL=$?
> echo
> [ $RETVAL = 0 ]&& touch $lockfile
> @@ -63,14 +59,14 @@ start() {
> touch $pidfile
> chown qpidd.qpidd $pidfile
> [ -x /sbin/restorecon ]&& /sbin/restorecon $pidfile
> - runuser -s /bin/sh qpidd -c "/usr/sbin/$prog --check --pid-dir
@localstatedir@/run/matahari $QPIDD_OPTIONS> $pidfile"
> + runuser -s /bin/sh qpidd -c "/usr/sbin/$prog --check> $pidfile"
> fi
> return $RETVAL
> }
>
> stop() {
> echo -n $"Stopping Matahari broker daemon: "
> - killproc -p $pidfile $prog
> + killproc -p $pidfile $proc
> RETVAL=$?
> echo
> [ $RETVAL = 0 ]&& rm -f $lockfile $pidfile
> @@ -88,11 +84,14 @@ restart() {
>
> # See how we were called.
> case "$1" in
> - start|stop|restart|reload)
> - $1
> + start)
> + status -p $pidfile -l $lockfile $proc>/dev/null || $1
> + ;;
> + stop|restart|reload)
> + $1
> ;;
> status)
> - status -p $pidfile -l $lockfile $prog
> + status -p $pidfile -l $lockfile $proc
> RETVAL=$?
> ;;
> force-reload)
> diff --git a/matahari-broker.service b/matahari-broker.service
> new file mode 100644
> index 0000000..7e6e9f7
> --- /dev/null
> +++ b/matahari-broker.service
> @@ -0,0 +1,19 @@
> +[Unit]
> +Description=Matahari Broker is an AMQP broker. It receives, stores, routes and
forwards messages using the AMQP protcol.
> +After=syslog.target network.target
> +Requires=syslog.target network.target
> +
> +[Install]
> +WantedBy=multi-user.target
> +
> +[Service]
> +Type=simple
> +NotifyAccess=all
> +SysVStartPriority=85
> +User=qpidd
> +Group=qpidd
> +
> +ExecStart=/usr/sbin/matahari-brokerd
> +
> +# Restart options include: no, on-success, on-failure, on-abort or always
> +Restart=on-failure
> diff --git a/matahari-brokerd.in b/matahari-brokerd.in
> new file mode 100644
> index 0000000..0bab652
> --- /dev/null
> +++ b/matahari-brokerd.in
> @@ -0,0 +1,8 @@
> +#!/bin/sh
> +
> +if [ -f @sysconfdir@/sysconfig/matahari-broker ]; then
> + . @sysconfdir@/sysconfig/matahari-broker
> +fi
> +
> +/usr/sbin/qpidd --data-dir @localstatedir@/lib/matahari $QPIDD_OPTIONS $*
> +
> diff --git a/matahari.init.in b/matahari.init.in
> index ea51d2d..75f9739 100644
> --- a/matahari.init.in
> +++ b/matahari.init.in
> @@ -1,25 +1,25 @@
> #!/bin/sh
>
> # the following is chkconfig init header
> -# matahari-@BASE@: QMF agent for @BASE@
> +# matahari-@BASE@@BASE_SUB@: Matahari QMF @BASE@@BASE_SUB@ daemon
> ### BEGIN INIT INFO
> # Provides:
> # Required-Start: $local_fs matahari-broker
> # Required-Stop: $local_fs matahari-broker
> # Default-Start: 2 3 4 5
> # Default-Stop: 0 1 6
> -# Short-Description: Starts and stops the @BASE@ matahari agent
> -# Description: Matahari agent providing a @BASE@-related API using the AMQP
protocol.
> +# Short-Description: Starts and stops the @BASE@@BASE_SUB@ matahari daemon
> +# Description: Matahari @BASE@@BASE_SUB@ daemon using the AMQP protocol.
> ### END INIT INFO
>
> # chkconfig: - 99 01
> -# description: Matahari agent providing a @BASE@-related API using the AMQP
protocol.
> -# processname: matahari-@BASE@d
> -# pidfile: @localstatedir@/run/matahari-@BASE@.pid
> +# description: Matahari @BASE@@BASE_SUB@ daemon using the AMQP protocol.
> +# processname: matahari-@BASE@@BASE_SUB@d
> +# pidfile: @localstatedir@/run/matahari-@BASE@@BASE_SUB@.pid
> #
>
> # Turn off automatic systemd redirects
> -# SYSTEMCTL_SKIP_REDIRECT=1
> +#SYSTEMCTL_SKIP_REDIRECT=1
>
> # Source function library.
> . @sysconfdir(a)/rc.d/init.d/functions
> @@ -33,21 +33,16 @@ test -f @sysconfdir@/sysconfig/matahari&& .
@sysconfdir@/sysconfig/matahari
>
> start() {
> echo -n $"Starting the $SERVICE daemon: "
> - if [ ! -z "$MATAHARI_BROKER" ]; then
> - MATAHARI_AGENT_ARGS="$MATAHARI_AGENT_ARGS --broker $MATAHARI_BROKER"
> - fi
> - if [ ! -z "$MATAHARI_PORT" ]; then
> - MATAHARI_AGENT_ARGS="$MATAHARI_AGENT_ARGS --port $MATAHARI_PORT"
> - fi
> - if [ ! -z "$@BASE@_AGENT_ARGS" ]; then
> - MATAHARI_AGENT_ARGS="$MATAHARI_AGENT_ARGS $@BASE@_AGENT_ARGS"
> - fi
>
> - if [ ! -z "$@BASE@_CONSOLE_ARGS" ]; then
> - MATAHARI_AGENT_ARGS="$MATAHARI_AGENT_ARGS $MATAHARI_CONSOLE_ARGS
$@BASE@_CONSOLE_ARGS"
> + MATAHARI_ARGS="$MATAHARI_@DAEMON_TYPE@_ARGS
$@BASE@_@DAEMON_TYPE@_ARGS"
> + if [ -n "$MATAHARI_PORT" ]; then
> + MATAHARI_ARGS="--port $MATAHARI_PORT $MATAHARI_ARGS"
> + fi
> + if [ -n "$MATAHARI_BROKER" ]; then
> + MATAHARI_ARGS="--broker $MATAHARI_BROKER $MATAHARI_ARGS"
> fi
>
> - daemon $PROCESS $MATAHARI_AGENT_ARGS --daemon
> + daemon $PROCESS $MATAHARI_ARGS --daemon
> RETVAL=$?
> echo
> [ $RETVAL -eq 0 ]&& touch @localstatedir@/lock/subsys/$SERVICE
> @@ -82,12 +77,8 @@ reload() {
> # See how we were called.
> case "$1" in
> start)
> - if
> - ! status $PROCESS
> - then
> - $1
> - fi
> - ;;
> + status $PROCESS>/dev/null || $1
> + ;;
> stop|restart|reload)
> $1
> ;;
> diff --git a/matahari.service.in b/matahari.service.in
> index 2db4f9b..2a7a741 100644
> --- a/matahari.service.in
> +++ b/matahari.service.in
> @@ -1,6 +1,6 @@
> [Unit]
> -Description=Matahari agent providing a @BASE@-related API using the AMQP protocol.
> -After=syslog.target network.target
> +Description=Matahari @BASE@@BASE_SUB@ daemon using the AMQP protocol.
> +After=syslog.target network.target matahari-broker.service
> Requires=syslog.target network.target
>
> [Install]
> @@ -8,13 +8,11 @@ WantedBy=multi-user.target
>
> [Service]
> Type=simple
> -KillMode=main
> NotifyAccess=main
> SysVStartPriority=99
> -GuessMainPID=forking
>
> -EnvironmentFile=@sysconfdir@/sysconfig/matahari
> -ExecStart=matahari-qmf-@BASE@d
> +EnvironmentFile=-@sysconfdir@/sysconfig/matahari
> +ExecStart=/usr/sbin/matahari-qmf-@BASE@@BASE_SUB@d $MATAHARI_@DAEMON_TYPE@_ARGS
$@BASE@_@DAEMON_TYPE@_ARGS
>
> # Restart options include: no, on-success, on-failure, on-abort or always
> Restart=on-failure
> diff --git a/matahari.spec.in b/matahari.spec.in
> index 9671f3b..5031789 100644
> --- a/matahari.spec.in
> +++ b/matahari.spec.in
> @@ -189,7 +189,6 @@ make DESTDIR=%{buildroot} install
> %{__install} matahari-broker
$RPM_BUILD_ROOT/%{_sysconfdir}/rc.d/init.d/matahari-broker
>
> %{__install} matahari-broker.sysconf
$RPM_BUILD_ROOT/%{_sysconfdir}/sysconfig/matahari-broker
> -%{__ln_s} qpidd $RPM_BUILD_ROOT/%{_sbindir}/matahari-brokerd
>
> %{__install} -d -m0770 $RPM_BUILD_ROOT/%{_localstatedir}/lib/%{name}
> %{__install} -d -m0775 $RPM_BUILD_ROOT/%{_localstatedir}/run/%{name}
> @@ -409,6 +408,10 @@ test "x%{buildroot}" != "x"&& rm
-rf %{buildroot}
> %if %{with qmf}
> %attr(755, root, root) %{_initddir}/matahari-sysconfig-console
> %attr(755, root, root) %{_sbindir}/matahari-qmf-sysconfig-consoled
> +
> +%if %{defined _unitdir}
> +%{_unitdir}/matahari-sysconfig-console.service
> +%endif
> %endif
>
> %if %{with qmf}
> @@ -420,6 +423,10 @@ test "x%{buildroot}" != "x"&& rm
-rf %{buildroot}
> %{_sbindir}/matahari-brokerd
> %doc AUTHORS COPYING
>
> +%if %{defined _unitdir}
> +%{_unitdir}/matahari-broker.service
> +%endif
> +
> %else
> %exclude %{_sysconfdir}/matahari-broker.conf
> %endif
> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
> index 323c5e4..cb47956 100644
> --- a/src/CMakeLists.txt
> +++ b/src/CMakeLists.txt
> @@ -31,6 +31,8 @@ set(AGENT_MAN_DESC "Part of the Matahari suite of remote
agents")
> set(CONSOLE_MAN_SECTION "8")
> set(CONSOLE_MAN_DESC "Console for the Matahari suite of remote agents")
>
> +set(DAEMON_TYPE "AGENT")
> +
> include(FindPkgConfig)
> include_directories("${CMAKE_CURRENT_BINARY_DIR}")
> include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
> diff --git a/src/cmake/modules/MatahariMacros.cmake
b/src/cmake/modules/MatahariMacros.cmake
> index 9f04d4d..65e489b 100644
> --- a/src/cmake/modules/MatahariMacros.cmake
> +++ b/src/cmake/modules/MatahariMacros.cmake
> @@ -120,6 +120,6 @@ macro(create_service_scripts BASE)
>
> if(systemd_FOUND)
> configure_file(${CMAKE_SOURCE_DIR}/matahari.service.in
${CMAKE_CURRENT_BINARY_DIR}/matahari-${BASE}.service)
> - install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/matahari-${BASE}.service
DESTINATION ${systemdunitdir})
> + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/matahari-${BASE}.service
DESTINATION ${systemdunitdir})
> endif(systemd_FOUND)
> endmacro(create_service_scripts)
> diff --git a/src/lib/mh_agent.cpp b/src/lib/mh_agent.cpp
> index d2b9ee6..0b8fe7d 100644
> --- a/src/lib/mh_agent.cpp
> +++ b/src/lib/mh_agent.cpp
> @@ -56,6 +56,8 @@ using namespace qpid::management;
> using namespace qpid::client;
> using namespace std;
>
> +typedef qpid::types::Variant::Map OptionsMap;
> +
>
> struct MatahariAgentImpl {
> GMainLoop *_mainloop;
> @@ -152,28 +154,14 @@ static mh_option matahari_options[MAX_CHAR];
> static int
> map_option(int code, const char *name, const char *arg, void *userdata)
> {
> - qpid::types::Variant::Map *options =
static_cast<qpid::types::Variant::Map*>(userdata);
> + OptionsMap *options = (OptionsMap*)userdata;
>
> if(strcmp(name, "verbose") == 0) {
> mh_log_level++;
> mh_enable_stderr(1);
>
> } else if(strcmp(name, "broker") == 0) {
> -#ifdef WIN32
> (*options)["servername"] = arg;
> -#else
> - int rc;
> - struct addrinfo hints, *res;
> -
> - memset(&hints, 0, sizeof(struct addrinfo));
> - hints.ai_family = AF_UNSPEC;
> - rc = getaddrinfo(arg, NULL,&hints,&res);
> - if (rc == 0) {
> - (*options)["servername"] = arg;
> - } else {
> - mh_err("Broker '%s' is not resolvable - ignoring",
arg);
> - }
> -#endif
>
> } else if(strcmp(name, "port") == 0) {
> (*options)["serverport"] = atoi(arg);
> @@ -219,7 +207,7 @@ ssl_option(int code, const char *name, const char *arg, void
*userdata)
> static int
> connection_option(int code, const char *name, const char *arg, void *userdata)
> {
> - qpid::types::Variant::Map *options =
static_cast<qpid::types::Variant::Map*>(userdata);
> + OptionsMap *options = (OptionsMap*)userdata;
>
> if(strcmp(name, "service") == 0) {
> (*options)["sasl-service"] = arg;
> @@ -252,7 +240,7 @@ int print_help(int code, const char *name, const char *arg, void
*userdata)
> }
>
> qpid::messaging::Connection
> -mh_connect(qpid::types::Variant::Map mh_options, qpid::types::Variant::Map
amqp_options, int retry)
> +mh_connect(OptionsMap mh_options, OptionsMap amqp_options, int retry)
> {
> int retries = 0;
> int backoff = 0;
> @@ -326,11 +314,51 @@ mh_connect(qpid::types::Variant::Map mh_options,
qpid::types::Variant::Map amqp_
> return NULL;
> }
>
> -qpid::types::Variant::Map
> -mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Variant::Map&options)
> +#ifndef WIN32
> +static void
> +read_environment(OptionsMap& options)
> +{
> + const char *data;
> +
> + data = getenv("MATAHARI_BROKER");
> + if (data&& strlen(data)) {
> + options["servername"] = data;
> + }
> +
> + data = getenv("MATAHARI_PORT");
> + if (data&& strlen(data)) {
> + options["serverport"] = data;
> + }
> +}
> +
> +static void
> +verify_broker_option(OptionsMap& options)
> +{
> + if (!options.count("servername")) {
> + return;
> + }
> +
> + struct addrinfo hints, *res;
> + std::string broker = options["servername"];
> +
> + memset(&hints, 0, sizeof(struct addrinfo));
> + hints.ai_family = AF_UNSPEC;
> +
> + if (getaddrinfo(broker.c_str(), NULL,&hints,&res) == 0) {
> + freeaddrinfo(res);
> + } else {
> + OptionsMap::iterator iter = options.find("servername");
> + options.erase(iter);
> + mh_err("Broker '%s' is not resolvable - ignoring",
broker.c_str());
> + }
> +}
> +#endif
> +
> +OptionsMap
> +mh_parse_options(const char *proc_name, int argc, char **argv,
OptionsMap&options)
> {
> std::stringstream url;
> - qpid::types::Variant::Map amqp_options;
> + OptionsMap amqp_options;
>
> int lpc = 0;
>
> @@ -409,6 +437,8 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
> }
> }
>
> + read_environment(options);
> +
> while ((arg = getopt_long(argc, argv, opt_string, long_opts,&idx)) != -1)
{
> if(arg == 'h') {
> print_help('h', NULL, NULL, (void*)proc_name);
> @@ -425,6 +455,10 @@ mh_parse_options(const char *proc_name, int argc, char **argv,
qpid::types::Vari
> }
> }
> free(long_opts);
> +
> + if (!options.count("dns-srv")) {
> + verify_broker_option(options);
> + }
> #endif
>
> #ifdef MH_SSL
> @@ -491,7 +525,7 @@ qmf::AgentSession& MatahariAgent::getSession(void)
> int
> MatahariAgent::init(int argc, char **argv, const char* proc_name)
> {
> - qpid::types::Variant::Map options;
> + OptionsMap options;
> int res = 0;
> std::stringstream logname;
> logname<< "matahari-"<< proc_name;
> @@ -500,7 +534,7 @@ MatahariAgent::init(int argc, char **argv, const char*
proc_name)
> mh_log_init(proc_name, mh_log_level, FALSE);
> mh_add_option('d', no_argument, "daemon", "run as a
daemon", NULL, mh_should_daemonize);
>
> - qpid::types::Variant::Map amqp_options = mh_parse_options(proc_name, argc, argv,
options);
> + OptionsMap amqp_options = mh_parse_options(proc_name, argc, argv, options);
>
>
> /* Re-initialize logging now that we've completed option processing */
> diff --git a/src/sysconfig/CMakeLists.txt b/src/sysconfig/CMakeLists.txt
> index 0f988e6..e8eda56 100644
> --- a/src/sysconfig/CMakeLists.txt
> +++ b/src/sysconfig/CMakeLists.txt
> @@ -18,12 +18,10 @@ if(WITH-QMF)
> create_manpage(${QMF_AGENT} ${AGENT_MAN_SECTION} ${AGENT_MAN_DESC})
> create_service_scripts(${BASE})
>
> - if(NOT WIN32)
> - set(BASE_SUB "-console")
> - configure_file(${CMAKE_SOURCE_DIR}/matahari.init.in
${CMAKE_CURRENT_BINARY_DIR}/matahari-${BASE}${BASE_SUB})
> - install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/matahari-${BASE}${BASE_SUB}
DESTINATION ${initdir})
> - endif(NOT WIN32)
> -
> + set(BASE_SUB "-console")
> + set(DAEMON_TYPE "CONSOLE")
> + create_service_scripts(${BASE}${BASE_SUB})
> +
> install(TARGETS ${QMF_AGENT} DESTINATION sbin)
> install(TARGETS ${QMF_CONSOLE} DESTINATION sbin)
> endif(WITH-QMF)
>
> _______________________________________________
> Matahari mailing list
> Matahari(a)lists.fedorahosted.org
>
https://fedorahosted.org/mailman/listinfo/matahari
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari