Restart target server and then perform actions after it's back online

I am creating a runbook for restarting our database server. Everything connects to the database so we need to shut down various services, websites, etc while the database server is restarting so a runbook is the perfect vehicle for coordinating these activities.

One of the runbook steps is to initiate the server restart. The restart could take several minutes if updates are being installed. After the database server is back online, I need to reverse the initial set of changes (start up websites, services, etc.). However, I need to somehow pause the process until the server is back online before running the last steps. How can I do this?

Hi William,

Thanks for reaching out.

We don’t have anything built in, but you after the step that restarts the server, you could have a Run a Script step that is just a loop that either pings the server, or runs a SQL command to see if the database is up and operational. I would hedge toward the latter as a successful ping might happen before the server is actually operational.

Do you think that would fit your use case?

Please let me know.

Thanks,
Jeremy

Hi William,

I wanted to add this example of a step that may cover what you want. I have not tested it myself, so please read it thoroughly and run it in a test environment before putting it into prod.

{
  "Id": "b0167505-016b-4d04-a3d3-56958f42b996",
  "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\nWrite-Host \"Will start checking in 30 seconds\"\nStart-Sleep -Seconds 30\n$statusCode = 0\n$timeout = New-TimeSpan -Minutes 5\n$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()\ndo {\n  Start-Sleep -Seconds 5\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  \tWrite-Host \"Unable to load https://${OctopusMachineHostname}:11933/\"\n  \t$statusCode = 0\n  }    \n} while ($statusCode -ne 200 -and $stopwatch.elapsed -lt $timeout)\n\nWrite-Host \"Will proceed in 2 minutes\"\nStart-Sleep -Seconds 120"
  },
  "Parameters": [],
  "$Meta": {
    "ExportedAt": "2020-11-17T16:24:31.592Z",
    "OctopusVersion": "2020.2.11",
    "Type": "ActionTemplate"
  },
  "LastModifiedBy": "Your GitHub Username",
  "Category": "other"
}

Thanks @jeremy.miller. That looks like it could do the trick.

1 Like

You’re very welcome! Thanks for letting me know.

Please let me know if you run into any issues and I hope you have a great rest of your week.

Thanks,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.