Class: ElasticsearchUpdate::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch_update/downloader.rb

Overview

Downloader

ElasticsearchUpdate::Downloader class is used to download the Elasticsearch update file

Parameters

Initilization requires a hash and optional test parameter.

hash is a ruby hash in the following format: { host: String, port: Integer }

test is a boolean identifying whether or not we are in a test if we are, we set the Logger to logging FATAL errors only.

Example

ElasticsearchUpdate::Downloader.new({ host: 'localhost', port: 9200 })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, test = false) ⇒ Downloader

initialize

Allows us to create an instance of the Downloader

Parameters

initialize requires a hash and optional test parameter.

hash is a ruby hash in the following format: { host: String, port: Integer }

test is a boolean identifying whether or not we are in a test if we are, we set the Logger to logging FATAL errors only.

Example

ElasticsearchUpdate::Downloader.new({ host: 'localhost', port: 9200 })


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/elasticsearch_update/downloader.rb', line 43

def initialize(hash, test = false)
  @log = Logger.new(STDOUT)
  if test
    @log.level = Logger::FATAL
  else
    @log.level = Logger::INFO
  end

  @log.debug('Logger created for Downloader.')

  @extension = hash[:extension]
  @base = hash[:base_url]
  @version = hash[:version]
  @download_url = 'https://' + @base +
                  '/elasticsearch/elasticsearch/elasticsearch-' + @version +
                  @extension
  @verify_url = 'https://' + @base +
                '/elasticsearch/elasticsearch/elasticsearch-' + @version +
                @extension + '.sha1.txt'
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



25
26
27
# File 'lib/elasticsearch_update/downloader.rb', line 25

def base
  @base
end

#download_urlObject (readonly)

Returns the value of attribute download_url.



25
26
27
# File 'lib/elasticsearch_update/downloader.rb', line 25

def download_url
  @download_url
end

#extensionObject (readonly)

Returns the value of attribute extension.



25
26
27
# File 'lib/elasticsearch_update/downloader.rb', line 25

def extension
  @extension
end

#update_fileObject

Returns the value of attribute update_file.



26
27
28
# File 'lib/elasticsearch_update/downloader.rb', line 26

def update_file
  @update_file
end

#verify_urlObject (readonly)

Returns the value of attribute verify_url.



25
26
27
# File 'lib/elasticsearch_update/downloader.rb', line 25

def verify_url
  @verify_url
end

#versionObject (readonly)

Returns the value of attribute version.



25
26
27
# File 'lib/elasticsearch_update/downloader.rb', line 25

def version
  @version
end

Instance Method Details

#download_file(test = false) ⇒ Object

download_file

Begins the download process of the Elasticsearch update file.

Parameters

download_file takes an optional boolean argument, named test.

test is an optional boolean which allows us to avoid actually writing to a file while running our tests.

Example

# Not a test
download_file
# Test
download_file(true)


109
110
111
112
113
114
115
116
117
# File 'lib/elasticsearch_update/downloader.rb', line 109

def download_file(test = false)
  @update_file = Tempfile.new(['elasticsearch_update_file', @extension])

  @log.info('Downloading file from url.')

  write_file_from_url(@update_file, @download_url) unless test

  @update_file
end

#download_remote_sha1Object

download_remote_sha1

Begins the download of the Elasticsearch update file SHA1 text file. It then separates the SHA1 value from the filename.

Parameters

download_remote_sha1 takes no parameters.

Requires

download_remote_sha1 requires @verify_url to be set as a string to a .txt file.

Example

download_remote_sha1


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/elasticsearch_update/downloader.rb', line 164

def download_remote_sha1
  @log.info('Downloading Elasticsearch SHA1.')

  @remote_sha1 = ''
  open(@verify_url) do |file|
    @remote_sha1 = file.read
  end

  @remote_sha1 = @remote_sha1.split(/\s\s/)[0]

  @remote_sha1
end

#verify_update_fileObject

verify_update_file

Begins the verification process the Elasticsearch update file by using the instance variables of the file and the downloaded SHA1 value.

Parameters

verify_update_file takes no parameters.

Requires

verify_update_file requires @update_file to be set as a file.

Example

verify_update_file


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/elasticsearch_update/downloader.rb', line 134

def verify_update_file
  @log.info('Beginning integrity check of downloaded file .')
  @file_sha1 = Digest::SHA1.file(@update_file.path).hexdigest

  @log.info('Verifying integrity of downloaded file.')

  if download_remote_sha1 == @file_sha1
    @log.info('Integrity verified.')
    true
  else
    abort('File was not downloaded correctly. Please try again.')
  end
end

#write_file_from_url(file, url) ⇒ Object

write_file_from_url

Allows us to write data from a URL to a file.

Parameters

write_file_from_url requires a file to write to and a url from which to download the file from.

file is a ruby File or Tempfile object

url is a string for the URL.

Example

write_file_from_url(Tempfile.new('example'), http://foo.bar/file.zip)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/elasticsearch_update/downloader.rb', line 79

def write_file_from_url(file, url)
  Net::HTTP.start(@base) do |http|
    begin
      http.request_get(url) do |resp|
        resp.read_body do |segment|
          file.write(segment)
        end
      end
    ensure
      file.close
    end
  end
end