Class: FPM::Cookery::DependencyInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/fpm/cookery/dependency_inspector.rb

Class Method Summary collapse

Class Method Details

.install_package(package) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fpm/cookery/dependency_inspector.rb', line 63

def self.install_package(package)
  Log.info("Installing package: #{package}")
  return unless self.package_suitable?(package)

  # Use Puppet to install a package
  Puppet[:noop] = false
  resource = Puppet::Resource.new("package", package, :parameters => {
    :ensure => "present"
  })
  result = Puppet::Resource.indirection.save(resource)[1]
  failed = Puppet::Resource.indirection.save(resource)[1].resource_statuses.values.first.failed
  if failed
    Log.fatal "While processing depends package '#{package}':"
    result.logs.each {|log_line| Log.fatal log_line}
    exit 1
  else
    result.logs.each {|log_line| Log.info log_line}
  end
end

.missing_packages(*pkgs) ⇒ Object



44
45
46
47
48
# File 'lib/fpm/cookery/dependency_inspector.rb', line 44

def self.missing_packages(*pkgs)
  pkgs.flatten.reject do |package|
    self.package_installed?(package)
  end
end

.package_installed?(package) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fpm/cookery/dependency_inspector.rb', line 50

def self.package_installed?(package)
  Log.info("Verifying package: #{package}")
  return unless self.package_suitable?(package)

  # Use Puppet in noop mode to see if the package exists
  Puppet[:noop] = true
  resource = Puppet::Resource.new("package", package, :parameters => {
    :ensure => "present"
  })
  result    = Puppet::Resource.indirection.save(resource)[1]
  !result.resource_statuses.values.first.out_of_sync
end

.package_suitable?(package) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fpm/cookery/dependency_inspector.rb', line 83

def self.package_suitable?(package)
  # How can we handle "or" style depends?
  if package =~ / \| /
    Log.warn "Required package '#{package}' is an 'or' string; not attempting to find/install a package to satisfy"
    return false
  end

  # We can't handle >=, <<, >>, <=, <, >
  if package =~ />=|<<|>>|<=|<|>/
    Log.warn "Required package '#{package}' has a relative version requirement; not attempting to find/install a package to satisfy"
    return false
  end
  true
end

.verify!(depends, build_depends) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fpm/cookery/dependency_inspector.rb', line 17

def self.verify!(depends, build_depends)
  unless defined?(Puppet::Resource)
    Log.warn "Unable to load Puppet. Automatic dependency installation disabled."
    return
  end

  Log.info "Verifying build_depends and depends with Puppet"

  missing = missing_packages(build_depends + depends)

  if missing.length == 0
    Log.info "All build_depends and depends packages installed"
  else
    Log.info "Missing/wrong version packages: #{missing.join(', ')}"
    if Process.euid != 0
      Log.error "Not running as root; please run 'sudo fpm-cook install-deps' to install dependencies."
      exit 1
    else
      Log.info "Running as root; installing missing/wrong version build_depends and depends with Puppet"
      missing.each do |package|
        self.install_package(package)
      end
    end
  end

end