knife-vsphere 0.5.0 → 0.6.0

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.
@@ -0,0 +1,129 @@
1
+ #
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+
5
+ require 'chef/knife'
6
+ require 'chef/knife/BaseVsphereCommand'
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,4 +1,4 @@
1
1
  module KnifeVsphere
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
4
4
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: knife-vsphere
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.0
5
+ version: 0.6.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ezra Pagel
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-04-25 00:00:00 -05:00
13
+ date: 2013-06-02 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -65,10 +65,10 @@ files:
65
65
  - lib/chef/knife/vsphere_vm_delete.rb
66
66
  - lib/chef/knife/vsphere_vm_execute.rb
67
67
  - lib/chef/knife/vsphere_vm_list.rb
68
+ - lib/chef/knife/vsphere_vm_snapshot.rb
68
69
  - lib/chef/knife/vsphere_vm_state.rb
69
70
  - lib/chef/knife/vsphere_vm_vmdk_add.rb
70
71
  - lib/knife-vsphere/version.rb
71
- - lib/knife-vsphere/version.rb~
72
72
  has_rdoc: true
73
73
  homepage: http://github.com/ezrapagel/knife-vsphere
74
74
  licenses: []
@@ -1,4 +0,0 @@
1
- module KnifeVsphere
2
- VERSION = "0.4.0"
3
- end
4
-