github.com/vmware/govmomi@v0.43.0/govc/host/maintenance/exit.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 maintenance
    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 exit struct {
    30  	*flags.HostSystemFlag
    31  
    32  	timeout int32
    33  }
    34  
    35  func init() {
    36  	cli.Register("host.maintenance.exit", &exit{})
    37  }
    38  
    39  func (cmd *exit) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    41  	cmd.HostSystemFlag.Register(ctx, f)
    42  
    43  	f.Var(flags.NewInt32(&cmd.timeout), "timeout", "Timeout")
    44  }
    45  
    46  func (cmd *exit) Process(ctx context.Context) error {
    47  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    48  		return err
    49  	}
    50  	return nil
    51  }
    52  
    53  func (cmd *exit) Usage() string {
    54  	return "HOST..."
    55  }
    56  
    57  func (cmd *exit) Description() string {
    58  	return `Take HOST out of maintenance mode.
    59  
    60  This blocks if any concurrent running maintenance-only host configurations operations are being performed.
    61  For example, if VMFS volumes are being upgraded.
    62  
    63  The 'timeout' flag is the number of seconds to wait for the exit maintenance mode to succeed.
    64  If the timeout is less than or equal to zero, there is no timeout.`
    65  }
    66  
    67  func (cmd *exit) ExitMaintenanceMode(ctx context.Context, host *object.HostSystem) error {
    68  	task, err := host.ExitMaintenanceMode(ctx, cmd.timeout)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	logger := cmd.ProgressLogger(fmt.Sprintf("%s exiting maintenance mode... ", host.InventoryPath))
    74  	defer logger.Wait()
    75  
    76  	_, err = task.WaitForResult(ctx, logger)
    77  	return err
    78  }
    79  
    80  func (cmd *exit) Run(ctx context.Context, f *flag.FlagSet) error {
    81  	hosts, err := cmd.HostSystems(f.Args())
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	for _, host := range hosts {
    87  		err = cmd.ExitMaintenanceMode(ctx, host)
    88  		if err != nil {
    89  			return err
    90  		}
    91  	}
    92  
    93  	return nil
    94  }