Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Expose port for privoxy running on nspawn container
#!/bin/sh
CONTAINER_NAME="torspawn"
CONFIG_DIR="/etc/systemd/nspawn"
CONFIG_PREFIX="nspawn"
CONFIG_LOCATION="$CONFIG_DIR/$CONTAINER_NAME.$CONFIG_PREFIX"
INTERNAL="8118"
EXPOSING="58118"
# Wrong port already exposed: remove port configuration
grep $EXPOSING $CONFIG_LOCATION 1> /dev/null 2> /dev/null
EXPOSED_CORRECTLY=$?
if [ $EXPOSED_CORRECTLY -ne 0 ]; then
sed -i $CONFIG_LOCATION -e '/[Pp]ort/d'
fi
egrep '[Pp]ort' $CONFIG_LOCATION 1> /dev/null 2> /dev/null
PORT_CONFIGURED=$?
if [ $PORT_CONFIGURED -ne 0 ]; then
sed -i $CONFIG_LOCATION \
-e 's/\(\[Network\]\)/\1\nPort='$EXPOSING':'$INTERNAL'/g'
fi
if [ $EXPOSED_CORRECTLY -ne 0 ] || [ $PORT_CONFIGURED -ne 0 ]; then
machinectl stop $CONTAINER_NAME
sleep 5
machinectl start $CONTAINER_NAME
fi
NAME=expose-privoxy-port-on-nspawn
all: install
install: ${NAME} ${NAME}.service ${NAME}.timer
install -D -m 755 ${NAME} /usr/local/bin/
install -D -m 644 ${NAME}.service /etc/systemd/system/
install -D -m 644 ${NAME}.timer /etc/systemd/system/
systemctl daemon-reload
systemctl enable ${NAME}.timer
uninstall:
systemctl disable ${NAME}.timer
rm -f /usr/local/bin/${NAME}
rm -f /etc/systemd/system/${NAME}.service
rm -f /etc/systemd/system/${NAME}.timer
systemctl daemon-reload
[Unit]
Description=Expose port for privoxy running inside nspawn container
[Service]
Type=oneshot
ExecStart=/usr/local/bin/expose-privoxy-port-on-nspawn
[Unit]
Description=Expose port for privoxy running inside nspawn container
[Timer]
OnBootSec=1min
OnUnitActiveSec=5min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.