knife-vsphere 0.9.7 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/chef/knife/base_vsphere_command.rb +8 -13
- data/lib/chef/knife/vsphere_datastore_maxfree.rb +49 -49
- data/lib/chef/knife/vsphere_datastorecluster_list.rb +60 -0
- data/lib/chef/knife/vsphere_datastorecluster_maxfree.rb +49 -0
- data/lib/chef/knife/vsphere_pool_query.rb +58 -0
- data/lib/chef/knife/vsphere_vm_query.rb +48 -49
- data/lib/knife-vsphere/version.rb +1 -1
- metadata +5 -2
@@ -126,21 +126,16 @@ class Chef
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def traverse_folders_for_vm(folder, vmname)
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
vms.each do |vm|
|
137
|
-
if vm.name == vmname
|
138
|
-
@vm = vm
|
139
|
-
return @vm
|
140
|
-
end
|
129
|
+
children = folder.children.find_all
|
130
|
+
children.each do |child|
|
131
|
+
if child.class == RbVmomi::VIM::VirtualMachine && child.name == vmname
|
132
|
+
return child
|
133
|
+
elsif child.class == RbVmomi::VIM::Folder
|
134
|
+
vm = traverse_folders_for_vm(child, vmname)
|
135
|
+
if vm then return vm end
|
141
136
|
end
|
142
137
|
end
|
143
|
-
return
|
138
|
+
return false
|
144
139
|
end
|
145
140
|
|
146
141
|
def traverse_folders_for_dc(folder, dcname)
|
@@ -1,49 +1,49 @@
|
|
1
|
-
# Copyright (C) 2012, SCM Ventures AB
|
2
|
-
# Author: Ian Delahorne <ian@scmventures.se>
|
3
|
-
#
|
4
|
-
# Permission to use, copy, modify, and/or distribute this software for
|
5
|
-
# any purpose with or without fee is hereby granted, provided that the
|
6
|
-
# above copyright notice and this permission notice appear in all
|
7
|
-
# copies.
|
8
|
-
#
|
9
|
-
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
10
|
-
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
11
|
-
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
12
|
-
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
13
|
-
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
14
|
-
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
15
|
-
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
16
|
-
# PERFORMANCE OF THIS SOFTWARE
|
17
|
-
|
18
|
-
require 'chef/knife'
|
19
|
-
require 'chef/knife/base_vsphere_command'
|
20
|
-
|
21
|
-
# Gets the data store with the most free space in datacenter
|
22
|
-
class Chef::Knife::VsphereDatastoreMaxfree < Chef::Knife::BaseVsphereCommand
|
23
|
-
|
24
|
-
banner "knife vsphere datastore maxfree"
|
25
|
-
|
26
|
-
option :regex,
|
27
|
-
:short => "-r REGEX",
|
28
|
-
:long => "--regex REGEX",
|
29
|
-
:description => "Regex to match the datastore name"
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
def run
|
35
|
-
$stdout.sync = true
|
36
|
-
|
37
|
-
vim = get_vim_connection
|
38
|
-
dcname = get_config(:vsphere_dc)
|
39
|
-
regex = /#{Regexp.escape( get_config(:regex))}/
|
40
|
-
dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
41
|
-
max = nil
|
42
|
-
dc.datastore.each do |store|
|
43
|
-
if regex.match(store.name) and (max == nil or max.summary[:freeSpace] < store.summary[:freeSpace])
|
44
|
-
max = store
|
45
|
-
end
|
46
|
-
end
|
47
|
-
puts max ? max.name : ""
|
48
|
-
end
|
49
|
-
end
|
1
|
+
# Copyright (C) 2012, SCM Ventures AB
|
2
|
+
# Author: Ian Delahorne <ian@scmventures.se>
|
3
|
+
#
|
4
|
+
# Permission to use, copy, modify, and/or distribute this software for
|
5
|
+
# any purpose with or without fee is hereby granted, provided that the
|
6
|
+
# above copyright notice and this permission notice appear in all
|
7
|
+
# copies.
|
8
|
+
#
|
9
|
+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
10
|
+
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
11
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
12
|
+
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
13
|
+
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
14
|
+
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
15
|
+
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
16
|
+
# PERFORMANCE OF THIS SOFTWARE
|
17
|
+
|
18
|
+
require 'chef/knife'
|
19
|
+
require 'chef/knife/base_vsphere_command'
|
20
|
+
|
21
|
+
# Gets the data store with the most free space in datacenter
|
22
|
+
class Chef::Knife::VsphereDatastoreMaxfree < Chef::Knife::BaseVsphereCommand
|
23
|
+
|
24
|
+
banner "knife vsphere datastore maxfree"
|
25
|
+
|
26
|
+
option :regex,
|
27
|
+
:short => "-r REGEX",
|
28
|
+
:long => "--regex REGEX",
|
29
|
+
:description => "Regex to match the datastore name"
|
30
|
+
|
31
|
+
get_common_options
|
32
|
+
$default[:regex] = ''
|
33
|
+
|
34
|
+
def run
|
35
|
+
$stdout.sync = true
|
36
|
+
|
37
|
+
vim = get_vim_connection
|
38
|
+
dcname = get_config(:vsphere_dc)
|
39
|
+
regex = /#{Regexp.escape( get_config(:regex))}/
|
40
|
+
dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
41
|
+
max = nil
|
42
|
+
dc.datastore.each do |store|
|
43
|
+
if regex.match(store.name) and (max == nil or max.summary[:freeSpace] < store.summary[:freeSpace])
|
44
|
+
max = store
|
45
|
+
end
|
46
|
+
end
|
47
|
+
puts max ? max.name : ""
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Copyright (C) 2012, SCM Ventures AB
|
2
|
+
# Author: Ian Delahorne <ian@scmventures.se>
|
3
|
+
#
|
4
|
+
# Permission to use, copy, modify, and/or distribute this software for
|
5
|
+
# any purpose with or without fee is hereby granted, provided that the
|
6
|
+
# above copyright notice and this permission notice appear in all
|
7
|
+
# copies.
|
8
|
+
#
|
9
|
+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
10
|
+
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
11
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
12
|
+
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
13
|
+
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
14
|
+
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
15
|
+
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
16
|
+
# PERFORMANCE OF THIS SOFTWARE
|
17
|
+
|
18
|
+
require 'chef/knife'
|
19
|
+
require 'chef/knife/base_vsphere_command'
|
20
|
+
|
21
|
+
def number_to_human_size(number)
|
22
|
+
number = number.to_f
|
23
|
+
storage_units_fmt = ["byte", "kB", "MB", "GB", "TB"]
|
24
|
+
base = 1024
|
25
|
+
if number.to_i < base
|
26
|
+
unit = storage_units_fmt[0]
|
27
|
+
else
|
28
|
+
max_exp = storage_units_fmt.size - 1
|
29
|
+
exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
|
30
|
+
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
31
|
+
number /= base ** exponent
|
32
|
+
unit = storage_units_fmt[exponent]
|
33
|
+
end
|
34
|
+
|
35
|
+
return sprintf("%0.2f %s", number, unit)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# Lists all known data store cluster in datacenter with sizes
|
40
|
+
class Chef::Knife::VsphereDatastoreclusterList < Chef::Knife::BaseVsphereCommand
|
41
|
+
|
42
|
+
banner "knife vsphere datastore list"
|
43
|
+
|
44
|
+
get_common_options
|
45
|
+
|
46
|
+
def run
|
47
|
+
$stdout.sync = true
|
48
|
+
|
49
|
+
vim = get_vim_connection
|
50
|
+
dc = get_datacenter
|
51
|
+
dc.datastoreFolder.childEntity.each do |store|
|
52
|
+
if store.class.to_s == "StoragePod"
|
53
|
+
avail = number_to_human_size(store.summary[:freeSpace])
|
54
|
+
cap = number_to_human_size(store.summary[:capacity])
|
55
|
+
puts "#{ui.color("DatastoreCluster", :cyan)}: #{store.name} (#{avail} / #{cap})"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# Copyright (C) 2012, SCM Ventures AB
|
2
|
+
# Author: Ian Delahorne <ian@scmventures.se>
|
3
|
+
#
|
4
|
+
# Permission to use, copy, modify, and/or distribute this software for
|
5
|
+
# any purpose with or without fee is hereby granted, provided that the
|
6
|
+
# above copyright notice and this permission notice appear in all
|
7
|
+
# copies.
|
8
|
+
#
|
9
|
+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
10
|
+
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
11
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
12
|
+
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
13
|
+
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
14
|
+
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
15
|
+
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
16
|
+
# PERFORMANCE OF THIS SOFTWARE
|
17
|
+
|
18
|
+
require 'chef/knife'
|
19
|
+
require 'chef/knife/base_vsphere_command'
|
20
|
+
|
21
|
+
# Gets the data store cluster with the most free space in datacenter
|
22
|
+
class Chef::Knife::VsphereDatastoreclusterMaxfree < Chef::Knife::BaseVsphereCommand
|
23
|
+
|
24
|
+
banner "knife vsphere datastorecluster maxfree"
|
25
|
+
|
26
|
+
option :regex,
|
27
|
+
:short => "-r REGEX",
|
28
|
+
:long => "--regex REGEX",
|
29
|
+
:description => "Regex to match the datastore cluster name"
|
30
|
+
$default[:regex] = ''
|
31
|
+
|
32
|
+
get_common_options
|
33
|
+
|
34
|
+
def run
|
35
|
+
$stdout.sync = true
|
36
|
+
|
37
|
+
vim = get_vim_connection
|
38
|
+
dcname = get_config(:vsphere_dc)
|
39
|
+
regex = /#{Regexp.escape( get_config(:regex))}/
|
40
|
+
dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
41
|
+
max = nil
|
42
|
+
dc.datastoreFolder.childEntity.each do |store|
|
43
|
+
if regex.match(store.name) and (store.class.to_s == "StoragePod") and (max == nil or max.summary[:freeSpace] < store.summary[:freeSpace])
|
44
|
+
max = store
|
45
|
+
end
|
46
|
+
end
|
47
|
+
puts max ? max.name : ""
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'chef/knife'
|
2
|
+
require 'chef/knife/base_vsphere_command'
|
3
|
+
require 'rbvmomi'
|
4
|
+
require 'netaddr'
|
5
|
+
|
6
|
+
class Chef::Knife::VspherePoolQuery < Chef::Knife::BaseVsphereCommand
|
7
|
+
banner "knife vsphere pool query POOLNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.ComputeResource.html\" for allowed QUERY values."
|
8
|
+
|
9
|
+
get_common_options
|
10
|
+
|
11
|
+
def traverse_folders_for_pool(folder, poolname)
|
12
|
+
children = folder.children.find_all
|
13
|
+
children.each do |child|
|
14
|
+
if child.class == RbVmomi::VIM::ClusterComputeResource || child.class == RbVmomi::VIM::ComputeResource || child.class == RbVmomi::VIM::ResourcePool
|
15
|
+
if child.name == poolname then return child end
|
16
|
+
elsif child.class == RbVmomi::VIM::Folder
|
17
|
+
pool = traverse_folders_for_pool(child, poolname)
|
18
|
+
if pool then return pool end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
|
24
|
+
def run
|
25
|
+
$stdout.sync = true
|
26
|
+
poolname = @name_args[0]
|
27
|
+
if poolname.nil?
|
28
|
+
show_usage
|
29
|
+
fatal_exit("You must specify a resource poor or cluster name (see knife vsphere pool list)")
|
30
|
+
end
|
31
|
+
|
32
|
+
query_string = @name_args[1]
|
33
|
+
if query_string.nil?
|
34
|
+
show_usage
|
35
|
+
fatal_exit("You must specify a QUERY value (e.g. summary.overallStatus )")
|
36
|
+
end
|
37
|
+
|
38
|
+
vim = get_vim_connection
|
39
|
+
|
40
|
+
dc = get_datacenter
|
41
|
+
folder = dc.hostFolder
|
42
|
+
|
43
|
+
pool = traverse_folders_for_pool(folder, poolname) or abort "Pool #{poolname} not found"
|
44
|
+
|
45
|
+
# split QUERY by dots, and walk the object model
|
46
|
+
query = query_string.split '.'
|
47
|
+
result = pool
|
48
|
+
query.each do |part|
|
49
|
+
message, index = part.split(/[\[\]]/)
|
50
|
+
unless result.respond_to? message.to_sym
|
51
|
+
fatal_exit("\"#{query_string}\" not recognized.")
|
52
|
+
end
|
53
|
+
|
54
|
+
result = index ? result.send(message)[index.to_i] : result.send(message)
|
55
|
+
end
|
56
|
+
puts result
|
57
|
+
end
|
58
|
+
end
|
@@ -1,49 +1,48 @@
|
|
1
|
-
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
-
# License:: Apache License, Version 2.0
|
3
|
-
|
4
|
-
require 'chef/knife'
|
5
|
-
require 'chef/knife/base_vsphere_command'
|
6
|
-
require 'rbvmomi'
|
7
|
-
require 'netaddr'
|
8
|
-
|
9
|
-
class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
|
10
|
-
banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
|
11
|
-
|
12
|
-
get_common_options
|
13
|
-
|
14
|
-
def run
|
15
|
-
$stdout.sync = true
|
16
|
-
vmname = @name_args[0]
|
17
|
-
if vmname.nil?
|
18
|
-
show_usage
|
19
|
-
fatal_exit("You must specify a virtual machine name")
|
20
|
-
end
|
21
|
-
|
22
|
-
query_string = @name_args[1]
|
23
|
-
if query_string.nil?
|
24
|
-
show_usage
|
25
|
-
fatal_exit("You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)")
|
26
|
-
end
|
27
|
-
|
28
|
-
vim = get_vim_connection
|
29
|
-
|
30
|
-
dc = get_datacenter
|
31
|
-
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
32
|
-
|
33
|
-
vm =
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
1
|
+
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
|
4
|
+
require 'chef/knife'
|
5
|
+
require 'chef/knife/base_vsphere_command'
|
6
|
+
require 'rbvmomi'
|
7
|
+
require 'netaddr'
|
8
|
+
|
9
|
+
class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
|
10
|
+
banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
|
11
|
+
|
12
|
+
get_common_options
|
13
|
+
|
14
|
+
def run
|
15
|
+
$stdout.sync = true
|
16
|
+
vmname = @name_args[0]
|
17
|
+
if vmname.nil?
|
18
|
+
show_usage
|
19
|
+
fatal_exit("You must specify a virtual machine name")
|
20
|
+
end
|
21
|
+
|
22
|
+
query_string = @name_args[1]
|
23
|
+
if query_string.nil?
|
24
|
+
show_usage
|
25
|
+
fatal_exit("You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)")
|
26
|
+
end
|
27
|
+
|
28
|
+
vim = get_vim_connection
|
29
|
+
|
30
|
+
dc = get_datacenter
|
31
|
+
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
32
|
+
|
33
|
+
vm = traverse_folders_for_vm(folder, vmname) or abort "VM #{vmname} not found"
|
34
|
+
|
35
|
+
# split QUERY by dots, and walk the object model
|
36
|
+
query = query_string.split '.'
|
37
|
+
result = vm
|
38
|
+
query.each do |part|
|
39
|
+
message, index = part.split(/[\[\]]/)
|
40
|
+
unless result.respond_to? message.to_sym
|
41
|
+
fatal_exit("\"#{query_string}\" not recognized.")
|
42
|
+
end
|
43
|
+
|
44
|
+
result = index ? result.send(message)[index.to_i] : result.send(message)
|
45
|
+
end
|
46
|
+
puts result
|
47
|
+
end
|
48
|
+
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.9.
|
5
|
+
version: 0.9.8
|
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:
|
13
|
+
date: 2014-01-30 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -58,9 +58,12 @@ files:
|
|
58
58
|
- lib/chef/knife/base_vsphere_command.rb
|
59
59
|
- lib/chef/knife/vshpere_vm_move.rb
|
60
60
|
- lib/chef/knife/vsphere_customization_list.rb
|
61
|
+
- lib/chef/knife/vsphere_datastorecluster_list.rb
|
62
|
+
- lib/chef/knife/vsphere_datastorecluster_maxfree.rb
|
61
63
|
- lib/chef/knife/vsphere_datastore_list.rb
|
62
64
|
- lib/chef/knife/vsphere_datastore_maxfree.rb
|
63
65
|
- lib/chef/knife/vsphere_pool_list.rb
|
66
|
+
- lib/chef/knife/vsphere_pool_query.rb
|
64
67
|
- lib/chef/knife/vsphere_template_list.rb
|
65
68
|
- lib/chef/knife/vsphere_vlan_list.rb
|
66
69
|
- lib/chef/knife/vsphere_vm_clone.rb
|