Class: Chef::Knife::VsphereVmSnapshot

Inherits:
BaseVsphereCommand show all
Defined in:
lib/chef/knife/vsphere_vm_snapshot.rb

Overview

Manage snapshots of a virtual machine

Instance Method Summary collapse

Methods inherited from BaseVsphereCommand

#fatal_exit, #find_all_in_folder, #find_datastore, #find_device, #find_folder, #find_in_folder, #find_network, #find_pool, get_common_options, #get_config, #get_password, #get_vim_connection, #tcp_test_port

Instance Method Details

#display_node(node, current, shift = 1) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 116

def display_node(node,current,shift=1) 
  out = ""
  out << "+--"*shift 
  if node.snapshot == current
      out << "#{ui.color(node.name, :cyan)}" << "\n"
  else 
      out << "#{node.name}" << "\n"
  end
  if ! node.childSnapshotList.empty?
      node.childSnapshotList.each{|item| out << display_node(item,current,shift+1)}
  end 
  out 
end

#find_node(tree, name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 104

def find_node(tree,name)
     snapshot = nil
     tree.each do |node|
         if node.name == name
             snapshot = node.snapshot 
         elsif !node.childSnapshotList.empty?
             snapshot = find_node(node.childSnapshotList,name)
         end
     end
     return snapshot
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 44

def run

  $stdout.sync = true

  vmname = @name_args[0]
  if vmname.nil?
    show_usage
    ui.fatal("You must specify a virtual machine name")
    exit 1
  end
 
  vim = get_vim_connection

  baseFolder = find_folder(get_config(:folder));

  vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
    abort "VM #{vmname} not found"

  if vm.snapshot
    snapshot_list = vm.snapshot.rootSnapshotList
    current_snapshot = vm.snapshot.currentSnapshot
  end

  if config[:list] && vm.snapshot
    puts "Current snapshot tree: "
    puts "#{vmname}"
    snapshot_list.each{|i| puts display_node(i,current_snapshot)}
  end

  if config[:create_new_snapshot]
    vm.CreateSnapshot_Task(:name => config[:create_new_snapshot], :description => "", :memory => false, :quiesce => false)
  end

  if config[:remove_named_snapshot]
      ss_name  = config[:remove_named_snapshot]
      snapshot = find_node(snapshot_list,ss_name)
      puts "Found snapshot #{ss_name} removing."
      snapshot.RemoveSnapshot_Task(:removeChildren => false)
  end

  if config[:revert_current_snapshot]
      puts "Reverting to Current Snapshot"
      vm.RevertToCurrentSnapshot_Task(:suppressPowerOn => false).wait_for_completion
      if get_config(:power)
          vm.PowerOnVM_Task.wait_for_completion
          puts "Powered on virtual machine #{vmname}"
      end
  end

  if config[:revert_snapshot]
      ss_name  = config[:revert_snapshot]
      snapshot = find_node(snapshot_list,ss_name)
      snapshot.RevertToSnapshot_Task(:suppressPowerOn => false).wait_for_completion
      if get_config(:power)
          vm.PowerOnVM_Task.wait_for_completion
          puts "Powered on virtual machine #{vmname}"
      end
  end
end