knife-vsphere 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chef/knife/base_vsphere_command.rb +383 -371
  3. data/lib/chef/knife/customization_helper.rb +40 -0
  4. data/lib/chef/knife/vsphere_cluster_list.rb +47 -0
  5. data/lib/chef/knife/vsphere_cpu_ratio.rb +41 -45
  6. data/lib/chef/knife/vsphere_customization_list.rb +24 -29
  7. data/lib/chef/knife/vsphere_datastore_list.rb +68 -72
  8. data/lib/chef/knife/vsphere_datastore_maxfree.rb +48 -49
  9. data/lib/chef/knife/vsphere_datastorecluster_list.rb +66 -71
  10. data/lib/chef/knife/vsphere_datastorecluster_maxfree.rb +75 -84
  11. data/lib/chef/knife/vsphere_folder_list.rb +28 -30
  12. data/lib/chef/knife/vsphere_hosts_list.rb +42 -42
  13. data/lib/chef/knife/vsphere_pool_list.rb +46 -48
  14. data/lib/chef/knife/vsphere_pool_query.rb +58 -58
  15. data/lib/chef/knife/vsphere_template_list.rb +30 -32
  16. data/lib/chef/knife/vsphere_vlan_create.rb +51 -0
  17. data/lib/chef/knife/vsphere_vlan_list.rb +35 -37
  18. data/lib/chef/knife/vsphere_vm_clone.rb +834 -581
  19. data/lib/chef/knife/vsphere_vm_config.rb +48 -46
  20. data/lib/chef/knife/vsphere_vm_delete.rb +70 -66
  21. data/lib/chef/knife/vsphere_vm_execute.rb +62 -66
  22. data/lib/chef/knife/vsphere_vm_list.rb +57 -61
  23. data/lib/chef/knife/vsphere_vm_markastemplate.rb +48 -54
  24. data/lib/chef/knife/vsphere_vm_migrate.rb +73 -0
  25. data/lib/chef/knife/vsphere_vm_move.rb +88 -0
  26. data/lib/chef/knife/vsphere_vm_net.rb +57 -0
  27. data/lib/chef/knife/vsphere_vm_property_get.rb +44 -46
  28. data/lib/chef/knife/vsphere_vm_property_set.rb +83 -84
  29. data/lib/chef/knife/vsphere_vm_query.rb +48 -48
  30. data/lib/chef/knife/vsphere_vm_snapshot.rb +124 -130
  31. data/lib/chef/knife/vsphere_vm_state.rb +122 -127
  32. data/lib/chef/knife/vsphere_vm_toolsconfig.rb +54 -52
  33. data/lib/chef/knife/vsphere_vm_vmdk_add.rb +234 -241
  34. data/lib/chef/knife/vsphere_vm_wait_sysprep.rb +54 -0
  35. data/lib/knife-vsphere/version.rb +3 -4
  36. metadata +43 -15
  37. data/lib/chef/knife/vshpere_vm_migrate.rb +0 -80
  38. data/lib/chef/knife/vshpere_vm_move.rb +0 -92
  39. data/lib/chef/knife/vshpere_vm_net.rb +0 -57
