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,50 +1,49 @@
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::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
10
- banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
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
- query_string = @name_args[1]
23
- if query_string.nil?
24
- show_usage
25
- fatal_exit("You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)")
26
- end
27
-
28
- vim = get_vim_connection
29
-
30
- dcname = get_config(:vsphere_dc)
31
- dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
32
- folder = find_folder(get_config(:folder)) || dc.vmFolder
33
-
34
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
35
- abort "VM #{vmname} not found"
36
-
37
- # split QUERY by dots, and walk the object model
38
- query = query_string.split '.'
39
- result = vm
40
- query.each do |part|
41
- message, index = part.split(/[\[\]]/)
42
- unless result.respond_to? message.to_sym
43
- fatal_exit("\"#{query_string}\" not recognized.")
44
- end
45
-
46
- result = index ? result.send(message)[index.to_i] : result.send(message)
47
- end
48
- puts result
49
- end
50
- 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::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
10
+ banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
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
+ query_string = @name_args[1]
23
+ if query_string.nil?
24
+ show_usage
25
+ fatal_exit("You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)")
26
+ end
27
+
28
+ vim = get_vim_connection
29
+
30
+ dc = get_datacenter
31
+ folder = find_folder(get_config(:folder)) || dc.vmFolder
32
+
33
+ vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) or
34
+ abort "VM #{vmname} not found"
35
+
36
+ # split QUERY by dots, and walk the object model
37
+ query = query_string.split '.'
38
+ result = vm
39
+ query.each do |part|
40
+ message, index = part.split(/[\[\]]/)
41
+ unless result.respond_to? message.to_sym
42
+ fatal_exit("\"#{query_string}\" not recognized.")
43
+ end
44
+
45
+ result = index ? result.send(message)[index.to_i] : result.send(message)
46
+ end
47
+ puts result
48
+ end
49
+ end
@@ -1,129 +1,129 @@
1
- #
2
- # License:: Apache License, Version 2.0
3
- #
4
-
5
- require 'chef/knife'
6
- require 'chef/knife/base_vsphere_command'
7
- require 'rbvmomi'
8
- require 'netaddr'
9
-
10
-
11
- # Manage snapshots of a virtual machine
12
- class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
13
-
14
- banner "knife vsphere vm snapshot VMNAME (options)"
15
-
16
- get_common_options
17
-
18
- option :list,
19
- :long => "--list",
20
- :description => "The current tree of snapshots"
21
-
22
- option :create_new_snapshot,
23
- :long => "--create SNAPSHOT",
24
- :description => "Create a new snapshot off of the current snapshot."
25
-
26
- option :remove_named_snapshot,
27
- :long => "--remove SNAPSHOT",
28
- :description => "Remove a named snapshot."
29
-
30
- option :revert_snapshot,
31
- :long => "--revert SNAPSHOT",
32
- :description => "Revert to a named snapshot."
33
-
34
- option :revert_current_snapshot,
35
- :long => "--revert-current",
36
- :description => "Revert to current snapshot.",
37
- :boolean => false
38
-
39
- option :power,
40
- :long => "--start",
41
- :description => "Indicates whether to start the VM after a successful revert",
42
- :boolean => false
43
-
44
- def run
45
-
46
- $stdout.sync = true
47
-
48
- vmname = @name_args[0]
49
- if vmname.nil?
50
- show_usage
51
- ui.fatal("You must specify a virtual machine name")
52
- exit 1
53
- end
54
-
55
- vim = get_vim_connection
56
-
57
- baseFolder = find_folder(get_config(:folder));
58
-
59
- vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
60
- abort "VM #{vmname} not found"
61
-
62
- if vm.snapshot
63
- snapshot_list = vm.snapshot.rootSnapshotList
64
- current_snapshot = vm.snapshot.currentSnapshot
65
- end
66
-
67
- if config[:list] && vm.snapshot
68
- puts "Current snapshot tree: "
69
- puts "#{vmname}"
70
- snapshot_list.each { |i| puts display_node(i, current_snapshot) }
71
- end
72
-
73
- if config[:create_new_snapshot]
74
- vm.CreateSnapshot_Task(:name => config[:create_new_snapshot], :description => "", :memory => false, :quiesce => false)
75
- end
76
-
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)
82
- end
83
-
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
91
- end
92
-
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
101
- end
102
- end
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
114
- end
115
-
116
- def display_node(node, current, shift=1)
117
- out = ""
118
- out << "+--"*shift
119
- if node.snapshot == current
120
- out << "#{ui.color(node.name, :cyan)}" << "\n"
121
- else
122
- out << "#{node.name}" << "\n"
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
129
- end
1
+ #
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+
5
+ require 'chef/knife'
6
+ require 'chef/knife/base_vsphere_command'
7
+ require 'rbvmomi'
8
+ require 'netaddr'
9
+
10
+
11
+ # Manage snapshots of a virtual machine
12
+ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
13
+
14
+ banner "knife vsphere vm snapshot VMNAME (options)"
15
+
16
+ get_common_options
17
+
18
+ option :list,
19
+ :long => "--list",
20
+ :description => "The current tree of snapshots"
21
+
22
+ option :create_new_snapshot,
23
+ :long => "--create SNAPSHOT",
24
+ :description => "Create a new snapshot off of the current snapshot."
25
+
26
+ option :remove_named_snapshot,
27
+ :long => "--remove SNAPSHOT",
28
+ :description => "Remove a named snapshot."
29
+
30
+ option :revert_snapshot,
31
+ :long => "--revert SNAPSHOT",
32
+ :description => "Revert to a named snapshot."
33
+
34
+ option :revert_current_snapshot,
35
+ :long => "--revert-current",
36
+ :description => "Revert to current snapshot.",
37
+ :boolean => false
38
+
39
+ option :power,
40
+ :long => "--start",
41
+ :description => "Indicates whether to start the VM after a successful revert",
42
+ :boolean => false
43
+
44
+ def run
45
+
46
+ $stdout.sync = true
47
+
48
+ vmname = @name_args[0]
49
+ if vmname.nil?
50
+ show_usage
51
+ ui.fatal("You must specify a virtual machine name")
52
+ exit 1
53
+ end
54
+
55
+ vim = get_vim_connection
56
+
57
+ baseFolder = find_folder(get_config(:folder));
58
+
59
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
60
+ abort "VM #{vmname} not found"
61
+
62
+ if vm.snapshot
63
+ snapshot_list = vm.snapshot.rootSnapshotList
64
+ current_snapshot = vm.snapshot.currentSnapshot
65
+ end
66
+
67
+ if config[:list] && vm.snapshot
68
+ puts "Current snapshot tree: "
69
+ puts "#{vmname}"
70
+ snapshot_list.each { |i| puts display_node(i, current_snapshot) }
71
+ end
72
+
73
+ if config[:create_new_snapshot]
74
+ vm.CreateSnapshot_Task(:name => config[:create_new_snapshot], :description => "", :memory => false, :quiesce => false)
75
+ end
76
+
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)
82
+ end
83
+
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
91
+ end
92
+
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
101
+ end
102
+ end
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
114
+ end
115
+
116
+ def display_node(node, current, shift=1)
117
+ out = ""
118
+ out << "+--"*shift
119
+ if node.snapshot == current
120
+ out << "#{ui.color(node.name, :cyan)}" << "\n"
121
+ else
122
+ out << "#{node.name}" << "\n"
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
129
+ end
@@ -1,111 +1,111 @@
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
- require 'netaddr'
10
-
11
- PsOn = 'poweredOn'
12
- PsOff = 'poweredOff'
13
- PsSuspended = 'suspended'
14
-
15
- PowerStates = {
16
- PsOn => 'powered on',
17
- PsOff => 'powered off',
18
- PsSuspended => 'suspended'
19
- }
20
-
21
- # Manage power state of a virtual machine
22
- class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
23
-
24
- banner "knife vsphere vm state VMNAME (options)"
25
-
26
- get_common_options
27
-
28
- option :state,
29
- :short => "-s STATE",
30
- :long => "--state STATE",
31
- :description => "The power state to transition the VM into; one of on|off|suspended"
32
-
33
- option :wait_port,
34
- :short => "-w PORT",
35
- :long => "--wait-port PORT",
36
- :description => "Wait for VM to be accessible on a port"
37
-
38
- option :shutdown,
39
- :short => "-g",
40
- :long => "--shutdown",
41
- :description => "Guest OS shutdown"
42
-
43
- def run
44
-
45
- $stdout.sync = true
46
-
47
- vmname = @name_args[0]
48
- if vmname.nil?
49
- show_usage
50
- ui.fatal("You must specify a virtual machine name")
51
- exit 1
52
- end
53
-
54
- vim = get_vim_connection
55
-
56
- baseFolder = find_folder(get_config(:folder));
57
-
58
- vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
59
- abort "VM #{vmname} not found"
60
-
61
- state = vm.runtime.powerState
62
-
63
- if config[:state].nil?
64
- puts "VM #{vmname} is currently " + PowerStates[vm.runtime.powerState]
65
- else
66
-
67
- case config[:state]
68
- when 'on'
69
- if state == PsOn
70
- puts "Virtual machine #{vmname} was already powered on"
71
- else
72
- vm.PowerOnVM_Task.wait_for_completion
73
- puts "Powered on virtual machine #{vmname}"
74
- end
75
- when 'off'
76
- if state == PsOff
77
- puts "Virtual machine #{vmname} was already powered off"
78
- else
79
- if get_config(:shutdown)
80
- vm.ShutdownGuest
81
- print "Waiting for virtual machine #{vmname} to shut down..."
82
- until vm.runtime.powerState == PsOff do
83
- sleep 2
84
- print "."
85
- end
86
- puts "done"
87
- else
88
- vm.PowerOffVM_Task.wait_for_completion
89
- puts "Powered off virtual machine #{vmname}"
90
- end
91
- end
92
- when 'suspend'
93
- if state == PowerStates['suspended']
94
- puts "Virtual machine #{vmname} was already suspended"
95
- else
96
- vm.SuspendVM_Task.wait_for_completion
97
- puts "Suspended virtual machine #{vmname}"
98
- end
99
- when 'reset'
100
- vm.ResetVM_Task.wait_for_completion
101
- puts "Reset virtual machine #{vmname}"
102
- end
103
-
104
- if get_config(:wait_port)
105
- print "Waiting for port #{get_config(:wait_port)}..."
106
- print "." until tcp_test_port_vm(vm, get_config(:wait_port))
107
- puts "done"
108
- end
109
- end
110
- end
111
- 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
+ require 'netaddr'
10
+
11
+ PsOn = 'poweredOn'
12
+ PsOff = 'poweredOff'
13
+ PsSuspended = 'suspended'
14
+
15
+ PowerStates = {
16
+ PsOn => 'powered on',
17
+ PsOff => 'powered off',
18
+ PsSuspended => 'suspended'
19
+ }
20
+
21
+ # Manage power state of a virtual machine
22
+ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
23
+
24
+ banner "knife vsphere vm state VMNAME (options)"
25
+
26
+ get_common_options
27
+
28
+ option :state,
29
+ :short => "-s STATE",
30
+ :long => "--state STATE",
31
+ :description => "The power state to transition the VM into; one of on|off|suspended"
32
+
33
+ option :wait_port,
34
+ :short => "-w PORT",
35
+ :long => "--wait-port PORT",
36
+ :description => "Wait for VM to be accessible on a port"
37
+
38
+ option :shutdown,
39
+ :short => "-g",
40
+ :long => "--shutdown",
41
+ :description => "Guest OS shutdown"
42
+
43
+ def run
44
+
45
+ $stdout.sync = true
46
+
47
+ vmname = @name_args[0]
48
+ if vmname.nil?
49
+ show_usage
50
+ ui.fatal("You must specify a virtual machine name")
51
+ exit 1
52
+ end
53
+
54
+ vim = get_vim_connection
55
+
56
+ baseFolder = find_folder(get_config(:folder));
57
+
58
+ vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
59
+ abort "VM #{vmname} not found"
60
+
61
+ state = vm.runtime.powerState
62
+
63
+ if config[:state].nil?
64
+ puts "VM #{vmname} is currently " + PowerStates[vm.runtime.powerState]
65
+ else
66
+
67
+ case config[:state]
68
+ when 'on'
69
+ if state == PsOn
70
+ puts "Virtual machine #{vmname} was already powered on"
71
+ else
72
+ vm.PowerOnVM_Task.wait_for_completion
73
+ puts "Powered on virtual machine #{vmname}"
74
+ end
75
+ when 'off'
76
+ if state == PsOff
77
+ puts "Virtual machine #{vmname} was already powered off"
78
+ else
79
+ if get_config(:shutdown)
80
+ vm.ShutdownGuest
81
+ print "Waiting for virtual machine #{vmname} to shut down..."
82
+ until vm.runtime.powerState == PsOff do
83
+ sleep 2
84
+ print "."
85
+ end
86
+ puts "done"
87
+ else
88
+ vm.PowerOffVM_Task.wait_for_completion
89
+ puts "Powered off virtual machine #{vmname}"
90
+ end
91
+ end
92
+ when 'suspend'
93
+ if state == PowerStates['suspended']
94
+ puts "Virtual machine #{vmname} was already suspended"
95
+ else
96
+ vm.SuspendVM_Task.wait_for_completion
97
+ puts "Suspended virtual machine #{vmname}"
98
+ end
99
+ when 'reset'
100
+ vm.ResetVM_Task.wait_for_completion
101
+ puts "Reset virtual machine #{vmname}"
102
+ end
103
+
104
+ if get_config(:wait_port)
105
+ print "Waiting for port #{get_config(:wait_port)}..."
106
+ print "." until tcp_test_port_vm(vm, get_config(:wait_port))
107
+ puts "done"
108
+ end
109
+ end
110
+ end
111
+ end