knife-vsphere 0.9.0 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +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
+ 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
@@ -4,7 +4,7 @@
4
4
  #
5
5
 
6
6
  require 'chef/knife'
7
- require 'chef/knife/BaseVsphereCommand'
7
+ require 'chef/knife/base_vsphere_command'
8
8
  require 'rbvmomi'
9
9
 
10
10
  # These two are needed for the '--purge' deletion case
@@ -14,53 +14,53 @@ require 'chef/api_client'
14
14
  # Delete a virtual machine from vCenter
15
15
  class Chef::Knife::VsphereVmDelete < Chef::Knife::BaseVsphereCommand
16
16
 
17
- banner "knife vsphere vm delete VMNAME"
17
+ banner "knife vsphere vm delete VMNAME"
18
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."
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
24
 
25
- get_common_options
25
+ get_common_options
26
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
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
37
 
38
- def run
39
- $stdout.sync = true
38
+ def run
39
+ $stdout.sync = true
40
40
 
41
- vmname = @name_args[0]
41
+ vmname = @name_args[0]
42
42
 
43
- if vmname.nil?
44
- show_usage
45
- fatal_exit("You must specify a virtual machine name")
46
- end
43
+ if vmname.nil?
44
+ show_usage
45
+ fatal_exit("You must specify a virtual machine name")
46
+ end
47
47
 
48
- vim = get_vim_connection
48
+ vim = get_vim_connection
49
49
 
50
- baseFolder = find_folder(get_config(:folder));
50
+ baseFolder = find_folder(get_config(:folder));
51
51
 
52
- vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
53
- fatal_exit("VM #{vmname} not found")
52
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
53
+ fatal_exit("VM #{vmname} not found")
54
54
 
55
- vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
56
- vm.Destroy_Task
57
- puts "Deleted virtual machine #{vmname}"
55
+ vm.PowerOffVM_Task.wait_for_completion unless vm.runtime.powerState == "poweredOff"
56
+ vm.Destroy_Task
57
+ puts "Deleted virtual machine #{vmname}"
58
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
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
66
  end
@@ -1,68 +1,67 @@
1
-
2
- # Author:: Ian Delahorne (<ian@delahorne.com>)
3
- # License:: Apache License, Version 2.0
4
-
5
- require 'chef/knife'
6
- require 'chef/knife/BaseVsphereCommand'
7
- require 'rbvmomi'
8
- require 'netaddr'
9
-
10
- class Chef::Knife::VsphereVmExecute < Chef::Knife::BaseVsphereCommand
11
- banner "knife vsphere vm execute VMNAME COMMAND ARGS"
12
-
13
- option :exec_user,
14
- :long => "--exec-user USER",
15
- :description => "User to execute as",
16
- :required => true
17
-
18
- option :exec_passwd,
19
- :long => "--exec-passwd PASSWORD",
20
- :description => "Password for execute user",
21
- :required => true
22
-
23
- option :exec_dir,
24
- :long => "--exec-dir DIRECTORY",
25
- :description => "Working directory to execute in"
26
-
27
- get_common_options
28
-
29
- def run
30
- $stdout.sync = true
31
- vmname = @name_args[0]
32
- if vmname.nil?
33
- show_usage
34
- fatal_exit("You must specify a virtual machine name")
35
- end
36
- command = @name_args[1]
37
- if command.nil?
38
- show_usage
39
- fatal_exit("You must specify a command to execute")
40
- end
41
-
42
- args = @name_args[2]
43
- if args.nil?
44
- args = ""
45
- end
46
-
47
- vim = get_vim_connection
48
-
49
- dcname = get_config(:vsphere_dc)
50
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
51
- folder = find_folder(get_config(:folder)) || dc.vmFolder
52
-
53
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
54
- abort "VM #{vmname} not found"
55
-
56
- gom = vim.serviceContent.guestOperationsManager
57
-
58
- guest_auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false,
59
- :username => config[:exec_user],
60
- :password => config[:exec_passwd])
61
- prog_spec = RbVmomi::VIM::GuestProgramSpec(:programPath => command,
62
- :arguments => args,
63
- :workingDirectory => get_config(:exec_dir))
64
-
65
- gom.processManager.StartProgramInGuest(:vm => vm, :auth => guest_auth, :spec => prog_spec)
66
-
67
- end
68
- 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
+ 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
@@ -3,64 +3,64 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
  require 'chef/knife'
6
- require 'chef/knife/BaseVsphereCommand'
6
+ require 'chef/knife/base_vsphere_command'
7
7
 
