class Elasticsearch::Embedded::Downloader

Constants

DEFAULT_VERSION

Default version of elasticsearch to download

TEMPORARY_PATH

Default temporary path used by downloader

Attributes

path[R]
version[R]

Public Class Methods

download(arguments = {}) click to toggle source

Download elasticsearch distribution and unzip it in the specified temporary path

# File lib/elasticsearch/embedded/downloader.rb, line 24
def self.download(arguments = {})
  new(arguments).perform
end
new(args = {}) click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 18
def initialize(args = {})
  @version = args[:version] || ENV['ELASTICSEARCH_VERSION'] || DEFAULT_VERSION
  @path = args[:path] || ENV['ELASTICSEARCH_DOWNLOAD_PATH'] || TEMPORARY_PATH
end

Public Instance Methods

dist_folder() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 42
def dist_folder
  @dist_folder ||= final_path.gsub /\.zip\Z/, ''
end
downloaded?() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 34
def downloaded?
  File.exists?(final_path)
end
executable() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 54
def executable
  @executable ||= File.join(dist_folder, 'bin', 'elasticsearch')
end
extracted?() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 38
def extracted?
  File.directory?(dist_folder)
end
final_path() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 46
def final_path
  @final_path ||= File.join(working_dir, "elasticsearch-#{version}.zip")
end
perform() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 28
def perform
  download_file
  extract_file
  self
end
working_dir() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 50
def working_dir
  @working_dir ||= File.realpath(path)
end

Private Instance Methods

build_progress_bar(total) click to toggle source

Build a progress bar to download elasticsearch

# File lib/elasticsearch/embedded/downloader.rb, line 89
def build_progress_bar(total)
  if total && total.to_i > 0
    @download_progress_bar = ProgressBar.create title: "Downloading elasticsearch #{version}", total: total,
                                                format: '%t |%bᗧ%i| %p%% (%r KB/sec) %e', progress_mark: ' ', remainder_mark: '・',
                                                rate_scale: ->(rate) { rate / 1024 }, smoothing: 0.7
  end
end
download_file() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 60
def download_file
  return if downloaded?
  open(final_path, 'wb') do |target|
    download_options = {
        content_length_proc: ->(t) { build_progress_bar(t) },
        progress_proc: ->(s) { increment_progress(s) }
    }
    # direct call here to avoid spec issues with Kernel#open
    distfile = OpenURI.open_uri("https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-#{version}.zip", download_options)
    target << distfile.read
  end
end
extract_file() click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 73
def extract_file
  return if extracted?
  Dir.chdir(working_dir) do
    # Extract archive in path, after block CWD is restored
    Zip::File.open(final_path) do |zip_file|
      # Extract all entries into working dir
      zip_file.each(&:extract)
    end
  end
  # ensure main executable has execute permission
  File.chmod(0755, executable)
  # Create folder for log files
  FileUtils.mkdir(File.join(dist_folder, 'logs'))
end
increment_progress(size) click to toggle source
# File lib/elasticsearch/embedded/downloader.rb, line 97
def increment_progress(size)
  @download_progress_bar.progress = size if @download_progress_bar
end