yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/esxicli/main.go (about)

     1  // Copyright 2019 Yunion
     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 main
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"net/url"
    21  	"os"
    22  
    23  	"golang.org/x/net/http/httpproxy"
    24  
    25  	"yunion.io/x/structarg"
    26  
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud/esxi"
    29  	_ "yunion.io/x/cloudmux/pkg/multicloud/esxi/shell"
    30  	"yunion.io/x/onecloud/pkg/util/shellutils"
    31  )
    32  
    33  type BaseOptions struct {
    34  	Host       string `help:"Host IP or NAME" default:"$VMWARE_HOST" metavar:"VMWARE_HOST"`
    35  	Port       int    `help:"Service port" default:"$VMWARE_PORT" metavar:"VMWARE_PORT"`
    36  	Account    string `help:"VCenter or ESXi Account" default:"$VMWARE_ACCOUNT" metavar:"VMWARE_ACCOUNT"`
    37  	Password   string `help:"Password" default:"$VMWARE_PASSWORD" metavar:"VMWARE_PASSWORD"`
    38  	Debug      bool
    39  	Format     string `choices:"xml|json" default:"json"`
    40  	SUBCOMMAND string `help:"aliyuncli subcommand" subcommand:"true"`
    41  }
    42  
    43  func getSubcommandParser() (*structarg.ArgumentParser, error) {
    44  	parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{},
    45  		"esxicli",
    46  		"Command-line interface to VMware VSphere Webservice API.",
    47  		`See "esxicli COMMAND --help" for help on a specific command.`)
    48  
    49  	if e != nil {
    50  		return nil, e
    51  	}
    52  
    53  	subcmd := parse.GetSubcommand()
    54  	if subcmd == nil {
    55  		return nil, fmt.Errorf("No subcommand argument.")
    56  	}
    57  	for _, v := range shellutils.CommandTable {
    58  		_, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
    59  		if e != nil {
    60  			return nil, e
    61  		}
    62  	}
    63  	return parse, nil
    64  }
    65  
    66  func showErrorAndExit(e error) {
    67  	fmt.Fprintf(os.Stderr, "%s", e)
    68  	fmt.Fprintln(os.Stderr)
    69  	os.Exit(1)
    70  }
    71  
    72  func newClient(options *BaseOptions) (*esxi.SESXiClient, error) {
    73  	if len(options.Host) == 0 {
    74  		return nil, fmt.Errorf("Missing host")
    75  	}
    76  
    77  	if len(options.Account) == 0 {
    78  		return nil, fmt.Errorf("Missing account")
    79  	}
    80  
    81  	if len(options.Password) == 0 {
    82  		return nil, fmt.Errorf("Missing password")
    83  	}
    84  
    85  	cfg := &httpproxy.Config{
    86  		HTTPProxy:  os.Getenv("HTTP_PROXY"),
    87  		HTTPSProxy: os.Getenv("HTTPS_PROXY"),
    88  		NoProxy:    os.Getenv("NO_PROXY"),
    89  	}
    90  	cfgProxyFunc := cfg.ProxyFunc()
    91  	proxyFunc := func(req *http.Request) (*url.URL, error) {
    92  		return cfgProxyFunc(req.URL)
    93  	}
    94  
    95  	return esxi.NewESXiClient2(
    96  		esxi.NewESXiClientConfig(
    97  			options.Host,
    98  			options.Port,
    99  			options.Account,
   100  			options.Password,
   101  		).
   102  			CloudproviderConfig(
   103  				cloudprovider.ProviderConfig{
   104  					ProxyFunc: proxyFunc,
   105  				},
   106  			).Debug(options.Debug).Format(options.Format),
   107  	)
   108  }
   109  
   110  func main() {
   111  	parser, e := getSubcommandParser()
   112  	if e != nil {
   113  		showErrorAndExit(e)
   114  	}
   115  	e = parser.ParseArgs(os.Args[1:], false)
   116  	options := parser.Options().(*BaseOptions)
   117  
   118  	if parser.IsHelpSet() {
   119  		fmt.Print(parser.HelpString())
   120  		return
   121  	}
   122  	subcmd := parser.GetSubcommand()
   123  	subparser := subcmd.GetSubParser()
   124  	if e != nil || subparser == nil {
   125  		if subparser != nil {
   126  			fmt.Print(subparser.Usage())
   127  		} else {
   128  			fmt.Print(parser.Usage())
   129  		}
   130  		showErrorAndExit(e)
   131  	}
   132  	suboptions := subparser.Options()
   133  	if subparser.IsHelpSet() {
   134  		fmt.Print(subparser.HelpString())
   135  		return
   136  	}
   137  	var esxicli *esxi.SESXiClient
   138  	esxicli, e = newClient(options)
   139  	if e != nil {
   140  		showErrorAndExit(e)
   141  	}
   142  	e = subcmd.Invoke(esxicli, suboptions)
   143  	if e != nil {
   144  		showErrorAndExit(e)
   145  	}
   146  }