-
Notifications
You must be signed in to change notification settings - Fork 127
/
vsphere_vm_execute.rb
64 lines (51 loc) · 1.93 KB
/
vsphere_vm_execute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Author:: Ian Delahorne (<ian@delahorne.com>)
# License:: Apache License, Version 2.0
require "chef/knife"
require_relative "helpers/base_vsphere_command"
# VsphereVMexecute extends the Basevspherecommand
class Chef::Knife::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
banner "knife vsphere vm execute VMNAME COMMAND ARGS"
deps do
Chef::Knife::BaseVsphereCommand.load_deps
require_relative "helpers/search_helper"
include SearchHelper
end
option :exec_user,
long: "--exec-user USER",
description: "User to execute as",
required: true
option :exec_passwd,
long: "--exec-passwd PASSWORD",
description: "Password for execute user",
required: true
option :exec_dir,
long: "--exec-dir DIRECTORY",
description: "Working directory to execute in"
common_options
# The main run method for vm_execute
#
def run
$stdout.sync = true
vmname = @name_args.shift
if vmname.nil?
show_usage
fatal_exit("You must specify a virtual machine name")
end
command = @name_args.shift
if command.nil?
show_usage
fatal_exit("You must specify a command to execute")
end
args = @name_args
args = [] if args.nil?
vm = get_vm_by_name(vmname, get_config(:folder)) || fatal_exit("Could not find #{vmname}")
gom = vim_connection.serviceContent.guestOperationsManager
guest_auth = RbVmomi::VIM::NamePasswordAuthentication(interactiveSession: false,
username: config[:exec_user],
password: config[:exec_passwd])
prog_spec = RbVmomi::VIM::GuestProgramSpec(programPath: command,
arguments: args.join(" "),
workingDirectory: get_config(:exec_dir))
gom.processManager.StartProgramInGuest(vm: vm, auth: guest_auth, spec: prog_spec)
end
end