class PackerFiles::Provision::ChefSoloHelper

Attributes

tar_gz_file[RW]

Just remember the name of the file

Public Class Methods

new(tar_gz_file) click to toggle source

Initialize the helper with the path of the .tar.gz file

# File lib/PackerFiles/Provision/ChefSoloHelper.rb, line 18
def initialize(tar_gz_file)
   @tar_gz_file = tar_gz_file
end

Public Instance Methods

download_files(top_dir) click to toggle source

Given a top level directory, and the tar_gz_file, download it locally if it is a remote file.

# File lib/PackerFiles/Provision/ChefSoloHelper.rb, line 24
def download_files(top_dir)
   url = URI::parse(@tar_gz_file)
   if url.scheme.nil?
      return @tar_gz_file
   else
      name    = File.join(top_dir, File.basename(@tar_gz_file))
      content = open(@tar_gz_file) {|f| f.read}
      File.write(name, content)
      return name
   end
end
extract_files(dest_dir, file) click to toggle source

Given a list of .tar.gz files, untar and ungzip them

# File lib/PackerFiles/Provision/ChefSoloHelper.rb, line 37
def extract_files(dest_dir, file)
   untar(ungzip(file), dest_dir)
end

Private Instance Methods

ungzip(tarfile) click to toggle source

un-gzips the given IO, returning the decompressed version as a StringIO

# File lib/PackerFiles/Provision/ChefSoloHelper.rb, line 44
def ungzip(tarfile)
   zip   = Zlib::GzipReader.open(tarfile)
   unzip = StringIO.new(zip.read)
   zip.close
   unzip
end
untar(io, dest_dir) click to toggle source

untars the given IO into the specified directory

# File lib/PackerFiles/Provision/ChefSoloHelper.rb, line 53
def untar(io, dest_dir)
   Gem::Package::TarReader.new io do |tar|
      tar.each do |tarfile|
         dest = File.join(dest_dir, tarfile.full_name)
         if tarfile.directory?
            FileUtils.mkdir_p(dest)
            next
         end
         dir = File.dirname(dest)
         FileUtils.mkdir_p dir unless File.directory?(dir)
         File.open dest, "wb" do |f|
            f.print tarfile.read
         end
      end
   end
end