media-stack icon indicating copy to clipboard operation
media-stack copied to clipboard

comment/uncomment idea

Open willwade opened this issue 8 months ago • 1 comments

toggle-vpn.sh

#!/bin/bash

# This script toggles VPN settings in docker-compose.yml
# It enables or disables VPN-related configuration for services

COMPOSE_FILE="docker-compose.yml"
BACKUP_FILE="docker-compose.yml.bak"

# Function to check if VPN is enabled
check_vpn_status() {
  # Look for uncommented network_mode in qbittorrent section
  if grep -q "^\s*network_mode: service:vpn" "$COMPOSE_FILE"; then
    echo "VPN is currently ENABLED"
    return 0
  else
    echo "VPN is currently DISABLED"
    return 1
  fi
}

# Function to make a backup of docker-compose.yml
backup_compose() {
  cp "$COMPOSE_FILE" "$BACKUP_FILE"
  echo "Created backup at $BACKUP_FILE"
}

# Function to enable VPN
enable_vpn() {
  # Make a backup first
  backup_compose

  # Use awk to process the file
  awk '
    # VPN section ports
    /# Uncomment\/enable below ports if VPN is used\/enabled/ {
      print $0;
      getline;
      sub(/^    # /, "    ");
      print;
      while (getline && !/restart:/) {
        sub(/^    #   /, "      ");
        print;
      }
      print;
      next;
    }

    # qBittorrent section
    /# Unomment below if vpn is enabled/ {
      print $0;
      while (getline && !/condition: service_healthy/) {
        sub(/^    # /, "    ");
        sub(/^      # /, "      ");
        sub(/^      #   /, "      ");
        print;
      }
      sub(/^      #   /, "      ");
      print;
      next;
    }

    # qBittorrent network_mode
    /# network_mode: service:vpn/ {
      sub(/^    # /, "    ");
      print;
      next;
    }

    # Comment qBittorrent networks
    /networks:.*# Comment this line if vpn is enabled/ {
      sub(/^    /, "    # ");
      print;
      next;
    }

    # Comment qBittorrent media-stack-net
    /- media-stack-net.*# Comment this line if vpn is enabled/ {
      sub(/^      /, "      # ");
      print;
      next;
    }

    # Comment qBittorrent ports
    /## Comment\/Disable below ports if VPN is enabled/ {
      print $0;
      while (getline && !/restart:/) {
        sub(/^    /, "    # ");
        sub(/^      /, "      # ");
        print;
      }
      print;
      next;
    }

    # Prowlarr section
    /# Uncomment below if vpn is enabled/ {
      print $0;
      while (getline && !/condition: service_healthy/) {
        sub(/^    # /, "    ");
        sub(/^      # /, "      ");
        sub(/^      #   /, "      ");
        print;
      }
      sub(/^      #   /, "      ");
      print;
      next;
    }

    # Prowlarr network_mode
    /# network_mode: service:vpn # Uncomment this line if vpn is enabled/ {
      sub(/^    # /, "    ");
      print;
      next;
    }

    # Comment Prowlarr networks
    /networks:.*# Comment this line if vpn is enabled/ {
      sub(/^    /, "    # ");
      print;
      next;
    }

    # Comment Prowlarr ports
    /# Comment below ports if VPN is enabled/ {
      print $0;
      while (getline && !/restart:/) {
        sub(/^    /, "    # ");
        sub(/^      /, "      # ");
        print;
      }
      print;
      next;
    }

    # Radarr and Sonarr networks
    /- media-stack-net.*# Comment this line if VPN is enabled/ {
      sub(/^      /, "      # ");
      print;
      next;
    }

    # Uncomment Radarr and Sonarr ipv4_address
    /## Uncomment below lines if VPN is enabled/ {
      print $0;
      while (getline && /^    #/) {
        sub(/^    # /, "    ");
        print;
      }
      print $0;
      next;
    }

    # Default action: print the line
    { print }
  ' "$COMPOSE_FILE" > "$COMPOSE_FILE.tmp" && mv "$COMPOSE_FILE.tmp" "$COMPOSE_FILE"

  echo "✅ VPN settings enabled successfully"
}

# Function to disable VPN
disable_vpn() {
  # Make a backup first
  backup_compose

  # Use awk to process the file
  awk '
    # VPN section ports
    /# Uncomment\/enable below ports if VPN is used\/enabled/ {
      print $0;
      getline;
      sub(/^    /, "    # ");
      print;
      while (getline && !/restart:/) {
        sub(/^      /, "    #   ");
        print;
      }
      print;
      next;
    }

    # qBittorrent section
    /Unomment below if vpn is enabled/ {
      print $0;
      while (getline && !/condition: service_healthy/) {
        sub(/^    /, "    # ");
        sub(/^      /, "      # ");
        print;
      }
      sub(/^      /, "      #   ");
      print;
      next;
    }

    # qBittorrent network_mode
    /network_mode: service:vpn/ {
      sub(/^    /, "    # ");
      print;
      next;
    }

    # Uncomment qBittorrent networks
    /# networks:.*# Comment this line if vpn is enabled/ {
      sub(/^    # /, "    ");
      print;
      next;
    }

    # Uncomment qBittorrent media-stack-net
    /# - media-stack-net.*# Comment this line if vpn is enabled/ {
      sub(/^      # /, "      ");
      print;
      next;
    }

    # Uncomment qBittorrent ports
    /## Comment\/Disable below ports if VPN is enabled/ {
      print $0;
      while (getline && !/restart:/) {
        sub(/^    # /, "    ");
        sub(/^      # /, "      ");
        print;
      }
      print;
      next;
    }

    # Prowlarr section
    /Uncomment below if vpn is enabled/ {
      print $0;
      while (getline && !/condition: service_healthy/) {
        sub(/^    /, "    # ");
        sub(/^      /, "      # ");
        print;
      }
      sub(/^      /, "      #   ");
      print;
      next;
    }

    # Prowlarr network_mode
    /network_mode: service:vpn # Uncomment this line if vpn is enabled/ {
      sub(/^    /, "    # ");
      print;
      next;
    }

    # Uncomment Prowlarr networks
    /# networks:.*# Comment this line if vpn is enabled/ {
      sub(/^    # /, "    ");
      print;
      next;
    }

    # Uncomment Prowlarr ports
    /Comment below ports if VPN is enabled/ {
      print $0;
      while (getline && !/restart:/) {
        sub(/^    # /, "    ");
        sub(/^      # /, "      ");
        print;
      }
      print;
      next;
    }

    # Radarr and Sonarr networks
    /# - media-stack-net.*# Comment this line if VPN is enabled/ {
      sub(/^      # /, "      ");
      print;
      next;
    }

    # Comment Radarr and Sonarr ipv4_address
    /media-stack-net:/ {
      sub(/^      /, "    #   ");
      print;
      getline;
      sub(/^        /, "    #     ");
      print;
      next;
    }

    # Default action: print the line
    { print }
  ' "$COMPOSE_FILE" > "$COMPOSE_FILE.tmp" && mv "$COMPOSE_FILE.tmp" "$COMPOSE_FILE"

  echo "✅ VPN settings disabled successfully"
}

if [[ "$1" == "on" ]]; then
  echo "🔐 Enabling VPN settings..."
  enable_vpn

elif [[ "$1" == "off" ]]; then
  echo "🔓 Disabling VPN settings..."
  disable_vpn

elif [[ "$1" == "status" ]]; then
  check_vpn_status

else
  echo "Usage: $0 [on|off|status]"
  echo "  on     - Enable VPN settings"
  echo "  off    - Disable VPN settings"
  echo "  status - Check current VPN status"
  exit 1
fi

UPDATE: Updated this. far more solid

willwade avatar Apr 07 '25 11:04 willwade

Thanks for the suggestion. I will check if this will be feasible

navilg avatar Apr 13 '25 09:04 navilg