Last active
November 20, 2020 20:34
-
-
Save BlythMeister/b21127070ec3b44fcd768a16d16595a8 to your computer and use it in GitHub Desktop.
Octopus Tentacle Reboot Step Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Id": "dfbf8776-b178-46f4-9bd2-f242c74d2551", | |
"Name": "Reboot Machine", | |
"Description": "Will reboot a tentacle without it noticing in a deploy", | |
"ActionType": "Octopus.Script", | |
"Version": 1, | |
"CommunityActionTemplateId": null, | |
"Packages": [], | |
"Properties": { | |
"Octopus.Action.Script.ScriptSource": "Inline", | |
"Octopus.Action.Script.Syntax": "PowerShell", | |
"Octopus.Action.Script.ScriptBody": "function Test-RegistryKey([string]$Key) \n{ \n if (Get-Item -Path $Key -ErrorAction Ignore) \n {\n $true\n }\n}\n\nfunction Test-RegistryValue([string]$Key,[string]$Value)\n{\n if (Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) {\n $true\n }\n}\n\nfunction Test-RegistryValueNotNull([string]$Key,[string]$Value)\n{\n if (($regVal = Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) -and $regVal.($Value)) {\n $true\n }\n}\n\nfunction Test-RebootRequired()\n{\n $tests = @(\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending' }\n { Test-RegistryKey -Key 'HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootInProgress' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\RebootRequired' }\n { Test-RegistryKey -Key 'HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\PackagesPending' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\PostRebootReporting' }\n { Test-RegistryValueNotNull -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager' -Value 'PendingFileRenameOperations' }\n { Test-RegistryValueNotNull -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager' -Value 'PendingFileRenameOperations2' }\n { \n 'HKLM:\\SOFTWARE\\Microsoft\\Updates' | Where-Object { test-path $_ -PathType Container } | ForEach-Object { \n (Get-ItemProperty -Path $_ -Name 'UpdateExeVolatile' -ErrorAction Ignore | Select-Object -ExpandProperty UpdateExeVolatile) -ne 0 \n }\n }\n { Test-RegistryValue -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce' -Value 'DVDRebootSignal' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\ServerManager\\CurrentRebootAttemps' }\n { Test-RegistryValue -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Netlogon' -Value 'JoinDomain' }\n { Test-RegistryValue -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Netlogon' -Value 'AvoidSpnSet' }\n {\n ( 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName' | Where-Object { test-path $_ } | %{ (Get-ItemProperty -Path $_ ).ComputerName } ) -ne \n ( 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName' | Where-Object { Test-Path $_ } | %{ (Get-ItemProperty -Path $_ ).ComputerName } )\n }\n {\n 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Services\\Pending' | Where-Object { \n (Test-Path $_) -and (Get-ChildItem -Path $_) \n } | ForEach-Object { $true }\n }\n )\n\n foreach ($test in $tests) {\n Write-Verbose \"Running scriptblock: [$($test.ToString())]\"\n if (& $test) {\n $true\n break\n }\n }\n}\n\nif(Test-RegistryValue -Key 'HKLM:\\SOFTWARE\\OctopusSteps' -Value 'RebootStepEnable')\n{\n Write-Host \"RegKey reboot in 15 seconds\"\n Set-OctopusVariable -name \"RebootStarted\" -value \"True\"\n Remove-ItemProperty -Path \"HKLM:\\SOFTWARE\\OctopusSteps\" -Name \"RebootStepEnable\"\n Invoke-Command LocalHost -Scriptblock { Start-Sleep -Seconds 15; Restart-Computer -Force } -InDisconnectedSession\n}\nelseif($ForceReboot -eq $true)\n{\n Write-Host \"Forced reboot in 15 seconds\"\n Set-OctopusVariable -name \"RebootStarted\" -value \"True\"\n Invoke-Command LocalHost -Scriptblock { Start-Sleep -Seconds 15; Restart-Computer -Force } -InDisconnectedSession\n}\nelseif(Test-RebootRequired)\n{\n Write-Host \"Reboot in 15 seconds\"\n Set-OctopusVariable -name \"RebootStarted\" -value \"True\"\n Invoke-Command LocalHost -Scriptblock { Start-Sleep -Seconds 15; Restart-Computer -Force } -InDisconnectedSession\n}\nelse\n{\n Write-Host \"No reboot pending\"\n Set-OctopusVariable -name \"RebootStarted\" -value \"False\"\n}" | |
}, | |
"Parameters": [ | |
{ | |
"Id": "e5575bca-2e76-4044-a044-a3616e44d81c", | |
"Name": "ForceReboot", | |
"Label": "Force Reboot", | |
"HelpText": "Should a reboot happen even if not one pending", | |
"DefaultValue": "false", | |
"DisplaySettings": { | |
"Octopus.ControlType": "Checkbox" | |
} | |
} | |
], | |
"$Meta": { | |
"ExportedAt": "2020-11-20T20:34:06.044Z", | |
"OctopusVersion": "2020.2.11", | |
"Type": "ActionTemplate" | |
}, | |
"LastModifiedBy": "Your GitHub Username", | |
"Category": "other" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Id": "e6ce44cf-6dca-4900-8db3-d62e908680b7", | |
"Name": "Wait For Tentacle", | |
"Description": "Used after a tentacle is restarted, poll until tentacle back online", | |
"ActionType": "Octopus.Script", | |
"Version": 1, | |
"CommunityActionTemplateId": null, | |
"Packages": [], | |
"Properties": { | |
"Octopus.Action.Script.ScriptSource": "Inline", | |
"Octopus.Action.Script.Syntax": "PowerShell", | |
"Octopus.Action.Script.ScriptBody": "#Octopus Cert is not valid, so ignore cert errros\nAdd-Type @\"\n using System.Net;\n using System.Security.Cryptography.X509Certificates;\n public class TrustAllCertsPolicy : ICertificatePolicy {\n public bool CheckValidationResult(\n ServicePoint srvPoint, X509Certificate certificate,\n WebRequest request, int certificateProblem) {\n return true;\n }\n }\n\"@\n[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy\n\nif($RebootStepName -ne $null -and $RebootStepName -ne \"\")\n{\n\tWrite-Host \"Checking if step '$RebootStepName' started a reboot\"\n\n $RebootStarted = $OctopusParameters[\"Octopus.Action[$RebootStepName].Output.RebootStarted\"]\n if($RebootStarted -ne $null)\n {\n if($RebootStarted -eq $true)\n {\n Write-Host \"Will start checking in 30 seconds\"\n Start-Sleep -Seconds 30\n $statusCode = 0\n $timeout = New-TimeSpan -Minutes 5\n $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()\n do {\n try {\n $response = Invoke-WebRequest -Method Get -Uri \"https://${OctopusMachineHostname}:11933/\" -UseBasicParsing\n $statusCode = $response.StatusCode\n Write-Host \"Status was $statusCode\"\n }\n catch \n {\n Write-Host \"Unable to load https://${OctopusMachineHostname}:11933/\"\n $statusCode = 0\n }\n if($statusCode -ne 200)\n {\n Start-Sleep -Seconds 5\n }\n } while ($statusCode -ne 200 -and $stopwatch.elapsed -lt $timeout)\n\n Write-Host \"Will proceed in 30 seconds\"\n Start-Sleep -Seconds 30\n }\n else\n {\n Write-Host \"Reboot was not started\"\n }\n }\n else\n {\n Write-Host \"No reboot step named '$RebootStepName' found\"\n }\n}\nelse\n{\n Write-Host \"No reboot step name parameter\"\n}" | |
}, | |
"Parameters": [ | |
{ | |
"Id": "b85fbfa4-acea-4e5b-8d99-4b5639a3e11c", | |
"Name": "RebootStepName", | |
"Label": "Reboot Step Name", | |
"HelpText": null, | |
"DefaultValue": "\"\"", | |
"DisplaySettings": { | |
"Octopus.ControlType": "SingleLineText" | |
} | |
} | |
], | |
"$Meta": { | |
"ExportedAt": "2020-11-20T15:48:28.595Z", | |
"OctopusVersion": "2020.2.11", | |
"Type": "ActionTemplate" | |
}, | |
"LastModifiedBy": "Your GitHub Username", | |
"Category": "other" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment