summaryrefslogtreecommitdiff
path: root/lib/chef/provider/remote_file.rb
blob: 4f82dea9a03d048ecc6be99b96e37a3a9fecabc5 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#
# Author:: Jesse Campbell (<hikeit@gmail.com>)
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/file'
require 'uri'
require 'tempfile'

class Chef
  class Provider
    class RemoteFile < Chef::Provider::File

      include Chef::Mixin::EnforceOwnershipAndPermissions

      def load_current_resource
        @current_resource = Chef::Resource::RemoteFile.new(@new_resource.name)
        super
        fileinfo = load_fileinfo
        if fileinfo && fileinfo["checksum"] == @current_resource.checksum
          @current_resource.etag fileinfo["etag"]
          @current_resource.last_modified fileinfo["last_modified"]
          @current_resource.source fileinfo["src"]
        end
      end

      def action_create
        Chef::Log.debug("#{@new_resource} checking for changes")

        if current_resource_matches_target_checksum?
          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)
          if raw_file.nil?
            Chef::Log.info("#{@new_resource} matched #{raw_file_source}, not updating")
          elsif matches_current_checksum?(raw_file)
            Chef::Log.info("#{@new_resource} downloaded from #{raw_file_source}, checksums match, not updating")
            raw_file.close!
          else
            Chef::Log.info("#{@new_resource} downloaded from #{raw_file_source}")
            description = []
            description << "copy file downloaded from #{raw_file_source} into #{@new_resource.path}"
            description << diff_current(raw_file.path)
            converge_by(description) do
              backup_new_resource
              FileUtils.cp raw_file.path, @new_resource.path
              Chef::Log.info "#{@new_resource} updated"
              raw_file.close!
              save_fileinfo(raw_file_source)
            end
            # whyrun mode cleanup - the temp file will never be used,
            # so close/unlink it here.
            if whyrun_mode?
              raw_file.close!
            end
          end
        end
        set_all_access_controls
        update_new_file_state
      end

      def current_resource_matches_target_checksum?
        @new_resource.checksum && @current_resource.checksum && @current_resource.checksum =~ /^#{Regexp.escape(@new_resource.checksum)}/
      end

      def matches_current_checksum?(candidate_file)
        Chef::Log.debug "#{@new_resource} checking for file existence of #{@new_resource.path}"
        @new_resource.checksum(checksum(candidate_file.path))
        if ::File.exists?(@new_resource.path)
          Chef::Log.debug "#{@new_resource} file exists at #{@new_resource.path}"
          Chef::Log.debug "#{@new_resource} target checksum: #{@current_resource.checksum}"
          Chef::Log.debug "#{@new_resource} source checksum: #{@new_resource.checksum}"

          @new_resource.checksum == @current_resource.checksum
        else
          Chef::Log.debug "#{@new_resource} creating #{@new_resource.path}"
          false
        end
      end

      def backup_new_resource
        if ::File.exists?(@new_resource.path)
          Chef::Log.debug "#{@new_resource} checksum changed from #{@current_resource.checksum} to #{@new_resource.checksum}"
          backup @new_resource.path
        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 SocketError, Errno::ECONNREFUSED, Errno::ENOENT, Errno::EACCES, Timeout::Error, Net::HTTPFatalError, Net::FTPError => e
          Chef::Log.warn("#{@new_resource} cannot be downloaded from #{source}: #{e.to_s}")
          if source = sources.shift
            Chef::Log.info("#{@new_resource} trying to download from another mirror")
            retry
          else
            raise e
          end
        end
        if uri.userinfo
          uri.password = "********"
        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_modified_since = @new_resource.last_modified
        if_none_match = @new_resource.etag
        # The current resource's source uri was saved with a masked password
        uri_dup = uri.dup
        if uri_dup.userinfo
          uri_dup.password = "********"
        end
        if uri_dup.to_s == @current_resource.source[0]
          if_modified_since ||= @current_resource.last_modified
          if_none_match ||= @current_resource.etag
        end
        if URI::HTTP === uri
          #HTTP or HTTPS
          raw_file, mtime, etag = RemoteFile::HTTP.fetch(uri, if_modified_since, if_none_match)
        elsif URI::FTP === uri
          #FTP
          raw_file, mtime = RemoteFile::FTP.fetch(uri, @new_resource.ftp_active_mode, if_modified_since)
          etag = nil
        elsif uri.scheme == "file"
          #local/network file
          raw_file, mtime = RemoteFile::LocalFile.fetch(uri, if_modified_since)
          etag = nil
        else
          raise ArgumentError, "Invalid uri. Only http(s), ftp, and file are currently supported"
        end
        unless raw_file.nil?
          @new_resource.etag etag unless @new_resource.etag
          @new_resource.last_modified mtime unless @new_resource.last_modified
        end
        return raw_file
      end

      def cache_path
        cache_path = new_resource.name.sub(/^([A-Za-z]:)/, "")  # strip drive letter on Windows
      end

      def load_fileinfo
        begin
          Chef::JSONCompat.from_json(Chef::FileCache.load("remote_file/#{cache_path}"))
        rescue Chef::Exceptions::FileNotFound
          nil
        end
      end

      def save_fileinfo(source)
        cache = Hash.new
        cache["etag"] = @new_resource.etag
        cache["last_modified"] = @new_resource.last_modified
        cache["src"] = source
        cache["checksum"] = @new_resource.checksum
        Chef::FileCache.store("remote_file/#{cache_path}", cache.to_json)
      end
    end
  end
end