Skip to content

Commit

Permalink
F OpenNebula#85: Retry report READY=YES
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Ospalý <[email protected]>
  • Loading branch information
Petr Ospalý committed Apr 6, 2021
1 parent 3a76947 commit 8180afe
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions src/context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ function reportReady($context, $contextLetter)
$oneGateEndpoint = $context['ONEGATE_ENDPOINT']
$vmId = $context['VMID']
$token = $context['ONEGATE_TOKEN']
$retryCount = 3
$retryWaitPeriod = 10

if ($reportReady -and $reportReady.ToUpper() -eq 'YES') {
logmsg '* Report Ready to OneGate'
Expand All @@ -919,45 +921,57 @@ function reportReady($context, $contextLetter)
}
}

try {
$retryNumber = 1
while ($true) {
try {
$body = 'READY=YES'
$target= $oneGateEndpoint + '/vm'

[System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.Timeout = 10000
$webRequest.Method = 'PUT'
$webRequest.Headers.Add('X-ONEGATE-TOKEN', $token)
$webRequest.Headers.Add('X-ONEGATE-VMID', $vmId)
$buffer = [System.Text.Encoding]::UTF8.GetBytes($body)
$webRequest.ContentLength = $buffer.Length

if ($oneGateEndpoint -ilike "https://*") {
#For reporting on HTTPS OneGateEndpoint
logmsg " ... Use HTTPS for OneGateEndpoint report: $oneGateEndpoint"
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::Expect100Continue = $false
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
}

$body = 'READY=YES'
$target= $oneGateEndpoint + '/vm'

[System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.Timeout = 10000
$webRequest.Method = 'PUT'
$webRequest.Headers.Add('X-ONEGATE-TOKEN', $token)
$webRequest.Headers.Add('X-ONEGATE-VMID', $vmId)
$buffer = [System.Text.Encoding]::UTF8.GetBytes($body)
$webRequest.ContentLength = $buffer.Length

if ($oneGateEndpoint -ilike "https://*") {
#For reporting on HTTPS OneGateEndpoint
logmsg " ... Use HTTPS for OneGateEndpoint report: $oneGateEndpoint"
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::Expect100Continue = $false
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
}
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($buffer, 0, $buffer.Length)
$requestStream.Flush()
$requestStream.Close()

$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($buffer, 0, $buffer.Length)
$requestStream.Flush()
$requestStream.Close()
$response = $webRequest.getResponse()
if ($response.StatusCode -eq 'OK') {
logmsg ' ... Success'
break
} else {
logmsg " ... Failed: $($response.StatusCode)"
}
}
catch {
$errorMessage = $_.Exception.Message
logmsg " ... Failed: $errorMessage"
}

$response = $webRequest.getResponse()
if ($response.StatusCode -eq 'OK') {
logmsg ' ... Success'
logmsg " ... Report ready failed (${retryNumber}. try out of ${retryCount})"
$retryNumber++
if ($retryNumber -le $retryCount) {
logmsg " ... sleep for ${retryWaitPeriod} seconds and try again..."
Start-Sleep -Seconds $retryWaitPeriod
} else {
logmsg " ... Failed: $($response.StatusCode)"
logmsg " ... All retries failed!"
break
}
}
catch {
$errorMessage = $_.Exception.Message

logmsg " ... Failed:`r`n$errorMessage"
}
}
}

Expand Down

0 comments on commit 8180afe

Please sign in to comment.