8
8
  # Lists all known virtual machines in the configured datacenter
9
9
  class Chef::Knife::VsphereVmList < Chef::Knife::BaseVsphereCommand
10
10
 
11
- banner "knife vsphere vm list"
11
+ banner "knife vsphere vm list"
12
12
 
13
- get_common_options
13
+ get_common_options
14
14
 
15
- option :recursive,
16
- :long => "--recursive",
17
- :short => "-r",
18
- :description => "Recurse down through sub-folders"
15
+ option :recursive,
16
+ :long => "--recursive",
17
+ :short => "-r",
18
+ :description => "Recurse down through sub-folders"
19
19
 
20
- option :only_folders,
21
- :long => "--only-folders",
22
- :description => "Print only sub-folders"
20
+ option :only_folders,
21
+ :long => "--only-folders",
22
+ :description => "Print only sub-folders"
23
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
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
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
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
47
 
48
- def print_subfolders(folder)
49
- folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
48
+ def print_subfolders(folder)
49
+ folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
50
50
  folders.each do |subfolder|
51
51
  puts "#{ui.color("Folder Name", :cyan)}: #{subfolder.name}"
52
52
  end
53
- end
53
+ end
54
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
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
66
  end
@@ -1,9 +1,8 @@
1
-
2
- # Author:: Ian Delahorne (<ian@delahorne.com>)
1
+ # Author:: Brian Dupras (<bdupras@rallydev.com>)
3
2
  # License:: Apache License, Version 2.0
4
3
 
5
4
  require 'chef/knife'
6
- require 'chef/knife/BaseVsphereCommand'
5
+ require 'chef/knife/base_vsphere_command'
7
6
  require 'rbvmomi'
8
7
  require 'netaddr'
9
8
 
@@ -33,7 +32,7 @@ class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
33
32
  folder = find_folder(get_config(:folder)) || dc.vmFolder
34
33
 
35
34
  vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
36
- abort "VM #{vmname} not found"
35
+ abort "VM #{vmname} not found"
37
36
 
38
37
  # split QUERY by dots, and walk the object model
39
38
  query = query_string.split '.'
@@ -3,7 +3,7 @@
3
3
  #
4
4
 
5
5
  require 'chef/knife'
6
- require 'chef/knife/BaseVsphereCommand'
6
+ require 'chef/knife/base_vsphere_command'
7
7
  require 'rbvmomi'
8
8
  require 'netaddr'
9
9
 
@@ -16,33 +16,33 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
16
16
  get_common_options
17
17
 
18
18
  option :list,
19
- :long => "--list",
20
- :description => "The current tree of snapshots"
19
+ :long => "--list",
20
+ :description => "The current tree of snapshots"
21
21
 
22
22
  option :create_new_snapshot,
23
- :long => "--create SNAPSHOT",
24
- :description => "Create a new snapshot off of the current snapshot."
23
+ :long => "--create SNAPSHOT",
24
+ :description => "Create a new snapshot off of the current snapshot."
25
25
 
26
26
  option :remove_named_snapshot,
27
- :long => "--remove SNAPSHOT",
28
- :description => "Remove a named snapshot."
27
+ :long => "--remove SNAPSHOT",
28
+ :description => "Remove a named snapshot."
29
29
 
30
30
  option :revert_snapshot,
31
- :long => "--revert SNAPSHOT",
32
- :description => "Revert to a named snapshot."
31
+ :long => "--revert SNAPSHOT",
32
+ :description => "Revert to a named snapshot."
33
33
 
34
34
  option :revert_current_snapshot,
35
- :long => "--revert-current",
36
- :description => "Revert to current snapshot.",
37
- :boolean => false
35
+ :long => "--revert-current",
36
+ :description => "Revert to current snapshot.",
37
+ :boolean => false
38
38
 
39
39
  option :power,
40
- :long => "--start",
41
- :description => "Indicates whether to start the VM after a successful revert",
42
- :boolean => false
40
+ :long => "--start",
41
+ :description => "Indicates whether to start the VM after a successful revert",
42
+ :boolean => false
43
43
 
44
44
  def run
45
-
45
+
46
46
  $stdout.sync = true
47
47
 
48
48
  vmname = @name_args[0]
@@ -51,13 +51,13 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
51
51
  ui.fatal("You must specify a virtual machine name")
52
52
  exit 1
53
53
  end
54
-
54
+
55
55
  vim = get_vim_connection
56
56
 
57
57
  baseFolder = find_folder(get_config(:folder));
58
58
 
59
59
  vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
