github.com/coreos/mantle@v0.13.0/cmd/ore/aws/aws.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/coreos/mantle/cli"
    22  	"github.com/coreos/mantle/platform"
    23  	"github.com/coreos/mantle/platform/api/aws"
    24  	"github.com/coreos/pkg/capnslog"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var (
    29  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "ore/aws")
    30  
    31  	AWS = &cobra.Command{
    32  		Use:   "aws [command]",
    33  		Short: "aws image and vm utilities",
    34  	}
    35  
    36  	API             *aws.API
    37  	region          string
    38  	credentialsFile string
    39  	profileName     string
    40  	accessKeyID     string
    41  	secretAccessKey string
    42  )
    43  
    44  func init() {
    45  	defaultRegion := os.Getenv("AWS_REGION")
    46  	if defaultRegion == "" {
    47  		defaultRegion = "us-west-2"
    48  	}
    49  
    50  	AWS.PersistentFlags().StringVar(&credentialsFile, "credentials-file", "", "AWS credentials file")
    51  	AWS.PersistentFlags().StringVar(&profileName, "profile", "", "AWS profile name")
    52  	AWS.PersistentFlags().StringVar(&accessKeyID, "access-id", "", "AWS access key")
    53  	AWS.PersistentFlags().StringVar(&secretAccessKey, "secret-key", "", "AWS secret key")
    54  	AWS.PersistentFlags().StringVar(&region, "region", defaultRegion, "AWS region")
    55  	cli.WrapPreRun(AWS, preflightCheck)
    56  }
    57  
    58  func preflightCheck(cmd *cobra.Command, args []string) error {
    59  	plog.Debugf("Running AWS Preflight check. Region: %v", region)
    60  	api, err := aws.New(&aws.Options{
    61  		Region:          region,
    62  		CredentialsFile: credentialsFile,
    63  		Profile:         profileName,
    64  		Options:         &platform.Options{},
    65  	})
    66  	if err != nil {
    67  		fmt.Fprintf(os.Stderr, "could not create AWS client: %v\n", err)
    68  		os.Exit(1)
    69  	}
    70  	if err := api.PreflightCheck(); err != nil {
    71  		fmt.Fprintf(os.Stderr, "could not complete AWS preflight check: %v\n", err)
    72  		os.Exit(1)
    73  	}
    74  
    75  	plog.Debugf("Preflight check success; we have liftoff")
    76  	API = api
    77  	return nil
    78  }