Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add "safesystem" function
Basically, its a replacement for system. If the command fails, then
it raises an exception and prints out the entire command that was
attempted.

This will prevent issues where fpm finished (with return code 0) and
produces an artifact with real size (>0 bytes) but the artifact
is worthless as it contains nothing.

Fixes #86
  • Loading branch information
tabletcorry committed Aug 2, 2011
1 parent f284420 commit bdfee82
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 41 deletions.
7 changes: 4 additions & 3 deletions lib/fpm/builder.rb
@@ -1,4 +1,5 @@
require "fileutils"
require "fpm/util"
require "pathname"

class FPM::Builder
Expand Down Expand Up @@ -102,10 +103,10 @@ def assemble!
data_tarball = File.join(builddir, "data.tar.gz")
Dir.chdir(builddir) do
FileUtils.mkdir_p(@package.unpack_data_to)
system("gzip -d #{data_tarball}")
safesystem("gzip -d #{data_tarball}")
Dir.chdir(@package.unpack_data_to) do
@source.root = Dir.pwd
system("tar -xf #{data_tarball.gsub(/\.gz$/, "")}")
safesystem("tar -xf #{data_tarball.gsub(/\.gz$/, "")}")
end
end
end
Expand Down Expand Up @@ -185,7 +186,7 @@ def edit_specfile
# TODO(sissel): support editing multiple files for targets like
# puppet which generate multiple manifests.
editor = ENV['FPM_EDITOR'] || ENV['EDITOR'] || 'vi'
system("#{editor} '#{package.specfile(builddir)}'")
safesystem("#{editor} '#{package.specfile(builddir)}'")
unless File.size? package.specfile(builddir)
puts "Empty specfile. Aborting."
exit 1
Expand Down
5 changes: 3 additions & 2 deletions lib/fpm/source.rb
@@ -1,4 +1,5 @@
require "fpm/namespace"
require "fpm/util"

# Abstract class for a "thing to build a package from"
class FPM::Source
Expand Down Expand Up @@ -124,15 +125,15 @@ def tar(output, paths, chdir=".")
+ dirs

::Dir.chdir(chdir) do
system(*dir_tar) if dirs.any?
safesystem(*dir_tar) if dirs.any?
end

files_tar = [ tar_cmd ] \
+ excludes \
+ [ "--owner=root", "--group=root", "-rf", output ] \
+ paths
::Dir.chdir(chdir) do
system(*files_tar)
safesystem(*files_tar)
end
end # def tar

Expand Down
7 changes: 4 additions & 3 deletions lib/fpm/source/dir.rb
@@ -1,6 +1,7 @@
require "fpm/source"
require "fileutils"
require "fpm/rubyfixes"
require "fpm/util"

class FPM::Source::Dir < FPM::Source
def get_metadata
Expand Down Expand Up @@ -31,7 +32,7 @@ def make_tarball!(tar_path, builddir)
::FileUtils.mkdir_p(dest)
rsync = ["rsync", "-a", path, dest]
p rsync
system(*rsync)
safesystem(*rsync)

# FileUtils.cp_r is pretty silly about how it copies files in some
# cases (funky permissions, etc)
Expand All @@ -45,14 +46,14 @@ def make_tarball!(tar_path, builddir)
end

::Dir.chdir("#{builddir}/tarbuild") do
system("ls #{builddir}/tarbuild")
safesystem("ls #{builddir}/tarbuild")
tar(tar_path, ".")
end
else
tar(tar_path, paths)
end

# TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path])
safesystem(*["gzip", "-f", tar_path])
end # def make_tarball!
end # class FPM::Source::Dir
5 changes: 3 additions & 2 deletions lib/fpm/source/gem.rb
Expand Up @@ -3,6 +3,7 @@
require "rubygems/package"
require "rubygems"
require "fileutils"
require "fpm/util"

class FPM::Source::Gem < FPM::Source
def self.flags(opts, settings)
Expand Down Expand Up @@ -136,14 +137,14 @@ def make_tarball!(tar_path, builddir)
end

args << gem
system(*args)
safesystem(*args)

# make paths relative (/foo becomes ./foo)
tar(tar_path, @paths.collect {|p| ".#{p}"}, tmpdir)
FileUtils.rm_r(tmpdir)

# TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path])
safesystem(*["gzip", "-f", tar_path])
end

