github.com/vmware/govmomi@v0.37.2/govc/cluster/add.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cluster
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/object"
    27  )
    28  
    29  type add struct {
    30  	*flags.ClusterFlag
    31  	*flags.HostConnectFlag
    32  
    33  	connect bool
    34  	license string
    35  }
    36  
    37  func init() {
    38  	cli.Register("cluster.add", &add{})
    39  }
    40  
    41  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
    43  	cmd.ClusterFlag.Register(ctx, f)
    44  
    45  	cmd.HostConnectFlag, ctx = flags.NewHostConnectFlag(ctx)
    46  	cmd.HostConnectFlag.Register(ctx, f)
    47  
    48  	f.StringVar(&cmd.license, "license", "", "Assign license key")
    49  
    50  	f.BoolVar(&cmd.connect, "connect", true, "Immediately connect to host")
    51  }
    52  
    53  func (cmd *add) Process(ctx context.Context) error {
    54  	if err := cmd.ClusterFlag.Process(ctx); err != nil {
    55  		return err
    56  	}
    57  	if err := cmd.HostConnectFlag.Process(ctx); err != nil {
    58  		return err
    59  	}
    60  	if cmd.HostName == "" {
    61  		return flag.ErrHelp
    62  	}
    63  	if cmd.UserName == "" {
    64  		return flag.ErrHelp
    65  	}
    66  	if cmd.Password == "" {
    67  		return flag.ErrHelp
    68  	}
    69  	return nil
    70  }
    71  
    72  func (cmd *add) Description() string {
    73  	return `Add HOST to CLUSTER.
    74  
    75  The host is added to the cluster specified by the 'cluster' flag.
    76  
    77  Examples:
    78    thumbprint=$(govc about.cert -k -u host.example.com -thumbprint | awk '{print $2}')
    79    govc cluster.add -cluster ClusterA -hostname host.example.com -username root -password pass -thumbprint $thumbprint
    80    govc cluster.add -cluster ClusterB -hostname 10.0.6.1 -username root -password pass -noverify`
    81  }
    82  
    83  func (cmd *add) Add(ctx context.Context, cluster *object.ClusterComputeResource) error {
    84  	spec := cmd.HostConnectSpec
    85  
    86  	var license *string
    87  	if cmd.license != "" {
    88  		license = &cmd.license
    89  	}
    90  
    91  	task, err := cluster.AddHost(ctx, cmd.Spec(cluster.Client()), cmd.connect, license, nil)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	logger := cmd.ProgressLogger(fmt.Sprintf("adding %s to cluster %s... ", spec.HostName, cluster.InventoryPath))
    97  	defer logger.Wait()
    98  
    99  	_, err = task.WaitForResult(ctx, logger)
   100  	return err
   101  }
   102  
   103  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
   104  	if f.NArg() != 0 {
   105  		return flag.ErrHelp
   106  	}
   107  
   108  	cluster, err := cmd.Cluster()
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	return cmd.Fault(cmd.Add(ctx, cluster))
   114  }