Class: Chef::Knife::XenserverVmList

Inherits:
Chef::Knife show all
Includes:
XenserverBase
Defined in:
lib/chef/knife/xenserver_vm_list.rb

Instance Method Summary collapse

Methods included from XenserverBase

#bytes_to_megabytes, #connection, included, #locate_config_value

Instance Method Details

#gen_headingsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/knife/xenserver_vm_list.rb', line 75

def gen_headings
  headings = %w{NAME}
  if config[:mem]
    headings << 'MEM'
  end
  if config[:power_state]
    headings << 'POWER'
  end
  if config[:tools]
    headings << 'TOOLS'
  end
  if config[:networks]
    headings << 'NETWORKS'
  end
  if config[:ips]
    headings << 'IPs'
  end
  headings
end

#gen_tableObject



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

def gen_table
  # row format
  # [uuid, name, [ips], [networks], mem, power, tools]
  table = []
  connection.servers.each do |vm|
    if config[:match] and vm.name !~ /#{config[:match]}/
      next
    end
    row = [vm.uuid, vm.name] 
    if vm.tools_installed?
      ips = []
      vm.guest_metrics.networks.each do |k,v|
        ips << v
      end
      row << ips
    else
      row << []
    end
    networks = []
    vm.vifs.each do |vif|
      name = vif.network.name
      if name.size > 20
        name = name[0..16] + '...'
      end
      networks << name
    end
    row << networks
    row << bytes_to_megabytes(vm.memory_static_max)
    row << vm.power_state
    row << vm.tools_installed?
    table << row
  end
  table
end


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/knife/xenserver_vm_list.rb', line 153

def print_csv
  lines = []
  header = ""
  gen_table.each do |row|
    uuid, name, ips, networks, mem, power, tools = row
    elements = []
    elements << name
    elements << mem if config[:mem]
    elements << power if config[:power_state]
    elements << tools if config[:tools]
    elements << networks.join(";") if config[:networks]
    elements << ips.join(";") if config[:ips]
    if config[:uuid]
      header = "UUID,#{gen_headings.join(',')}"
      lines << "#{uuid},#{elements.join(',')}"
    else
      header = "#{gen_headings.join(',')}"
      lines << "#{elements.join(',')}"
    end
  end

  puts header
  lines.each do |l| 
    puts l
  end
end


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chef/knife/xenserver_vm_list.rb', line 130

def print_table
  vm_table = table do |t|
    t.headings = gen_headings
    gen_table.each do |row|
      # [uuid, name, [ips], [networks], mem, power, tools]
      uuid, name, ips, networks, mem, power, tools = row
      elements = []
      if config[:uuid]
        elements << "#{uuid}\n  #{ui.color('name: ', :yellow)}#{name.ljust(32)}"
      else
        elements << "#{ui.color('name: ', :yellow)}#{name.ljust(32)}"
      end
      elements << mem if config[:mem]
      elements << power if config[:power_state]
      elements << tools if config[:tools]
      elements << networks.join("\n") if config[:networks]
      elements << ips.join("\n") if config[:ips]
      t << elements
    end
  end
  puts vm_table if connection.servers.size > 0
end

#runObject



180
181
182
183
184
185
186
187
# File 'lib/chef/knife/xenserver_vm_list.rb', line 180

def run
  $stdout.sync = true
  if config[:csv]
    print_csv
  else
    print_table
  end
end