Class: Puppet::ModuleTool::Applications::Unpacker

Inherits:
Application show all
Defined in:
lib/puppet/module_tool/applications/unpacker.rb

Constant Summary

Constants inherited from Application

Application::DOCPATTERN, Application::SHOULD_PARSE_CONFIG_DEPRECATION_MSG

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Attribute Summary

Attributes inherited from Application

#command_line, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

[], #app_defaults, available_application_names, banner, clear!, clear?, clear_everything_for_tests, #configure_indirector_routes, controlled_run, exit, find, #handle_logdest_arg, #handlearg, #help, #initialize_app_defaults, interrupted?, #main, #name, option, option_parser_commands, #parse_options, #preinit, restart!, restart_requested?, #run_command, run_mode, #set_log_level, #setup, #setup_logs, should_not_parse_config, should_parse_config, should_parse_config?, stop!, stop_requested?, try_load_class

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(filename, options = {}) ⇒ Unpacker

Returns a new instance of Unpacker.



23
24
25
26
27
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 23

def initialize(filename, options = {})
  @filename = Pathname.new(filename)
  super(options)
  @module_path = Pathname(options[:target_dir])
end

Class Method Details

.harmonize_ownership(source, target) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 14

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

.unpack(filename, target) ⇒ Object



8
9
10
11
12
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 8

def self.unpack(filename, target)
  app = self.new(filename, :target_dir => target)
  app.unpack
  app.move_into(target)
end

Instance Method Details

#module_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
66
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 63

def module_name
   = JSON.parse((root_dir + 'metadata.json').read)
  name = ['name'][/-(.*)/, 1]
end

#move_into(dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
74
75
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 69

def move_into(dir)
  dir = Pathname.new(dir)
  dir.rmtree if dir.exist?
  FileUtils.mv(root_dir, dir)
ensure
  FileUtils.rmtree(tmpdir)
end

#root_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 49

def root_dir
  return @root_dir if @root_dir

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

  if 
    @root_dir = Pathname.new().dirname
  else
    raise "No valid metadata.json found!"
  end
end

#runObject



29
30
31
32
33
34
35
36
37
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 29

def run
  unpack
  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

#tmpdirString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Obtain a suitable temporary path for unpacking tarballs



81
82
83
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 81

def tmpdir
  @dir ||= Dir.mktmpdir('tmp-unpacker', Puppet::Forge::Cache.base_path)
end

#unpackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
43
44
45
46
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 40

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