Skip to content

Commit

Permalink
Revert "Merge branch 'CHEF-1031'"
Browse files Browse the repository at this point in the history
This reverts commit a82935b, reversing
changes made to 464087c.

Conflicts:
	lib/chef/provider/remote_file/ftp.rb
  • Loading branch information
btm committed Mar 1, 2013
1 parent 4346211 commit f6027a5
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 375 deletions.
93 changes: 44 additions & 49 deletions lib/chef/provider/remote_file.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#
# Author:: Jesse Campbell (<hikeit@gmail.com>)
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
Expand All @@ -18,9 +17,10 @@
#

require 'chef/provider/file'
require 'rest_client'
require 'chef/rest'
require 'uri'
require 'tempfile'
require 'net/https'

class Chef
class Provider
Expand All @@ -40,12 +40,24 @@ def action_create
Chef::Log.debug("#{@new_resource} checksum matches target checksum (#{@new_resource.checksum}) - not updating")
else
sources = @new_resource.source
raw_file, raw_file_source = try_multiple_sources(sources)
source = sources.shift
begin
rest = Chef::REST.new(source, nil, nil, http_client_opts(source))
raw_file = rest.streaming_request(rest.create_url(source), {})
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error, Net::HTTPFatalError => e
Chef::Log.debug("#{@new_resource} cannot be downloaded from #{source}")
if source = sources.shift
Chef::Log.debug("#{@new_resource} trying to download from another mirror")
retry
else
raise e
end
end
if matches_current_checksum?(raw_file)
Chef::Log.debug "#{@new_resource} target and source checksums are the same - not updating"
else
description = []
description << "copy file downloaded from #{raw_file_source} into #{@new_resource.path}"
description << "copy file downloaded from #{@new_resource.source} into #{@new_resource.path}"
description << diff_current(raw_file.path)
converge_by(description) do
backup_new_resource
Expand Down Expand Up @@ -90,55 +102,38 @@ def backup_new_resource
end
end

private

# Given an array of source uris, iterate through them until one does not fail
def try_multiple_sources(sources)
sources = sources.dup
source = sources.shift
begin
uri = URI.parse(source)
raw_file = grab_file_from_uri(uri)
rescue ArgumentError => e
raise e
rescue => e
if e.is_a?(RestClient::Exception)
error = "Request returned #{e.message}"
else
error = e.to_s
end
Chef::Log.debug("#{@new_resource} cannot be downloaded from #{source}: #{error}")
if source = sources.shift
Chef::Log.debug("#{@new_resource} trying to download from another mirror")
retry
else
raise e
end
end
if uri.userinfo
uri.password = "********"
def source_file(source, current_checksum, &block)
if absolute_uri?(source)
fetch_from_uri(source, &block)
elsif !Chef::Config[:solo]
fetch_from_chef_server(source, current_checksum, &block)
else
fetch_from_local_cookbook(source, &block)
end
return raw_file, uri.to_s
end

# Given a source uri, return a Tempfile, or a File that acts like a Tempfile (close! method)
def grab_file_from_uri(uri)
if URI::HTTP === uri
#HTTP or HTTPS
raw_file = RestClient::Request.execute(:method => :get, :url => uri.to_s, :raw_response => true).file
elsif URI::FTP === uri
#FTP
raw_file = FTP::fetch(uri, @new_resource.ftp_active_mode)
elsif uri.scheme == "file"
#local/network file
raw_file = ::File.new(uri.path, "r")
def raw_file.close!
self.close
end
else
raise ArgumentError, "Invalid uri. Only http(s), ftp, and file are currently supported"
def http_client_opts(source)
opts={}
# CHEF-3140
# 1. If it's already compressed, trying to compress it more will
# probably be counter-productive.
# 2. Some servers are misconfigured so that you GET $URL/file.tgz but
# they respond with content type of tar and content encoding of gzip,
# which tricks Chef::REST into decompressing the response body. In this
# case you'd end up with a tar archive (no gzip) named, e.g., foo.tgz,
# which is not what you wanted.
if @new_resource.path =~ /gz$/ or source =~ /gz$/
opts[:disable_gzip] = true
end
raw_file
opts
end

private

def absolute_uri?(source)
URI.parse(source).absolute?
rescue URI::InvalidURIError
false
end

end
Expand Down
95 changes: 0 additions & 95 deletions lib/chef/provider/remote_file/ftp.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/chef/providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,5 @@
require 'chef/provider/deploy/revision'
require 'chef/provider/deploy/timestamped'

require 'chef/provider/remote_file/ftp'

require "chef/provider/lwrp_base"
require 'chef/provider/registry_key'
11 changes: 1 addition & 10 deletions lib/chef/resource/remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def initialize(name, run_context=nil)
super
@resource_name = :remote_file
@action = "create"
@source = []
@ftp_active_mode = false
@source = nil
@provider = Chef::Provider::RemoteFile
end

Expand All @@ -55,14 +54,6 @@ def checksum(args=nil)
)
end

def ftp_active_mode(args=nil)
set_or_return(
:ftp_active_mode,
args,
:kind_of => [ TrueClass, FalseClass ]
)
end

def after_created
validate_source(@source)
end
Expand Down
Binary file removed spec/data/remote_file/nyan_cat.png.gz
Binary file not shown.
43 changes: 10 additions & 33 deletions spec/functional/resource/remote_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
include_context Chef::Resource::File

let(:file_base) { "remote_file_spec" }
let(:source) { 'http://localhost:9000/nyan_cat.png' }
let(:expected_content) do
content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
content
end

def create_resource
node = Chef::Node.new
Expand Down Expand Up @@ -63,44 +71,13 @@ def create_resource
f.read
end
}
@api.get("/nyan_cat.png.gz", 200, nil, { 'Content-Type' => 'application/gzip', 'Content-Encoding' => 'gzip' } ) {
File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz'), "rb") do |f|
f.read
end
}
end

after(:all) do
@server.stop
end

context "when using normal encoding" do
let(:source) { 'http://localhost:9000/nyan_cat.png' }
let(:expected_content) do
content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
content
end

it_behaves_like "a file resource"
it_behaves_like "a file resource"

it_behaves_like "a securable resource with reporting"
end

context "when using gzip encoding" do
let(:source) { 'http://localhost:9000/nyan_cat.png.gz' }
let(:expected_content) do
content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png.gz'), "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
content
end

it_behaves_like "a file resource"

it_behaves_like "a securable resource with reporting"
end
it_behaves_like "a securable resource with reporting"
end
21 changes: 10 additions & 11 deletions spec/tiny_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,20 @@ def clear
@routes = {GET => [], PUT => [], POST => [], DELETE => []}
end

def get(path, response_code, data=nil, headers=nil, &block)
@routes[GET] << Route.new(path, Response.new(response_code, data, headers, &block))
def get(path, response_code, data=nil, &block)
@routes[GET] << Route.new(path, Response.new(response_code,data, &block))
end

def put(path, response_code, data=nil, headers=nil, &block)
@routes[PUT] << Route.new(path, Response.new(response_code, data, headers, &block))
def put(path, response_code, data=nil, &block)
@routes[PUT] << Route.new(path, Response.new(response_code,data, &block))
end

def post(path, response_code, data=nil, headers=nil, &block)
@routes[POST] << Route.new(path, Response.new(response_code, data, headers, &block))
def post(path, response_code, data=nil, &block)
@routes[POST] << Route.new(path, Response.new(response_code,data, &block))
end

def delete(path, response_code, data=nil, headers=nil, &block)
@routes[DELETE] << Route.new(path, Response.new(response_code, data, headers, &block))
def delete(path, response_code, data=nil, &block)
@routes[DELETE] << Route.new(path, Response.new(response_code,data, &block))
end

def call(env)
Expand Down Expand Up @@ -183,15 +183,14 @@ def to_s
class Response
HEADERS = {'Content-Type' => 'application/json'}

def initialize(response_code=200, data=nil, headers=nil, &block)
def initialize(response_code=200,data=nil, &block)
@response_code, @data = response_code, data
@response_headers = headers ? HEADERS.merge(headers) : HEADERS
@block = block_given? ? block : nil
end

def call
data = @data || @block.call
[@response_code, @response_headers, Array(data)]
[@response_code, HEADERS, Array(data)]
end

def to_s
Expand Down
Loading

0 comments on commit f6027a5

Please sign in to comment.