azurevpnazure-automation

Issue with wg-easy VPN service and setting up auto reboot using Powershell script on Automation Accounts


I am experiencing issues with the wg-easy VPN service, as the VPS containers seem to go down frequently. I have set up a cron job to reboot every 2 hours, but it doesn't seem to help much.

I am also interested in setting up an auto reboot using a Powershell script on Automation Accounts, but I am not sure if my runbook script is correct. Here is the script I have written:

$resourceGroupName = "xxx-resource-group"
$vmNames = @(
    "xxx-virtual-machine",
    "xxx_virtual_machine",
    "xxx-virtual-machine"
)

$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -ScriptBlock {
    Connect-AzAccount -Identity

    foreach ($vmName in $vmNames) {
        Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
    }
} -Trigger $trigger

I would appreciate any help with troubleshooting the wg-easy VPN service issues and with verifying if my Powershell script is correct.

Thank you.

enter image description here


Solution

  • I wasn't able to solve the issue through the Powershell script and decided to make the docker.sh that reboots the VPN container. The cronjob initiates it.

    crontab -e
    
    # Ansible: a job to run the docker.sh script every few hours
    0 */12 * * * /home/user/dev/scripts/docker.sh
    
    
    docker.sh
    
    #!/bin/bash
    
    ## VPN Startup Script
    cd /home/user/dev/vpn && docker-compose up -d --force-recreate --remove-orphans
    
    
    # Run Docker Compose pull in all subdirectories
    
    for dir in "/home/user/dev"/*/; do
        [ -d "$dir" ] && cd "$dir" && docker compose pull --quiet
    
        # List all Docker images
        docker images -a
    
        # List Docker containers
        docker ps -a --format "{{.Names}}"
    
        # Delete unused Docker images
        for image in $(docker images -a -q); do
            if ! echo $(docker ps -a --format "{{.Names}}") | grep -q $(echo $image | awk -F ":" '{print $1}'); then
                echo "Deleting unused image: $image"
                docker rmi $image
            fi
        done
    
        cd "/home/user/dev"
    done