Class: CollinsShell::AssetPrinter

Inherits:
Object
  • Object
show all
Includes:
PrinterUtil
Defined in:
lib/collins_shell/util/asset_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PrinterUtil

#dynamic_terminal_width, #format_datetime, #wrap_text

Constructor Details

#initialize(asset, thor, options = {}) ⇒ AssetPrinter

Returns a new instance of AssetPrinter.



12
13
14
15
16
17
18
19
20
21
# File 'lib/collins_shell/util/asset_printer.rb', line 12

def initialize asset, thor, options = {}
  @asset = asset
  @table = Terminal::Table.new
  @thor = thor
  @separator = options[:separator]
  @detailed = options.fetch(:detailed, true)
  @col_size = 6
  @color = options.fetch(:color, true)
  @logs = options[:logs]
end

Instance Attribute Details

#assetObject

Returns the value of attribute asset.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def asset
  @asset
end

#col_sizeObject

Returns the value of attribute col_size.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def col_size
  @col_size
end

#colorObject

Returns the value of attribute color.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def color
  @color
end

#detailedObject

Returns the value of attribute detailed.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def detailed
  @detailed
end

#logsObject

Returns the value of attribute logs.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def logs
  @logs
end

#separatorObject

Returns the value of attribute separator.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def separator
  @separator
end

#tableObject

Returns the value of attribute table.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def table
  @table
end

#thorObject

Returns the value of attribute thor.



10
11
12
# File 'lib/collins_shell/util/asset_printer.rb', line 10

def thor
  @thor
end

Instance Method Details

#collect_asset_basicsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/collins_shell/util/asset_printer.rb', line 47

def collect_asset_basics
  header = [:tag,:status,:type,:created,:updated,:location]
  table << get_row(header, true, header.length)
  table << :separator
  table << get_row(header.map{|s| asset.send(s)}, false, header.length)
  if asset.state && !asset.state.empty? then
    state = asset.state
    state_headers = [
      title_text("State Name"),
      title_text("State Label"),
      {:value => title_text("State Description"), :colspan => 4}
    ]
    table << :separator
    table << state_headers
    table << :separator
    table << [state.name, state.label, {:value => state.description, :colspan => 4}]
  end
  @col_size = table.number_of_columns
end

#collect_disk_dataObject



67
68
69
70
71
72
73
74
75
# File 'lib/collins_shell/util/asset_printer.rb', line 67

def collect_disk_data
  disk_header = ["SIZE_HUMAN", "SIZE", "TYPE", "DESCRIPTION"]
  row_title "Disks"
  table << get_row(disk_header, true)
  table << :separator
  asset.disks.each do |disk|
    table << get_row(disk_header.map{|s| disk[s]})
  end
end

#collect_ip_dataObject



111
112
113
114
115
116
117
# File 'lib/collins_shell/util/asset_printer.rb', line 111

def collect_ip_data
  address_header = [:address,:gateway,:netmask,:pool,:is_private?,:is_public?]
  row_title "IP Addresses"
  table << get_row(address_header, true)
  table << :separator
  asset.addresses.each{|a| table << get_row(address_header.map{|s| a.send(s)})}
end

#collect_ipmi_dataObject



119
120
121
122
123
124
125
# File 'lib/collins_shell/util/asset_printer.rb', line 119

def collect_ipmi_data
  ipmi_header = [:address,:gateway,:netmask,:username,:password]
  row_title "IPMI Information"
  table << get_row(ipmi_header, true)
  table << :separator
  table << get_row(ipmi_header.map {|i| asset.ipmi.send(i)})
end

#collect_log_dataObject



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
# File 'lib/collins_shell/util/asset_printer.rb', line 77

def collect_log_data
  return if (not logs or logs.empty?)
  row_title "Log Entries"
  headers = [
    title_text("Created"),
    title_text("Severity"),
    title_text("Source"),
    title_text("Format"),
    {:value => title_text("Message"), :colspan => 2}
  ]
  table << headers
  table << :separator
  have_multi_row = false
  logs_formatted = logs.map do |log|
    rewritten_msg = log.MESSAGE.strip
    message = wrap_text(rewritten_msg).strip
    if message != rewritten_msg then
      have_multi_row = true
    end
    [
      format_datetime(log.CREATED), log.TYPE, log.SOURCE, log.FORMAT,
      {:value => message, :colspan => 2}
    ]
  end
  last_log_idx = logs.length - 1
  logs_formatted.each_with_index do |row, index|
    table << row
    # Don't print a separator if we don't have a multi-row message
    if index != last_log_idx && have_multi_row then
      table << :separator
    end
  end
end

#collect_mem_dataObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/collins_shell/util/asset_printer.rb', line 135

def collect_mem_data
  memory_header = ["BANK","SIZE","SIZE_HUMAN","DESCRIPTION"]
  row_title "Physical Memory"
  table << get_row(memory_header, true)
  table << :separator
  memory_total = 0
  asset.memory.each do |mem|
    memory_total += mem["SIZE"].to_i
    if mem["SIZE"].to_i != 0 then
      table << get_row(memory_header.map{|s| mem[s]})
    end
  end
  table << :separator
  table << [title_text("TOTAL SIZE (Bytes)"), {:value => memory_total.to_s, :colspan => 5}]
  table << [title_text("TOTAL SIZE (Human)"), {:value => memory_total.to_human_size, :colspan => 5}]
