github.com/vmware/govmomi@v0.37.2/govc/device/serial/connect.go (about)

     1  /*
     2  Copyright (c) 2014-2016 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 serial
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"path"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/mo"
    28  )
    29  
    30  type connect struct {
    31  	*flags.VirtualMachineFlag
    32  
    33  	proxy  string
    34  	device string
    35  	client bool
    36  }
    37  
    38  func init() {
    39  	cli.Register("device.serial.connect", &connect{})
    40  }
    41  
    42  func (cmd *connect) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    44  	cmd.VirtualMachineFlag.Register(ctx, f)
    45  
    46  	f.StringVar(&cmd.proxy, "vspc-proxy", "", "vSPC proxy URI")
    47  	f.StringVar(&cmd.device, "device", "", "serial port device name")
    48  	f.BoolVar(&cmd.client, "client", false, "Use client direction")
    49  }
    50  
    51  func (cmd *connect) Usage() string {
    52  	return "URI"
    53  }
    54  
    55  func (cmd *connect) Description() string {
    56  	return `Connect service URI to serial port.
    57  
    58  If "-" is given as URI, connects file backed device with file name of
    59  device name + .log suffix in the VM Config.Files.LogDirectory.
    60  
    61  Defaults to the first serial port if no DEVICE is given.
    62  
    63  Examples:
    64    govc device.ls | grep serialport-
    65    govc device.serial.connect -vm $vm -device serialport-8000 telnet://:33233
    66    govc device.info -vm $vm serialport-*
    67    govc device.serial.connect -vm $vm "[datastore1] $vm/console.log"
    68    govc device.serial.connect -vm $vm -
    69    govc datastore.tail -f $vm/serialport-8000.log`
    70  }
    71  
    72  func (cmd *connect) Process(ctx context.Context) error {
    73  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    74  		return err
    75  	}
    76  	return nil
    77  }
    78  
    79  func (cmd *connect) Run(ctx context.Context, f *flag.FlagSet) error {
    80  	if f.NArg() != 1 {
    81  		return flag.ErrHelp
    82  	}
    83  
    84  	vm, err := cmd.VirtualMachine()
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	if vm == nil {
    90  		return flag.ErrHelp
    91  	}
    92  
    93  	devices, err := vm.Device(ctx)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	d, err := devices.FindSerialPort(cmd.device)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	uri := f.Arg(0)
   104  
   105  	if uri == "-" {
   106  		var mvm mo.VirtualMachine
   107  		err = vm.Properties(ctx, vm.Reference(), []string{"config.files.logDirectory"}, &mvm)
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		uri = path.Join(mvm.Config.Files.LogDirectory, fmt.Sprintf("%s.log", devices.Name(d)))
   113  	}
   114  
   115  	return vm.EditDevice(ctx, devices.ConnectSerialPort(d, uri, cmd.client, cmd.proxy))
   116  }