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
@@ -1,46 +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::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
10
- banner "knife vsphere vm config VMNAME PROPERTY VALUE. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\" for allowed ATTRIBUTE values (any property of type xs:string is supported)."
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
- property_name = @name_args[1]
23
- if property_name.nil?
24
- show_usage
25
- fatal_exit("You must specify a PROPERTY name (e.g. annotation)")
26
- end
27
- property_name = property_name.to_sym
28
-
29
- property_value = @name_args[2]
30
- if property_value.nil?
31
- show_usage
32
- fatal_exit("You must specify a PROPERTY value")
33
- end
34
-
35
- vim = get_vim_connection
36
-
37
- dc = get_datacenter
38
- folder = find_folder(get_config(:folder)) || dc.vmFolder
39
-
40
- vm = traverse_folders_for_vm(folder, vmname) or abort "VM #{vmname} not found"
41
-
42
- properties = {}
43
- properties[property_name] = property_value
44
- vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(properties)).wait_for_completion
45
- end
46
- 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::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
10
+ banner "knife vsphere vm config VMNAME PROPERTY VALUE.
11
+ See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\"
12
+ for allowed ATTRIBUTE values (any property of type xs:string is supported)."
13
+
14
+ common_options
15
+
16
+ def run
17
+ $stdout.sync = true
18
+ vmname = @name_args[0]
19
+ if vmname.nil?
20
+ show_usage
21
+ fatal_exit('You must specify a virtual machine name')
22
+ end
23
+
24
+ property_name = @name_args[1]
25
+ if property_name.nil?
26
+ show_usage
27
+ fatal_exit('You must specify a PROPERTY name (e.g. annotation)')
28
+ end
29
+ property_name = property_name.to_sym
30
+
31
+ property_value = @name_args[2]
32
+ if property_value.nil?
33
+ show_usage
34
+ fatal_exit('You must specify a PROPERTY value')
35
+ end
36
+
37
+ vim_connection
38
+
39
+ dc = datacenter
40
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
41
+
42
+ vm = traverse_folders_for_vm(folder, vmname) || abort("VM #{vmname} not found")
43
+
44
+ properties = {}
45
+ properties[property_name] = property_value
46
+ vm.ReconfigVM_Task(spec: RbVmomi::VIM.VirtualMachineConfigSpec(properties)).wait_for_completion
47
+ end
48
+ end
@@ -1,66 +1,70 @@
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/base_vsphere_command'
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 = traverse_folders_for_vm(baseFolder, 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/base_vsphere_command'
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
+ banner 'knife vsphere vm delete VMNAME (options)'
17
+
18
+ option :purge,
19
+ short: '-P',
20
+ long: '--purge',
21
+ boolean: true,
22
+ description: 'Destroy corresponding node and client on the Chef Server, in addition to destroying the VM itself.'
23
+
24
+ option :chef_node_name,
25
+ short: '-N NAME',
26
+ long: '--node-name NAME',
27
+ description: 'Use this option if the Chef node name is different from the VM name'
28
+
29
+ common_options
30
+
31
+ # Extracted from Chef::Knife.delete_object, because it has a
32
+ # confirmation step built in... By specifying the '--purge'
33
+ # flag (and also explicitly confirming the server destruction!)
34
+ # the user is already making their intent known. It is not
35
+ # necessary to make them confirm two more times.
36
+ def destroy_item(itemClass, name, type_name)
37
+ object = itemClass.load(name)
38
+ object.destroy
39
+ puts "Deleted #{type_name} #{name}"
40
+ end
41
+
42
+ def run
43
+ $stdout.sync = true
44
+
45
+ vmname = @name_args[0]
46
+
47
+ if vmname.nil?
48
+ show_usage
49
+ fatal_exit('You must specify a virtual machine name')
50
+ end
51
+
52
+ vim_connection
53
+
54
+ base_folder = find_folder(get_config(:folder))
55
+
56
+ vm = traverse_folders_for_vm(base_folder, vmname) || fatal_exit("VM #{vmname} not found")
57
+
58
+ vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == 'poweredOff'
59
+ vm.Destroy_Task.wait_for_completion
60
+ puts "Deleted virtual machine #{vmname}"
61
+
62
+ if config[:purge]
63
+ vmname = config[:chef_node_name] if config[:chef_node_name]
64
+ destroy_item(Chef::Node, vmname, 'node')
65
+ destroy_item(Chef::ApiClient, vmname, 'client')
66
+ else
67
+ puts "Corresponding node and client for the #{vmname} server were not deleted and remain registered with the Chef Server"
68
+ end
69
+ end
70
+ end
@@ -1,66 +1,62 @@
1
- # Author:: Ian Delahorne (<ian@delahorne.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::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
10
- banner "knife vsphere vm execute VMNAME COMMAND ARGS"
11
-
12
- option :exec_user,
13
- :long => "--exec-user USER",
14
- :description => "User to execute as",
15
- :required => true
16
-
17
- option :exec_passwd,
18
- :long => "--exec-passwd PASSWORD",
19
- :description => "Password for execute user",
20
- :required => true
21
-
22
- option :exec_dir,
23
- :long => "--exec-dir DIRECTORY",
24
- :description => "Working directory to execute in"
25
-
26
- get_common_options
27
-
28
- def run
29
- $stdout.sync = true
30
- vmname = @name_args[0]
31
- if vmname.nil?
32
- show_usage
33
- fatal_exit("You must specify a virtual machine name")
34
- end
35
- command = @name_args[1]
36
- if command.nil?
37
- show_usage
38
- fatal_exit("You must specify a command to execute")
39
- end
40
-
41
- args = @name_args[2]
42
- if args.nil?
43
- args = ""
44
- end
45
-
46
- vim = get_vim_connection
47
-
48
- dc = get_datacenter
49
- folder = find_folder(get_config(:folder)) || dc.vmFolder
50
-
51
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
52
- abort "VM #{vmname} not found"
53
-
54
- gom = vim.serviceContent.guestOperationsManager
55
-
56
- guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
57
- :username => config[:exec_user],
58
- :password => config[:exec_passwd])
59
- prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
60
- :arguments => args,
61
- :workingDirectory => get_config(:exec_dir))
62
-
63
- gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
64
-
65
- end
66
- end
1
+ # Author:: Ian Delahorne (<ian@delahorne.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::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
10
+ banner 'knife vsphere vm execute VMNAME COMMAND ARGS'
11
+
12
+ option :exec_user,
13
+ long: '--exec-user USER',
14
+ description: 'User to execute as',
15
+ required: true
16
+
17
+ option :exec_passwd,
18
+ long: '--exec-passwd PASSWORD',
19
+ description: 'Password for execute user',
20
+ required: true
21
+
22
+ option :exec_dir,
23
+ long: '--exec-dir DIRECTORY',
24
+ description: 'Working directory to execute in'
25
+
26
+ common_options
27
+
28
+ def run
29
+ $stdout.sync = true
30
+ vmname = @name_args[0]
31
+ if vmname.nil?
32
+ show_usage
33
+ fatal_exit('You must specify a virtual machine name')
34
+ end
35
+ command = @name_args[1]
36
+ if command.nil?
37
+ show_usage
38
+ fatal_exit('You must specify a command to execute')
39
+ end
40
+
41
+ args = @name_args[2]
42
+ args = '' if args.nil?
43
+
44
+ vim = vim_connection
45
+
46
+ dc = datacenter
47
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
48
+
49
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
50
+
51
+ gom = vim.serviceContent.guestOperationsManager
52
+
53
+ guest_auth = RbVmomi::VIM::NamePasswordAuthentication(interactiveSession: false,
54
+ username: config[:exec_user],
55
+ password: config[:exec_passwd])
56
+ prog_spec = RbVmomi::VIM::GuestProgramSpec(programPath: command,
57
+ arguments: args,
58
+ workingDirectory: get_config(:exec_dir))
59
+
60
+ gom.processManager.StartProgramInGuest(vm: vm, auth: guest_auth, spec: prog_spec)
61
+ end
62
+ end
@@ -1,61 +1,57 @@
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 known virtual machines in the configured datacenter
9
- class Chef::Knife::VsphereVmList < Chef::Knife::BaseVsphereCommand
10
-
11
- banner "knife vsphere vm list"
12
-
13
- get_common_options
14
-
15
- option :recursive,
16
- :long => "--recursive",
17
- :short => "-r",
18
- :description => "Recurse into sub-folders"
19
-
20
- def traverse_folders(folder, is_top = false, recurse = false)
21
-
22
- vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine).select {|v| v.config && !v.config.template }
23
- if vms.any?
24
- puts "#{ui.color("Folder", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
25
- vms.each { |v| print_vm(v) }
26
- elsif is_top
27
- puts "#{ui.color("No VMs", :cyan)}"
28
- end
29
-
30
- if (recurse)
31
- folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
32
- folders.each do |child|
33
- traverse_folders(child, false, recurse)
34
- end
35
- end
36
-
37
- end
38
-
39
- def print_vm(vm)
40
- state = case vm.runtime.powerState
41
- when PsOn
42
- ui.color("on", :green)
43
- when PsOff
44
- ui.color("off", :red)
45
- when PsSuspended
46
- ui.color("suspended", :yellow)
47
- end
48
- puts "\t#{ui.color("VM Name:", :cyan)} #{vm.name}"
49
- "\t\t#{ui.color("IP:", :magenta)} #{vm.guest.ipAddress}"
50
- "\t\t#{ui.color("RAM:", :magenta)} #{vm.summary.config.memorySizeMB}"
51
- "\t\t#{ui.color("State:", :cyan)} #{state}"
52
- end
53
-
54
- def run
55
- vim = get_vim_connection
56
- baseFolder = find_folder(get_config(:folder));
57
- recurse = get_config(:recursive)
58
- is_top = true
59
- traverse_folders(baseFolder, is_top, recurse)
60
- end
61
- end
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 known virtual machines in the configured datacenter
9
+ class Chef::Knife::VsphereVmList < Chef::Knife::BaseVsphereCommand
10
+ banner 'knife vsphere vm list'
11
+
12
+ common_options
13
+
14
+ option :recursive,
15
+ long: '--recursive',
16
+ short: '-r',
17
+ description: 'Recurse into sub-folders'
18
+
19
+ def traverse_folders(folder, is_top = false, recurse = false)
20
+ vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine).select { |v| v.config && !v.config.template }
21
+ if vms.any?
22
+ puts "#{ui.color('Folder', :cyan)}: " + (folder.path[3..-1].map { |x| x[1] }.* '/')
23
+ vms.each { |v| print_vm(v) }
24
+ elsif is_top
25
+ puts "#{ui.color('No VMs', :cyan)}"
26
+ end
27
+
28
+ return unless recurse
29
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
30
+ folders.each do |child|
31
+ traverse_folders(child, false, recurse)
32
+ end
33
+ end
34
+
35
+ def print_vm(vm)
36
+ state = case vm.runtime.powerState
37
+ when PS_ON
38
+ ui.color('on', :green)
39
+ when PS_OFF
40
+ ui.color('off', :red)
41
+ when PS_SUSPENDED
42
+ ui.color('suspended', :yellow)
43
+ end
44
+ puts "\t#{ui.color('VM Name:', :cyan)} #{vm.name}"
45
+ puts "\t\t#{ui.color('IP:', :magenta)} #{vm.guest.ipAddress}"
46
+ puts "\t\t#{ui.color('RAM:', :magenta)} #{vm.summary.config.memorySizeMB}"
47
+ puts "\t\t#{ui.color('State:', :magenta)} #{state}"
48
+ end
49
+
50
+ def run
51
+ vim_connection
52
+ base_folder = find_folder(get_config(:folder))
53
+ recurse = get_config(:recursive)
54
+ is_top = true
55
+ traverse_folders(base_folder, is_top, recurse)
56
+ end
57
+ end