Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
change how portforwarding starts for dapps
Browse files Browse the repository at this point in the history
  • Loading branch information
Laszlo Megyer committed Mar 13, 2019
1 parent 36024ae commit a5b17ad
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
5 changes: 2 additions & 3 deletions meta-titania/recipes-titania/dapp/dapp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ RDEPENDS_${PN} = " \
python3-fusepy \
python3-modules \
python3-misc \
jq \
"

# TODO: move dapp_prepare.sh logic inside systemd
SRC_URI += "file://apps.json \
file://dapp_prepare.sh \
file://dapp_register.sh \
file://dapp_forward_ports.sh \
file://dapp_pull.sh \
file://dapp_load.sh \
file://dapp_version.sh \
Expand All @@ -24,15 +26,12 @@ SRC_URI += "file://apps.json \

inherit systemd

# TODO: how exactly do we ship nginx?
# Drop-in directory for JSON maybe
FILES_${PN} = "${systemd_unitdir}/system/[email protected] /opt/titania/* ${sysconfdir}/systemd/* /opt/titania/"

# Systemd recipe doesn't know how to parse things with multiple dots
# openembedded-core `master` branch has the correct code.
# They get replaced in a ROOTFS_POSTPROCESS command (see rpi-titania-image)
# TODO: Either backport or upgrade branch (painful!), remove this hack
# TODO: make latter a .target, use ExecStartPost etc.
SYSTEMD_SERVICE_${PN} = "dapp@world,libertaria,nginx.service \
dapp-systemd-bridge.service \
dapp-json-merge.service"
Expand Down
56 changes: 56 additions & 0 deletions meta-titania/recipes-titania/dapp/files/dapp_forward_ports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
# Open up ports on the router via natpmp for the public ports

if [[ $1 != "start" && $1 != "stop" ]]
then
echo "Usage: $0 [start|stop] <dapp_name>"
exit 1
fi

if [[ -z $2 ]]
then
echo "dapp name must be specified"
exit 1
fi

ACTION=$1
DAPP_ID=$2

function public_ports()
{
local DAPP_ID_=$1
local PROTOCOL=$2

jq ".[] | select(.id == \"$DAPP_ID_\") | .ports | .[] | select(.type == \"public\") | select(.protocol == \"$PROTOCOL\") | .port" /run/apps.json
}

case $ACTION in
start)
echo "Setting up portforwarding via natpmp for public ports."
for typ in tcp udp
do
for port in $(public_ports $DAPP_ID $typ)
do
echo Setting up $typ portforward on port $port
systemctl start forward-port@${port}-${typ}.service || true # service fails if natpmp is not enabled on router
done
done
;;

stop)
echo "Removing natpmp portforwards"
for typ in tcp udp
do
for port in $(public_ports $DAPP_ID $typ)
do
echo Removing $typ portforward from port $port
systemctl stop forward-port@${port}-${typ}.service || true # service fails if natpmp is not enabled on router
done
done
;;

*)
echo "start/stop command must be specified"
exit 1
;;
esac
10 changes: 2 additions & 8 deletions meta-titania/recipes-titania/dapp/files/dapp_systemd_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,9 @@ def __genconfig(self, dapp):
#
[Unit]
Description={}
'''.format(d['name'])

# Port forwarding setup
ports = ('Wants=forward-port@{port}-{protocol}.service'.format(**port) for port in d['ports'] if port['type']=='public')
conf += '\n'.join(ports)
conf += '\n\n'

conf += '[Service]\n'
[Service]
'''.format(d['name'])

# Function to build a -p option
def gen_port_spec(pobject):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ ExecStartPre=/opt/titania/bin/dapp_prepare.sh %i \
# Run the container and attach for journal
ExecStart=/usr/bin/docker start -a -i %i
ExecStop=/usr/bin/docker stop %i

# Utility to create per-dapp http(s) forwards
ExecStartPost=/opt/titania/bin/dapp_register.sh start %i $DAPP_STATIC_PATH
ExecStopPost=/opt/titania/bin/dapp_register.sh stop %i $DAPP_STATIC_PATH

# Set up natpmp portforwards (public ports)
ExecStartPost=/opt/titania/bin/dapp_forward_ports.sh start %i
ExecStopPost=/opt/titania/bin/dapp_forward_ports.sh stop %i

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: monitor router restarts
# TODO: periodically refresh

# Unit tempate argument is in the form
# PORT/proto, e.g. 80/tcp, 53/udp
# Systemd escaping rules however make it
Expand All @@ -10,7 +7,6 @@

[Unit]
Description=Port forwarding for %I
# TODO: if we support more protocols, make it a Want rather than Require
Requires=natpmp-support.service
After=natpmp-support.service

Expand Down

0 comments on commit a5b17ad

Please sign in to comment.