|
|
|
@ -4,22 +4,29 @@ $nodeList = docker node ls --format '{{json .}}'| ConvertFrom-Json | ForEach-Obj
|
|
|
|
|
docker node inspect $_.ID | ConvertFrom-Json
|
|
|
|
|
}
|
|
|
|
|
$onlineNodeList = $nodeList | Where-Object {$_.Spec.Availability -eq 'active'}
|
|
|
|
|
$offlineNodeList = $nodeList | Where-Object {$_.Spec.Availability -ne 'active'}
|
|
|
|
|
|
|
|
|
|
if (!$offlineNodeList.Count) {
|
|
|
|
|
if (!($nodeList | Where-Object {$_.Spec.Availability -ne 'active'})) {
|
|
|
|
|
Write-Output 'all hosts online, nothing to failover'
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$failoverAddressList = (Invoke-RestMethod -Uri 'https://robot-ws.your-server.de/failover' -Authentication Basic -Credential $hetznerCredentials).failover
|
|
|
|
|
$offlineAddressList = $failoverAddressList | Where-Object active_server_ip -notin $onlineNodeList.Status.Addr
|
|
|
|
|
$onlineNodeAddressList = $onlineNodeList.Description.Hostname | ForEach-Object {
|
|
|
|
|
dig +short $_
|
|
|
|
|
}
|
|
|
|
|
$offlineAddressList = $failoverAddressList | Where-Object active_server_ip -notin $onlineNodeAddressList
|
|
|
|
|
|
|
|
|
|
$maximumAddressCount = [Math]::Ceiling($failoverAddressList.Count / $onlineNodeList.Count)
|
|
|
|
|
|
|
|
|
|
$i = 0
|
|
|
|
|
$addressGroupList = $failoverAddressList+($onlineNodeList.Status.Addr | ForEach-Object {@{active_server_ip=$_}}) `
|
|
|
|
|
| Where-Object active_server_ip -in $onlineNodeList.Status.Addr `
|
|
|
|
|
| Group-Object active_server_ip `
|
|
|
|
|
$addressGroupList = $onlineNodeAddressList
|
|
|
|
|
| ForEach-Object {
|
|
|
|
|
$nodeAddress = $_
|
|
|
|
|
@{
|
|
|
|
|
Count = [int]($addressGroupList | Where-Object {$_.Name -eq $nodeAddress}).Count
|
|
|
|
|
NodeAddress = $nodeAddress
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
| Sort-Object Count
|
|
|
|
|
$distributionTarget = :distribution foreach ($addressGroup in $addressGroupList) {
|
|
|
|
|
$currentAddressCount = $addressGroup.Count - 1
|
|
|
|
@ -31,9 +38,9 @@ $distributionTarget = :distribution foreach ($addressGroup in $addressGroupList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@{
|
|
|
|
|
active_server_ip = $addressGroup.Name
|
|
|
|
|
active_server_ip = $addressGroup.NodeAddress
|
|
|
|
|
ip = $offlineAddressList[$i++].ip
|
|
|
|
|
node = $nodeList | Where-Object {$_.Status.Addr -eq $addressGroup.Name}
|
|
|
|
|
node = (dig +short -x $($addressGroup.NodeAddress)) -replace '\.$'
|
|
|
|
|
}
|
|
|
|
|
$currentAddressCount++
|
|
|
|
|
}
|
|
|
|
@ -49,7 +56,7 @@ $distributionTarget | ForEach-Object {
|
|
|
|
|
$cleanupNodeList = docker node ls --filter $filter --format '{{json .}}' | ConvertFrom-Json
|
|
|
|
|
|
|
|
|
|
$label = $labelName+'=true'
|
|
|
|
|
docker node update --label-add $label $($_.node.ID)
|
|
|
|
|
docker node update --label-add $label $($_.node)
|
|
|
|
|
|
|
|
|
|
$cleanupNodeList | ForEach-Object {
|
|
|
|
|
docker node update --label-rm $labelName $($_.ID)
|
|
|
|
@ -57,4 +64,4 @@ $distributionTarget | ForEach-Object {
|
|
|
|
|
}
|
|
|
|
|
$distributionTarget | ForEach-Object -ThrottleLimit $failoverAddressList.Count -Parallel {
|
|
|
|
|
Invoke-RestMethod -Method Post -Uri ('https://robot-ws.your-server.de/failover/{0}' -f $_.ip) -Authentication Basic -Credential $using:hetznerCredentials -Body @{active_server_ip=$_.active_server_ip}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|