class Berkshelf::Packager

A class for archiving and compressing directory containing one or more cookbooks.

Example:

Archiving a path containing the cookbooks:
  * "/path/to/cookbooks/my_face"
  * "/path/to/cookbooks/nginx"

irb> source   = "/path/to/cookbooks"
irb> packager = Berkshelf::Packager.new("some/path/cookbooks.tar.gz")
irb> packager.run(source) #=> "some/path/cookbooks.tar.gz"

Attributes

filename[R]

@return [String]

out_dir[R]

@return [String]

out_file[R]

@return [String]

Public Class Methods

new(out_file) click to toggle source

@param [#to_s] out_file

path to write the archive to
# File lib/berkshelf/packager.rb, line 28
def initialize(out_file)
  @out_file           = out_file.to_s
  @out_dir, @filename = File.split(@out_file)
end
validate_destination(path) click to toggle source
# File lib/berkshelf/packager.rb, line 18
def validate_destination(path)
  path.to_s
end

Public Instance Methods

run(source) click to toggle source

Archive the contents of given path

@param [#to_s] source

the filepath to archive the contents of

@raise [PackageError]

if an error is encountered while writing the out_file

@return [String]

path to the generated archive
# File lib/berkshelf/packager.rb, line 43
def run(source)
  begin
    dest = Zlib::GzipWriter.open(out_file)
    tar = RelativeTarWriter.new(dest, source)
    Find.find(source) do |entry|
      next if source == entry

      Archive::Tar::Minitar.pack_file(entry, tar)
    end
  ensure
    tar.close
    dest.close
  end

  out_file
rescue SystemCallError => ex
  raise PackageError, ex
end
validate!() click to toggle source

Validate that running the packager would be successful. Returns nil if would be successful and raises an error if would not.

@raise [PackageError]

if running the packager would absolutely not result in a success

@return [nil]

# File lib/berkshelf/packager.rb, line 69
def validate!
  raise PackageError, "Path is not a directory: #{out_dir}" unless File.directory?(out_dir)
  raise PackageError, "Directory is not writable: #{out_dir}" unless File.writable?(out_dir)
end