Skip to content

Commit

Permalink
create cluster creates region if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Oct 21, 2021
1 parent 26fd1b1 commit ed70a2e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
21 changes: 21 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/apppackio/apppack/auth"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/codebuild"
Expand Down Expand Up @@ -187,6 +188,26 @@ func cloudformationStackURL(region, stackID *string) string {
return fmt.Sprintf("https://%s.console.aws.amazon.com/cloudformation/home#/stacks/events?stackId=%s", *region, url.QueryEscape(*stackID))
}

// stackExists checks if a named Cfn Stack already exists in the region
func stackExists(sess *session.Session, stackName string) (*bool, error) {
cfnSvc := cloudformation.New(sess)
stackOutput, err := cfnSvc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: &stackName,
})
var exists bool
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == "ValidationError" {
exists = false
return &exists, nil
}
}
return nil, err
}
exists = len(stackOutput.Stacks) > 0 && *stackOutput.Stacks[0].StackStatus != cloudformation.StackStatusDeleteComplete
return &exists, nil
}

type stackItem struct {
PrimaryID string `json:"primary_id"`
SecondaryID string `json:"secondary_id"`
Expand Down
32 changes: 27 additions & 5 deletions cmd/createCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -120,12 +121,29 @@ var createClusterCmd = &cobra.Command{
} else {
clusterName = args[0]
}
questions := []*survey.Question{}
answers := make(map[string]interface{})
addQuestionFromFlag(cmd.Flags().Lookup("domain"), &questions, nil)
startSpinner()
sess, err := adminSession()
checkErr(err)
regionExists, err := stackExists(sess, fmt.Sprintf("apppack-region-%s", *sess.Config.Region))
checkErr(err)
if !*regionExists {
Spinner.Stop()
fmt.Println(aurora.Blue(fmt.Sprintf("ℹ %s region is not initialized", *sess.Config.Region)))
fmt.Printf("If this is your first cluster or you want to setup up a new region, type '%s' to continue.\n", aurora.White("yes"))
fmt.Print(aurora.White(fmt.Sprintf("Create cluster in %s region? ", *sess.Config.Region)).String())
var confirm string
fmt.Scanln(&confirm)
if confirm != "yes" {
checkErr(fmt.Errorf("aborting due to user input"))
}
fmt.Printf("running %s...\n", aurora.White("apppack create region"))
createRegionCmd.Run(cmd, []string{})
fmt.Println("")
}
questions := []*survey.Question{}
answers := make(map[string]interface{})
addQuestionFromFlag(cmd.Flags().Lookup("domain"), &questions, nil)

_, err = stackFromDDBItem(sess, fmt.Sprintf("CLUSTER#%s", clusterName))
if err == nil {
checkErr(fmt.Errorf("cluster %s already exists", clusterName))
Expand Down Expand Up @@ -205,9 +223,13 @@ var createClusterCmd = &cobra.Command{

func init() {
createCmd.AddCommand(createClusterCmd)
// All flags need to be added to `initCmd` as well so it can call this cmd
createClusterCmd.Flags().String("domain", "", "parent domain for apps in the cluster")
createClusterCmd.Flags().String("domain", "", "cluster domain name")
createClusterCmd.Flags().Bool("ec2", false, "setup cluster with EC2 instances")
createClusterCmd.Flags().String("instance-class", "", "autoscaling instance class -- see https://aws.amazon.com/ec2/pricing/on-demand/")
createClusterCmd.Flags().String("cidr", "10.100.0.0/16", "network CIDR for VPC")
// from createRegion
createClusterCmd.Flags().String("dockerhub-username", "", "Docker Hub username")
createClusterCmd.Flags().String("dockerhub-access-token", "", "Docker Hub Access Token (https://hub.docker.com/settings/security)")
createClusterCmd.Flags().MarkHidden("dockerhub-username")
createClusterCmd.Flags().MarkHidden("dockerhub-access-token")
}
2 changes: 1 addition & 1 deletion cmd/createRegion.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var createRegionCmd = &cobra.Command{

func init() {
createCmd.AddCommand(createRegionCmd)
// All flags need to be added to `initCmd` as well so it can call this cmd
// All flags need to be added to `createCluster` as well so it can call this cmd
createRegionCmd.Flags().String("dockerhub-username", "", "Docker Hub username")
createRegionCmd.Flags().String("dockerhub-access-token", "", "Docker Hub Access Token (https://hub.docker.com/settings/security)")
}

0 comments on commit ed70a2e

Please sign in to comment.