diff options
author | Alberto Gonzalez Iniesta <agi@inittab.org> | 2014-04-16 18:46:01 +0200 |
---|---|---|
committer | Alberto Gonzalez Iniesta <agi@inittab.org> | 2014-04-16 18:46:01 +0200 |
commit | 5f171919d858a425739d1781c4434bab761b36c8 (patch) | |
tree | d8dc8c1efe289c84e33be70d37a03f60e0322204 | |
parent | e5caec6bb07020a3e552fc78f679e0517c4569cf (diff) |
Initial systemd .service
-rwxr-xr-x | debian/openvpn-systemd-helper | 103 | ||||
-rw-r--r-- | debian/openvpn.install | 2 | ||||
-rw-r--r-- | debian/openvpn.service | 16 |
3 files changed, 121 insertions, 0 deletions
diff --git a/debian/openvpn-systemd-helper b/debian/openvpn-systemd-helper new file mode 100755 index 0000000..be03932 --- /dev/null +++ b/debian/openvpn-systemd-helper @@ -0,0 +1,103 @@ +#!/bin/sh +# Copyright (C) 2014 Alberto Gonzalez Iniesta <agi@inittab.org> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# +# This scripts tries to mimic the behaviour of the SysV init script +# in systemd. It's an alternative to declaring an instance for each +# VPN you want to start on boot +# + +echo "$*" > /tmp/ovpn + +test $DEBIAN_SCRIPT_DEBUG && set -v -x + +DAEMON=/usr/sbin/openvpn +CONFIG_DIR=/etc/openvpn +test -x $DAEMON || exit 0 +test -d $CONFIG_DIR || exit 0 + +# Source defaults file; edit that file to configure this script. +AUTOSTART="all" +STATUSREFRESH=10 +STATUS=0 +VPNS="" +if test -e /etc/default/openvpn ; then + . /etc/default/openvpn +fi + +# Nothing to do if no VPN is set in AUTOSTART +if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then + exit 0 +fi + +# Create list with VPNs to be managed +if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then + # all of them + for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do + NAME=${CONFIG%%.conf} + VPNS="$VPNS $NAME" + done +else + # only manage specified VPNs + for NAME in $AUTOSTART ; do + [ -e "${CONFIG_DIR}/${NAME}.conf" ] && VPNS="$VPNS $NAME" + done +fi + + +case "$1" in +start) + for NAME in $VPNS ; do + systemctl start openvpn@${NAME} + [ "$?" -ne 0 ] && { echo "Error starting $NAME"; STATUS=1; } + done +;; +stop) + for NAME in $VPNS ; do + systemctl stop openvpn@${NAME} + [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; } + done +;; +# Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'. +reload) + for NAME in $VPNS ; do + # If openvpn if running under a different user than root we'll need to restart + if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then + systemctl restart openvpn@${NAME} + [ "$?" -ne 0 ] && { echo "Error restarting $NAME"; STATUS=1; } + else + systemctl reload openvpn@${NAME} + fi + done + ;; +restart) + for NAME in $VPNS ; do + systemctl restart openvpn@${NAME} + [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; } + done + ;; +cond-restart|soft-restart) + for NAME in $VPNS ; do + systemctl condrestart openvpn@${NAME} + [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; } + done + ;; +*) + echo "Usage: $0 {start|stop|reload|restart|cond-restart}" >&2 + exit 1 + ;; +esac + +exit $STATUS + +# vim:set ai sts=2 sw=2 tw=0: diff --git a/debian/openvpn.install b/debian/openvpn.install index 22d87e6..3877165 100644 --- a/debian/openvpn.install +++ b/debian/openvpn.install @@ -1,2 +1,4 @@ debian/openvpn@.service /lib/systemd/system debian/openvpn.conf /usr/lib/tmpfiles.d +debian/openvpn.service /lib/systemd/system +debian/openvpn-systemd-helper /usr/sbin diff --git a/debian/openvpn.service b/debian/openvpn.service new file mode 100644 index 0000000..b76e36e --- /dev/null +++ b/debian/openvpn.service @@ -0,0 +1,16 @@ +[Unit] +Description=OpenVPN service +After=network.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/sbin/openvpn-systemd-helper start +ExecStop=/usr/sbin/openvpn-systemd-helper stop +ExecReload=/usr/sbin/openvpn-systemd-helper reload +WorkingDirectory=/etc/openvpn + +[Install] +WantedBy=multi-user.target + + |