[Asterisk-code-review] A systemd service (asterisk[master])

Tzafrir Cohen asteriskteam at digium.com
Mon May 16 09:41:36 CDT 2016


Tzafrir Cohen has uploaded a new change for review.

  https://gerrit.asterisk.org/2844

Change subject: A systemd service
......................................................................

A systemd service

Add a systemd service for Asterisk. Include support for roughly all
features provided by safe_asterisk:
* Monitoring for core files (using an optional script that is provided.
* optionsl TTY9 if you really want it.

Setting extra values could be done with the standard systemd overriding
mechanisms.

ASTERISK-25988 #close

Change-Id: Ifde5d054ea0ba23d833e28ba80a6105d80070bc6
Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
---
M Makefile
A contrib/asterisk.service
A contrib/scripts/asterisk_cleanup
M contrib/scripts/live_ast
4 files changed, 97 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/44/2844/1

diff --git a/Makefile b/Makefile
index 7528f83..168f63d 100644
--- a/Makefile
+++ b/Makefile
@@ -825,6 +825,8 @@
 
 config:
 	@if [ "${OSARCH}" = "linux-gnu" -o "${OSARCH}" = "kfreebsd-gnu" ]; then \
+		$(INSTALL) -d $(DESTDIR)/lib/systemd/system; \
+		./build_tools/install_subst -d contrib/asterisk.service $(DESTDIR)/lib/systemd/system/asterisk.service; \
 		if [ -f /etc/redhat-release -o -f /etc/fedora-release ]; then \
 			./build_tools/install_subst contrib/init.d/rc.redhat.asterisk  "$(DESTDIR)/etc/rc.d/init.d/asterisk"; \
 			if [ ! -f "$(DESTDIR)/etc/sysconfig/asterisk" ] ; then \
diff --git a/contrib/asterisk.service b/contrib/asterisk.service
new file mode 100644
index 0000000..fc426a2
--- /dev/null
+++ b/contrib/asterisk.service
@@ -0,0 +1,39 @@
+[Unit]
+Description=Asterisk PBX
+Documentation=man:asterisk(8)
+Wants=network-online.target
+After=network-online.target
+
+[Service]
+ExecStart=__ASTERISK_SBIN_DIR__/asterisk -g -f -U asterisk
+ExecReload=__ASTERISK_SBIN_DIR__/asterisk -rx 'core reload'
+Restart=on-failure
+RestartSec=1
+WorkingDirectory=__ASTERISK_VARLIB_DIR__
+
+# You can run a script to clean up after asterisk. An example script is
+# included in contrib/scripts/asterisk_cleanup.
+#ExecStopPost=/path/to/script
+
+#Nice=0
+#UMask=0002
+#LimitCORE=infinity
+#LimitNOFILE=
+
+# safe_asterisk runs Asterisk in a virtual console. This allows easy
+# access to the asterisk command-line without logging it. on the other
+# hand, it allows anyone with physical access to the console full access
+# to Asterisk. To enable this console, unrem the following lines and add
+# '-c' to the ExecStart line above:
+#TTYPath=/dev/tty9
+#StandardInput=tty
+#StandardOutput=tty
+#StandardError=tty
+
+# For more information on what these parameters mean see:
+#
+# http://0pointer.de/public/systemd-man/systemd.service.html
+# http://0pointer.de/public/systemd-man/systemd.exec.html
+
+[Install]
+WantedBy=multi-user.target
diff --git a/contrib/scripts/asterisk_cleanup b/contrib/scripts/asterisk_cleanup
new file mode 100644
index 0000000..04b6663
--- /dev/null
+++ b/contrib/scripts/asterisk_cleanup
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+# asterisk_cleanup: A script intended to optionally be run when Asterisk
+# has exited. This example script is used to move core files.
+
+VARLIBDIR=${VARLIBDIR:/var/lib/asterisk}
+RUNDIR=${VARRUNDIR:/var/run/asterisk}
+DUMPDROP=${DUMPDROP:-/var/spool/asterisk/cores}
+ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid
+PID=`cat ${ASTPIDFILE}`
+DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
+if test -f ${RUNDIR}/core.${PID} ; then
+	mkdir -p "${DUMPDROP}"
+	mv ${RUNDIR}/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
+elif test -f ${RUNDIR}/core ; then
+	mkdir -p "${DUMPDROP}"
+	mv ${RUNDIR}/core ${DUMPDROP}/core.`hostname`-$DATE &
+fi
diff --git a/contrib/scripts/live_ast b/contrib/scripts/live_ast
index 0b5151f..7a14abd 100755
--- a/contrib/scripts/live_ast
+++ b/contrib/scripts/live_ast
@@ -175,6 +175,43 @@
   | sed  -e '/^#* \(Begin\|End\) Samples/d' >"$LIVE_CONF"
 }
 
+# (re?)generate a unit file that could be installed on the system to use
+# this copy of Asterisk.
+gen_ast_live_service() {
+  local service_file="$BASE_DIR/asterisk-live.service"
+  local included_dir='lib'
+  local included_dir_remmed='etc'
+  if [ -f /etc/systemd/system/asterisk.service ]; then
+    included_dir='etc'
+    included_dir_remmed='lib'
+  fi
+  local varlibdir=`awk '/astvarlibdir/ {print $3}' "$AST_CONF" | head -n1`
+  cat <<EOF >"$service_file"
+# This file is based on your existing asterisk systemd service unit, and
+# overrides some fields. If you have an overriding unit in /etc, you
+# need to include it, rather than the one in /lib:
+.include /$included_dir/systemd/system/asterisk.service
+#.include /$included_dir_remmed/systemd/system/asterisk.service
+
+# This file is not installed. If you want to use it, you need to copy it
+# to /etc/systemd/system and run 'systemctl daemon-reload'. You'll then
+# need to be able to start this manually (note that it will not be
+# enabled and thus the standard bash completion will not complete it).
+# If you have multiple live asterisk instances you want to run on the
+# same system, be sure to give each one a unique name.
+
+[Unit]
+Description = Asterisk PBX (Live Asterisk)
+
+# Note: if you changed the parameters passed in the command-line (ExecStart)
+# be sure to copy them over here as well.
+[Service]
+ExecStart = $BASE_DIR/asterisk g -f -U asterisk
+ExecReload = $BASE_DIR/asterisk -rx 'core reload'
+WorkingDirectory = $varlibdir
+EOF
+}
+
 # (re?)generate the ./live/asterisk wrapper script
 gen_live_asterisk() {
   cat <<EOF >"$BASE_DIR/asterisk"
@@ -244,6 +281,7 @@
   chmod +x "$BASE_DIR/asterisk"
   # Generate a sample config file for live_ast itself:
   gen_live_conf
+  gen_ast_live_service
   ;;
 conf-file)
   # Just regenerate live.conf from the sample if it does not already exist:

-- 
To view, visit https://gerrit.asterisk.org/2844
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifde5d054ea0ba23d833e28ba80a6105d80070bc6
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Tzafrir Cohen <tzafrir.cohen at xorcom.com>



More information about the asterisk-code-review mailing list