Class: Berkshelf::Packager

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/packager.rb

Overview

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"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_file) ⇒ Packager

Returns a new instance of Packager.

Parameters:

  • out_file (#to_s)

    path to write the archive to



27
28
29
30
# File 'lib/berkshelf/packager.rb', line 27

def initialize(out_file)
  @out_file           = out_file.to_s
  @out_dir, @filename = File.split(@out_file)
end

Instance Attribute Details

#out_fileString (readonly)

Returns:



23
24
25
# File 'lib/berkshelf/packager.rb', line 23

def out_file
  @out_file
end

Class Method Details

.validate_destination(path) ⇒ Object



17
18
19
# File 'lib/berkshelf/packager.rb', line 17

def validate_destination(path)
  path = path.to_s
end

Instance Method Details

#run(source) ⇒ String

Archive the contents of given path

Parameters:

  • source (#to_s)

    the filepath to archive the contents of

Returns:

  • (String)

    path to the generated archive

Raises:

  • (PackageError)

    if an error is encountered while writing the out_file



42
43
44
45
46
47
48
49
50
51
# File 'lib/berkshelf/packager.rb', line 42

def run(source)
  Dir.chdir(source.to_s) do |dir|
    tgz = Zlib::GzipWriter.new(File.open(out_file, "wb"))
    Archive::Tar::Minitar.pack(".", tgz)
  end

  out_file
rescue SystemCallError => ex
  raise PackageError, ex
end

#validate!nil

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

Returns:

  • (nil)

Raises:

  • (PackageError)

    if running the packager would absolutely not result in a success



60
61
62
63
# File 'lib/berkshelf/packager.rb', line 60

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