Method: TreasureData::Updater::ModuleDefinition#update

Defined in:
lib/td/updater.rb

#update(autoupdate = false) ⇒ Object



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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/td/updater.rb', line 152

def update(autoupdate = false)
  wait_for_lock(updating_lock_path, 5) do
    require "td"
    require 'open-uri'
    require "tmpdir"
    require "zip/zip"

    latest_version = fetch(version_endpoint)

    if compare_versions(latest_version, latest_local_version) > 0
      Dir.mktmpdir do |download_dir|

        indicator = Command::TimeBasedDownloadProgressIndicator.new(
          "Downloading updated toolbelt package", Time.new.to_i, 2)
        # downloading the update compressed file
        File.open("#{download_dir}/td-update.zip", "wb") do |file|
          endpoint = update_package_endpoint
          $stdout.puts "\npackage '#{endpoint}'... " unless ENV['TD_TOOLBELT_DEBUG'].nil?
          stream_fetch(endpoint, file) {
            indicator.update
          }
        end
        indicator.finish

        $stdout.print "Unpacking updated toolbelt package..."
        Zip::ZipFile.open("#{download_dir}/td-update.zip") do |zip|
          zip.each do |entry|
            target = File.join(download_dir, entry.to_s)
            FileUtils.mkdir_p(File.dirname(target))
            zip.extract(entry, target) { true }
          end
        end
        $stdout.print " done\n"

        FileUtils.rm "#{download_dir}/td-update.zip"

        old_version = latest_local_version
        new_version = client_version_from_path(download_dir)

        if compare_versions(new_version, old_version) < 0 && !autoupdate
          raise_error "Installed version (#{old_version}) is newer than the latest available update (#{new_version})"
        end

        FileUtils.rm_rf updated_client_path
        FileUtils.mkdir_p File.dirname(updated_client_path)
        FileUtils.cp_r(download_dir, updated_client_path)

        new_version
      end
    else
      false # already up to date
    end
  end
ensure
  FileUtils.rm_f(updating_lock_path)
end