end

#collect_nic_dataObject



127
128
129
130
131
132
133
# File 'lib/collins_shell/util/asset_printer.rb', line 127

def collect_nic_data
  nic_header = ["SPEED_HUMAN", "MAC_ADDRESS", "DESCRIPTION"]
  row_title "Network Interfaces"
  table << get_row(nic_header, true)
  table << :separator
  asset.nics.each {|nic| table << get_row(nic_header.map{|s| nic[s]})}
end

#collect_power_dataObject



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/collins_shell/util/asset_printer.rb', line 152

def collect_power_data
  power_header = [:type,:key,:value,:label]
  row_title "Power Information"
  table << get_row(power_header, true)
  table << :separator
  asset.power.each do |power|
    power.units.each do |unit|
      table << get_row(power_header.map {|key| unit.send(key)})
    end
  end
end

#collect_xtra_dataObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/collins_shell/util/asset_printer.rb', line 177

def collect_xtra_data
  return unless (asset.extras and asset.extras["ATTRIBS"] && asset.extras["ATTRIBS"]["0"])
  row_title "Extra Attributes"
  attribs = []
  attribs_unsorted = asset.extras["ATTRIBS"]["0"]
  attribs_sorted = attribs_unsorted.keys.sort.inject({}) do |result,key|
    result.update(key => attribs_unsorted[key])
  end
  attribs_sorted.each do |key,value|
    attribs << title_text(key)
    attribs << format_text(key, value)
    if attribs.length == 6 then
      table << attribs
      attribs = []
    end
    if CollinsShell::Util::SIZABLE_ATTRIBUTES.include?(key.downcase.to_sym) then
      attribs << title_text("#{key} (Human)")
      attribs << value.to_f.to_human_size
    end
    if attribs.length == 6 then
      table << attribs
      attribs = []
    end
  end
  if attribs.length != 0 then
    until attribs.length == 6
      attribs << " "
    end
    table << attribs
  end
end

#format_text(key, value) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/collins_shell/util/asset_printer.rb', line 164

def format_text key, value
  key_s = key.to_s.downcase
  if key_s.start_with?("json_") or key_s.end_with?("_json") then
    begin
      JSON.parse(value, :symbolize_names => true).to_s
    rescue Exception => e
      value
    end
  else
    value
  end
end

#get_row(row_data, header = false, cols = 0) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/collins_shell/util/asset_printer.rb', line 227

def get_row row_data, header = false, cols = 0
  num_cols = @col_size
  row_length = row_data.length
  last_index = row_length - 1
  average_size = (num_cols.to_f / row_length.to_f)
  est_size = (num_cols / row_length)
  est_is_actual = (average_size == est_size)
  used_cols = 0
  row = []
  row_data.each_with_index do |col,index|
    if est_is_actual then
      size_per_col = est_size
    elsif index == 0 then
      size_per_col = 1
    elsif average_size > (used_cols.to_f/index.to_f) then
      size_per_col = 2
    else
      size_per_col = 1
    end
    if col.is_a?(DateTime) then
      col = format_datetime(col)
    end
    used_cols += size_per_col
    alignment = if size_per_col > 1 then :center else :left end
    value = if header then title_text(col) else regular_text(col) end
    row << {:value => value, :alignment => alignment, :colspan => size_per_col}
  end
  row
end

#header_text(txt) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/collins_shell/util/asset_printer.rb', line 209

def header_text txt
  if @color then
    thor.set_color(txt, :bold, :magenta)
  else
    txt
  end
end

#regular_text(txt) ⇒ Object



223
224
225
# File 'lib/collins_shell/util/asset_printer.rb', line 223

def regular_text txt
  txt
end

#renderObject Also known as: to_s



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/collins_shell/util/asset_printer.rb', line 23

def render
  table.title = header_text("Asset #{asset.tag}")
  collect_asset_basics
  collect_disk_data if detailed
  collect_ip_data if detailed
  collect_ipmi_data if detailed
  collect_nic_data if detailed
  collect_mem_data if detailed
  collect_power_data if detailed
  collect_xtra_data
  collect_log_data
  table_s = table.render
  if separator then
    width = (0...@col_size).inject(0) do |result, idx|
      result + table.column_width(idx)
    end
    sep_s = separator * (1.13 * width.to_f).to_i
    table_s + "\n#{sep_s}\n"
  else
    table_s
  end
end

#row_title(txt) ⇒ Object



257
258
259
260
261
# File 'lib/collins_shell/util/asset_printer.rb', line 257

def row_title txt
  table << :separator
  table << [{:value => header_text(txt), :alignment => :center, :colspan => @col_size}]
  table << :separator
end

#title_text(txt) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/collins_shell/util/asset_printer.rb', line 216

def title_text txt
  if @color then
    thor.set_color(txt, :bold, :white)
  else
    txt
  end
end