Last active
August 29, 2015 14:00
-
-
Save brandocorp/11065553 to your computer and use it in GitHub Desktop.
Remote File Pre-Caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/handler' | |
require 'chef/rest' | |
require 'chef/version_constraint' | |
class Chef | |
class RemoteFile | |
class CacheHandler < ::Chef::Handler | |
attr_reader :config | |
def initialize(config={}) | |
@config = config | |
end | |
def report | |
@run_status.all_resources.each do |resource| | |
threads = [] | |
if resource.provider == Chef::Provider::RemoteFileCache | |
threads << Thread.new { resource.run_action(:pre_cache) } | |
end | |
threads.each {|t| t.join } | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/provider/file' | |
require 'chef/deprecation/provider/remote_file' | |
require 'chef/deprecation/warnings' | |
class Chef | |
class Provider | |
class RemoteFileCache < Chef::Provider::File | |
def initialize(new_resource, run_context) | |
@content_class = Chef::Provider::RemoteFile::Content | |
super | |
end | |
def load_current_resource | |
@current_resource = Chef::Resource::RemoteFileCache.new(@new_resource.name) | |
super | |
end | |
private | |
def managing_content? | |
return true if @new_resource.checksum | |
return true if !@new_resource.source.nil? && @action != :create_if_missing | |
false | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/resource/file' | |
require 'chef/provider/remote_file' | |
require 'chef/mixin/securable' | |
class Chef | |
class Resource | |
class RemoteFile < Chef::Resource::File | |
include Chef::Mixin::Securable | |
provides :remote_file_cache, :on_platforms => :all | |
def initialize(name, run_context=nil) | |
super | |
@resource_name = :remote_file_cache | |
@action = "pre_cache" | |
@source = [] | |
@use_etag = true | |
@use_last_modified = true | |
@ftp_active_mode = false | |
@headers = {} | |
@provider = Chef::Provider::RemoteFileCache | |
end | |
def source(*args) | |
if !args.empty? | |
args = Array(args).flatten | |
validate_source(args) | |
@source = args | |
elsif self.instance_variable_defined?(:@source) == true | |
@source | |
end | |
end | |
def checksum(args=nil) | |
set_or_return( | |
:checksum, | |
args, | |
:kind_of => String | |
) | |
end | |
# Disable or enable ETag and Last Modified conditional GET. Equivalent to | |
# use_etag(true_or_false) | |
# use_last_modified(true_or_false) | |
def use_conditional_get(true_or_false) | |
use_etag(true_or_false) | |
use_last_modified(true_or_false) | |
end | |
def use_etag(args=nil) | |
set_or_return( | |
:use_etag, | |
args, | |
:kind_of => [ TrueClass, FalseClass ] | |
) | |
end | |
alias :use_etags :use_etag | |
def use_last_modified(args=nil) | |
set_or_return( | |
:use_last_modified, | |
args, | |
:kind_of => [ TrueClass, FalseClass ] | |
) | |
end | |
def ftp_active_mode(args=nil) | |
set_or_return( | |
:ftp_active_mode, | |
args, | |
:kind_of => [ TrueClass, FalseClass ] | |
) | |
end | |
def headers(args=nil) | |
set_or_return( | |
:headers, | |
args, | |
:kind_of => Hash | |
) | |
end | |
def after_created | |
validate_source(@source) | |
end | |
private | |
def validate_source(source) | |
raise ArgumentError, "#{resource_name} has an empty source" if source.empty? | |
source.each do |src| | |
unless absolute_uri?(src) | |
raise Exceptions::InvalidRemoteFileURI, | |
"#{src.inspect} is not a valid `source` parameter for #{resource_name}. `source` must be an absolute URI or an array of URIs." | |
end | |
end | |
end | |
def absolute_uri?(source) | |
source.kind_of?(String) and URI.parse(source).absolute? | |
rescue URI::InvalidURIError | |
false | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment