Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 30ced74fcb
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 90 lines (72 sloc) 2.5 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
require 'chef/resource'
require 'shellwords'

class Chef::Resource::Archive < Chef::Resource
  def initialize(name, run_context = nil)
    super

    case ::File.extname(name)
    when '.tar'
      @provider = Chef::Provider::Archive::Tar
    when '.gz'
      @provider = Chef::Provider::Archive::Gzip
    when '.zip'
      @provider = Chef::Provider::Archive::Zip
    when '.dmg'
      @provider = Chef::Provider::Archive::Dmg
    end

    @resource_name = :archive
    @action = "extract"
    @allowed_actions.push(:extract)
  end

  def user(arg = nil)
    set_or_return(:user, arg, :kind_of => [String])
  end

  def path(arg = nil)
    set_or_return(:path, arg, :kind_of => [String], :default => ::File.dirname(name))
  end
end


require 'chef/provider'

class Chef::Provider::Archive < Chef::Provider
  include Chef::Mixin::Command

  def load_current_resource
  end

  def action_extract
    run_command(:command => "mkdir -p #{@new_resource.path.shellescape}", :user => @new_resource.user)

    extract @new_resource

    @new_resource.updated_by_last_action true
    Chef::Log.info("Ran #{@new_resource} successfully")
  end

  def extract(resource)
    raise NotImplemented
  end
end

class Chef::Provider::Archive::Tar < Chef::Provider::Archive
  def extract(resource)
    command = "tar -xf #{resource.name.shellescape}"
    run_command(:command => command, :cwd => resource.path, :user => resource.user)
  end
end

class Chef::Provider::Archive::Gzip < Chef::Provider::Archive
  def extract(resource)
    command = "tar -zxf #{resource.name.shellescape}"
    run_command(:command => command, :cwd => resource.path, :user => resource.user)
  end
end

class Chef::Provider::Archive::Zip < Chef::Provider::Archive
  def extract(resource)
    command = "unzip -o #{resource.name.shellescape}"
    run_command(:command => command, :cwd => resource.path, :user => resource.user)
  end
end

class Chef::Provider::Archive::Dmg < Chef::Provider::Archive
  def extract(resource)
    status, stdout, stderr = output_of_command("hdiutil attach #{resource.name.shellescape}", {:user => resource.user})
    if status == 0
      volume = stdout.split("\t").last.chomp
      volume_files = ::File.join(volume, "*")
      run_command(:command => "cp -R #{volume_files.shellescape} #{resource.path.shellescape}", :user => resource.user)
      run_command(:command => "hdiutil detach #{volume.shellescape}", :user => resource.user)
    else
      raise "could not mount #{resource.name}"
    end
  end
end
Something went wrong with that request. Please try again.