github.com/coreos/mantle@v0.13.0/cmd/ore/packet/packet.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 packet
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/coreos/mantle/auth"
    22  	"github.com/coreos/mantle/cli"
    23  	"github.com/coreos/mantle/platform/api/gcloud"
    24  	"github.com/coreos/mantle/platform/api/packet"
    25  	"github.com/coreos/pkg/capnslog"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  var (
    30  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "ore/packet")
    31  
    32  	Packet = &cobra.Command{
    33  		Use:   "packet [command]",
    34  		Short: "Packet machine utilities",
    35  	}
    36  
    37  	API       *packet.API
    38  	options   packet.Options
    39  	gsOptions gcloud.Options
    40  )
    41  
    42  func init() {
    43  	options.GSOptions = &gsOptions
    44  	Packet.PersistentFlags().StringVar(&options.StorageURL, "storage-url", "gs://users.developer.core-os.net/"+os.Getenv("USER")+"/mantle", "Google Storage base URL for temporary uploads")
    45  	Packet.PersistentFlags().StringVar(&gsOptions.JSONKeyFile, "gs-json-key", "", "use a Google service account's JSON key to authenticate to Google Storage")
    46  	Packet.PersistentFlags().BoolVar(&gsOptions.ServiceAuth, "gs-service-auth", false, "use non-interactive Google auth when running within GCE")
    47  	Packet.PersistentFlags().StringVar(&options.ConfigPath, "config-file", "", "config file (default \"~/"+auth.PacketConfigPath+"\")")
    48  	Packet.PersistentFlags().StringVar(&options.Profile, "profile", "", "profile (default \"default\")")
    49  	Packet.PersistentFlags().StringVar(&options.ApiKey, "api-key", "", "API key (overrides config file)")
    50  	Packet.PersistentFlags().StringVar(&options.Project, "project", "", "project UUID (overrides config file)")
    51  	cli.WrapPreRun(Packet, preflightCheck)
    52  }
    53  
    54  func preflightCheck(cmd *cobra.Command, args []string) error {
    55  	plog.Debugf("Running Packet preflight check")
    56  	api, err := packet.New(&options)
    57  	if err != nil {
    58  		return fmt.Errorf("could not create Packet client: %v", err)
    59  	}
    60  	if err := api.PreflightCheck(); err != nil {
    61  		return fmt.Errorf("could not complete Packet preflight check: %v", err)
    62  	}
    63  
    64  	plog.Debugf("Preflight check success; we have liftoff")
    65  	API = api
    66  	return nil
    67  }