rbvmomi 2.0.0 → 2.0.1

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.
@@ -1,88 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright (c) 2011-2017 VMware, Inc. All Rights Reserved.
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'optimist'
7
- require 'rbvmomi'
8
- require 'rbvmomi/optimist'
9
-
10
- VIM = RbVmomi::VIM
11
-
12
- opts = Optimist.options do
13
- banner <<-EOS
14
- Clone a VM.
15
-
16
- Usage:
17
- clone_vm.rb [options] source_vm dest_vm
18
-
19
- VIM connection options:
20
- EOS
21
-
22
- rbvmomi_connection_opts
23
-
24
- text <<-EOS
25
-
26
- VM location options:
27
- EOS
28
-
29
- rbvmomi_datacenter_opt
30
-
31
- text <<-EOS
32
-
33
- Other options:
34
- EOS
35
-
36
- opt :linked_clone, "Use a linked clone instead of a full clone"
37
- end
38
-
39
- Optimist.die("must specify host") unless opts[:host]
40
- ARGV.size == 2 or abort "must specify VM source name and VM target name"
41
- vm_source = ARGV[0]
42
- vm_target = ARGV[1]
43
-
44
- vim = VIM.connect opts
45
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
46
- vm = dc.find_vm(vm_source) or abort "VM not found"
47
-
48
- if opts[:linked_clone]
49
- # The API for linked clones is quite strange. We can't create a linked
50
- # straight from any VM. The disks of the VM for which we can create a
51
- # linked clone need to be read-only and thus VC demands that the VM we
52
- # are cloning from uses delta-disks. Only then it will allow us to
53
- # share the base disk.
54
- #
55
- # Thus, this code first create a delta disk on top of the base disk for
56
- # the to-be-cloned VM, if delta disks aren't used already.
57
- disks = vm.config.hardware.device.grep(VIM::VirtualDisk)
58
- disks.select { |x| x.backing.parent == nil }.each do |disk|
59
- spec = {
60
- :deviceChange => [
61
- {
62
- :operation => :remove,
63
- :device => disk
64
- },
65
- {
66
- :operation => :add,
67
- :fileOperation => :create,
68
- :device => disk.dup.tap { |x|
69
- x.backing = x.backing.dup
70
- x.backing.fileName = "[#{disk.backing.datastore.name}]"
71
- x.backing.parent = disk.backing
72
- },
73
- }
74
- ]
75
- }
76
- vm.ReconfigVM_Task(:spec => spec).wait_for_completion
77
- end
78
-
79
- relocateSpec = VIM.VirtualMachineRelocateSpec(:diskMoveType => :moveChildMostDiskBacking)
80
- else
81
- relocateSpec = VIM.VirtualMachineRelocateSpec
82
- end
83
-
84
- spec = VIM.VirtualMachineCloneSpec(:location => relocateSpec,
85
- :powerOn => false,
86
- :template => false)
87
-
88
- vm.CloneVM_Task(:folder => vm.parent, :name => vm_target, :spec => spec).wait_for_completion
@@ -1,97 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright (c) 2011-2017 VMware, Inc. All Rights Reserved.
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'optimist'
7
- require 'rbvmomi'
8
- require 'rbvmomi/optimist'
9
-
10
- VIM = RbVmomi::VIM
11
-
12
- opts = Optimist.options do
13
- banner <<-EOS
14
- Create a VM.
15
-
16
- Usage:
17
- create_vm-1.9.rb [options]
18
-
19
- VIM connection options:
20
- EOS
21
-
22
- rbvmomi_connection_opts
23
-
24
- text <<-EOS
25
-
26
- VM location options:
27
- EOS
28
-
29
- rbvmomi_datacenter_opt
30
-
31
- text <<-EOS
32
-
33
- Other options:
34
- EOS
35
- end
36
-
37
- Optimist.die("must specify host") unless opts[:host]
38
- vm_name = ARGV[0] or abort "must specify VM name"
39
-
40
- vim = VIM.connect opts
41
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
42
- vmFolder = dc.vmFolder
43
- hosts = dc.hostFolder.children
44
- rp = hosts.first.resourcePool
45
-
46
- vm_cfg = {
47
- name: vm_name,
48
- guestId: 'otherGuest',
49
- files: { vmPathName: '[datastore1]' },
50
- numCPUs: 1,
51
- memoryMB: 128,
52
- deviceChange: [
53
- {
54
- operation: :add,
55
- device: VIM.VirtualLsiLogicController(
56
- key: 1000,
57
- busNumber: 0,
58
- sharedBus: :noSharing,
59
- )
60
- }, {
61
- operation: :add,
62
- fileOperation: :create,
63
- device: VIM.VirtualDisk(
64
- key: 0,
65
- backing: VIM.VirtualDiskFlatVer2BackingInfo(
66
- fileName: '[datastore1]',
67
- diskMode: :persistent,
68
- thinProvisioned: true,
69
- ),
70
- controllerKey: 1000,
71
- unitNumber: 0,
72
- capacityInKB: 4000000,
73
- )
74
- }, {
75
- operation: :add,
76
- device: VIM.VirtualE1000(
77
- key: 0,
78
- deviceInfo: {
79
- label: 'Network Adapter 1',
80
- summary: 'VM Network',
81
- },
82
- backing: VIM.VirtualEthernetCardNetworkBackingInfo(
83
- deviceName: 'VM Network',
84
- ),
85
- addressType: 'generated'
86
- )
87
- }
88
- ],
89
- extraConfig: [
90
- {
91
- key: 'bios.bootOrder',
92
- value: 'ethernet0'
93
- }
94
- ]
95
- }
96
-
97
- vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp).wait_for_completion
@@ -1,97 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright (c) 2011-2017 VMware, Inc. All Rights Reserved.
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'optimist'
7
- require 'rbvmomi'
8
- require 'rbvmomi/optimist'
9
-
10
- VIM = RbVmomi::VIM
11
-
12
- opts = Optimist.options do
13
- banner <<-EOS
14
- Create a VM.
15
-
16
- Usage:
17
- create_vm.rb [options]
18
-
19
- VIM connection options:
20
- EOS
21
-
22
- rbvmomi_connection_opts
23
-
24
- text <<-EOS
25
-
26
- VM location options:
27
- EOS
28
-
29
- rbvmomi_datacenter_opt
30
-
31
- text <<-EOS
32
-
33
- Other options:
34
- EOS
35
- end
36
-
37
- Optimist.die("must specify host") unless opts[:host]
38
- vm_name = ARGV[0] or abort "must specify VM name"
39
-
40
- vim = VIM.connect opts
41
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
42
- vmFolder = dc.vmFolder
43
- hosts = dc.hostFolder.children
44
- rp = hosts.first.resourcePool
45
-
46
- vm_cfg = {
47
- :name => vm_name,
48
- :guestId => 'otherGuest',
49
- :files => { :vmPathName => '[datastore1]' },
50
- :numCPUs => 1,
51
- :memoryMB => 128,
52
- :deviceChange => [
53
- {
54
- :operation => :add,
55
- :device => VIM.VirtualLsiLogicController(
56
- :key => 1000,
57
- :busNumber => 0,
58
- :sharedBus => :noSharing
59
- )
60
- }, {
61
- :operation => :add,
62
- :fileOperation => :create,
63
- :device => VIM.VirtualDisk(
64
- :key => 0,
65
- :backing => VIM.VirtualDiskFlatVer2BackingInfo(
66
- :fileName => '[datastore1]',
67
- :diskMode => :persistent,
68
- :thinProvisioned => true
69
- ),
70
- :controllerKey => 1000,
71
- :unitNumber => 0,
72
- :capacityInKB => 4000000
73
- )
74
- }, {
75
- :operation => :add,
76
- :device => VIM.VirtualE1000(
77
- :key => 0,
78
- :deviceInfo => {
79
- :label => 'Network Adapter 1',
80
- :summary => 'VM Network'
81
- },
82
- :backing => VIM.VirtualEthernetCardNetworkBackingInfo(
83
- :deviceName => 'VM Network'
84
- ),
85
- :addressType => 'generated'
86
- )
87
- }
88
- ],
89
- :extraConfig => [
90
- {
91
- :key => 'bios.bootOrder',
92
- :value => 'ethernet0'
93
- }
94
- ]
95
- }
96
-
97
- vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp).wait_for_completion
@@ -1,57 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright (c) 2011-2017 VMware, Inc. All Rights Reserved.
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'optimist'
7
- require 'rbvmomi'
8
- require 'rbvmomi/optimist'
9
-
10
- VIM = RbVmomi::VIM
11
-
12
- opts = Optimist.options do
13
- banner <<-EOS
14
- Delete a disk from a VM.
15
-
16
- Usage:
17
- delete_disk_from_vm.rb [options] vm_name disk_unit_number
18
-
19
- VIM connection options:
20
- EOS
21
-
22
- rbvmomi_connection_opts
23
-
24
- text <<-EOS
25
-
26
- VM location options:
27
- EOS
28
-
29
- rbvmomi_datacenter_opt
30
- end
31
-
32
- Optimist.die("must specify host") unless opts[:host]
33
- ARGV.size == 2 or abort "must specify VM name and disk unit number"
34
- vm_name = ARGV[0]
35
- disk_unit_number = ARGV[1].to_i
36
-
37
- vim = VIM.connect opts
38
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
39
- vm = dc.find_vm(vm_name) or abort "VM not found"
40
-
41
- disk = vm.config.hardware.device.detect do |device|
42
- device.kind_of?(VIM::VirtualDisk) && device.unitNumber == disk_unit_number
43
- end
44
-
45
- raise "Disk #{disk_unit_number} not found" if disk.nil?
46
-
47
- spec = VIM::VirtualMachineConfigSpec(
48
- :deviceChange => [
49
- VIM::VirtualDeviceConfigSpec(
50
- :device => disk,
51
- :fileOperation => VIM.VirtualDeviceConfigSpecFileOperation(:destroy),
52
- :operation => VIM::VirtualDeviceConfigSpecOperation(:remove)
53
- )
54
- ]
55
- )
56
-
57
- vm.ReconfigVM_Task(:spec => spec).wait_for_completion
@@ -1,57 +0,0 @@
1
- # Copyright (c) 2011-2017 VMware, Inc. All Rights Reserved.
2
- # SPDX-License-Identifier: MIT
3
-
4
- require 'optimist'
5
- require 'rbvmomi'
6
- require 'rbvmomi/optimist'
7
-
8
- VIM = RbVmomi::VIM
9
- CMDS = %w(list set)
10
-
11
- opts = Optimist.options do
12
- banner <<-EOS
13
- View and modify VM extraConfig options.
14
-
15
- Usage:
16
- extraConfig.rb [options] VM list
17
- extraConfig.rb [options] VM set key=value [key=value...]
18
-
19
- Commands: #{CMDS * ' '}
20
-
21
- VIM connection options:
22
- EOS
23
-
24
- rbvmomi_connection_opts
25
-
26
- text <<-EOS
27
-
28
- VM location options:
29
- EOS
30
-
31
- rbvmomi_datacenter_opt
32
-
33
- text <<-EOS
34
-
35
- Other options:
36
- EOS
37
-
38
- stop_on CMDS
39
- end
40
-
41
- vm_name = ARGV[0] or Optimist.die("no VM name given")
42
- cmd = ARGV[1] or Optimist.die("no command given")
43
- abort "invalid command" unless CMDS.member? cmd
44
- Optimist.die("must specify host") unless opts[:host]
45
-
46
- vim = VIM.connect opts
47
-
48
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
49
- vm = dc.find_vm(vm_name) or abort "VM not found"
50
-
51
- case cmd
52
- when 'list'
53
- vm.config.extraConfig.each { |x| puts "#{x.key}: #{x.value}" }
54
- when 'set'
55
- extraConfig = ARGV[2..-1].map { |x| x.split("=", 2) }.map { |k,v| { :key => k, :value => v } }
56
- vm.ReconfigVM_Task(:spec => VIM.VirtualMachineConfigSpec(:extraConfig => extraConfig)).wait_for_completion
57
- end
@@ -1,106 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Copyright (c) 2012-2017 VMware, Inc. All Rights Reserved.
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'optimist'
7
- require 'rbvmomi'
8
- require 'rbvmomi/optimist'
9
- require 'rbvmomi/utils/leases'
10
- require 'yaml'
11
-
12
- VIM = RbVmomi::VIM
13
- CMDS = ['set_lease_on_leaseless_vms', 'show_expired_vms',
14
- 'show_soon_expired_vms', 'kill_expired_vms']
15
-
16
- opts = Optimist.options do
17
- banner <<-EOS
18
- Tool for managing leases on VMs where leases are stored in YAML on VM annotations.
19
-
20
- Usage:
21
- lease_tool.rb [options] <cmd>
22
-
23
- Commands: #{CMDS * ' '}
24
-
25
- VIM connection options:
26
- EOS
27
-
28
- rbvmomi_connection_opts
29
-
30
- text <<-EOS
31
-
32
- VM location options:
33
- EOS
34
-
35
- rbvmomi_datacenter_opt
36
-
37
- text <<-EOS
38
-
39
- Other options:
40
- EOS
41
-
42
- opt :vm_folder_path, "Path to VM folder to deploy VM into", :type => :string
43
- opt :force, "Really perform VMs. Used with kill_expired_vms"
44
-
45
- stop_on CMDS
46
- end
47
-
48
- Optimist.die("must specify host") unless opts[:host]
49
- cmd = ARGV[0] or Optimist.die("no command given")
50
- Optimist.die("no vm folder path given") unless opts[:vm_folder_path]
51
-
52
- vim = VIM.connect opts
53
- dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
54
-
55
- root_vm_folder = dc.vmFolder
56
- vm_folder = root_vm_folder.traverse(opts[:vm_folder_path], VIM::Folder)
57
-
58
- lease_tool = LeaseTool.new
59
- vms_props_list = (['runtime.powerState'] + lease_tool.vms_props_list).uniq
60
- inventory = vm_folder.inventory_flat('VirtualMachine' => vms_props_list)
61
- inventory = inventory.select{|obj, props| obj.is_a?(VIM::VirtualMachine)}
62
- case cmd
63
- when 'set_lease_on_leaseless_vms'
64
- lease_tool.set_lease_on_leaseless_vms(
65
- inventory.keys, inventory,
66
- :lease_minutes => 3 * 24 * 60 * 60 # 3 days
67
- )
68
- when 'show_expired_vms'
69
- vms = lease_tool.filter_expired_vms inventory.keys, inventory
70
- vms.each do |vm, time_to_expiration|
71
- puts "VM '#{inventory[vm]['name']}' is expired"
72
- end
73
- when 'kill_expired_vms'
74
- vms = lease_tool.filter_expired_vms inventory.keys, inventory
75
- vms.each do |vm, time_to_expiration|
76
- puts "VM '#{inventory[vm]['name']}' is expired"
77
- if !opts[:force]
78
- puts "NOT killing VM '#{inventory[vm]['name']}' because --force not set"
79
- else
80
- puts "Killing expired VM '#{inventory[vm]['name']}'"
81
- # Destroying VMs is very stressful for vCenter, and we aren't in a rush
82
- # so do one VM at a time
83
- if inventory[vm]['runtime.powerState'] == 'poweredOn'
84
- vm.PowerOffVM_Task.wait_for_completion
85
- end
86
- vm.Destroy_Task.wait_for_completion
87
- end
88
- end
89
- when 'show_soon_expired_vms'
90
- vms = lease_tool.filter_expired_vms(
91
- inventory.keys, inventory,
92
- :time_delta => 3.5 * 24 * 60 * 60, # 3.5 days
93
- )
94
- # We could send the user emails here, but for this example, just print the
95
- # VMs that will expire within the next 3.5 days
96
- vms.each do |vm, time_to_expiration|
97
- if time_to_expiration > 0
98
- hours_to_expiration = time_to_expiration / (60.0 * 60.0)
99
- puts "VM '%s' expires in %.2fh" % [inventory[vm]['name'], hours_to_expiration]
100
- else
101
- puts "VM '#{inventory[vm]['name']}' is expired"
102
- end
103
- end
104
- else
105
- abort "invalid command"
106
- end