Sha256: 0ec6c81cb7d1d9906b8738b308f5b712c7bf36bd6801448d9261739debb6228d

Contents?: true

Size: 1.79 KB

Versions: 15

Compression:

Stored size: 1.79 KB

Contents

require 'pathname'
require 'tmpdir'

module Puppet::ModuleTool
  module Applications
    class Unpacker < Application

      def initialize(filename, options = {})
        @filename = Pathname.new(filename)
        parsed = parse_filename(filename)
        @module_name = parsed[:module_name]
        super(options)
        @module_path = Pathname(options[:target_dir])
        @module_dir = @module_path + parsed[:dir_name]
      end

      def run
        extract_module_to_install_dir

        # Return the Pathname object representing the directory where the
        # module release archive was unpacked the to, and the module release
        # name.
        @module_dir
      end

      # Obtain a suitable temporary path for building and unpacking tarballs
      #
      # @return [Pathname] path to temporary build location
      def build_dir
        Puppet::Forge::Cache.base_path + "tmp-unpacker-#{Digest::SHA1.hexdigest(@filename.basename.to_s)}"
      end

      private

      def extract_module_to_install_dir
        delete_existing_installation_or_abort!

        build_dir.mkpath
        begin
          begin
            Puppet::ModuleTool::Tar.instance(@module_name).unpack(@filename.to_s, build_dir.to_s, [@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

          # grab the first directory
          extracted = build_dir.children.detect { |c| c.directory? }
          FileUtils.mv extracted, @module_dir
        ensure
          build_dir.rmtree
        end
      end

      def delete_existing_installation_or_abort!
        return unless @module_dir.exist?
        FileUtils.rm_rf(@module_dir, :secure => true)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
puppet-3.4.3 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.4.2 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.4.1 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.4.0 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.4.0.rc2 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.4.0.rc1 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.2 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.1 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.1.rc3 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.1.rc2 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.1.rc1 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.0 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.0.rc3 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.3.0.rc2 lib/puppet/module_tool/applications/unpacker.rb
puppet-3.2.4 lib/puppet/module_tool/applications/unpacker.rb