diff options
| -rw-r--r-- | LICENSE | 19 | ||||
| -rwxr-xr-x | spawnctl | 95 | ||||
| -rw-r--r-- | spawnctl-port-forward.service | 13 |
3 files changed, 126 insertions, 1 deletions
@@ -0,0 +1,19 @@ +Copyright (c) 2023 Nathan P. Reiner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. @@ -1,10 +1,11 @@ #!/bin/sh - +export BACKUP_DIR="YOUR BACKUP DIRECTORY" export IMAGE_DIR="$HOME/.cache/images" export MACHINE_DIR='/var/lib/machines' mkdir -p "$IMAGE_DIR" +mkdir -p "$BACKUP_DIR" help() { echo 'Usage: spawnctl' @@ -20,12 +21,19 @@ help() { echo ' remove <name>' echo ' update' echo ' list' + echo ' backup' echo ' help' echo echo ' storage <name>' echo ' resize [+-]<num><unit>' echo ' usage' echo + echo ' forward list' + echo ' forward start' + echo ' forward stop' + echo ' forward add [source] [port] [target] [port] [user]' + echo ' forward remove [source] [port] [target] [port] [user]' + echo } @@ -112,6 +120,89 @@ storage() { fi } +forward_add() { + [ "$#" != "5" ] && help && exit + + echo "$1 $2 $3 $4 $5" >> /etc/spawnctl-ports + echo "add $1 $2 $3 $4 $5" > /tmp/spawnctl-forward/cmd-fifo +} + +forward_remove() { + [ "$#" = "5" ] && help && exit + sed -i "/$1 $2 $3 $4 $5/d" + echo "remove $1 $2 $3 $4 $5" > /tmp/spawnctl-forward/cmd-fifo +} + +forward_start() { + mkdir -p /tmp/spawnctl-forward/ + mkfifo /tmp/spawnctl-forward/cmd-fifo + echo "" > /tmp/spawnctl-forward/status + + if [ -e "/tmp/spawnctl-ports" ]; then + while read line; do + src=$(echo $line | awk '{ print $1 }') + srcport=$(echo $line | awk '{ print $2 }') + target=$(echo $line | awk '{ print $3 }') + targetport=$(echo $line | awk '{ print $4 }') + targetuser=$(echo $line | awk '{ print $5 }') + ssh -NR "$targetport:$src:$srcport" "$targetuser@$target" & + echo "$! $line" >> /tmp/spawnctl-forward/status + done < /etc/spawnctl-ports + fi + + while true; do + cmd=$(cat /tmp/spawnctl-forward/cmd-fifo) + + case "$cmd" in + "stop") + break;; + "add"*) + line=$(echo "$cmd" | sed 's/add\s*//g') + echo adding $line + src=$(echo $line | awk '{ print $1 }') + srcport=$(echo $line | awk '{ print $2 }') + target=$(echo $line | awk '{ print $3 }') + targetport=$(echo $line | awk '{ print $4 }') + targetuser=$(echo $line | awk '{ print $5 }') + ssh -NR "$targetport:$src:$srcport" "$targetuser@$target" + echo "$! $line" >> /tmp/spawnctl-forward/status + ;; + "remove"*) + line=$(echo "$cmd" | sed 's/remove\s*//g') + pid=$(grep "$line" /tmp/spawnctl-forward/status | awk '{ print $1 }') + killall "$pid" + sed -i "/$pid/d" + esac + done + + kill $(ps -o pid= --ppid "$$") + rm -r /tmp/spawnctl-forward +} + +forward_stop() { + echo stop > /tmp/spawnctl-forward/cmd-fifo +} + +forward() { + cmd=$1 + [ "$#" != "0" ] && shift 1 + + case "$cmd" in + "list") echo Source Port Target Port && [ -e "/etc/spawnctl-ports" ] && cat /etc/spawnctl-ports;; + "add") forward_add $@;; + "remove") forward_remove $@;; + "start") forward_start $@;; + "stop") forward_stop $@;; + *) help; + esac +} + + +backup() { + bdir="$BACKUP_DIR/$(date '+%d-%m-%y').d" + cp -r "$MACHINE_DIR" "$bdir" +} + cmd=$1 [ "$#" != "0" ] && shift 1 @@ -127,6 +218,8 @@ case "$cmd" in "remove") remove $@;; "list") list $@;; "storage") storage $@;; + "forward") forward $@;; + "backup") backup;; "help") help;; "") help;; *) diff --git a/spawnctl-port-forward.service b/spawnctl-port-forward.service new file mode 100644 index 0000000..515a849 --- /dev/null +++ b/spawnctl-port-forward.service @@ -0,0 +1,13 @@ +[Unit] +Description=Forward Ports Of NSpawn Containers +After=machines.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/spawnctl forward start +ExecStop=/usr/local/bin/spawnctl forward stop +Restart=always + + +[Install] +WantedBy=multi-user.target |