Skip to content

Commit

Permalink
"up" cmd should use previous values parameters that haven't been prov…
Browse files Browse the repository at this point in the history
…ided
  • Loading branch information
aidansteele committed Mar 5, 2018
1 parent cab5e3e commit cbe63dc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
4 changes: 1 addition & 3 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func parseCLIInput(
previousTemplate bool) stackit.StackitUpInput {
input := stackit.StackitUpInput{
StackName: aws.String(stackName),
PopulateMissing: true,
}

if len(serviceRole) > 0 {
Expand Down Expand Up @@ -157,7 +158,6 @@ func parseCLIInput(
}
}


configFileParameters := viper.GetStringSlice("parameters")
populateParamMap(configFileParameters)
populateParamMap(cliParamValues)
Expand Down Expand Up @@ -195,7 +195,6 @@ func parseCLIInput(
input.Tags = cfnTags
}


if len(notificationArns) > 0 {
cfnNotificationArns := []*string{}

Expand All @@ -206,7 +205,6 @@ func parseCLIInput(
input.NotificationARNs = cfnNotificationArns
}


input.Capabilities = aws.StringSlice([]string{"CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"})

return input
Expand Down
90 changes: 63 additions & 27 deletions stackit/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,75 @@ import (
)

type StackitUpInput struct {
StackName *string
RoleARN *string
StackPolicyBody *string
TemplateBody *string
StackName *string
RoleARN *string
StackPolicyBody *string
TemplateBody *string
PreviousTemplate *bool
Parameters []*cloudformation.Parameter
Tags []*cloudformation.Tag
Parameters []*cloudformation.Parameter
Tags []*cloudformation.Tag
NotificationARNs []*string
Capabilities []*string
Capabilities []*string
PopulateMissing bool
}

type StackUpOutput struct {
Channel *chan TailStackEvent
StackId string
NoOp bool
NoOp bool
}

func Up(sess *session.Session, input StackitUpInput) (*StackUpOutput, error) {
func populateMissing(input *StackitUpInput, stack *cloudformation.Stack) {
paramExists := func(name string) bool {
for _, param := range input.Parameters {
if *param.ParameterKey == name {
return true
}
}
return false
}

for _, param := range stack.Parameters {
if !paramExists(*param.ParameterKey) {
input.Parameters = append(input.Parameters, &cloudformation.Parameter{
ParameterKey: param.ParameterKey,
UsePreviousValue: aws.Bool(true),
})
}
}

if input.TemplateBody == nil || len(*input.TemplateBody) == 0 {
input.PreviousTemplate = aws.Bool(true)
}
}

func CleanStackExists(sess *session.Session, name string) (bool, *cloudformation.Stack) {
cfn := cloudformation.New(sess)

resp, err := cfn.DescribeStacks(&cloudformation.DescribeStacksInput{StackName: input.StackName})
resp, err := cfn.DescribeStacks(&cloudformation.DescribeStacksInput{StackName: &name})
stackExists := err == nil

if resp != nil && len(resp.Stacks) > 0 {
stack := resp.Stacks[0]
if *stack.StackStatus == "CREATE_FAILED" || *stack.StackStatus == "ROLLBACK_COMPLETE" {
cfn.DeleteStack(&cloudformation.DeleteStackInput{StackName: input.StackName})
cfn.DeleteStack(&cloudformation.DeleteStackInput{StackName: &name})
stackExists = false
time.Sleep(time.Duration(3) * time.Second) // wait for cloudformation to register stack deletion
}
}

if stackExists {
return true, resp.Stacks[0]
} else {
return false, nil
}
}

func Up(sess *session.Session, input StackitUpInput) (*StackUpOutput, error) {
stackExists, stack := CleanStackExists(sess, *input.StackName)

if stackExists {
if input.PopulateMissing { populateMissing(&input, stack) }
return updateStack(sess, input)
} else {
return createStack(sess, input)
Expand All @@ -59,15 +95,15 @@ func updateStack(sess *session.Session, input StackitUpInput) (*StackUpOutput, e
return nil, err
}
_, err = cfn.UpdateStack(&cloudformation.UpdateStackInput{
StackName: input.StackName,
Capabilities: input.Capabilities,
RoleARN: input.RoleARN,
StackName: input.StackName,
Capabilities: input.Capabilities,
RoleARN: input.RoleARN,
StackPolicyDuringUpdateBody: input.StackPolicyBody,
TemplateBody: input.TemplateBody,
UsePreviousTemplate: input.PreviousTemplate,
Parameters: input.Parameters,
Tags: input.Tags,
NotificationARNs: input.NotificationARNs,
TemplateBody: input.TemplateBody,
UsePreviousTemplate: input.PreviousTemplate,
Parameters: input.Parameters,
Tags: input.Tags,
NotificationARNs: input.NotificationARNs,
})

event := describeResp.StackEvents[0]
Expand All @@ -80,7 +116,7 @@ func updateStack(sess *session.Session, input StackitUpInput) (*StackUpOutput, e
return &StackUpOutput{
Channel: nil,
StackId: stackId,
NoOp: true,
NoOp: true,
}, nil
}
}
Expand All @@ -92,21 +128,21 @@ func updateStack(sess *session.Session, input StackitUpInput) (*StackUpOutput, e
return &StackUpOutput{
Channel: &channel,
StackId: stackId,
NoOp: false,
NoOp: false,
}, nil
}

func createStack(sess *session.Session, input StackitUpInput) (*StackUpOutput, error) {
cfn := cloudformation.New(sess)

resp, err := cfn.CreateStack(&cloudformation.CreateStackInput{
StackName: input.StackName,
StackName: input.StackName,
Capabilities: input.Capabilities,
RoleARN: input.RoleARN,
RoleARN: input.RoleARN,
//StackPolicyBody: input.StackPolicyBody,
TemplateBody: input.TemplateBody,
Parameters: input.Parameters,
Tags: input.Tags,
TemplateBody: input.TemplateBody,
Parameters: input.Parameters,
Tags: input.Tags,
NotificationARNs: input.NotificationARNs,
})

Expand All @@ -119,7 +155,7 @@ func createStack(sess *session.Session, input StackitUpInput) (*StackUpOutput, e
return &StackUpOutput{
Channel: &channel,
StackId: *resp.StackId,
NoOp: false,
NoOp: false,
}, nil
}
}

0 comments on commit cbe63dc

Please sign in to comment.