github.com/vmware/govmomi@v0.43.0/govc/host/reconnect.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 host
    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  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type reconnect struct {
    31  	*flags.HostSystemFlag
    32  	*flags.HostConnectFlag
    33  
    34  	types.HostSystemReconnectSpec
    35  }
    36  
    37  func init() {
    38  	cli.Register("host.reconnect", &reconnect{})
    39  }
    40  
    41  func (cmd *reconnect) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    43  	cmd.HostSystemFlag.Register(ctx, f)
    44  
    45  	cmd.HostConnectFlag, ctx = flags.NewHostConnectFlag(ctx)
    46  	cmd.HostConnectFlag.Register(ctx, f)
    47  
    48  	cmd.HostSystemReconnectSpec.SyncState = types.NewBool(false)
    49  	f.BoolVar(cmd.HostSystemReconnectSpec.SyncState, "sync-state", false, "Sync state")
    50  }
    51  
    52  func (cmd *reconnect) Process(ctx context.Context) error {
    53  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	if err := cmd.HostConnectFlag.Process(ctx); err != nil {
    57  		return err
    58  	}
    59  	return nil
    60  }
    61  
    62  func (cmd *reconnect) Description() string {
    63  	return `Reconnect HOST to vCenter.
    64  
    65  This command can also be used to change connection properties (hostname, fingerprint, username, password),
    66  without disconnecting the host.`
    67  }
    68  
    69  func (cmd *reconnect) Reconnect(ctx context.Context, host *object.HostSystem) error {
    70  	task, err := host.Reconnect(ctx, &cmd.HostConnectSpec, &cmd.HostSystemReconnectSpec)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	logger := cmd.ProgressLogger(fmt.Sprintf("%s reconnecting... ", host.InventoryPath))
    76  	defer logger.Wait()
    77  
    78  	_, err = task.WaitForResult(ctx, logger)
    79  	return err
    80  }
    81  
    82  func (cmd *reconnect) Run(ctx context.Context, f *flag.FlagSet) error {
    83  	hosts, err := cmd.HostSystems(f.Args())
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	for _, host := range hosts {
    89  		err = cmd.Reconnect(ctx, host)
    90  		if err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	return nil
    96  }