60
- abort "VM #{vmname} not found"
60
+ abort "VM #{vmname} not found"
61
61
 
62
62
  if vm.snapshot
63
63
  snapshot_list = vm.snapshot.rootSnapshotList
@@ -67,7 +67,7 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
67
67
  if config[:list] && vm.snapshot
68
68
  puts "Current snapshot tree: "
69
69
  puts "#{vmname}"
70
- snapshot_list.each{|i| puts display_node(i,current_snapshot)}
70
+ snapshot_list.each { |i| puts display_node(i, current_snapshot) }
71
71
  end
72
72
 
73
73
  if config[:create_new_snapshot]
@@ -75,55 +75,55 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
75
75
  end
76
76
 
77
77
  if config[:remove_named_snapshot]
78
- ss_name = config[:remove_named_snapshot]
79
- snapshot = find_node(snapshot_list,ss_name)
80
- puts "Found snapshot #{ss_name} removing."
81
- snapshot.RemoveSnapshot_Task(:removeChildren => false)
78
+ ss_name = config[:remove_named_snapshot]
79
+ snapshot = find_node(snapshot_list, ss_name)
80
+ puts "Found snapshot #{ss_name} removing."
81
+ snapshot.RemoveSnapshot_Task(:removeChildren => false)
82
82
  end
83
83
 
84
84
  if config[:revert_current_snapshot]
85
- puts "Reverting to Current Snapshot"
86
- vm.RevertToCurrentSnapshot_Task(:suppressPowerOn => false).wait_for_completion
87
- if get_config(:power)
88
- vm.PowerOnVM_Task.wait_for_completion
89
- puts "Powered on virtual machine #{vmname}"
90
- end
85
+ puts "Reverting to Current Snapshot"
86
+ vm.RevertToCurrentSnapshot_Task(:suppressPowerOn => false).wait_for_completion
87
+ if get_config(:power)
88
+ vm.PowerOnVM_Task.wait_for_completion
89
+ puts "Powered on virtual machine #{vmname}"
90
+ end
91
91
  end
92
92
 
93
93
  if config[:revert_snapshot]
94
- ss_name = config[:revert_snapshot]
95
- snapshot = find_node(snapshot_list,ss_name)
96
- snapshot.RevertToSnapshot_Task(:suppressPowerOn => false).wait_for_completion
97
- if get_config(:power)
98
- vm.PowerOnVM_Task.wait_for_completion
99
- puts "Powered on virtual machine #{vmname}"
100
- end
94
+ ss_name = config[:revert_snapshot]
95
+ snapshot = find_node(snapshot_list, ss_name)
96
+ snapshot.RevertToSnapshot_Task(:suppressPowerOn => false).wait_for_completion
97
+ if get_config(:power)
98
+ vm.PowerOnVM_Task.wait_for_completion
99
+ puts "Powered on virtual machine #{vmname}"
100
+ end
101
101
  end
102
102
  end
103
103
 
104
- def find_node(tree,name)
105
- snapshot = nil
106
- tree.each do |node|
107
- if node.name == name
108
- snapshot = node.snapshot
109
- elsif !node.childSnapshotList.empty?
110
- snapshot = find_node(node.childSnapshotList,name)
111
- end
112
- end
113
- return snapshot
104
+ def find_node(tree, name)
105
+ snapshot = nil
106
+ tree.each do |node|
107
+ if node.name == name
108
+ snapshot = node.snapshot
109
+ elsif !node.childSnapshotList.empty?
110
+ snapshot = find_node(node.childSnapshotList, name)
111
+ end
112
+ end
113
+ return snapshot
114
114
  end
115
115
 
116
- def display_node(node,current,shift=1)
116
+ def display_node(node, current, shift=1)
117
117
  out = ""
118
- out << "+--"*shift
118
+ out << "+--"*shift
119
119
  if node.snapshot == current
120
- out << "#{ui.color(node.name, :cyan)}" << "\n"
121
- else
122
- out << "#{node.name}" << "\n"
120
+ out << "#{ui.color(node.name, :cyan)}" << "\n"
121
+ else
122
+ out << "#{node.name}" << "\n"
123
123
  end
124
- if ! node.childSnapshotList.empty?
125
- node.childSnapshotList.each{|item| out << display_node(item,current,shift+1)}
126
- end
127
- out
128
- end
124
+ if !node.childSnapshotList.empty?
125
+ node.childSnapshotList.each { |item| out << display_node(item, current, shift+1) }
126
+ end
127
+ out
128
+ end
129
129
  end