Skip to content

Commit

Permalink
- some code cleanup
Browse files Browse the repository at this point in the history
- source rpm => target puppet works now
  • Loading branch information
jordansissel committed Jun 21, 2011
1 parent 3f24e79 commit 66bfb56
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 43 deletions.
3 changes: 1 addition & 2 deletions lib/fpm/builder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def assemble!
# Perhaps something like @package.prepare # Perhaps something like @package.prepare
if @package.respond_to?(:unpack_data_to) if @package.respond_to?(:unpack_data_to)
data_tarball = File.join(builddir, "data.tar.gz") data_tarball = File.join(builddir, "data.tar.gz")
FileUtils.mkdir_p(output) Dir.chdir(builddir) do
Dir.chdir(output) do
FileUtils.mkdir_p(@package.unpack_data_to) FileUtils.mkdir_p(@package.unpack_data_to)
system("gzip -d #{data_tarball}") system("gzip -d #{data_tarball}")
Dir.chdir(@package.unpack_data_to) do Dir.chdir(@package.unpack_data_to) do
Expand Down
12 changes: 6 additions & 6 deletions lib/fpm/package.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class FPM::Package
def initialize(source) def initialize(source)
@source = source @source = source
@logger = Logger.new(STDERR) @logger = Logger.new(STDERR)
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN


@name = source[:name] # || fail @name = source[:name] # || fail


Expand Down Expand Up @@ -119,10 +120,9 @@ def type


def template(path=nil) def template(path=nil)
path ||= "#{type}.erb" path ||= "#{type}.erb"
@template ||= begin @logger.info("Reading template: #{path}")
tpl = File.read("#{FPM::DIRS[:templates]}/#{path}") tpl = File.read("#{FPM::DIRS[:templates]}/#{path}")
ERB.new(tpl, nil, "-") return ERB.new(tpl, nil, "-")
end
end # def template end # def template


def render_spec def render_spec
Expand Down Expand Up @@ -152,15 +152,15 @@ def default_output
end # def default_output end # def default_output


def fixpath(path) def fixpath(path)
p :fixpath => path
if path.first != "/" if path.first != "/"
path = File.join(@source.root, path) path = File.join(@source.root, path)
end end
return path if File.symlink?(path) return path if File.symlink?(path)
@logger.info(:fixpath => path)
realpath = Pathname.new(path).realpath.to_s realpath = Pathname.new(path).realpath.to_s
re = Regexp.new("^#{Regexp.escape(@source.root)}") re = Regexp.new("^#{Regexp.escape(@source.root)}")
realpath.gsub!(re, "") realpath.gsub!(re, "")
p :fixpath => {:path => realpath, :caller => caller[0] } @logger.info(:fixpath_result => realpath)
return realpath return realpath
end # def fixpath end # def fixpath
end # class FPM::Package end # class FPM::Package
4 changes: 3 additions & 1 deletion lib/fpm/source.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def dependencies
attr_accessor :root attr_accessor :root


def initialize(paths, root, params={}) def initialize(paths, root, params={})
@logger = Logger.new(STDERR)
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN

@paths = paths @paths = paths
@root = root @root = root


Expand Down Expand Up @@ -119,7 +122,6 @@ def tar(output, paths, chdir=".")
+ excludes \ + excludes \
+ ["-cf", output, "--no-recursion" ] \ + ["-cf", output, "--no-recursion" ] \
+ dirs + dirs
p :dirs => dirs