@@ -0,0 +1,40 @@
1
+ #
2
+ # Author:: Malte Heidenreich (https://github.com/mheidenr)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'rbvmomi'
7
+
8
+ module CustomizationHelper
9
+ def self.wait_for_sysprep(vm, vim_connection, timeout, sleep_time)
10
+ vem = vim_connection.serviceContent.eventManager
11
+
12
+ wait = true
13
+ waited_seconds = 0
14
+
15
+ print 'Waiting for sysprep...'
16
+ while wait
17
+ events = query_customization_succeeded(vm, vem)
18
+
19
+ if events.size > 0
20
+ events.each do |e|
21
+ puts "\n#{e.fullFormattedMessage}"
22
+ end
23
+ wait = false
24
+ elsif waited_seconds >= timeout
25
+ abort "\nCustomization of VM #{vm.name} not succeeded within #{timeout} seconds."
26
+ else
27
+ print '.'
28
+ sleep(sleep_time)
29
+ waited_seconds += sleep_time
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.query_customization_succeeded(vm, vem)
35
+ vem.QueryEvents(filter:
36
+ RbVmomi::VIM::EventFilterSpec(entity:
37
+ RbVmomi::VIM::EventFilterSpecByEntity(entity: vm, recursion:
38
+ RbVmomi::VIM::EventFilterSpecRecursionOption(:self)), eventTypeId: ['CustomizationSucceeded']))
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Author:: Jesse Campbell (<hikeit@gmail.com>)
3
+ # Contributor:: Dennis Pattmann (https://github.com/DennisBP)
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ require 'chef/knife'
7
+ require 'chef/knife/base_vsphere_command'
8
+
9
+ # Lists all known clusters in the configured datacenter
10
+ class Chef::Knife::VsphereClusterList < Chef::Knife::BaseVsphereCommand
11
+ banner 'knife vsphere cluster list'
12
+
13
+ common_options
14
+
15
+ def traverse_folders(folder)
16
+ return if folder.is_a? RbVmomi::VIM::VirtualApp
17
+
18
+ if folder.is_a? RbVmomi::VIM::ClusterComputeResource
19
+ clusters = folder.path[3..-1].reject { |p| p.last == 'ClusterComputeResource' }
20
+ puts "#{ui.color('Cluster', :cyan)}: " + clusters.map(&:last).join('/')
21
+ end
22
+
23
+ folders = find_all_in_folder(folder, RbVmomi::VIM::ManagedObject) || []
24
+ folders.each do |child|
25
+ traverse_folders(child)
26
+ end
27
+ end
28
+
29
+ def find_cluster_folder(folderName)
30
+ dc = datacenter
31
+ base_entity = dc.hostFolder
32
+ entity_array = folderName.split('/')
33
+ entity_array.each do |entityArrItem|
34
+ if entityArrItem != ''
35
+ base_entity = base_entity.childEntity.grep(RbVmomi::VIM::ManagedObject).find { |f| f.name == entityArrItem } ||
36
+ abort("no such folder #{folderName} while looking for #{entityArrItem}")
37
+ end
38
+ end
39
+ base_entity
40
+ end
41
+
42
+ def run
43
+ vim_connection
44
+ base_folder = find_cluster_folder(get_config(:folder))
45
+ traverse_folders(base_folder)
46
+ end
47
+ end
@@ -1,45 +1,41 @@
1
- require 'chef/knife'
2
- require 'chef/knife/base_vsphere_command'
3
-
4
- class Chef::Knife::VsphereCpuRatio < Chef::Knife::BaseVsphereCommand
5
- banner 'knife vsphere cpu ratio [CLUSTER] [HOST]'
6
-
7
- get_common_options
8
-
9
- def run
10
- $stdout.sync = true
11
-
12
- cluster_name = @name_args[0]
13
- host_name = @name_args[1]
14
-
15
- get_vim_connection
16
-
17
- dc = get_datacenter
18
- hf = dc.hostFolder
19
-
20
- cluster = cluster_name.nil? ? hf.childEntity : hf.childEntity.select { |c| c.name == cluster_name }
21
-
22
- if cluster.empty?
23
- fatal_exit("Cluster #{cluster_name} not found.")
24
- end
25
-
26
- cluster.each { |c|
27
- host = host_name.nil? ? c.host : c.host.select { |h| h.name == host_name }
28
- if host.empty?
29
- fatal_exit("Host not found in cluster #{c.name}.")
30
- end
31
-
32
- puts "### Cluster #{c.name} ###"
33
-
34
- host.each { |h|
35
- v_cpu = h.vm.inject(0) { |sum, vm| sum + vm.config.hardware.numCPU }
36
- p_cpu = h.summary.hardware.numCpuThreads
37
-
38
- ratio = 1.0 * v_cpu/p_cpu
39
-
40
- puts "#{h.name}: #{ratio}"
41
- }
42
- puts ''
43
- }
44
- end
45
- end
1
+ require 'chef/knife'
2
+ require 'chef/knife/base_vsphere_command'
3
+
4
+ class Chef::Knife::VsphereCpuRatio < Chef::Knife::BaseVsphereCommand
5
+ banner 'knife vsphere cpu ratio [CLUSTER] [HOST]'
6
+
7
+ common_options
8
+
9
+ def run
10
+ $stdout.sync = true
11
+
12
+ cluster_name = @name_args[0]
13
+ host_name = @name_args[1]
14
+
15
+ vim_connection
16
+
17
+ dc = datacenter
18
+ hf = dc.hostFolder
19
+
20
+ cluster = cluster_name.nil? ? hf.childEntity : hf.childEntity.select { |c| c.name == cluster_name }
21
+
22
+ fatal_exit("Cluster #{cluster_name} not found.") if cluster.empty?
23
+
24
+ cluster.each do |c|
25
+ host = host_name.nil? ? c.host : c.host.select { |h| h.name == host_name }
26
+ fatal_exit("Host not found in cluster #{c.name}.") if host.empty?
27
+
28
+ puts "### Cluster #{c.name} ###"
29
+
30
+ host.each do |h|
31
+ v_cpu = h.vm.inject(0) { |a, e| a + e.config.hardware.numCPU }
32
+ p_cpu = h.summary.hardware.numCpuThreads
33
+
34
+ ratio = 1.0 * v_cpu / p_cpu
35
+
36
+ puts "#{h.name}: #{ratio}"
37
+ end
38
+ puts ''
39
+ end
40
+ end
41
+ end
@@ -1,29 +1,24 @@
1
- #
2
- # Author:: Ezra Pagel (<ezra@cpan.org>)
3
- # License:: Apache License, Version 2.0
4
- #
5
- require 'chef/knife'
6
- require 'chef/knife/base_vsphere_command'
7
-
8
- # Lists all customization specifications in the configured datacenter
9
- class Chef::Knife::VsphereCustomizationList < Chef::Knife::BaseVsphereCommand
10
-
11
- banner "knife vsphere customization list"
12
-
13
- get_common_options
14
-
15
- def run
16
-
17
- $stdout.sync = true
18
-
19
- vim = get_vim_connection
20
-
21
- csm = vim.serviceContent.customizationSpecManager
22
- csm.info.each do |c|
23
- puts "#{ui.color("Customization Name", :cyan)}: #{c.name}"
24
-
25
- end
26
-
27
- end
28
- end
29
-
1
+ #
2
+ # Author:: Ezra Pagel (<ezra@cpan.org>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+
8
+ # Lists all customization specifications in the configured datacenter
9
+ class Chef::Knife::VsphereCustomizationList < Chef::Knife::BaseVsphereCommand
10
+ banner 'knife vsphere customization list'
11
+
12
+ common_options
13
+
14
+ def run
15
+ $stdout.sync = true
16
+
17
+ vim = vim_connection
18
+
19
+ csm = vim.serviceContent.customizationSpecManager
20
+ csm.info.each do |c|
21
+ puts "#{ui.color('Customization Name', :cyan)}: #{c.name}"
22
+ end
23
+ end
24
+ end
@@ -1,72 +1,68 @@
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 stores in datacenter with sizes
40
- class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
41
-
42
- banner "knife vsphere datastore list"
43
-
44
- get_common_options
45
-
46
- option :list,
47
- :long => "--list",
48
- :short => "-L",
49
- :description => "Indicates whether to list VM's in datastore",
50
- :boolean => true
51
-
52
- def run
53
- $stdout.sync = true
54
-
55
- vim = get_vim_connection
56
- dc = get_datacenter
57
- dc.datastore.each do |store|
58
- avail = number_to_human_size(store.summary[:freeSpace])
59
- cap = number_to_human_size(store.summary[:capacity])
60
- puts "#{ui.color("Datastore", :cyan)}: #{store.name} (#{avail} / #{cap})"
61
- if get_config(:list)
62
- store.vm.each do |vms|
63
- hostName = vms.guest[:hostName]
64
- guestFullName = vms.guest[:guestFullName]
65
- guestState = vms.guest[:guestState]
66
- puts "#{ui.color("VM Name:", :green)} #{hostName} #{ui.color("OS:", :magenta)} #{guestFullName} #{ui.color("State:", :cyan)} #{guestState}"
67
- end
68
- end
69
- end
70
- end
71
- end
72
-
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 = %w(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
+ format('%0.2f %s', number, unit)
36
+ end
37
+
38
+ # Lists all known data stores in datacenter with sizes
39
+ class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
40
+ banner 'knife vsphere datastore list'
41
+
42
+ common_options
43
+
44
+ option :list,
45
+ long: '--list',
46
+ short: '-L',
47
+ description: "Indicates whether to list VM's in datastore",
48
+ boolean: true
49
+
50
+ def run
51
+ $stdout.sync = true
52
+
53
+ vim_connection
54
+ dc = datacenter
55
+ dc.datastore.each do |store|
56
+ avail = number_to_human_size(store.summary[:freeSpace])
57
+ cap = number_to_human_size(store.summary[:capacity])
58
+ puts "#{ui.color('Datastore', :cyan)}: #{store.name} (#{avail} / #{cap})"
59
+ next unless get_config(:list)
60
+ store.vm.each do |vms|
61
+ host_name = vms.guest[:hostName]
62
+ guest_full_name = vms.guest[:guest_full_name]
63
+ guest_state = vms.guest[:guest_state]
64
+ puts "#{ui.color('VM Name:', :green)} #{host_name} #{ui.color('OS:', :magenta)} #{guest_full_name} #{ui.color('State:', :cyan)} #{guest_state}"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,49 +1,48 @@
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
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
+ banner 'knife vsphere datastore maxfree'
24
+
25
+ option :regex,
26
+ short: '-r REGEX',
27
+ long: '--regex REGEX',
28
+ description: 'Regex to match the datastore name',
29
+ default: ''
30
+
31
+ common_options
32
+
33
+ def run
34
+ $stdout.sync = true
35
+
36
+ vim_connection
37
+ dcname = get_config(:vsphere_dc)
38
+ regex = /#{Regexp.escape(get_config(:regex))}/
39
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) || abort('datacenter not found')
40
+ max = nil
41
+ dc.datastore.each do |store|
42
+ if regex.match(store.name) && (max.nil? || max.summary[:freeSpace] < store.summary[:freeSpace])
43
+ max = store
44
+ end
45
+ end
46
+ puts max ? max.name : ''
47
+ end
48
+ end