Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from ergomake/save-state-on-spawn-error
Browse files Browse the repository at this point in the history
save instance state even when fail to spawn
  • Loading branch information
vieiralucas authored Sep 2, 2023
2 parents e05cc65 + c909e8b commit d3433d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/list_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Prints a table of the most important information about layer instances.`,
sortInstancesByDepth(instances, layersByName)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(w, "INSTANCE NAME\tLAYER NAME\tDEPENDENCIES")
fmt.Fprintln(w, "INSTANCE NAME\tLAYER NAME\tDEPENDENCIES\tSTATUS")
for _, instance := range instances {
layer := layersByName[instance.LayerName]
deps := ""
Expand All @@ -86,7 +86,7 @@ Prints a table of the most important information about layer instances.`,
deps += dep + "=" + depInstName
}

fmt.Fprintln(w, instance.StateName+"\t"+instance.LayerName+"\t"+deps)
fmt.Fprintln(w, instance.StateName+"\t"+instance.LayerName+"\t"+deps+"\t"+string(instance.Status))
}
err = w.Flush()

Expand Down
23 changes: 22 additions & 1 deletion internal/command/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,28 @@ func (c *spawnCommand) spawnLayer(
logger.Debug("Running terraform apply")
err = tf.Apply(ctx, applyOptions...)
if err != nil {
originalErr := err
s.Stop()
return "", errors.Wrap(err, "fail to terraform apply")

nextStateBytes, err = os.ReadFile(statePath)
if err != nil {
return "", errors.Wrap(err, "fail to read next state")
}

state = &layerstate.State{
LayerSHA: layer.SHA,
LayerName: layerName,
StateName: stateName,
DependenciesState: thisLayerDepStates,
Bytes: nextStateBytes,
Status: layerstate.InstanceStatusFaulty,
}
err = c.statesBackend.SaveState(ctx, state)
if err != nil {
return "", errors.Wrap(err, "fail to save state of failed instance")
}

return "", errors.Wrap(originalErr, "fail to terraform apply")
}

nextStateBytes, err = os.ReadFile(statePath)
Expand All @@ -336,6 +356,7 @@ func (c *spawnCommand) spawnLayer(
StateName: stateName,
DependenciesState: thisLayerDepStates,
Bytes: nextStateBytes,
Status: layerstate.InstanceStatusAlive,
}
err = c.statesBackend.SaveState(ctx, state)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/layerstate/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import (
"errors"
)

type InstanceStatus string

const (
InstanceStatusAlive InstanceStatus = InstanceStatus("alive")
InstanceStatusFaulty InstanceStatus = InstanceStatus("faulty")
)

type State struct {
LayerSHA []byte `json:"layerSHA"`
LayerName string `json:"layerName"`
StateName string `json:"stateName"`
DependenciesState map[string]string `json:"dependenciesState"`
Bytes []byte `json:"bytes"`
Status InstanceStatus `json:"status"`
}

const DEFAULT_LAYER_STATE_NAME = "default"
Expand Down

0 comments on commit d3433d2

Please sign in to comment.