::Dir.chdir(chdir) do ::Dir.chdir(chdir) do
system(*dir_tar) if dirs.any? system(*dir_tar) if dirs.any?
Expand Down
12 changes: 5 additions & 7 deletions lib/fpm/source/rpm.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
class FPM::Source::RPM < FPM::Source class FPM::Source::RPM < FPM::Source
def get_metadata def get_metadata
@rpm = @paths.first @rpm = @paths.first
self[:name] = %x{rpm -q --qf '%{name}' -p #{@rpm}} self[:name] = %x{rpm -q --qf '%{name}' -p #{@rpm}}.chomp


self[:version] = %x{rpm -q --qf '%{version}' -p #{@rpm}} self[:version] = %x{rpm -q --qf '%{version}' -p #{@rpm}}.chomp
self[:iteration] = %x{rpm -q --qf '%{release}' -p #{@rpm}} self[:iteration] = %x{rpm -q --qf '%{release}' -p #{@rpm}}.chomp
self[:summary] = %x{rpm -q --qf '%{summary}' -p #{@rpm}} self[:summary] = %x{rpm -q --qf '%{summary}' -p #{@rpm}}.chomp
#self[:description] = %x{rpm -q --qf '%{description}' -p #{@rpm}} #self[:description] = %x{rpm -q --qf '%{description}' -p #{@rpm}}
self[:dependencies] = %x{rpm -qRp #{@rpm}}.split("\n")\ self[:dependencies] = %x{rpm -qRp #{@rpm}}.split("\n")\
.collect { |line| line.strip } .collect { |line| line.strip }
Expand All @@ -20,9 +20,7 @@ def make_tarball!(tar_path, builddir)
::Dir.mkdir(tmpdir) ::Dir.mkdir(tmpdir)
system("rpm2cpio #{@rpm} | (cd #{tmpdir}; cpio -i --make-directories)") system("rpm2cpio #{@rpm} | (cd #{tmpdir}; cpio -i --make-directories)")
tar(tar_path, ".", tmpdir) tar(tar_path, ".", tmpdir)
@paths = "." @paths = ["."]
@root = tmpdir

# TODO(sissel): Make a helper method. # TODO(sissel): Make a helper method.
system(*["gzip", "-f", tar_path]) system(*["gzip", "-f", tar_path])
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/fpm/target/deb.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def architecture
# to ask the system about. # to ask the system about.
arch = %x{dpkg --print-architecture}.chomp arch = %x{dpkg --print-architecture}.chomp
if $?.exitstatus != 0 if $?.exitstatus != 0
arch = %x{uname -m} arch = %x{uname -m}.chomp
@logger.warn("Can't find 'dpkg' tool (need it to get default " \ @logger.warn("Can't find 'dpkg' tool (need it to get default " \
"architecture!). Please specificy --architecture " \ "architecture!). Please specificy --architecture " \
"specifically. (Defaulting now to #{arch})") "specifically. (Defaulting now to #{arch})")
Expand Down
66 changes: 41 additions & 25 deletions lib/fpm/target/puppet.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
require "fpm/package" require "fpm/package"
require "fpm/errors" require "fpm/errors"
require "etc" require "etc"
require "ftools"


class ::Dir #class ::Dir
class << self #class << self
alias :orig_mkdir :mkdir #alias :orig_mkdir :mkdir

#
def mkdir(*args) #def mkdir(*args)
p :mkdir => { :args => args, :caller => caller } ##p :mkdir => { :args => args, :caller => caller }
orig_mkdir(*args) #orig_mkdir(*args)
end #end
end #end
end #end


require "fileutils" # for FileUtils require "fileutils" # for FileUtils


Expand All @@ -36,18 +37,29 @@ def specfile(builddir)
# this package. # this package.
def generate_specfile(builddir) def generate_specfile(builddir)
paths = [] paths = []
@source.paths.each do |path| @logger.info("PWD: #{File.join(builddir, unpack_data_to)}")
Find.find(path) { |p| paths << p } fileroot = File.join(builddir, unpack_data_to)
Dir.chdir(fileroot) do
Find.find(".") do |p|
next if p == "."
paths << p
end
end end
@logger.info(paths[-1])
manifests = %w{package.pp package/remove.pp} manifests = %w{package.pp package/remove.pp}
manifests.each do |name|
dir = File.join(builddir, File.dirname(name)) ::Dir.mkdir(File.join(builddir, "manifests"))
manifests.each do |manifest|
dir = File.join(builddir, "manifests", File.dirname(manifest))
@logger.info("manifests targeting: #{dir}") @logger.info("manifests targeting: #{dir}")
Dir.mkdir(dir) if !File.directory?(dir) ::Dir.mkdir(dir) if !File.directory?(dir)


@logger.info("Generating #{name}") File.open(File.join(builddir, "manifests", manifest), "w") do |f|
File.open(File.join(builddir, name), "w") do |f| @logger.info("manifest: #{f.path}")
f.puts template(File.join("puppet", "#{name}.erb")).result(binding) template = template(File.join("puppet", "#{manifest}.erb"))
::Dir.chdir(fileroot) do
f.puts template.result(binding)
end
end end
end end
end # def generate_specfile end # def generate_specfile
Expand All @@ -63,6 +75,7 @@ def render_spec
#@logger.info(:paths => paths.sort) #@logger.info(:paths => paths.sort)
template.result(binding) template.result(binding)
end # def render_spec end # def render_spec

def unpack_data_to def unpack_data_to
"files" "files"
end end
Expand All @@ -84,15 +97,18 @@ def build!(params)
"exists. Delete it or choose another output (-p flag)") "exists. Delete it or choose another output (-p flag)")
end end


Dir.mkdir(params[:output]) ::Dir.mkdir(params[:output])
builddir = Dir.pwd builddir = ::Dir.pwd


Dir.chdir(params[:output]) do # Copy 'files' from builddir to :output/files
Dir.mkdir("manifests") Find.find("files", "manifests") do |path|
FileUtils.cp(specfile(builddir), "manifests") @logger.info("Copying path: #{path}")
if File.directory?(path)
::Dir.mkdir(File.join(params[:output], path))
else
File.copy(path, File.join(params[:output], path))
end
end end
# Files are now in the 'files' path
# Generate a manifest 'package.pp' with all the information from
end # def build! end # def build!


# The directory we create should just be the name of the package as the # The directory we create should just be the name of the package as the
Expand Down
4 changes: 3 additions & 1 deletion templates/puppet/package/remove.pp.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ class <%= name %>::package::remove {
$version = "<%= version %>" $version = "<%= version %>"
$iteration = "<%= iteration %>" $iteration = "<%= iteration %>"


fail("DO NOT USE THIS. IT IS NOT WELL DEFINED YET.")

file { file {
<% paths.each do |path| -%> <% paths.each do |path| -%>
"<%= fixpath(path) %>": "<%= fixpath(path) %>":
<%= settings %>; ensure => absent;
<% end # paths.each -%> <% end # paths.each -%>
} }
} }

0 comments on commit 66bfb56

Please sign in to comment.