Skip to content

Commit

Permalink
incusd/instances: Add support for boot.host_shutdown_action
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Jan 25, 2024
1 parent c9c34a7 commit 2cf1383
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions cmd/incusd/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,24 @@ func instancesStart(s *state.State, instances []instance.Instance) {
// Get the instance config.
config := inst.ExpandedConfig()
autoStartDelay := config["boot.autostart.delay"]
shutdownAction := config["boot.host_shutdown_action"]

instLogger := logger.AddContext(logger.Ctx{"project": inst.Project().Name, "instance": inst.Name()})

// Try to start the instance.
var attempt = 0
for {
attempt++
err := inst.Start(false)

var err error
if shutdownAction == "stateful-stop" {
// Attempt to restore state.
err = inst.Start(true)
} else {
// Normal startup.
err = inst.Start(false)
}

if err != nil {
if api.StatusErrorCheck(err, http.StatusServiceUnavailable) {
break // Don't log or retry instances that are not ready to start yet.
Expand Down Expand Up @@ -379,13 +389,26 @@ func instancesShutdown(s *state.State, instances []instance.Instance) {
timeoutSeconds, _ = strconv.Atoi(value)
}

err := inst.Shutdown(time.Second * time.Duration(timeoutSeconds))
if err != nil {
logger.Warn("Failed shutting down instance, forcefully stopping", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
err = inst.Stop(false)
action := inst.ExpandedConfig()["boot.host_shutdown_action"]
if action == "stateful-stop" {
err := inst.Stop(true)
if err != nil {
logger.Warn("Failed statefully stopping instance", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
}
} else if action == "force-stop" {
err := inst.Stop(false)
if err != nil {
logger.Warn("Failed forcefully stopping instance", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
}
} else {
err := inst.Shutdown(time.Second * time.Duration(timeoutSeconds))
if err != nil {
logger.Warn("Failed shutting down instance, forcefully stopping", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
err = inst.Stop(false)
if err != nil {
logger.Warn("Failed forcefully stopping instance", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
}
}
}

if inst.ID() > 0 {
Expand Down

0 comments on commit 2cf1383

Please sign in to comment.