github.com/vmware/govmomi@v0.43.0/govc/host/shutdown.go (about)

     1  /*
     2  Copyright (c) 2017 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/methods"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  type shutdown struct {
    32  	*flags.HostSystemFlag
    33  	force  bool
    34  	reboot bool
    35  }
    36  
    37  func init() {
    38  	cli.Register("host.shutdown", &shutdown{})
    39  }
    40  
    41  func (cmd *shutdown) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    43  	cmd.HostSystemFlag.Register(ctx, f)
    44  
    45  	f.BoolVar(&cmd.force, "f", false, "Force shutdown when host is not in maintenance mode")
    46  	f.BoolVar(&cmd.reboot, "r", false, "Reboot host")
    47  }
    48  
    49  func (cmd *shutdown) Process(ctx context.Context) error {
    50  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  func (cmd *shutdown) Usage() string {
    57  	return `HOST...`
    58  }
    59  
    60  func (cmd *shutdown) Description() string {
    61  	return `Shutdown HOST.`
    62  }
    63  
    64  func (cmd *shutdown) Shutdown(ctx context.Context, host *object.HostSystem) error {
    65  	req := types.ShutdownHost_Task{
    66  		This:  host.Reference(),
    67  		Force: cmd.force,
    68  	}
    69  
    70  	res, err := methods.ShutdownHost_Task(ctx, host.Client(), &req)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	task := object.NewTask(host.Client(), res.Returnval)
    76  
    77  	logger := cmd.ProgressLogger(fmt.Sprintf("%s shutdown... ", host.InventoryPath))
    78  	defer logger.Wait()
    79  
    80  	_, err = task.WaitForResult(ctx, logger)
    81  	return err
    82  }
    83  
    84  func (cmd *shutdown) Reboot(ctx context.Context, host *object.HostSystem) error {
    85  	req := types.RebootHost_Task{
    86  		This:  host.Reference(),
    87  		Force: cmd.force,
    88  	}
    89  
    90  	res, err := methods.RebootHost_Task(ctx, host.Client(), &req)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	task := object.NewTask(host.Client(), res.Returnval)
    96  
    97  	logger := cmd.ProgressLogger(fmt.Sprintf("%s reboot... ", host.InventoryPath))
    98  	defer logger.Wait()
    99  
   100  	_, err = task.WaitForResult(ctx, logger)
   101  	return err
   102  }
   103  
   104  func (cmd *shutdown) Run(ctx context.Context, f *flag.FlagSet) error {
   105  	hosts, err := cmd.HostSystems(f.Args())
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	s := cmd.Shutdown
   111  	if cmd.reboot {
   112  		s = cmd.Reboot
   113  	}
   114  
   115  	for _, host := range hosts {
   116  		err = s(ctx, host)
   117  		if err != nil {
   118  			return err
   119  		}
   120  	}
   121  
   122  	return nil
   123  }