github.com/kubernetes-incubator/kube-aws@v0.16.4/cmd/apply.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"bufio"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/kubernetes-incubator/kube-aws/core/root"
    11  	"github.com/kubernetes-incubator/kube-aws/logger"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var (
    16  	cmdApply = &cobra.Command{
    17  		Use:          "apply",
    18  		Short:        "Create or Update your cluster",
    19  		Long:         ``,
    20  		RunE:         runCmdApply,
    21  		SilenceUsage: true,
    22  	}
    23  
    24  	applyOpts = struct {
    25  		awsDebug, prettyPrint, skipWait, export bool
    26  		force                                   bool
    27  		profile                                 string
    28  		targets                                 []string
    29  	}{}
    30  )
    31  
    32  func init() {
    33  	RootCmd.AddCommand(cmdApply)
    34  	cmdApply.Flags().BoolVar(&applyOpts.export, "export", false, "Don't create cluster, instead export cloudformation stack file")
    35  	cmdApply.Flags().BoolVar(&applyOpts.awsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
    36  	cmdApply.Flags().BoolVar(&applyOpts.prettyPrint, "pretty-print", false, "Pretty print the resulting CloudFormation")
    37  	cmdApply.Flags().BoolVar(&applyOpts.skipWait, "skip-wait", false, "Don't wait the resources finish")
    38  	cmdApply.Flags().BoolVar(&applyOpts.force, "force", false, "Don't ask for confirmation")
    39  	cmdApply.Flags().StringVar(&applyOpts.profile, "profile", "", "The AWS profile to use from credentials file")
    40  	cmdApply.Flags().StringSliceVar(&applyOpts.targets, "targets", root.AllOperationTargetsAsStringSlice(), "Update nothing but specified sub-stacks.  Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
    41  }
    42  
    43  func runCmdApply(_ *cobra.Command, _ []string) error {
    44  	if !applyOpts.force && !applyConfirmation() {
    45  		logger.Info("Operation cancelled")
    46  		return nil
    47  	}
    48  
    49  	opts := root.NewOptions(applyOpts.prettyPrint, applyOpts.skipWait, applyOpts.profile)
    50  
    51  	cluster, err := root.LoadClusterFromFile(configPath, opts, applyOpts.awsDebug)
    52  	if err != nil {
    53  		return fmt.Errorf("failed to read cluster config: %v", err)
    54  	}
    55  
    56  	targets := root.OperationTargetsFromStringSlice(applyOpts.targets)
    57  
    58  	if _, err := cluster.ValidateStack(targets); err != nil {
    59  		return err
    60  	}
    61  
    62  	if applyOpts.export {
    63  		if err := cluster.Export(); err != nil {
    64  			return err
    65  		}
    66  		return nil
    67  	}
    68  
    69  	err = cluster.Apply(targets)
    70  	if err != nil {
    71  		return fmt.Errorf("error updating cluster: %v", err)
    72  	}
    73  
    74  	info, err := cluster.Info()
    75  	if err != nil {
    76  		return fmt.Errorf("failed fetching cluster info: %v", err)
    77  	}
    78  
    79  	successMsg :=
    80  		`Success! Your AWS resources are being provisioned:
    81  %s
    82  `
    83  	logger.Infof(successMsg, info)
    84  	return nil
    85  }
    86  
    87  func applyConfirmation() bool {
    88  	reader := bufio.NewReader(os.Stdin)
    89  	fmt.Print("This operation will create/update the cluster. Are you sure? [y,n]: ")
    90  	text, _ := reader.ReadString('\n')
    91  	text = strings.TrimSuffix(strings.ToLower(text), "\n")
    92  
    93  	return text == "y" || text == "yes"
    94  }