Class: Chef::Knife::XapiVdiDelete

Inherits:
Chef::Knife show all
Includes:
XapiBase
Defined in:
lib/chef/knife/xapi_vdi_delete.rb

Instance Attribute Summary

Attributes included from XapiBase

#defaults

Instance Method Summary collapse

Methods included from XapiBase

#add_vif_by_name, #cleanup, #clear_vm_vifs, #color_kv, #create_vbd, #create_vdi, defaults, #destroy_vdi, #detach_vdi, #fail, #find_default_sr, #find_template, #generate_mac, #get_all_vdis, get_default, #get_guest_ip, #get_host_ref, #get_sr_by_name, #get_sr_by_uuid, #get_task_ref, #get_template, #get_vbd_by_uuid, #get_vbds_from_vdi, #get_vdi_by_name_label, #get_vdi_by_uuid, #h, included, #input_to_bytes, #is_uuid?, #locate_config_value, #print_record, #print_vdi_info, set_defaults, #start, #stop, #user_select, #wait_on_task, #xapi, #yes_no?

Instance Method Details

#interactiveObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/knife/xapi_vdi_delete.rb', line 45

def interactive
  # Get all VDIs known to the system
  vdis = get_all_vdis
  first = true

  for vdi_ in vdis do
    vbds = get_vbds_from_vdi(vdi_)
    if vbds.empty? && xapi.VDI.get_type(vdi_).match('system')
      if first
        first = false
      end

      prinlt_vdi_info(vdi_)
      destroy_vdi(vdi_) if yes_no?('Destroy this volume? ')
    end
  end
end

#runObject



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
# File 'lib/chef/knife/xapi_vdi_delete.rb', line 82

def run
  if config[:interactive]
    interactive
    return
  elsif config[:cleanup]
    vdi_cleanup
    return
  end

  vdi_name = @name_args[0]
  if vdi_name.nil?
    puts 'Error: No VDI Name specified...'
    puts 'Usage: ' + banner
    exit 1
  end

  vdis = []
  if config[:uuid]
    vdis << get_vdi_by_uuid(vdi_name)
  else
    vdis << get_vdi_by_name_label(vdi_name)
  end
  vdis.flatten!

  if vdis.empty?
    ui.msg "VDI not found: #{h.color vdi_name, :red}"
    exit 1
  elsif vdis.length > 1
    ui.msg 'Multiple VDI matches found. Use vdi list if you are unsure'
    vdi = user_select(vdis)
  else
    vdi = vdis.first
  end

  if vdi == :all
    vdis.each { |vdi|  destroy_vdi(vdi) }
  else
    destroy_vdi(vdi)
  end
end

#vdi_cleanupObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/xapi_vdi_delete.rb', line 63

def vdi_cleanup
  orphaned_vdis = []
  vdis = get_all_vdis

  for vdi_ in vdis do
    vbds = get_vbds_from_vdi(vdi_)
    if vbds.empty? && xapi.VDI.get_type(vdi_).match('system')
      orphaned_vdis << vdi_
    end
  end

  orphaned_vdis.each { |item| print_vdi_info(item) }
  unless orphaned_vdis.empty?
    if yes_no?('Destroy all these volumes? ')
      orphaned_vdis.each { |item| destroy_vdi(item) }
    end
  end
end