Skip to content

Commit

Permalink
"cluster-create" now can create cluster without key pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
d5 committed Oct 25, 2016
1 parent 245601a commit e65a15b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
6 changes: 5 additions & 1 deletion aws/autoscaling/client.go
Expand Up @@ -8,6 +8,7 @@ import (
_aws "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
_autoscaling "github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/coldbrewcloud/coldbrew-cli/utils"
"github.com/coldbrewcloud/coldbrew-cli/utils/conv"
)

Expand All @@ -26,12 +27,15 @@ func (c *Client) CreateLaunchConfiguration(launchConfigurationName, instanceType
IamInstanceProfile: _aws.String(iamInstanceProfileNameOrARN),
ImageId: _aws.String(imageID),
InstanceType: _aws.String(instanceType),
KeyName: _aws.String(keyPairName),
LaunchConfigurationName: _aws.String(launchConfigurationName),
SecurityGroups: _aws.StringSlice(securityGroupIDs),
UserData: _aws.String(userData),
}

if !utils.IsBlank(keyPairName) {
params.KeyName = _aws.String(keyPairName)
}

_, err := c.svc.CreateLaunchConfiguration(params)
if err != nil {
return err
Expand Down
67 changes: 36 additions & 31 deletions commands/clustercreate/command.go
Expand Up @@ -51,31 +51,45 @@ func (c *Command) Run() error {
}

// keypair
keyPairName := strings.TrimSpace(conv.S(c.commandFlags.KeyPairName))
if utils.IsBlank(keyPairName) {
keyPairs, err := c.awsClient.EC2().ListKeyPairs()
if err != nil {
return console.ExitWithErrorString("Failed to list EC2 Key Pairs: %s", err.Error())
}
keyPairName := ""
if !conv.B(c.commandFlags.NoKeyPair) {
strings.TrimSpace(conv.S(c.commandFlags.KeyPairName))
if utils.IsBlank(keyPairName) {
keyPairs, err := c.awsClient.EC2().ListKeyPairs()
if err != nil {
return console.ExitWithErrorString("Failed to list EC2 Key Pairs: %s", err.Error())
}

defaultKeyPairName := ""
if len(keyPairs) > 0 {
defaultKeyPairName = conv.S(keyPairs[0].KeyName)
}
defaultKeyPairName := ""
if len(keyPairs) > 0 {
defaultKeyPairName = conv.S(keyPairs[0].KeyName)

keyPairName = console.AskQuestionWithNote(
"Enter EC2 Key Pair name",
defaultKeyPairName,
"EC2 Key Pair name is required to create a new cluster.")
} else {
if !conv.B(c.commandFlags.ForceCreate) && !console.AskConfirmWithNote(
"Do you still want to create cluster without EC2 Key Pair?",
false,
"Could not find any EC2 Key Pairs available to use.") {
console.Info("Please create an EC2 Key Pair and try again.")
return nil
}
}

keyPairName = console.AskQuestionWithNote(
"Enter key pair name",
defaultKeyPairName,
"Key pair name is required to create a new cluster.")
}
}

// check if key pair exists
keyPairInfo, err := c.awsClient.EC2().RetrieveKeyPair(keyPairName)
if err != nil {
return console.ExitWithErrorString("Failed to retrieve EC2 Key Pair [%s]: %s", keyPairName, err.Error())
}
if keyPairInfo == nil {
return console.ExitWithErrorString("EC2 Key Pair [%s] was not found.", keyPairName)
// check if key pair exists
if !utils.IsBlank(keyPairName) {
keyPairInfo, err := c.awsClient.EC2().RetrieveKeyPair(keyPairName)
if err != nil {
return console.ExitWithErrorString("Failed to retrieve EC2 Key Pair [%s]: %s", keyPairName, err.Error())
}
if keyPairInfo == nil {
return console.ExitWithErrorString("EC2 Key Pair [%s] was not found.", keyPairName)
}
}
}

console.Info("Determining AWS resources to create...")
Expand Down Expand Up @@ -204,15 +218,6 @@ func (c *Command) Run() error {
if createLaunchConfiguration {
console.AddingResource("Creating EC2 Launch Configuration", launchConfigName, true)

// key pair
keyPairInfo, err := c.awsClient.EC2().RetrieveKeyPair(keyPairName)
if err != nil {
return console.ExitWithErrorString("Failed to retrieve key pair info [%s]: %s", keyPairName, err.Error())
}
if keyPairInfo == nil {
return console.ExitWithErrorString("Key pair [%s] was not found\n", keyPairName)
}

// container instance type
instanceType := strings.TrimSpace(conv.S(c.commandFlags.InstanceType))
if instanceType == "" {
Expand Down
13 changes: 9 additions & 4 deletions commands/clustercreate/flags.go
@@ -1,21 +1,26 @@
package clustercreate

import "gopkg.in/alecthomas/kingpin.v2"
import (
"github.com/coldbrewcloud/coldbrew-cli/core"
"gopkg.in/alecthomas/kingpin.v2"
)

type Flags struct {
InstanceType *string `json:"instance_type"`
InitialCapacity *uint16 `json:"initial_capacity"`
NoKeyPair *bool `json:"no-keypair"`
KeyPairName *string `json:"keypair_name"`
InstanceProfile *string `json:"instance_profile"`
ForceCreate *bool `json:"force"`
}

func NewFlags(kc *kingpin.CmdClause) *Flags {
return &Flags{
InstanceType: kc.Flag("instance-type", "Container instance type").String(),
InstanceType: kc.Flag("instance-type", "Container instance type").Default(core.DefaultContainerInstanceType()).String(),
InitialCapacity: kc.Flag("instance-count", "Initial number of container instances").Default("1").Uint16(),
KeyPairName: kc.Flag("key", "EC2 keypair name").String(),
InstanceProfile: kc.Flag("instance-profile", "IAM instance profile name for container instances").String(),
NoKeyPair: kc.Flag("disable-keypair", "Do not assign EC2 keypairs").Bool(),
KeyPairName: kc.Flag("key", "EC2 keypair name").Default("").String(),
InstanceProfile: kc.Flag("instance-profile", "IAM instance profile name for container instances").Default("").String(),
ForceCreate: kc.Flag("yes", "Create all resource with no confirmation").Short('y').Default("false").Bool(),
}
}
2 changes: 1 addition & 1 deletion console/ask.go
Expand Up @@ -14,7 +14,7 @@ func AskConfirmWithNote(message string, defaultYes bool, note string) bool {
reader := bufio.NewReader(os.Stdin)

if note != "" {
stdout("%\n", ColorFnAskConfirmNote(note))
stdout("%s\n", ColorFnAskConfirmNote(note))
}

for {
Expand Down

0 comments on commit e65a15b

Please sign in to comment.