Class: Omnibus::HealthCheck

Inherits:
Object
  • Object
show all
Includes:
Instrumentation, Logging, Sugarable, Util
Defined in:
lib/omnibus/health_check.rb

Constant Summary

Constants included from Util

Util::SHELLOUT_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sugarable

extended, included, #node

Methods included from Util

#compiler_safe_path, #copy_file, #create_directory, #create_file, #create_link, included, #path_key, #remove_directory, #remove_file, #retry_block, #shellout, #shellout!, #windows_safe_path

Methods included from Logging

included

Methods included from Instrumentation

#measure

Constructor Details

#initialize(project) ⇒ HealthCheck

Run the healthchecks against the given project. It is assumed that the project has already been built.

Parameters:

  • project (Project)

    the project to health check



53
54
55
# File 'lib/omnibus/health_check.rb', line 53

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectProject (readonly)

The project to healthcheck.

Returns:



44
45
46
# File 'lib/omnibus/health_check.rb', line 44

def project
  @project
end

Class Method Details

.run!(project) ⇒ Object

See Also:

  • (HealthCheck(HealthCheck#new)


34
35
36
# File 'lib/omnibus/health_check.rb', line 34

def run!(project)
  new(project).run!
end

Instance Method Details

#health_check_aixHash<String, Hash<String, Hash<String, Int>>>

Run healthchecks against aix.

Returns:

  • (Hash<String, Hash<String, Hash<String, Int>>>)

    the bad libraries (library_name -> dependency_name -> satisfied_lib_path -> count)



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/omnibus/health_check.rb', line 318

def health_check_aix
  current_library = nil
  bad_libs = {}
  good_libs = {}

  read_shared_libs("find #{project.install_dir}/ -type f | xargs file | grep \"XCOFF\" | awk -F: '{print $1}'", "xargs -n 1 ldd") do |line|
    case line
    when /^(.+) needs:$/
      current_library = Regexp.last_match[1]
      log.debug(log_key) { "Analyzing dependencies for #{current_library}" }
    when /^\s+(.+)$/
      name = Regexp.last_match[1]
      linked = Regexp.last_match[1]
      ( bad_libs, good_libs ) = check_for_bad_library(bad_libs, good_libs, current_library, name, linked)
    when /File is not an executable XCOFF file/ # ignore non-executable files
    else
      log.warn(log_key) { "Line did not match for #{current_library}\n#{line}" }
    end
  end

  [bad_libs, good_libs]
end

#health_check_freebsdHash<String, Hash<String, Hash<String, Int>>>

Run healthchecks on FreeBSD

Returns:

  • (Hash<String, Hash<String, Hash<String, Int>>>)

    the bad libraries (library_name -> dependency_name -> satisfied_lib_path -> count)



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/omnibus/health_check.rb', line 387

def health_check_freebsd
  current_library = nil
  bad_libs = {}
  good_libs = {}

  read_shared_libs("find #{project.install_dir}/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'", "xargs ldd") do |line|
    case line
    when /^(.+):$/
      current_library = Regexp.last_match[1]
      log.debug(log_key) { "Analyzing dependencies for #{current_library}" }
    when /^\s+(.+) \=\>\s+(.+)( \(.+\))?$/
      name = Regexp.last_match[1]
      linked = Regexp.last_match[2]
      ( bad_libs, good_libs ) = check_for_bad_library(bad_libs, good_libs, current_library, name, linked)
    when /^\s+(.+) \(.+\)$/
      next
    when /^\s+statically linked$/
      next
    when /^\s+not a dynamic executable$/ # ignore non-executable files
    else
      log.warn(log_key) do
        "Line did not match for #{current_library}\n#{line}"
      end
    end
  end

  [bad_libs, good_libs]
end

#health_check_linuxHash<String, Hash<String, Hash<String, Int>>>

Run healthchecks against ldd.

Returns:

  • (Hash<String, Hash<String, Hash<String, Int>>>)

    the bad libraries (library_name -> dependency_name -> satisfied_lib_path -> count)



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/omnibus/health_check.rb', line 422

def health_check_linux
  current_library = nil
  bad_libs = {}
  good_libs = {}

  read_shared_libs("find #{project.install_dir}/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'", "xargs ldd") do |line|
    case line
    when /^(.+):$/
      current_library = Regexp.last_match[1]
      log.debug(log_key) { "Analyzing dependencies for #{current_library}" }
    when /^\s+(.+) \=\>\s+(.+)( \(.+\))?$/
      name = Regexp.last_match[1]
      linked = Regexp.last_match[2]
      ( bad_libs, good_libs ) = check_for_bad_library(bad_libs, good_libs, current_library, name, linked)
    when /^\s+(.+) \(.+\)$/
      next
    when /^\s+statically linked$/
      next
    when /^\s+libjvm.so/ # FIXME: should remove if it doesn't blow up server
      next
    when /^\s+libjava.so/ # FIXME: should remove if it doesn't blow up server
      next
    when /^\s+libmawt.so/ # FIXME: should remove if it doesn't blow up server
      next
    when /^\s+not a dynamic executable$/ # ignore non-executable files
    else
      log.warn(log_key) do
        "Line did not match for #{current_library}\n#{line}"
      end
    end
  end

  [bad_libs, good_libs]
end

#health_check_otoolHash<String, Hash<String, Hash<String, Int>>>

Run healthchecks against otool.

Returns:

  • (Hash<String, Hash<String, Hash<String, Int>>>)

    the bad libraries (library_name -> dependency_name -> satisfied_lib_path -> count)



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/omnibus/health_check.rb', line 293

def health_check_otool
  current_library = nil
  bad_libs = {}
  good_libs = {}

  read_shared_libs("find #{project.install_dir}/ -type f | egrep '\.(dylib|bundle)$'", "xargs otool -L") do |line|
    case line
    when /^(.+):$/
      current_library = Regexp.last_match[1]
    when /^\s+(.+) \(.+\)$/
      linked = Regexp.last_match[1]
      name = File.basename(linked)
      bad_libs, good_libs = check_for_bad_library(bad_libs, good_libs, current_library, name, linked)
    end
  end

  [bad_libs, good_libs]
end

#health_check_solarisHash<String, Hash<String, Hash<String, Int>>>

Run healthchecks on Solaris.

Returns:

  • (Hash<String, Hash<String, Hash<String, Int>>>)

    the bad libraries (library_name -> dependency_name -> satisfied_lib_path -> count)



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/omnibus/health_check.rb', line 347

def health_check_solaris
  current_library = nil
  bad_libs = {}
  good_libs = {}

  # The case/when below depends on the "current_library" being output with a : at the end
  # and then the dependencies on subsequent lines in the form "library.so.1 => /lib/64/library.so.1"
  # This output format only happens if ldd is passed multiple libraries (for Solaris, similar to Linux)
  # FIXME if any of the `when` clauses in the `health_check_*` methods run before the `current_library`
  # they probably should error out with an explicit callout of formatting with their environment's
  # respective ldd parsing
  read_shared_libs("find #{project.install_dir}/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'", "xargs ldd") do |line|
    case line
    when /^(.+):$/
      current_library = Regexp.last_match[1]
    when /^\s+(.+) \=\>\s+(.+)( \(.+\))?$/
      name = Regexp.last_match[1]
      linked = Regexp.last_match[2]
      ( bad_libs, good_libs ) = check_for_bad_library(bad_libs, good_libs, current_library, name, linked)
    when /^\s+(.+) \(.+\)$/
      next
    when /^\s+statically linked$/
      next
    when /^\s+not a dynamic executable$/ # ignore non-executable files
    else
      log.warn(log_key) do
        "Line did not match for #{current_library}\n#{line}"
      end
    end
  end

  [bad_libs, good_libs]
end

#relocation_checkHash<String, Hash<Symbol, ...>>

Check dll image location overlap/conflicts on windows.

Returns:

  • (Hash<String, Hash<Symbol, ...>>)

    library_name ->

    :base -> base address
    :size -> the total image size in bytes
    :conflicts -> array of library names that overlap
    


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/omnibus/health_check.rb', line 243

def relocation_check
  conflict_map = {}

  embedded_bin = "#{project.install_dir}/embedded/bin"
  Dir.glob("#{embedded_bin}/*.dll") do |lib_path|
    log.debug(log_key) { "Analyzing dependencies for #{lib_path}" }

    File.open(lib_path, "rb") do |f|
      dump = PEdump.new(lib_path)
      pe = dump.pe f

      # Don't scan dlls for a different architecture.
      next if windows_arch_i386? == pe.x64?

      lib_name = File.basename(lib_path)
      base = pe.ioh.ImageBase
      size = pe.ioh.SizeOfImage
      conflicts = []

      # This can be done more smartly but O(n^2) is just fine for n = small
      conflict_map.each do |candidate_name, details|
        unless details[:base] >= base + size ||
            details[:base] + details[:size] <= base
          details[:conflicts] << lib_name
          conflicts << candidate_name
        end
      end

      conflict_map[lib_name] = {
        base: base,
        size: size,
        conflicts: conflicts,
      }

      log.debug(log_key) { "Discovered #{lib_name} at #{hex} + #{hex}" % [ base, size ] }
    end
  end

  # Filter out non-conflicting entries.
  conflict_map.delete_if do |lib_name, details|
    details[:conflicts].empty?
  end
end

#relocation_checkable?Boolean

Ensure the method relocation_check is able to run

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
# File 'lib/omnibus/health_check.rb', line 224

def relocation_checkable?
  return false unless windows?

  begin
    require "pedump"
    true
  rescue LoadError
    false
  end
end

#run!true

Run the given health check. Healthcheks are skipped on Windows.

Returns:

  • (true)

    if the healthchecks pass

Raises:



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
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
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/omnibus/health_check.rb', line 66

def run!
  unless Config.health_check
    log.info(log_key) { "Health check skipped as specified in config for #{project.name}" }
    return true
  end
  measure("Health check time") do
    log.info(log_key) { "Running health on #{project.name}" }
    bad_libs, good_libs =
      case Ohai["platform"]
      when "mac_os_x"
        health_check_otool
      when "aix"
        health_check_aix
      when "windows"
        # TODO: objdump -p will provided a very limited check of
        # explicit dependencies on windows. Most dependencies are
        # implicit and hence not detected.
        log.warn(log_key) { "Skipping dependency health checks on Windows." }
        [{}, {}]
      when "solaris2"
        health_check_solaris
      when "freebsd", "openbsd", "netbsd"
        health_check_freebsd
      else
        health_check_linux
      end

    unresolved = []
    unreliable = []
    detail = []

    if bad_libs.keys.length > 0
      bad_libs.each do |name, lib_hash|
        lib_hash.each do |lib, linked_libs|
          linked_libs.each do |linked, count|
            if linked =~ /not found/
              unresolved << lib unless unresolved.include? lib
            else
              unreliable << linked unless unreliable.include? linked
            end
            detail << "#{name}|#{lib}|#{linked}|#{count}"
          end
        end
      end

      log.error(log_key) { "Failed!" }
      bad_omnibus_libs, bad_omnibus_bins = bad_libs.keys.partition { |k| k.include? "embedded/lib" }

      log.error(log_key) do
        out = "The following libraries have unsafe or unmet dependencies:\n"

        bad_omnibus_libs.each do |lib|
          out << "    --> #{lib}\n"
        end

        out
      end

      log.error(log_key) do
        out = "The following binaries have unsafe or unmet dependencies:\n"

        bad_omnibus_bins.each do |bin|
          out << "    --> #{bin}\n"
        end

        out
      end

      if unresolved.length > 0
        log.error(log_key) do
          out = "The following requirements could not be resolved:\n"

          unresolved.each do |lib|
            out << "    --> #{lib}\n"
          end

          out
        end
      end

      if unreliable.length > 0
        log.error(log_key) do
          out =  "The following libraries cannot be guaranteed to be on "
          out << "target systems:\n"

          unreliable.each do |lib|
            out << "    --> #{lib}\n"
          end

          out
        end
      end

      log.error(log_key) do
        out = "The precise failures were:\n"

        detail.each do |line|
          item, dependency, location, count = line.split("|")
          reason = location =~ /not found/ ? "Unresolved dependency" : "Unsafe dependency"

          out << "    --> #{item}\n"
          out << "    DEPENDS ON: #{dependency}\n"
          out << "      COUNT: #{count}\n"
          out << "      PROVIDED BY: #{location}\n"
          out << "      FAILED BECAUSE: #{reason}\n"
        end

        out
      end

      raise HealthCheckFailed
    end

    if good_libs.keys.length == 0 && !windows?
      raise "Internal error: no good libraries were found"
    end

    conflict_map = {}

    conflict_map = relocation_check if relocation_checkable?

    if conflict_map.keys.length > 0
      log.warn(log_key) { "Multiple dlls with overlapping images detected" }

      conflict_map.each do |lib_name, data|
        base = data[:base]
        size = data[:size]
        next_valid_base = data[:base] + data[:size]

        log.warn(log_key) do
          out =  "Overlapping dll detected:\n"
          out << "    #{lib_name} :\n"
          out << "    IMAGE BASE: #{hex}\n" % base
          out << "    IMAGE SIZE: #{hex} (#{size} bytes)\n" % size
          out << "    NEXT VALID BASE: #{hex}\n" % next_valid_base
          out << "    CONFLICTS:\n"

          data[:conflicts].each do |conflict_name|
            cbase = conflict_map[conflict_name][:base]
            csize = conflict_map[conflict_name][:size]
            out << "    - #{conflict_name} #{hex} + #{hex}\n" % [cbase, csize]
          end

          out
        end
      end

      # Don't raise an error yet. This is only bad for FIPS mode.
    end

    true
  end
end