github.com/vmware/govmomi@v0.43.0/govc/vm/guest/start.go (about)

     1  /*
     2  Copyright (c) 2014-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  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  type start struct {
    30  	*GuestFlag
    31  
    32  	dir  string
    33  	vars env
    34  }
    35  
    36  type env []string
    37  
    38  func (e *env) String() string {
    39  	return fmt.Sprint(*e)
    40  }
    41  
    42  func (e *env) Set(value string) error {
    43  	*e = append(*e, value)
    44  	return nil
    45  }
    46  
    47  func init() {
    48  	cli.Register("guest.start", &start{})
    49  }
    50  
    51  func (cmd *start) Register(ctx context.Context, f *flag.FlagSet) {
    52  	cmd.GuestFlag, ctx = newGuestProcessFlag(ctx)
    53  	cmd.GuestFlag.Register(ctx, f)
    54  
    55  	f.StringVar(&cmd.dir, "C", "", "The absolute path of the working directory for the program to start")
    56  	f.Var(&cmd.vars, "e", "Set environment variable (key=val)")
    57  }
    58  
    59  func (cmd *start) Process(ctx context.Context) error {
    60  	if err := cmd.GuestFlag.Process(ctx); err != nil {
    61  		return err
    62  	}
    63  	return nil
    64  }
    65  
    66  func (cmd *start) Usage() string {
    67  	return "PATH [ARG]..."
    68  }
    69  
    70  func (cmd *start) Description() string {
    71  	return `Start program in VM.
    72  
    73  The process can have its status queried with govc guest.ps.
    74  When the process completes, its exit code and end time will be available for 5 minutes after completion.
    75  
    76  Examples:
    77    govc guest.start -vm $name /bin/mount /dev/hdb1 /data
    78    pid=$(govc guest.start -vm $name /bin/long-running-thing)
    79    govc guest.ps -vm $name -p $pid -X`
    80  }
    81  
    82  func (cmd *start) Run(ctx context.Context, f *flag.FlagSet) error {
    83  	m, err := cmd.ProcessManager()
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	spec := types.GuestProgramSpec{
    89  		ProgramPath:      f.Arg(0),
    90  		Arguments:        strings.Join(f.Args()[1:], " "),
    91  		WorkingDirectory: cmd.dir,
    92  		EnvVariables:     cmd.vars,
    93  	}
    94  
    95  	pid, err := m.StartProgram(ctx, cmd.Auth(), &spec)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	fmt.Printf("%d\n", pid)
   101  
   102  	return nil
   103  }