github.com/vmware/govmomi@v0.43.0/govc/host/maintenance/enter.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 enter struct {
    30  	*flags.HostSystemFlag
    31  
    32  	timeout  int32
    33  	evacuate bool
    34  }
    35  
    36  func init() {
    37  	cli.Register("host.maintenance.enter", &enter{})
    38  }
    39  
    40  func (cmd *enter) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    42  	cmd.HostSystemFlag.Register(ctx, f)
    43  
    44  	f.Var(flags.NewInt32(&cmd.timeout), "timeout", "Timeout")
    45  	f.BoolVar(&cmd.evacuate, "evacuate", false, "Evacuate powered off VMs")
    46  }
    47  
    48  func (cmd *enter) Process(ctx context.Context) error {
    49  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func (cmd *enter) Usage() string {
    56  	return "HOST..."
    57  }
    58  
    59  func (cmd *enter) Description() string {
    60  	return `Put HOST in maintenance mode.
    61  
    62  While this task is running and when the host is in maintenance mode,
    63  no VMs can be powered on and no provisioning operations can be performed on the host.`
    64  }
    65  
    66  func (cmd *enter) EnterMaintenanceMode(ctx context.Context, host *object.HostSystem) error {
    67  	task, err := host.EnterMaintenanceMode(ctx, cmd.timeout, cmd.evacuate, nil) // TODO: spec param
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	logger := cmd.ProgressLogger(fmt.Sprintf("%s entering maintenance mode... ", host.InventoryPath))
    73  	defer logger.Wait()
    74  
    75  	_, err = task.WaitForResult(ctx, logger)
    76  	return err
    77  }
    78  
    79  func (cmd *enter) Run(ctx context.Context, f *flag.FlagSet) error {
    80  	hosts, err := cmd.HostSystems(f.Args())
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	for _, host := range hosts {
    86  		err = cmd.EnterMaintenanceMode(ctx, host)
    87  		if err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	return nil
    93  }