class Puppet::ModuleTool::Applications::Unpacker

Public Instance Methods

module_name() click to toggle source

@api private

# File lib/puppet/module_tool/applications/unpacker.rb, line 77
def module_name
  metadata = JSON.parse((root_dir + 'metadata.json').read)
  name = metadata['name'][/-(.*)/, 1]
end
move_into(dir) click to toggle source

@api private

# File lib/puppet/module_tool/applications/unpacker.rb, line 83
def move_into(dir)
  dir = Pathname.new(dir)
  dir.rmtree if dir.exist?
  FileUtils.mv(root_dir, dir)
ensure
  FileUtils.rmtree(tmpdir)
end
root_dir() click to toggle source

@api private

# File lib/puppet/module_tool/applications/unpacker.rb, line 63
def root_dir
  return @root_dir if @root_dir

  # Grab the first directory containing a metadata.json file
  metadata_file = Dir["#{tmpdir}/**/metadata.json"].sort_by(&:length)[0]

  if metadata_file
    @root_dir = Pathname.new(metadata_file).dirname
  else
    raise "No valid metadata.json found!"
  end
end
run() click to toggle source
# File lib/puppet/module_tool/applications/unpacker.rb, line 31
def run
  unpack
  sanity_check
  module_dir = @module_path + module_name
  move_into(module_dir)

  # Return the Pathname object representing the directory where the
  # module release archive was unpacked the to.
  return module_dir
end
sanity_check() click to toggle source

@api private Error on symlinks and other junk

# File lib/puppet/module_tool/applications/unpacker.rb, line 44
def sanity_check
  symlinks = Dir.glob("#{tmpdir}/**/*", File::FNM_DOTMATCH).map { |f| Pathname.new(f) }.select {|p| Puppet::FileSystem.symlink? p}
  tmpdirpath = Pathname.new tmpdir

  symlinks.each do |s|
    Puppet.warning "Symlinks in modules are unsupported. Please investigate symlink #{s.relative_path_from tmpdirpath}->#{Puppet::FileSystem.readlink(s)}."
  end
end
tmpdir() click to toggle source

Obtain a suitable temporary path for unpacking tarballs

@api private @return [String] path to temporary unpacking location

# File lib/puppet/module_tool/applications/unpacker.rb, line 95
def tmpdir
  @dir ||= Dir.mktmpdir('tmp', Puppet::Forge::Cache.base_path)
end
unpack() click to toggle source

@api private

# File lib/puppet/module_tool/applications/unpacker.rb, line 54
def unpack
  begin
    Puppet::ModuleTool::Tar.instance.unpack(@filename.to_s, tmpdir, [@module_path.stat.uid, @module_path.stat.gid].join(':'))
  rescue Puppet::ExecutionFailure => e
    raise RuntimeError, "Could not extract contents of module archive: #{e.message}"
  end
end

Public Class Methods

harmonize_ownership(source, target) click to toggle source
# File lib/puppet/module_tool/applications/unpacker.rb, line 16
def self.harmonize_ownership(source, target)
  unless Puppet.features.microsoft_windows?
    source = Pathname.new(source) unless source.respond_to?(:stat)
    target = Pathname.new(target) unless target.respond_to?(:stat)

    FileUtils.chown_R(source.stat.uid, source.stat.gid, target)
  end
end
new(filename, options = {}) click to toggle source
# File lib/puppet/module_tool/applications/unpacker.rb, line 25
def initialize(filename, options = {})
  @filename = Pathname.new(filename)
  super(options)
  @module_path = Pathname(options[:target_dir])
end
unpack(filename, target) click to toggle source
# File lib/puppet/module_tool/applications/unpacker.rb, line 9
def self.unpack(filename, target)
  app = self.new(filename, :target_dir => target)
  app.unpack
  app.sanity_check
  app.move_into(target)
end