github.com/vmware/govmomi@v0.43.0/govc/vm/guest/run.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  	"bytes"
    21  	"context"
    22  	"flag"
    23  	"os"
    24  	"os/exec"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  )
    28  
    29  type run struct {
    30  	*GuestFlag
    31  
    32  	data string
    33  	dir  string
    34  	vars env
    35  }
    36  
    37  func init() {
    38  	cli.Register("guest.run", &run{})
    39  }
    40  
    41  func (cmd *run) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.GuestFlag, ctx = newGuestProcessFlag(ctx)
    43  	cmd.GuestFlag.Register(ctx, f)
    44  
    45  	f.StringVar(&cmd.data, "d", "", "Input data string. A value of '-' reads from OS stdin")
    46  	f.StringVar(&cmd.dir, "C", "", "The absolute path of the working directory for the program to start")
    47  	f.Var(&cmd.vars, "e", "Set environment variables")
    48  }
    49  
    50  func (cmd *run) Usage() string {
    51  	return "PATH [ARG]..."
    52  }
    53  
    54  func (cmd *run) Description() string {
    55  	return `Run program PATH in VM and display output.
    56  
    57  The guest.run command starts a program in the VM with i/o redirected, waits for the process to exit and
    58  propagates the exit code to the govc process exit code.  Note that stdout and stderr are redirected by default,
    59  stdin is only redirected when the '-d' flag is specified.
    60  
    61  Note that vmware-tools requires program PATH to be absolute.
    62  If PATH is not absolute and vm guest family is Windows,
    63  guest.run changes the command to: 'c:\\Windows\\System32\\cmd.exe /c "PATH [ARG]..."'
    64  Otherwise the command is changed to: '/bin/bash -c "PATH [ARG]..."'
    65  
    66  Examples:
    67    govc guest.run -vm $name ifconfig
    68    govc guest.run -vm $name ifconfig eth0
    69    cal | govc guest.run -vm $name -d - cat
    70    govc guest.run -vm $name -d "hello $USER" cat
    71    govc guest.run -vm $name curl -s :invalid: || echo $? # exit code 6
    72    govc guest.run -vm $name -e FOO=bar -e BIZ=baz -C /tmp env
    73    govc guest.run -vm $name -l root:mypassword ntpdate -u pool.ntp.org
    74    govc guest.run -vm $name powershell C:\\network_refresh.ps1`
    75  }
    76  
    77  func (cmd *run) Run(ctx context.Context, f *flag.FlagSet) error {
    78  	if f.NArg() == 0 {
    79  		return flag.ErrHelp
    80  	}
    81  	name := f.Arg(0)
    82  
    83  	c, err := cmd.Toolbox(ctx)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	ecmd := &exec.Cmd{
    89  		Path:   name,
    90  		Args:   f.Args()[1:],
    91  		Env:    cmd.vars,
    92  		Dir:    cmd.dir,
    93  		Stdout: os.Stdout,
    94  		Stderr: os.Stderr,
    95  	}
    96  
    97  	switch cmd.data {
    98  	case "":
    99  	case "-":
   100  		ecmd.Stdin = os.Stdin
   101  	default:
   102  		ecmd.Stdin = bytes.NewBuffer([]byte(cmd.data))
   103  	}
   104  
   105  	return c.Run(ctx, ecmd)
   106  }