Permalink
Browse files

Merge pull request #17 from andytinycat/dependency-checks

Dependency checks
  • Loading branch information...
2 parents 67cc3ca + c151cd8 commit d2c2e31c703110f85587634e8d50685e472d5a80 @bernd committed Mar 23, 2013
Showing with 103 additions and 5 deletions.
  1. +1 −1 Gemfile
  2. +2 −0 fpm-cookery.gemspec
  3. +4 −2 lib/fpm/cookery/cli.rb
  4. +89 −0 lib/fpm/cookery/dependency_inspector.rb
  5. +7 −2 lib/fpm/cookery/packager.rb
View
@@ -1,3 +1,3 @@
-source :rubygems
+source "https://rubygems.org"
gemspec
View
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "fpm", "~> 0.4"
s.add_development_dependency "minitest"
s.add_development_dependency "rake"
+ s.add_development_dependency "puppet"
s.add_runtime_dependency "fpm", "~> 0.4"
s.add_runtime_dependency "facter"
+ s.add_runtime_dependency "puppet"
end
View
@@ -21,8 +21,9 @@ def args(argv)
options.banner = \
"Usage: #{program} [options] [path/to/recipe.rb] action [...]"
options.separator "Actions:"
- options.separator " package - builds the package"
- options.separator " clean - cleans up"
+ options.separator " package - builds the package"
+ options.separator " clean - cleans up"
+ options.separator " install-deps - installs build and runtime dependencies"
options.separator "Options:"
options.on("-c", "--color",
@@ -120,6 +121,7 @@ def run
case action
when "clean" ; packager.cleanup
when "package" ; packager.dispense
+ when "install-deps" ; packager.install_deps
else
# TODO(sissel): fail if this happens
Log.error "Unknown action: #{action}"
@@ -0,0 +1,89 @@
+require 'puppet'
+require 'puppet/resource'
+require 'puppet/transaction/report'
+require 'fpm/cookery/facts'
+require 'fpm/cookery/log'
+
+# Init Puppet before using it
+Puppet.initialize_settings
+
+module FPM
+ module Cookery
+ class DependencyInspector
+
+ def self.verify!(depends, build_depends)
+
+ Log.info "Verifying build_depends and depends with Puppet"
+
+ missing = (build_depends + depends).reject do |package|
+ self.package_installed?(package)
+ end
+
+ 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
+
+ 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
+
+ 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
+
+ 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
+
+ end
+ end
+end
@@ -1,6 +1,6 @@
#require 'digest/md5'
#require 'fpm/cookery/recipe_inspector'
-#require 'fpm/cookery/dependency_inspector'
+require 'fpm/cookery/dependency_inspector'
require 'fpm/cookery/utils'
require 'fpm/cookery/source_integrity_check'
require 'fpm/cookery/path'
@@ -34,6 +34,11 @@ def cleanup
FileUtils.rm_rf(recipe.destdir)
end
+ def install_deps
+ DependencyInspector.verify!(recipe.depends, recipe.build_depends)
+ Log.info("All dependencies installed!")
+ end
+
def dispense
env = ENV.to_hash
package_name = "#{recipe.name}-#{recipe.version}"
@@ -44,7 +49,7 @@ def dispense
Log.info ''
# RecipeInspector.verify!(recipe)
- # DependencyInspector.verify!(recipe.depends, recipe.build_depends)
+ DependencyInspector.verify!(recipe.depends, recipe.build_depends)
recipe.installing = false

0 comments on commit d2c2e31

Please sign in to comment.