Top Level Namespace

Defined Under Namespace

Modules: CustomizationHelper, KnifeVsphere, SearchHelper Classes: Chef

Instance Method Summary collapse

Instance Method Details

#find_max_dscluster(folder, max_dscluster, regex) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/knife/vsphere_datastorecluster_maxfree.rb', line 31

def find_max_dscluster(folder, max_dscluster, regex)
  folder.childEntity.each do |child|
    if child.class.to_s == "Folder"
      sub_max = find_max_dscluster(child, max_dscluster, regex)
      max_dscluster = sub_max if max_dscluster(sub_max, max_dscluster)
    elsif child.class.to_s == "StoragePod"
      if max_dscluster(child, max_dscluster) && regex.match(child.name)
        max_dscluster = child
      end
    end
  end

  max_dscluster
end

#max_dscluster(dscluster, max_dscluster) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/chef/knife/vsphere_datastorecluster_maxfree.rb', line 21

def max_dscluster(dscluster, max_dscluster)
  return true unless max_dscluster

  if dscluster.summary[:freeSpace] > max_dscluster.summary[:freeSpace]
    return true
  end

  false
end

#number_to_human_size(number) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef/knife/vsphere_datastorecluster_list.rb', line 21

def number_to_human_size(number)
  number = number.to_f
  storage_units_fmt = %w{byte kB MB GB TB}
  base = 1024
  if number.to_i < base
    unit = storage_units_fmt[0]
  else
    max_exp = storage_units_fmt.size - 1
    exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
    exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
    number /= base**exponent
    unit = storage_units_fmt[exponent]
  end

  format("%0.2f %s", number, unit)
end


45
46
47
48
49
50
51
52
53
# File 'lib/chef/knife/vsphere_datastorecluster_list.rb', line 45

def print_dsclusters_in_folder(folder)
  folder.childEntity.each do |child|
    next unless child.class.to_s == "StoragePod"

    avail = number_to_human_size(child.summary[:freeSpace])
    cap = number_to_human_size(child.summary[:capacity])
    puts "#{ui.color("DatastoreCluster", :cyan)}: #{child.name} (#{avail} / #{cap})"
  end
end

#traverse_folders_for_dsclusters(folder) ⇒ Object



38
39
40
41
42
43
# File 'lib/chef/knife/vsphere_datastorecluster_list.rb', line 38

def traverse_folders_for_dsclusters(folder)
  print_dsclusters_in_folder(folder)
  folder.childEntity.each do |child|
    traverse_folders_for_dsclusters(child) if child.class.to_s == "Folder"
  end
end