update windows-cell drain script
returns 0 if unable to discover services
@@ -6,11 +6,13 @@ $log_dir="/var/vcap/sys/log/rep_windows"
$logfile="${log_dir}/drain.log"
$address="http://<%= p("diego.rep.enable_legacy_api_endpoints") ? p("diego.rep.listen_addr").sub(/^0\.0\.0\.0:/, "localhost:") : p("diego.rep.listen_addr_admin")%>"
-New-Item -ItemType "directory" -Force "${log_dir}" 2>&1 >> $logfile
+New-Item -ItemType "directory" -Force "${log_dir}" 2>&1 | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
function evacuate {
try {
- Invoke-WebRequest -URI "${address}/evacuate" -Method "Post" 2>&1 >> $logfile
+ Invoke-WebRequest -UseBasicParsing -URI "${address}/evacuate" -Method "Post" 2>&1 | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
} catch {
return $false
}
@@ -19,32 +21,46 @@ function evacuate {
function heartbeat {
try {
- Invoke-WebRequest -URI "${address}/ping" 2>&1 >> $logfile
+ Invoke-WebRequest -UseBasicParsing -URI "${address}/ping" 2>&1 | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
} catch {
return $false
}
return $true
}
-if ((Get-Service -Name "${service_name}").Status -ne "Running") {
- echo "$(Get-Date): service '${service_name}' not running" >> $logfile
- echo "0"
+$svc = (Get-Service | Where { $_.Name -eq $service_name })
+if ($svc -eq $null) {
+ "$(Get-Date): service not found: '${service_name}'" | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
+ "0"
+ Exit 0
+}
+if ($svc.Status -ne 'Running') {
+ "$(Get-Date): service '${service_name}' not running" | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
+ "0"
Exit 0
}
-Set-Service "${service_name}" -StartupType "Disabled"
-echo "$(Get-Date): service '${service_name}' StartupType set to 'Disabled'" >> $logfile
+$svc | Set-Service -StartupType Disabled
+"$(Get-Date): service '${service_name}' StartupType set to 'Disabled'" | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
-echo "$(Get-Date): drain triggered" >> $logfile
+"$(Get-Date): drain triggered" | Out-File -Append -Encoding utf8 -FilePath $logfile
if (evacuate) {
while (heartbeat) {
Start-Sleep -Seconds 5
- echo "$(Get-Date): waiting" >> $logfile
+ "$(Get-Date): waiting" | Out-File -Append -Encoding utf8 -FilePath $logfile
}
+ # NOTE: rep invokes 'kill_and_wait' here
+} else {
+ Exit 1 # NOTE: matching behavior of 'rep' drain script
}
-Set-Service "${service_name}" -StartupType "Automatic"
-echo "$(Get-Date): service '${service_name}' StartupType set to 'Automatic'" >> $logfile
+$svc | Set-Service -StartupType Automatic
+"$(Get-Date): service '${service_name}' StartupType set to 'Automatic'" | `
+ Out-File -Append -Encoding utf8 -FilePath $logfile
-echo "0"
+"0"
Exit 0
