knife-vsphere 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -267,6 +267,15 @@ class Chef
|
|
267
267
|
exit 1
|
268
268
|
end
|
269
269
|
|
270
|
+
def tcp_test_port_vm(vm,port)
|
271
|
+
ip = vm.guest.ipAddress
|
272
|
+
if ip.nil?
|
273
|
+
sleep 2
|
274
|
+
return false
|
275
|
+
end
|
276
|
+
tcp_test_port(ip, port)
|
277
|
+
end
|
278
|
+
|
270
279
|
def tcp_test_port(hostname,port)
|
271
280
|
tcp_socket = TCPSocket.new(hostname, port)
|
272
281
|
readable = IO.select([tcp_socket], nil, nil, 5)
|
@@ -155,17 +155,21 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
|
155
155
|
:short => "-d DISTRO",
|
156
156
|
:long => "--distro DISTRO",
|
157
157
|
:description => "Bootstrap a distro using a template"
|
158
|
-
$default[:distro] = "ubuntu10.04-gems"
|
159
158
|
|
160
159
|
option :template_file,
|
161
160
|
:long => "--template-file TEMPLATE",
|
162
161
|
:description => "Full path to location of template to use"
|
163
162
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
163
|
+
option :run_list,
|
164
|
+
:short => "-r RUN_LIST",
|
165
|
+
:long => "--run-list RUN_LIST",
|
166
|
+
:description => "Comma separated list of roles/recipes to apply"
|
167
|
+
$default[:run_list] = ''
|
168
|
+
|
169
|
+
option :secret_file,
|
170
|
+
:long => "--secret-file SECRET_FILE",
|
171
|
+
:description => "A file containing the secret key to use to encrypt data bag item values"
|
172
|
+
$default[:secret_file] = ''
|
169
173
|
|
170
174
|
option :no_host_key_verify,
|
171
175
|
:long => "--no-host-key-verify",
|
@@ -190,7 +194,7 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
|
190
194
|
:long => "--log_level",
|
191
195
|
:description => "Set the log level (debug, info, warn, error, fatal) for chef-client",
|
192
196
|
:proc => lambda { |l| l.to_sym }
|
193
|
-
|
197
|
+
|
194
198
|
def run
|
195
199
|
$stdout.sync = true
|
196
200
|
|
@@ -438,7 +442,8 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
|
438
442
|
bootstrap = Chef::Knife::Bootstrap.new
|
439
443
|
bootstrap.name_args = [config[:fqdn]]
|
440
444
|
bootstrap.config[:run_list] = get_config(:run_list).split(/[\s,]+/)
|
441
|
-
|
445
|
+
bootstrap.config[:secret_file] = get_config(:secret_file)
|
446
|
+
bootstrap.config[:ssh_user] = get_config(:ssh_user)
|
442
447
|
bootstrap.config[:ssh_password] = get_config(:ssh_password)
|
443
448
|
bootstrap.config[:ssh_port] = get_config(:ssh_port)
|
444
449
|
bootstrap.config[:identity_file] = get_config(:identity_file)
|
@@ -1,66 +1,66 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
|
6
|
-
require 'chef/knife'
|
7
|
-
require 'chef/knife/BaseVsphereCommand'
|
8
|
-
require 'rbvmomi'
|
9
|
-
|
10
|
-
# These two are needed for the '--purge' deletion case
|
11
|
-
require 'chef/node'
|
12
|
-
require 'chef/api_client'
|
13
|
-
|
14
|
-
# Delete a virtual machine from vCenter
|
15
|
-
class Chef::Knife::VsphereVmDelete < Chef::Knife::BaseVsphereCommand
|
16
|
-
|
17
|
-
banner "knife vsphere vm delete VMNAME"
|
18
|
-
|
19
|
-
option :purge,
|
20
|
-
:short => "-P",
|
21
|
-
:long => "--purge",
|
22
|
-
:boolean => true,
|
23
|
-
:description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the
|
24
|
-
|
25
|
-
get_common_options
|
26
|
-
|
27
|
-
# Extracted from Chef::Knife.delete_object, because it has a
|
28
|
-
# confirmation step built in... By specifying the '--purge'
|
29
|
-
# flag (and also explicitly confirming the server destruction!)
|
30
|
-
# the user is already making their intent known. It is not
|
31
|
-
# necessary to make them confirm two more times.
|
32
|
-
def destroy_item(itemClass, name, type_name)
|
33
|
-
object = itemClass.load(name)
|
34
|
-
object.destroy
|
35
|
-
puts "Deleted #{type_name} #{name}"
|
36
|
-
end
|
37
|
-
|
38
|
-
def run
|
39
|
-
$stdout.sync = true
|
40
|
-
|
41
|
-
vmname = @name_args[0]
|
42
|
-
|
43
|
-
if vmname.nil?
|
44
|
-
show_usage
|
45
|
-
fatal_exit("You must specify a virtual machine name")
|
46
|
-
end
|
47
|
-
|
48
|
-
vim = get_vim_connection
|
49
|
-
|
50
|
-
baseFolder = find_folder(get_config(:folder));
|
51
|
-
|
52
|
-
vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
|
53
|
-
fatal_exit("VM #{vmname} not found")
|
54
|
-
|
55
|
-
vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
|
56
|
-
vm.Destroy_Task
|
57
|
-
puts "Deleted virtual machine #{vmname}"
|
58
|
-
|
59
|
-
if config[:purge]
|
60
|
-
destroy_item(Chef::Node, vmname, "node")
|
61
|
-
destroy_item(Chef::ApiClient, vmname, "client")
|
62
|
-
else
|
63
|
-
puts "Corresponding node and client for the #{vmname} server were not deleted and remain registered with the Chef Server"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'chef/knife'
|
7
|
+
require 'chef/knife/BaseVsphereCommand'
|
8
|
+
require 'rbvmomi'
|
9
|
+
|
10
|
+
# These two are needed for the '--purge' deletion case
|
11
|
+
require 'chef/node'
|
12
|
+
require 'chef/api_client'
|
13
|
+
|
14
|
+
# Delete a virtual machine from vCenter
|
15
|
+
class Chef::Knife::VsphereVmDelete < Chef::Knife::BaseVsphereCommand
|
16
|
+
|
17
|
+
banner "knife vsphere vm delete VMNAME"
|
18
|
+
|
19
|
+
option :purge,
|
20
|
+
:short => "-P",
|
21
|
+
:long => "--purge",
|
22
|
+
:boolean => true,
|
23
|
+
:description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the VM itself."
|
24
|
+
|
25
|
+
get_common_options
|
26
|
+
|
27
|
+
# Extracted from Chef::Knife.delete_object, because it has a
|
28
|
+
# confirmation step built in... By specifying the '--purge'
|
29
|
+
# flag (and also explicitly confirming the server destruction!)
|
30
|
+
# the user is already making their intent known. It is not
|
31
|
+
# necessary to make them confirm two more times.
|
32
|
+
def destroy_item(itemClass, name, type_name)
|
33
|
+
object = itemClass.load(name)
|
34
|
+
object.destroy
|
35
|
+
puts "Deleted #{type_name} #{name}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def run
|
39
|
+
$stdout.sync = true
|
40
|
+
|
41
|
+
vmname = @name_args[0]
|
42
|
+
|
43
|
+
if vmname.nil?
|
44
|
+
show_usage
|
45
|
+
fatal_exit("You must specify a virtual machine name")
|
46
|
+
end
|
47
|
+
|
48
|
+
vim = get_vim_connection
|
49
|
+
|
50
|
+
baseFolder = find_folder(get_config(:folder));
|
51
|
+
|
52
|
+
vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
|
53
|
+
fatal_exit("VM #{vmname} not found")
|
54
|
+
|
55
|
+
vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
|
56
|
+
vm.Destroy_Task
|
57
|
+
puts "Deleted virtual machine #{vmname}"
|
58
|
+
|
59
|
+
if config[:purge]
|
60
|
+
destroy_item(Chef::Node, vmname, "node")
|
61
|
+
destroy_item(Chef::ApiClient, vmname, "client")
|
62
|
+
else
|
63
|
+
puts "Corresponding node and client for the #{vmname} server were not deleted and remain registered with the Chef Server"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
# Author:: Ian Delahorne (<ian@delahorne.com>)
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
|
5
|
+
require 'chef/knife'
|
6
|
+
require 'chef/knife/BaseVsphereCommand'
|
7
|
+
require 'rbvmomi'
|
8
|
+
require 'netaddr'
|
9
|
+
|
10
|
+
class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
|
11
|
+
banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
|
12
|
+
|
13
|
+
get_common_options
|
14
|
+
|
15
|
+
def run
|
16
|
+
$stdout.sync = true
|
17
|
+
vmname = @name_args[0]
|
18
|
+
if vmname.nil?
|
19
|
+
show_usage
|
20
|
+
fatal_exit("You must specify a virtual machine name")
|
21
|
+
end
|
22
|
+
|
23
|
+
query_string = @name_args[1]
|
24
|
+
if query_string.nil?
|
25
|
+
show_usage
|
26
|
+
fatal_exit("You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)")
|
27
|
+
end
|
28
|
+
|
29
|
+
vim = get_vim_connection
|
30
|
+
|
31
|
+
dcname = get_config(:vsphere_dc)
|
32
|
+
dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
33
|
+
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
34
|
+
|
35
|
+
vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
|
36
|
+
abort "VM #{vmname} not found"
|
37
|
+
|
38
|
+
# split QUERY by dots, and walk the object model
|
39
|
+
query = query_string.split '.'
|
40
|
+
result = vm
|
41
|
+
query.each do |part|
|
42
|
+
message, index = part.split(/[\[\]]/)
|
43
|
+
unless result.respond_to? message.to_sym
|
44
|
+
fatal_exit("\"#{query_string}\" not recognized.")
|
45
|
+
end
|
46
|
+
|
47
|
+
result = index ? result.send(message)[index.to_i] : result.send(message)
|
48
|
+
end
|
49
|
+
puts result
|
50
|
+
end
|
51
|
+
end
|
@@ -35,8 +35,13 @@ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
|
35
35
|
:long => "--wait-port PORT",
|
36
36
|
:description => "Wait for VM to be accessible on a port"
|
37
37
|
|
38
|
+
option :shutdown,
|
39
|
+
:short => "-g",
|
40
|
+
:long => "--shutdown",
|
41
|
+
:description => "Guest OS shutdown"
|
42
|
+
|
38
43
|
def run
|
39
|
-
|
44
|
+
|
40
45
|
$stdout.sync = true
|
41
46
|
|
42
47
|
vmname = @name_args[0]
|
@@ -45,7 +50,7 @@ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
|
45
50
|
ui.fatal("You must specify a virtual machine name")
|
46
51
|
exit 1
|
47
52
|
end
|
48
|
-
|
53
|
+
|
49
54
|
vim = get_vim_connection
|
50
55
|
|
51
56
|
baseFolder = find_folder(get_config(:folder));
|
@@ -71,8 +76,18 @@ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
|
71
76
|
if state == PsOff
|
72
77
|
puts "Virtual machine #{vmname} was already powered off"
|
73
78
|
else
|
74
|
-
|
75
|
-
|
79
|
+
if get_config(:shutdown)
|
80
|
+
vm.ShutdownGuest
|
81
|
+
print "Waiting for virtual machine #{vmname} to shut down..."
|
82
|
+
until vm.runtime.powerState == PsOff do
|
83
|
+
sleep 2
|
84
|
+
print "."
|
85
|
+
end
|
86
|
+
puts "done"
|
87
|
+
else
|
88
|
+
vm.PowerOffVM_Task.wait_for_completion
|
89
|
+
puts "Powered off virtual machine #{vmname}"
|
90
|
+
end
|
76
91
|
end
|
77
92
|
when 'suspend'
|
78
93
|
if state == PowerStates['suspended']
|
@@ -88,7 +103,7 @@ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
|
88
103
|
|
89
104
|
if get_config(:wait_port)
|
90
105
|
print "Waiting for port #{get_config(:wait_port)}..."
|
91
|
-
print "." until
|
106
|
+
print "." until tcp_test_port_vm(vm,get_config(:wait_port))
|
92
107
|
puts "done"
|
93
108
|
end
|
94
109
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knife-vsphere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.9.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ezra Pagel
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2013-08-
|
13
|
+
date: 2013-08-22 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -65,14 +65,15 @@ files:
|
|
65
65
|
- lib/chef/knife/vsphere_vm_delete.rb
|
66
66
|
- lib/chef/knife/vsphere_vm_execute.rb
|
67
67
|
- lib/chef/knife/vsphere_vm_list.rb
|
68
|
+
- lib/chef/knife/vsphere_vm_query.rb
|
68
69
|
- lib/chef/knife/vsphere_vm_snapshot.rb
|
69
70
|
- lib/chef/knife/vsphere_vm_state.rb
|
70
71
|
- lib/chef/knife/vsphere_vm_vmdk_add.rb
|
71
72
|
- lib/knife-vsphere/version.rb
|
72
73
|
has_rdoc: true
|
73
74
|
homepage: http://github.com/ezrapagel/knife-vsphere
|
74
|
-
licenses:
|
75
|
-
|
75
|
+
licenses:
|
76
|
+
- Apache
|
76
77
|
post_install_message:
|
77
78
|
rdoc_options: []
|
78
79
|
|