end # class FPM::Source::Gem
5 changes: 3 additions & 2 deletions lib/fpm/source/npm.rb
@@ -1,5 +1,6 @@
require "fpm/namespace"
require "fpm/source"
require "fpm/util"
require "fileutils"

class FPM::Source::Npm < FPM::Source
Expand All @@ -25,11 +26,11 @@ def make_tarball!(tar_path, builddir)
::FileUtils.mkdir_p(installdir)
args = ["gem", "install", "--quiet", "--no-ri", "--no-rdoc",
"--install-dir", installdir, "--ignore-dependencies", @paths.first]
system(*args)
safesystem(*args)
tar(tar_path, ".", tmpdir)

# TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path])
safesystem(*["gzip", "-f", tar_path])
end

end # class FPM::Source::Gem
11 changes: 4 additions & 7 deletions lib/fpm/source/python.rb
@@ -1,5 +1,6 @@
require "fpm/namespace"
require "fpm/source"
require "fpm/util"
require "rubygems/package"
require "rubygems"
require "fileutils"
Expand Down Expand Up @@ -47,11 +48,7 @@ def download(package, version=nil)
want_pkg = "#{package}==#{version}"
end

return_value = system(self[:settings][:easy_install], "--editable", "--build-directory", @tmpdir, want_pkg)

if return_value.nil?
raise "The execution of #{self[:settings][:easy_install]} failed"
end
safesystem(self[:settings][:easy_install], "--editable", "--build-directory", @tmpdir, want_pkg)

# easy_install will put stuff in @tmpdir/packagename/, flatten that.
# That is, we want @tmpdir/setup.py, and start with
Expand Down Expand Up @@ -101,7 +98,7 @@ def make_tarball!(tar_path, builddir)
# Some setup.py's assume $PWD == current directory of setup.py, so let's
# chdir first.
::Dir.chdir(dir) do
system(self[:settings][:python], "setup.py", "bdist")
safesystem(self[:settings][:python], "setup.py", "bdist")
end

dist_tar = ::Dir.glob(File.join(dir, "dist", "*.tar.gz")).first
Expand All @@ -110,7 +107,7 @@ def make_tarball!(tar_path, builddir)

@paths = [ "." ]

system("cp", dist_tar, "#{tar_path}.gz")
safesystem("cp", dist_tar, "#{tar_path}.gz")
end # def make_tarball!

def garbage
Expand Down
5 changes: 3 additions & 2 deletions lib/fpm/source/rpm.rb
@@ -1,4 +1,5 @@
require "fpm/source"
require "fpm/util"

class FPM::Source::RPM < FPM::Source
def get_metadata
Expand All @@ -18,10 +19,10 @@ def get_metadata
def make_tarball!(tar_path, builddir)
tmpdir = "#{tar_path}.dir"
::Dir.mkdir(tmpdir)
system("rpm2cpio #{@rpm} | (cd #{tmpdir}; cpio -i --make-directories)")
safesystem("rpm2cpio #{@rpm} | (cd #{tmpdir}; cpio -i --make-directories)")
tar(tar_path, ".", tmpdir)
@paths = ["."]
# TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path])
safesystem(*["gzip", "-f", tar_path])
end
end
7 changes: 4 additions & 3 deletions lib/fpm/source/tar.rb
@@ -1,6 +1,7 @@
require "fpm/rubyfixes"
require "fpm/source"
require "fpm/util"
require "fileutils"
require "fpm/rubyfixes"

class FPM::Source::Tar < FPM::Source
def get_metadata
Expand Down Expand Up @@ -31,7 +32,7 @@ def make_tarball!(tar_path, builddir)
end
#puts("tar #{flags}")
#sleep 5
system("tar #{flags}")
safesystem("tar #{flags}")

if self[:prefix]
@paths = [self[:prefix]]
Expand All @@ -44,6 +45,6 @@ def make_tarball!(tar_path, builddir)
end

# TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path])
safesystem(*["gzip", "-f", tar_path])
end # def make_tarball!
end # class FPM::Source::Dir
13 changes: 7 additions & 6 deletions lib/fpm/target/deb.rb
Expand Up @@ -2,6 +2,7 @@
require "fpm/namespace"
require "fpm/package"
require "fpm/errors"
require "fpm/util"

class FPM::Target::Deb < FPM::Package

