You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB
PowerShell

5 years ago
#Requires -Modules powershell-yaml
$ErrorActionPreference = 'Stop'
foreach($yaml in (Get-ChildItem -Filter '*.yml')) {
Write-Output ('Processing {0}' -f $yaml)
$definition = Get-Content -Path $yaml -Raw | ConvertFrom-Yaml
foreach($port in $definition.services.Values.ports) {
$nat = @{
protocol = $null
public_ip = $null
public_port = $null
internal_port = $null
}
if($port.Count -eq 4) {
#long form
$published_splitted = $port.published -split ':'
$nat.protocol = $port.protocol
$nat.public_ip = $published_splitted[0]
$nat.public_port = $port.target
$nat.internal_port = $published_splitted[1]
} else {
#short form
$ports_splitted = $port -split ':'
$nat.public_ip = $ports_splitted[0]
$nat.public_port = $ports_splitted[2]
$nat.internal_port = $ports_splitted[1]
}
if(!$nat.protocol) {
#this is also Docker's default
$nat.protocol = 'tcp'
}
if($nat.Values -contains $null) {
#if $nat doesn't conatain all needed attributes skip the port
$error_message = 'Skipping port, because $nat contains $null: {0}' `
-f ($nat | ConvertTo-Json)
Write-Error -Message $error_message `
-ErrorAction Continue
continue
}
if($nat.internal_port -ne $nat.public_port) {
Write-Output ('Additional NAT rule required, because published {0} and target {1} differ' `
-f $nat.internal_port,$nat.public_port)
$nat.protocol
$nat.public_ip
$nat.public_port
$nat.internal_port
}
}
}