github.com/vmware/govmomi@v0.43.0/govc/vm/guest/tools.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 guest
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/object"
    26  )
    27  
    28  type tools struct {
    29  	*flags.ClientFlag
    30  	*flags.SearchFlag
    31  
    32  	mount   bool
    33  	upgrade bool
    34  	options string
    35  	unmount bool
    36  }
    37  
    38  func init() {
    39  	cli.Register("vm.guest.tools", &tools{})
    40  }
    41  
    42  func (cmd *tools) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    44  	cmd.ClientFlag.Register(ctx, f)
    45  
    46  	cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
    47  	cmd.SearchFlag.Register(ctx, f)
    48  
    49  	f.BoolVar(&cmd.mount, "mount", false, "Mount tools CD installer in the guest")
    50  	f.BoolVar(&cmd.upgrade, "upgrade", false, "Upgrade tools in the guest")
    51  	f.StringVar(&cmd.options, "options", "", "Installer options")
    52  	f.BoolVar(&cmd.unmount, "unmount", false, "Unmount tools CD installer in the guest")
    53  }
    54  
    55  func (cmd *tools) Process(ctx context.Context) error {
    56  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    57  		return err
    58  	}
    59  	if err := cmd.SearchFlag.Process(ctx); err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }
    64  
    65  func (cmd *tools) Usage() string {
    66  	return "VM..."
    67  }
    68  
    69  func (cmd *tools) Description() string {
    70  	return `Manage guest tools in VM.
    71  
    72  Examples:
    73    govc vm.guest.tools -mount VM
    74    govc vm.guest.tools -unmount VM
    75    govc vm.guest.tools -upgrade -options "opt1 opt2" VM`
    76  }
    77  
    78  func (cmd *tools) Upgrade(ctx context.Context, vm *object.VirtualMachine) error {
    79  	task, err := vm.UpgradeTools(ctx, cmd.options)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	return task.Wait(ctx)
    85  }
    86  
    87  func (cmd *tools) Run(ctx context.Context, f *flag.FlagSet) error {
    88  	vms, err := cmd.VirtualMachines(f.Args())
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	for _, vm := range vms {
    94  		switch {
    95  		case cmd.mount:
    96  			err = vm.MountToolsInstaller(ctx)
    97  			if err != nil {
    98  				return err
    99  			}
   100  		case cmd.upgrade:
   101  			err = cmd.Upgrade(ctx, vm)
   102  			if err != nil {
   103  				return err
   104  			}
   105  		case cmd.unmount:
   106  			err = vm.UnmountToolsInstaller(ctx)
   107  			if err != nil {
   108  				return err
   109  			}
   110  		default:
   111  			return flag.ErrHelp
   112  		}
   113  	}
   114  
   115  	return nil
   116  }