Commit a7ed3c1d authored by Julien Muchembled's avatar Julien Muchembled

Add NetworkManager/ifupdown support for setups that are bound to an interface

Also provides systemd units.
parent 942439d5
#!/usr/bin/python -S
# Is there something standard like Debian's start-stop-daemon ?
import errno, os, subprocess, signal, sys, time
DAEMON = "re6stnet"
CONFDIR = "/etc/re6stnet"
os.environ["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
iface = sys.argv[1]
action = sys.argv[2]
pid_file = "/var/run/re6stnet-%s.pid" % iface
if action in ("up", "vpn-up"):
os.chdir(CONFDIR)
if os.path.exists("re6stnet.conf") and not subprocess.call(
(DAEMON, "@re6stnet.conf", "--test", "main_interface != %r" % iface)):
pid_fd = os.open(pid_file, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0666)
try:
pid = os.fork()
if not pid:
os.setsid()
os.execlp(DAEMON, DAEMON, "@re6stnet.conf")
os.write(pid_fd, str(pid))
except:
os.remove(pid_file)
raise
elif action in ("down", "vpn-down"):
try:
pid = open(pid_file).read()
os.remove(pid_file)
except IOError, e:
if e.errno != errno.ENOENT:
raise
else:
stat = open('/proc/%s/stat' % pid).read().split()
if stat[0] == pid and stat[1] == "(re6stnet)":
pid = int(pid)
def kill(sig):
try:
os.kill(pid, sig)
except OSError, e:
if e.errno == errno.ESRCH:
sys.exit()
raise
kill(signal.SIGTERM)
sleep = .1
while sleep < 5:
time.sleep(sleep)
kill(0)
sleep *= 1.5
# we waited for about 11 seconds
pid = -int(stat[4])
kill(signal.SIGKILL)
[Unit]
Description=Server application for re6snet
ConditionPathExists=/etc/re6stnet/re6st-registry.conf
[Service]
WorkingDirectory=/etc/re6stnet
ExecStart=/usr/bin/re6st-registry @re6st-registry.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
[Unit]
Description=Resilient, Scalable, IPv6 Network application
ConditionPathExists=/etc/re6stnet/re6stnet.conf
[Service]
WorkingDirectory=/etc/re6stnet
# systemd plans to implement "something like ConditionExec= or ExecStartPre= without failure state" (cf its TODO file)
ExecStart=/bin/sh -c 'set re6stnet @re6stnet.conf; "$@" --test main_interface==\"lo\" || exec "$@"'
Restart=on-failure
[Install]
WantedBy=multi-user.target
......@@ -6,6 +6,7 @@ MANPAGELIST := $(patsubst %.rst, %.1, $(wildcard docs/*.rst))
#export DH_VERBOSE=1
INIT=debian/re6stnet/etc/init.d
NM=/etc/NetworkManager/dispatcher.d/50re6stnet
override_dh_auto_clean:
dh_auto_clean
......@@ -18,17 +19,25 @@ override_dh_install:
dh_install
install -d debian/re6stnet/usr/sbin
mv debian/re6stnet/usr/bin/re6stnet debian/re6stnet/usr/sbin
install -Dpm 0644 debian/README.conf debian/re6stnet/etc/re6stnet/README
install -Dp daemon/network-manager debian/re6stnet$(NM)
for a in up down; do \
set debian/re6stnet/etc/network/if-$$a.d/re6stnet; \
install -d $${1%/*}; \
printf '#!/bin/sh -e\n[ "$$METHOD" = NetworkManager ] ||exec $(NM) "$$IFACE" %s\n' $$a >$$1; \
chmod +x $$1; \
done
override_dh_installinit:
install -d $(INIT)
sed 's/#NAME#/re6st-registry/; s/#DEPENDS#//; s,#DAEMON_DIR#,/usr/bin,' \
<debian/init.d >$(INIT)/re6st-registry
sed 's/#NAME#/re6stnet/; s/#DEPENDS#/re6st-registry/; s,#DAEMON_DIR#,/usr/sbin,' \
sed 's/#NAME#/re6stnet/; s/#DEPENDS#/re6st-registry/; s,#DAEMON_DIR#,/usr/sbin,; /start)$$/a \
cd $$CONFDIR; $$DAEMON @$$NAME.conf --test "main_interface != '\'lo\''" || exit 0' \
<debian/init.d >$(INIT)/re6stnet
for x in re6st-registry re6stnet; \
do chmod +x $(INIT)/$$x && dh_installinit --onlyscripts --name=$$x; \
done
install -Dpm 0644 debian/README.conf debian/re6stnet/etc/re6stnet/README
override_dh_installman: $(MANPAGELIST)
dh_installman $^
......
......@@ -42,6 +42,10 @@ def getConfig():
help="Same as --up, but run in background: the command will be killed"
" at exit (with a TERM signal, followed by KILL 5 seconds later"
" if process is still alive).")
_('--test', metavar='EXPR',
help="Exit after configuration parsing. Status code is the"
" result of the given Python expression. For example:\n"
" main_interface != 'eth0'")
_ = parser.add_argument_group('routing').add_argument
_('-B', dest='babel_args', metavar='ARG', action='append', default=[],
......@@ -112,6 +116,9 @@ def main():
'--key', config.key)
# TODO: verify certificates (should we moved to M2Crypto ?)
if config.test:
sys.exit(eval(config.test, None, config.__dict__))
# Set logging
utils.setupLog(config.verbose, os.path.join(config.log, 're6stnet.log'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment