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

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

Overview

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

API:

  • private

Constant Summary

Constants inherited from Application

Application::DOCPATTERN

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

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, #deprecate, #deprecated?, environment_mode, exit, find, get_environment_mode, #handle_logdest_arg, #handlearg, #help, #initialize_app_defaults, interrupted?, #log_runtime_environment, #main, #name, option, option_parser_commands, #parse_options, #preinit, restart!, restart_requested?, #run_command, run_mode, #set_log_level, #setup, #setup_logs, stop!, stop_requested?, #summary, try_load_class

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

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

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

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

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.

Returns a new instance of Unpacker.

API:

  • private



26
27
28
29
30
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 26

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

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.

API:

  • private



17
18
19
20
21
22
23
24
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 17

def self.harmonize_ownership(source, target)
  unless Puppet::Util::Platform.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

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.

API:

  • private



10
11
12
13
14
15
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 10

def self.unpack(filename, target)
  app = self.new(filename, :target_dir => target)
  app.unpack
  app.sanity_check
  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.

API:

  • private



78
79
80
81
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 78

def module_name
   = Puppet::Util::Json.load((root_dir + 'metadata.json').read)
  ['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.

API:

  • private



84
85
86
87
88
89
90
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 84

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.

API:

  • private



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

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

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.

API:

  • private



32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 32

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_checkObject

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.

Error on symlinks and other junk

API:

  • private



45
46
47
48
49
50
51
52
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 45

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 %{from}->%{to}.") % { from: s.relative_path_from(tmpdirpath), to: Puppet::FileSystem.readlink(s) }
  end
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

Returns:

  • path to temporary unpacking location

API:

  • private



96
97
98
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 96

def tmpdir
  @dir ||= Dir.mktmpdir('tmp', 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.

API:

  • private



55
56
57
58
59
60
61
# File 'lib/puppet/module_tool/applications/unpacker.rb', line 55

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: %{message}") % { message: e.message }
  end
end