Puppet: System Administration Automated

Support

Ticket #457: puppet_pkgdmg_curl.patch

File puppet_pkgdmg_curl.patch, 4.3 kB (added by puppet, 2 years ago)

adds curl support to pkgdmg provider for a dramatic performance increase

  • lib/puppet/provider/package/pkgdmg.rb

    old new  
    2020    confine :exists => "/Library/Receipts" 
    2121    commands :installer => "/usr/sbin/installer" 
    2222    commands :hdiutil => "/usr/bin/hdiutil" 
     23    commands :curl => "/usr/bin/curl" 
    2324 
    2425    # JJM We store a cookie for each installed .pkg.dmg in /var/db 
    2526    def self.listbyname 
    2627        Dir.entries("/var/db").find_all { |f| 
    2728            f =~ /^\.puppet_pkgdmg_installed_/ 
    28         }.collect { |f| 
     29        }.collect do |f| 
    2930            name = f.sub(/^\.puppet_pkgdmg_installed_/, '') 
    3031            yield name if block_given? 
    31  
    3232            name 
    33         } 
     33        end 
    3434    end 
    3535 
    3636    def self.list 
     
    4444    end 
    4545 
    4646    def self.installpkg(source, name, orig_source) 
    47       installer "-pkg", source, "-target", "/" 
     47      begin 
     48          installer "-pkg", source, "-target", "/" 
     49      rescue Puppet::ExecutionFailure 
     50          return nil 
     51      end 
    4852      File.open("/var/db/.puppet_pkgdmg_installed_#{name}", "w") do |t| 
    4953          t.print "name: '#{name}'\n" 
    5054          t.print "source: '#{orig_source}'\n" 
    51       end       
     55      end 
    5256    end 
    5357     
    5458    def self.installpkgdmg(source, name) 
     
    5761        end 
    5862        require 'open-uri' 
    5963        require 'puppet/util/plist' 
    60         open(source) do |dmg| 
    61             cmd = "#{command(:hdiutil)} mount -plist -nobrowse -readonly -mountrandom /tmp #{dmg.path}" 
    62             IO.popen(cmd) do |pipe| 
    63                 xml_str = pipe.read 
    64                 ptable = Plist::parse_xml xml_str 
    65                 # JJM Filter out all mount-paths into a single array, discard the rest. 
    66                 mounts = ptable['system-entities'].collect { |entity| 
    67                     entity['mount-point'] 
    68                 }.select { |mountloc|; mountloc } 
    69                 mounts.each do |fspath| 
    70                     Dir.entries(fspath).select { |f| 
    71                         f =~ /\.m{0,1}pkg$/i 
    72                     }.each { |pkg| 
    73                         installpkg("#{fspath}/#{pkg}", name, source) 
    74                     } 
    75                 end 
    76             hdiutil "eject", mounts[0] 
     64        cached_source = source 
     65        if %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ cached_source 
     66            cached_source = "/tmp/#{name}" 
     67            begin 
     68                curl "-o", cached_source, "-C", "-", "-k", "--retry", "3", "--retry-delay", "15", "-s", "--url", source 
     69                Puppet.debug "Success: curl transfered [#{name}]" 
     70            rescue Puppet::ExecutionFailure 
     71                Puppet.debug "curl did not transfer [#{name}].  Falling back to slower open-uri transfer methods." 
     72                cached_source = source 
    7773            end 
    7874        end 
    79     end 
     75         
     76        begin 
     77            open(cached_source) do |dmg| 
     78                cmd = "#{command(:hdiutil)} mount -plist -nobrowse -readonly -mountrandom /tmp #{dmg.path}" 
     79                IO.popen(cmd) do |pipe| 
     80                    xml_str = pipe.read 
     81                    ptable = Plist::parse_xml xml_str 
     82                    # JJM Filter out all mount-paths into a single array, discard the rest. 
     83                    mounts = ptable['system-entities'].collect { |entity| 
     84                        entity['mount-point'] 
     85                    }.select { |mountloc|; mountloc } 
     86                    begin 
     87                        mounts.each do |fspath| 
     88                            Dir.entries(fspath).select { |f| 
     89                                f =~ /\.m{0,1}pkg$/i 
     90                                }.each do |pkg| 
     91                                    installpkg("#{fspath}/#{pkg}", name, source) 
     92                                end 
     93                        end # mounts.each do 
     94                    ensure 
     95                        hdiutil "eject", mounts[0] 
     96                    end # begin 
     97                end # IO.popen() do 
     98            end # open() do 
     99        ensure 
     100            # JJM Remove the file if open-uri didn't already do so. 
     101            File.unlink(cached_source) if File.exist?(cached_source) 
     102        end # begin 
     103    end # def self.installpkgdmg 
    80104 
    81105    def query 
    82106        if FileTest.exists?("/var/db/.puppet_pkgdmg_installed_#{@model[:name]}")