knife-vsphere 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,47 @@
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
- dcname = get_config(:vsphere_dc)
38
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
39
- folder = find_folder(get_config(:folder)) || dc.vmFolder
40
-
41
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
42
- 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
+ # 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 = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
41
+ abort "VM #{vmname} not found"
42
+
43
+ properties = {}
44
+ properties[property_name] = property_value
45
+ vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(properties)).wait_for_completion
46
+ end
47
+ end
@@ -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/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 = 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/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 = 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,67 +1,66 @@
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
- dcname = get_config(:vsphere_dc)
49
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
50
- folder = find_folder(get_config(:folder)) || dc.vmFolder
51
-
52
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
53
- abort "VM #{vmname} not found"
54
-
55
- gom = vim.serviceContent.guestOperationsManager
56
-
57
- guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
58
- :username => config[:exec_user],
59
- :password => config[:exec_passwd])
60
- prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
61
- :arguments => args,
62
- :workingDirectory => get_config(:exec_dir))
63
-
64
- gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
65
-
66
- end
67
- 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
+ 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,66 +1,66 @@
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 down through sub-folders"
19
-
20
- option :only_folders,
21
- :long => "--only-folders",
22
- :description => "Print only sub-folders"
23
-
24
- def traverse_folders(folder)
25
- puts "#{ui.color("Folder", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
26
- print_vms_in_folder(folder) unless get_config(:only_folders)
27
- folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
28
- folders.each do |child|
29
- traverse_folders(child)
30
- end
31
- end
32
-
33
- def print_vms_in_folder(folder)
34
- vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine)
35
- vms.each do |vm|
36
- state = case vm.runtime.powerState
37
- when PsOn
38
- ui.color("on", :green)
39
- when PsOff
40
- ui.color("off", :red)
41
- when PsSuspended
42
- ui.color("suspended", :yellow)
43
- end
44
- puts "#{ui.color("VM Name:", :cyan)} #{vm.name}\t#{ui.color("IP:", :magenta)} #{vm.guest.ipAddress}\t#{ui.color("RAM:", :magenta)} #{vm.summary.config.memorySizeMB}\t#{ui.color("State:", :cyan)} #{state}"
45
- end
46
- end
47
-
48
- def print_subfolders(folder)
49
- folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
50
- folders.each do |subfolder|
51
- puts "#{ui.color("Folder Name", :cyan)}: #{subfolder.name}"
52
- end
53
- end
54
-
55
- def run
56
- $stdout.sync = true
57
- vim = get_vim_connection
58
- baseFolder = find_folder(get_config(:folder));
59
- if get_config(:recursive)
60
- traverse_folders(baseFolder)
61
- else
62
- print_subfolders(baseFolder)
63
- print_vms_in_folder(baseFolder)
64
- end
65
- end
66
- 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
+
11
+ banner "knife vsphere vm list"
12
+
13
+ get_common_options
14
+
15
+ option :recursive,
16
+ :long => "--recursive",
17
+ :short => "-r",
18
+ :description => "Recurse down through sub-folders"
19
+
20
+ option :only_folders,
21
+ :long => "--only-folders",
22
+ :description => "Print only sub-folders"
23
+
24
+ def traverse_folders(folder)
25
+ puts "#{ui.color("Folder", :cyan)}: "+(folder.path[3..-1].map { |x| x[1] }.* '/')
26
+ print_vms_in_folder(folder) unless get_config(:only_folders)
27
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
28
+ folders.each do |child|
29
+ traverse_folders(child)
30
+ end
31
+ end
32
+
33
+ def print_vms_in_folder(folder)
34
+ vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine)
35
+ vms.each do |vm|
36
+ state = case vm.runtime.powerState
37
+ when PsOn
38
+ ui.color("on", :green)
39
+ when PsOff
40
+ ui.color("off", :red)
41
+ when PsSuspended
42
+ ui.color("suspended", :yellow)
43
+ end
44
+ puts "#{ui.color("VM Name:", :cyan)} #{vm.name}\t#{ui.color("IP:", :magenta)} #{vm.guest.ipAddress}\t#{ui.color("RAM:", :magenta)} #{vm.summary.config.memorySizeMB}\t#{ui.color("State:", :cyan)} #{state}"
45
+ end
46
+ end
47
+
48
+ def print_subfolders(folder)
49
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
50
+ folders.each do |subfolder|
51
+ puts "#{ui.color("Folder Name", :cyan)}: #{subfolder.name}"
52
+ end
53
+ end
54
+
55
+ def run
56
+ $stdout.sync = true
57
+ vim = get_vim_connection
58
+ baseFolder = find_folder(get_config(:folder));
59
+ if get_config(:recursive)
60
+ traverse_folders(baseFolder)
61
+ else
62
+ print_subfolders(baseFolder)
63
+ print_vms_in_folder(baseFolder)
64
+ end
65
+ end
66
+ end