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 #95 from lanjoni/feat/kill-force-flag
Browse files Browse the repository at this point in the history
feat: --force flag to layerform kill
Closes #60
  • Loading branch information
vieiralucas authored Sep 18, 2023
2 parents c6ea71b + c04b96c commit 26c3541
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
9 changes: 8 additions & 1 deletion cmd/cli/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func init() {
killCmd.Flags().StringArray("var", []string{}, "a map of variables for the layer's Terraform files. I.e. 'foo=bar,baz=qux'")
killCmd.Flags().Bool("force", false, "force the destruction of the layer instance even if it has dependants")

rootCmd.AddCommand(killCmd)
}
Expand Down Expand Up @@ -47,6 +48,12 @@ Please notice that the kill command cannot destroy a layer instance which has de
os.Exit(1)
return
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to get --force flag, this is a bug in layerform"))
os.Exit(1)
return
}
kill, err := cfg.GetKillCommand(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to get kill command"))
Expand All @@ -56,7 +63,7 @@ Please notice that the kill command cannot destroy a layer instance which has de
layerName := args[0]
instanceName := args[1]

err = kill.Run(ctx, layerName, instanceName, false, vars)
err = kill.Run(ctx, layerName, instanceName, false, vars, force)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions pkg/command/kill/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (e *cloudKillCommand) Run(
definitionName, instanceName string,
autoApprove bool,
vars []string,
force bool,
) error {
logger := hclog.FromContext(ctx)
logger.Debug("Killing instance remotely")
Expand Down
66 changes: 66 additions & 0 deletions pkg/command/kill/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/ergomake/layerform/pkg/layerinstances"
)

type DependenceInfo struct {
DefinitionName string
InstanceName string
}

func HasDependants(
ctx context.Context,
instancesBackend layerinstances.Backend,
Expand Down Expand Up @@ -49,3 +54,64 @@ func HasDependants(

return false, nil
}

func GetDependants(
ctx context.Context,
instancesBackend layerinstances.Backend,
definitionsBackend layerdefinitions.Backend,
layerName, instanceName string,
visited map[string]bool,
) ([]DependenceInfo, error) {
hclog.FromContext(ctx).Debug("Finding dependant layers", "layer", layerName, "instance", instanceName)

definitions, err := definitionsBackend.ListLayers(ctx)
if err != nil {
return nil, errors.Wrap(err, "fail to list layers")
}

dependantLayers := []DependenceInfo{}

visited[layerName] = true

for _, definition := range definitions {
isChild := false
for _, d := range definition.Dependencies {
if d == layerName {
isChild = true
break
}
}

if isChild {
instances, err := instancesBackend.ListInstancesByLayer(ctx, definition.Name)
if err != nil {
return nil, errors.Wrap(err, "fail to list layer instances")
}

for _, instance := range instances {
parentInstanceName := instance.GetDependencyInstanceName(layerName)
if parentInstanceName == instanceName {
dependantLayers = append(dependantLayers, DependenceInfo{
DefinitionName: definition.Name,
InstanceName: instance.InstanceName,
})
} else if !visited[definition.Name] {
childDependantLayers, err := GetDependants(
ctx,
instancesBackend,
definitionsBackend,
definition.Name,
instance.InstanceName,
visited,
)
if err != nil {
return nil, err
}
dependantLayers = append(dependantLayers, childDependantLayers...)
}
}
}
}

return dependantLayers, nil
}
2 changes: 1 addition & 1 deletion pkg/command/kill/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

type Kill interface {
Run(ctx context.Context, definitionName, instanceName string, autoApprove bool, vars []string) error
Run(ctx context.Context, definitionName, instanceName string, autoApprove bool, vars []string, force bool) error
}
18 changes: 15 additions & 3 deletions pkg/command/kill/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (c *localKillCommand) Run(
layerName, instanceName string,
autoApprove bool,
vars []string,
force bool,
) error {
logger := hclog.FromContext(ctx)

Expand Down Expand Up @@ -82,23 +83,34 @@ func (c *localKillCommand) Run(
),
)

hasDependants, err := HasDependants(
dependants, err := GetDependants(
ctx,
c.instancesBackend,
c.definitionsBackend,
layerName,
instanceName,
make(map[string]bool),
)
if err != nil {
s.Error()
sm.Stop()
return errors.Wrap(err, "fail to check if layer has dependants")
}

if hasDependants {
if len(dependants) > 0 && !force {
s.Error()
sm.Stop()
return errors.New("can't kill this layer because other layers depend on it")
return errors.New("can't kill this layer because other layers depend on it\nuse the --force flag to kill it anyway")
}

if force {
autoApprove = true
for _, d := range dependants {
err = c.Run(ctx, d.DefinitionName, d.InstanceName, autoApprove, vars, force)
if err != nil {
return errors.Wrapf(err, "fail to kill dependant %s=%s", d.DefinitionName, d.InstanceName)
}
}
}

envVars, err := c.envVarsBackend.ListVariables(ctx)
Expand Down

0 comments on commit 26c3541

Please sign in to comment.