-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/logrusorgru/aurora" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func updateChangeSetAndWait(sess *session.Session, stackInput *cloudformation.UpdateStackInput) (*cloudformation.DescribeChangeSetOutput, error) { | ||
cfnSvc := cloudformation.New(sess) | ||
changeSetName := fmt.Sprintf("update-%d", int32(time.Now().Unix())) | ||
_, err := cfnSvc.CreateChangeSet(&cloudformation.CreateChangeSetInput{ | ||
ChangeSetType: aws.String("UPDATE"), | ||
ChangeSetName: &changeSetName, | ||
StackName: stackInput.StackName, | ||
TemplateURL: stackInput.TemplateURL, | ||
Parameters: stackInput.Parameters, | ||
Capabilities: stackInput.Capabilities, | ||
Tags: stackInput.Tags, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
describeChangeSetInput := cloudformation.DescribeChangeSetInput{ | ||
ChangeSetName: &changeSetName, | ||
StackName: stackInput.StackName, | ||
} | ||
err = cfnSvc.WaitUntilChangeSetCreateComplete(&describeChangeSetInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
changeSet, err := cfnSvc.DescribeChangeSet(&describeChangeSetInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return changeSet, nil | ||
} | ||
|
||
func updateStackAndWait(sess *session.Session, stackInput *cloudformation.UpdateStackInput) (*cloudformation.Stack, error) { | ||
cfnSvc := cloudformation.New(sess) | ||
_, err := cfnSvc.UpdateStack(stackInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
describeStacksInput := cloudformation.DescribeStacksInput{StackName: stackInput.StackName} | ||
err = cfnSvc.WaitUntilStackUpdateComplete(&describeStacksInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stack, err := cfnSvc.DescribeStacks(&describeStacksInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return stack.Stacks[0], nil | ||
} | ||
|
||
// upgradeCmd represents the upgrade command | ||
var upgradeCmd = &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "upgrade AppPack stacks", | ||
Long: `Upgrade AppPack stacks`, | ||
} | ||
|
||
// upgradeCmd represents the upgrade command | ||
var upgradeAppCmd = &cobra.Command{ | ||
Use: "app [NAME]", | ||
Short: "upgrade the AppPack stack for a given application", | ||
Long: `Upgrade the AppPack stack for a given application`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
startSpinner() | ||
sess := session.Must(session.NewSession()) | ||
cfnSvc := cloudformation.New(sess) | ||
appName := args[0] | ||
stackName := fmt.Sprintf("apppack-app-%s", appName) | ||
stackOutput, err := cfnSvc.DescribeStacks(&cloudformation.DescribeStacksInput{ | ||
StackName: &stackName, | ||
}) | ||
checkErr(err) | ||
Spinner.Stop() | ||
fmt.Println(aurora.Faint(fmt.Sprintf("upgrading %s", *stackOutput.Stacks[0].StackId))) | ||
startSpinner() | ||
updateStackInput := cloudformation.UpdateStackInput{ | ||
StackName: &stackName, | ||
TemplateURL: aws.String(appFormationURL), | ||
Parameters: stackOutput.Stacks[0].Parameters, | ||
Capabilities: []*string{aws.String("CAPABILITY_IAM")}, | ||
} | ||
if createChangeSet { | ||
_, err = updateChangeSetAndWait(sess, &updateStackInput) | ||
} else { | ||
_, err = updateStackAndWait(sess, &updateStackInput) | ||
} | ||
checkErr(err) | ||
Spinner.Stop() | ||
printSuccess("stack upgraded") | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(upgradeCmd) | ||
upgradeCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "Check stack in Cloudformation before creating") | ||
upgradeCmd.AddCommand(upgradeAppCmd) | ||
} |