Expand Down Expand Up @@ -66,16 +67,16 @@ def build!(params)
self.scripts.each do |name, path|
case name
when "pre-install"
system("cp #{path} ./preinst")
safesystem("cp #{path} ./preinst")
control_files << "preinst"
when "post-install"
system("cp #{path} ./postinst")
safesystem("cp #{path} ./postinst")
control_files << "postinst"
when "pre-uninstall"
system("cp #{path} ./prerm")
safesystem("cp #{path} ./prerm")
control_files << "prerm"
when "post-uninstall"
system("cp #{path} ./postrm")
safesystem("cp #{path} ./postrm")
control_files << "postrm"
else raise "Unsupported script name '#{name}' (path: #{path})"
end # case name
Expand All @@ -87,13 +88,13 @@ def build!(params)
end

# Make the control
system("tar -zcf control.tar.gz #{control_files.map{ |f| "./#{f}" }.join(" ")}")
safesystem("tar -zcf control.tar.gz #{control_files.map{ |f| "./#{f}" }.join(" ")}")

# create debian-binary
File.open("debian-binary", "w") { |f| f.puts "2.0" }

# pack up the .deb
system("ar -qc #{params[:output]} debian-binary control.tar.gz data.tar.gz")
safesystem("ar -qc #{params[:output]} debian-binary control.tar.gz data.tar.gz")
end # def build

def default_output
Expand Down
8 changes: 3 additions & 5 deletions lib/fpm/target/rpm.rb
@@ -1,4 +1,5 @@
require "fpm/package"
require "fpm/util"

class FPM::Target::Rpm < FPM::Package
def architecture
Expand Down Expand Up @@ -44,14 +45,11 @@ def build!(params)
"--define", "_sourcedir #{Dir.pwd}",
"--define", "_rpmdir #{Dir.pwd}/RPMS",
"#{name}.spec"]
ret = system(*args)
if !ret
raise "rpmbuild failed (exit code: #{$?.exitstatus})"
end
safesystem(*args)

Dir["#{Dir.pwd}/RPMS/**/*.rpm"].each do |path|
# This should only output one rpm, should we verify this?
system("mv", path, params[:output])
safesystem("mv", path, params[:output])
end
end # def build!
end # class FPM::Target::RPM
13 changes: 7 additions & 6 deletions lib/fpm/target/solaris.rb
Expand Up @@ -2,6 +2,7 @@
require "fpm/namespace"
require "fpm/package"
require "fpm/errors"
require "fpm/util"

# TODO(sissel): Add dependency checking support.
# IIRC this has to be done as a 'checkinstall' step.
Expand All @@ -25,10 +26,10 @@ def build!(params)
self.scripts.each do |name, path|
case name
when "pre-install"
system("cp #{path} ./preinstall")
safesystem("cp #{path} ./preinstall")
File.chmod(0755, "./preinstall")
when "post-install"
system("cp #{path} ./postinstall")
safesystem("cp #{path} ./postinstall")
File.chmod(0755, "./postinstall")
when "pre-uninstall"
raise FPM::InvalidPackageConfiguration.new(
Expand All @@ -43,9 +44,9 @@ def build!(params)

# Unpack data.tar.gz so we can build a package from it.
Dir.mkdir("data")
system("gzip -d data.tar.gz");
safesystem("gzip -d data.tar.gz");
Dir.chdir("data") do
system("tar -xf ../data.tar");
safesystem("tar -xf ../data.tar");
end

#system("(echo 'i pkginfo'; pkgproto data=/) > Prototype")
Expand All @@ -69,10 +70,10 @@ def build!(params)
end # File prototype

# Should create a package directory named by the package name.
system("pkgmk -o -d .")
safesystem("pkgmk -o -d .")

# Convert the 'package directory' built above to a real solaris package.
system("pkgtrans -s . #{params[:output]} #{name}")
safesystem("pkgtrans -s . #{params[:output]} #{name}")
end # def build

def default_output
Expand Down
7 changes: 7 additions & 0 deletions lib/fpm/util.rb
@@ -0,0 +1,7 @@
def safesystem(*call)
return_val = system(*call)
if !return_val
raise "'#{call}' failed with error code: #{$?.exitstatus}"
end
return return_val
end # def safesystem

0 comments on commit bdfee82

Please sign in to comment.