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

#choose_datastore, common_options, #conn_opts, #datacenter, #fatal_exit, #find_all_in_folder, #find_datastore, #find_datastorecluster, #find_datastores_regex, #find_device, #find_folder, #find_in_folder, #find_network, #find_pool, #find_pools_and_clusters, #get_config, #get_password_from_stdin, #get_path_to_object, #get_vm, #get_vms, #linux?, #number_to_human_size, #password, #tcp_test_port, #tcp_test_port_vm, #traverse_folders_for_computeresources, #traverse_folders_for_dc, #traverse_folders_for_pools, #traverse_folders_for_vm, #traverse_folders_for_vms, #vim_connection, #windows?

Methods inherited from Chef::Knife

#log_verbose?

Instance Method Details

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



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 144

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
  unless node.childSnapshotList.empty?
    node.childSnapshotList.each { |item| out << display_node(item, current, shift + 1) }
  end
  out
end

#find_node(tree, name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 131

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

#runObject



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/knife/vsphere_vm_snapshot.rb', line 63

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_connection

  vm = if get_config(:find)
         puts "No folder entered, searching for #{vmname}"
         src_folder = find_folder(get_config(:folder))
         traverse_folders_for_vm(src_folder, vmname)
       else
         base_folder = find_folder get_config(:folder)
         find_in_folder(base_folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
       end

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

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

  if get_config(:create_new_snapshot)
    snapshot_task = vm.CreateSnapshot_Task(name: get_config(:create_new_snapshot),
                                           description: '',
                                           memory: get_config(:dump_memory),
                                           quiesce: get_config(:quiesce))
    snapshot_task = snapshot_task.wait_for_completion if get_config(:wait)
    snapshot_task
  end

  if get_config(:remove_named_snapshot)
    ss_name = get_config(:remove_named_snapshot)
    snapshot = find_node(snapshot_list, ss_name)
    puts "Found snapshot #{ss_name} removing."
    snapshot_task = snapshot.RemoveSnapshot_Task(removeChildren: false)
    snapshot_task = snapshot_task.wait_for_completion if get_config(:wait)
    snapshot_task
  end

  if get_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

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