elasticsearch_update 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8184b6ab6ae68ea0bee8b38f1cc54059ed3bbb7d
4
+ data.tar.gz: f8b5bca19aed12f5e68ef3aaea2c9ff4170f2042
5
+ SHA512:
6
+ metadata.gz: 8d17edc14813b94caa113d2d3ca5b790e7df47093b8330fc63c10b511390aceb9713c781badc00ec85fcea0b664da25322ede0003c6232542d901b713d64e670
7
+ data.tar.gz: f6fce5f86ddb2f53e19077cc24e00d5d1a31d74a18972597d0289a17d4f433ee97ec268865890c6c339c8db5fc9eb7eec1d8f9e68bc64bd6474fd4f724610d52
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ - "2.1.0"
5
+ - "2.2.0"
6
+
7
+ before_install:
8
+ - gem update --system
9
+ - gem --version
10
+
11
+ gemfile:
12
+ - Gemfile
13
+
14
+ notifications:
15
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticsearch_update.gemspec
4
+ gemspec
5
+ gem 'highline'
6
+ gem 'rake', group: :test, require: nil
7
+ gem 'minitest', group: :test, require: nil
8
+ gem 'bundler', group: :test, require: nil
9
+ gem 'rubyzip'
10
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kevin Kirsche
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ [![Build Status](https://travis-ci.org/kkirsche/elasticsearch_update.svg?branch=master)](https://travis-ci.org/kkirsche/elasticsearch_update) [![Code Climate](https://codeclimate.com/github/kkirsche/elasticsearch_update/badges/gpa.svg)](https://codeclimate.com/github/kkirsche/elasticsearch_update) [![Test Coverage](https://codeclimate.com/github/kkirsche/elasticsearch_update/badges/coverage.svg)](https://codeclimate.com/github/kkirsche/elasticsearch_update) [![Dependency Status](https://gemnasium.com/kkirsche/elasticsearch_update.svg)](https://gemnasium.com/kkirsche/elasticsearch_update)
2
+ # Elasticsearch Update v1.0.1
3
+
4
+ This gem allows users to easily update their 1.0 and later Elasticsearch instance on the local machine from one of the following formats.
5
+
6
+ 1. .zip (in progress)
7
+ 2. .tar.gz (in progress)
8
+ 3. .deb (complete)
9
+ 4. .rpm (complete)
10
+
11
+ Installation follows the upgrade recommendations of Elasticsearch found [here](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-upgrade.html).
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'elasticsearch_update'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install elasticsearch_update
28
+
29
+ ## Usage
30
+
31
+ After installing the gem, execute the `elasticsearch_update` command and it will walk you through the upgrade process.
32
+
33
+ For development, execute `bundle exec bin/elasticsearch_update` and the gem will walk you through the upgrade process.
34
+
35
+ For testing, execute `rake test` and the gem will run the local tests.
36
+
37
+ ## Versioning
38
+
39
+ For transparency into my release cycle and in striving to maintain backward compatibility, Elasticsearch Update is maintained under [the Semantic Versioning guidelines](http://semver.org/). Sometimes I screw up, but I'll adhere to those rules whenever possible.
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/kkirsche/elasticsearch_update/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
48
+
49
+ ## Warning
50
+ Before performing an upgrade, it’s a good idea to back up the data on your system. This will allow you to roll back in the event of a problem with the upgrade. The upgrades sometimes include upgrades to the Lucene libraries used by Elasticsearch to access the index files, and after an index file has been updated to work with a new version of Lucene, it may not be accessible to the versions of Lucene present in earlier Elasticsearch releases. I am not responsible for any loss of data, damaged data, etc. caused by use of this gem.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test/"
6
+ t.libs << "bin/"
7
+ t.libs << "lib/"
8
+ t.pattern = "test/elasticsearch_update/*_spec.rb"
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'elasticsearch_update'
3
+
4
+ args = ARGV.dup
5
+ ARGV.clear
6
+ command = args.shift.strip rescue 'help'
7
+
8
+ ElasticsearchUpdate::Cli.start
data/bin/update.rb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'elasticsearch_update'
3
+
4
+ args = ARGV.dup
5
+ ARGV.clear
6
+ command = args.shift.strip rescue 'help'
7
+
8
+ ElasticsearchUpdate::Cli.start
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elasticsearch_update/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elasticsearch_update"
8
+ spec.version = ElasticsearchUpdate::VERSION
9
+ spec.authors = ["Kevin Kirsche"]
10
+ spec.email = ["kev.kirsche@gmail.com"]
11
+ spec.summary = %q{Updates the elasticsearch instance on the local machine.}
12
+ spec.description = %q{Updates the elasticsearch instance from a deb or rpm on the local machine. Assumes elasticsearch is a service.}
13
+ spec.homepage = "https://github.com/kkirsche/elasticsearch_update"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables << 'elasticsearch_update'
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "5.4.3"
24
+ spec.add_runtime_dependency 'highline', '1.6.21'
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'elasticsearch_update/version'
2
+ require 'elasticsearch_update/cli'
3
+ require 'elasticsearch_update/downloader'
4
+ require 'elasticsearch_update/elasticsearch'
5
+ require 'elasticsearch_update/installer'
6
+ require 'elasticsearch_update/version'
7
+ require 'elasticsearch_update/wizard'
8
+
9
+ # This module updates a debian Elasticsearch instance
10
+ module ElasticsearchUpdate
11
+ end
@@ -0,0 +1,32 @@
1
+ require 'logger'
2
+
3
+ module ElasticsearchUpdate
4
+ # This class is in charge of retrieving and downloading data.
5
+ class Cli
6
+ def self.start
7
+ @log = Logger.new(STDOUT)
8
+ @log.level = Logger::INFO
9
+
10
+ @log.debug('Logger created for CLI.')
11
+
12
+ wizard = Wizard.new
13
+
14
+ es_client = Elasticsearch.new wizard.es_location_hash
15
+ es_client.cluster_routing_allocation('none')
16
+ es_client.shutdown_local_node
17
+
18
+ downloader = Downloader.new(wizard.download_hash)
19
+ file = downloader.download_file
20
+ downloader.verify_update_file
21
+
22
+ installer = Installer.new(wizard.sudo_password,
23
+ downloader.extension)
24
+ installer.install_file(file)
25
+
26
+ es_client.start_elasticsearch(installer)
27
+ @log.info('Waiting for Elasticsearch to start.')
28
+ sleep 10
29
+ es_client.cluster_routing_allocation('all')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,83 @@
1
+ require 'logger'
2
+ require 'digest/sha1'
3
+ require 'tempfile'
4
+ require 'open-uri'
5
+
6
+ module ElasticsearchUpdate
7
+ # This class is in charge of retrieving and downloading data.
8
+ class Downloader
9
+ attr_reader :extension, :base, :version, :download_url, :verify_url
10
+ attr_accessor :update_file
11
+ def initialize(hash, test = false)
12
+ @log = Logger.new(STDOUT)
13
+ if test
14
+ @log.level = Logger::FATAL
15
+ else
16
+ @log.level = Logger::INFO
17
+ end
18
+
19
+ @log.debug('Logger created for Downloader.')
20
+
21
+ @extension = hash[:extension]
22
+ @base = hash[:base_url]
23
+ @version = hash[:version]
24
+ @download_url = 'https://' + @base +
25
+ '/elasticsearch/elasticsearch/elasticsearch-' + @version +
26
+ @extension
27
+ @verify_url = 'https://' + @base +
28
+ '/elasticsearch/elasticsearch/elasticsearch-' + @version +
29
+ @extension + '.sha1.txt'
30
+ end
31
+
32
+ def write_file_from_url(file, url)
33
+ Net::HTTP.start(@base) do |http|
34
+ begin
35
+ http.request_get(url) do |resp|
36
+ resp.read_body do |segment|
37
+ file.write(segment)
38
+ end
39
+ end
40
+ ensure
41
+ file.close
42
+ end
43
+ end
44
+ end
45
+
46
+ def download_file(test = false)
47
+ @update_file = Tempfile.new(['elasticsearch_update_file', @extension])
48
+
49
+ @log.info('Downloading file from url.')
50
+
51
+ write_file_from_url(@update_file, @download_url) unless test
52
+
53
+ @update_file
54
+ end
55
+
56
+ def verify_update_file
57
+ @log.info('Beginning integrity check of downloaded file .')
58
+ @file_sha1 = Digest::SHA1.file(@update_file.path).hexdigest
59
+
60
+ @log.info('Verifying integrity of downloaded file.')
61
+
62
+ if download_remote_sha1 == @file_sha1
63
+ @log.info('Integrity verified.')
64
+ true
65
+ else
66
+ abort('File was not downloaded correctly. Please try again.')
67
+ end
68
+ end
69
+
70
+ def download_remote_sha1
71
+ @log.info('Downloading Elasticsearch SHA1.')
72
+
73
+ @remote_sha1 = ''
74
+ open(@verify_url) do |file|
75
+ @remote_sha1 = file.read
76
+ end
77
+
78
+ @remote_sha1 = @remote_sha1.split(/\s\s/)[0]
79
+
80
+ @remote_sha1
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,72 @@
1
+ require 'logger'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module ElasticsearchUpdate
6
+ # This class is in charge of retrieving and downloading data.
7
+ class Elasticsearch
8
+ attr_reader :es_host, :es_port
9
+ def initialize(hash = { host: 'localhost', port: 9200 }, test = false)
10
+ @log = Logger.new(STDOUT)
11
+ if test
12
+ @log.level = Logger::FATAL
13
+ else
14
+ @log.level = Logger::INFO
15
+ end
16
+
17
+ @log.debug('Logger created for Elasticsearch.')
18
+
19
+ @es_host = hash[:host]
20
+ @es_port = hash[:port]
21
+ end
22
+
23
+ def cluster_routing_allocation(type)
24
+ @log.info('Disabling cluster routing allocation')
25
+
26
+ begin
27
+ req = Net::HTTP::Put.new('/_cluster/settings',
28
+ 'Content-Type' => 'application/javascript')
29
+ req.body = { transient: { 'cluster.routing.allocation.enable' => type } }
30
+ req.body = req.body.to_json
31
+ response = Net::HTTP.new(@es_host, @es_port).start {|http| http.request(req) }
32
+ rescue Errno::ECONNREFUSED
33
+ puts 'Connection could not be made to Elasticsearch at'
34
+ puts @es_host + ':' + @es_port + '/_cluster/settings'
35
+ abort('Please verify that Elasticsearch is available at this address.')
36
+ end
37
+
38
+ response
39
+ end
40
+
41
+ def shutdown_local_node
42
+ @log.info('Shutting down local node')
43
+ @shutdown_uri = URI('http://' + @es_host + ':' + @es_port.to_s + '/_cluster/nodes/_local/_shutdown')
44
+ response = Net::HTTP.post_form(@shutdown_uri, {})
45
+
46
+ response
47
+ end
48
+
49
+ def start_elasticsearch_service(password)
50
+ @log.info('Starting elasticsearch service')
51
+ system('echo ' + password + ' | sudo -S service elasticsearch start')
52
+ end
53
+
54
+ def start_elasticsearch_binary(path)
55
+ @log.info('Starting elasticsearch binary')
56
+ system(path + 'bin/elasticsearch')
57
+ end
58
+
59
+ def start_elasticsearch(installer_obj)
60
+ case installer_obj.extension
61
+ when '.zip'
62
+ start_elasticsearch_binary(wizard.test)
63
+ when '.deb'
64
+ start_elasticsearch_service(installer_obj.sudo_password)
65
+ when '.rpm'
66
+ start_elasticsearch_service(installer_obj.sudo_password)
67
+ when '.tar.gz'
68
+
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,49 @@
1
+ require 'logger'
2
+
3
+ module ElasticsearchUpdate
4
+ # This class is in charge of retrieving and downloading data.
5
+ class Installer
6
+ attr_accessor :sudo_password, :extension
7
+ def initialize(password, extension, test = false)
8
+ @log = Logger.new(STDOUT)
9
+ if test
10
+ @log.level = Logger::FATAL
11
+ else
12
+ @log.level = Logger::INFO
13
+ end
14
+
15
+ @log.debug('Logger created for Installer.')
16
+
17
+ @sudo_password = password
18
+ @extension = extension
19
+ end
20
+
21
+ def install_file(file)
22
+ case @extension
23
+ when '.zip'
24
+ true
25
+ when '.deb'
26
+ install_update_file(file, @extension)
27
+ when '.rpm'
28
+ install_update_file(file, @extension)
29
+ when '.tar.gz'
30
+ true
31
+ end
32
+ end
33
+
34
+ def install_update_file(file, extension)
35
+ @log.info('Installing' + extension + 'file.')
36
+ command = 'echo ' + @sudo_password + ' | '
37
+ case extension
38
+ when '.deb'
39
+ command += 'sudo -S dpkg -i "' + file.path + '"'
40
+ when '.rpm'
41
+ command += 'sudo -S rpm -i "' + file.path + '"'
42
+ end
43
+ Kernel.system(command)
44
+ end
45
+
46
+ def unzip_file(file)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module ElasticsearchUpdate
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'highline/import'
2
+
3
+ module ElasticsearchUpdate
4
+ # This class is in charge of retrieving and downloading data.
5
+ class Wizard
6
+ def extension
7
+ choose do |menu|
8
+ menu.prompt = 'Which type of upgrade are we doing? '
9
+
10
+ menu.choice(:deb) { @choice = '.deb' }
11
+ # menu.choice(:zip) { @choice = '.zip' }
12
+ menu.choice(:rpm) { @choice = '.rpm' }
13
+ # menu.choice(:tar) { @choice = '.tar.gz' }
14
+ end
15
+
16
+ @choice
17
+ end
18
+
19
+ def version
20
+ ask('What version of Elasticsearch should we update to? (major.minor.path) ', String) { |q| q.validate = /\d\.\d\.\d/ }
21
+ end
22
+
23
+ def host
24
+ response = ask('What is your Elasticsearch hostname? (Default: localhost) ', String)
25
+ response = 'localhost' if response == ''
26
+
27
+ response
28
+ end
29
+
30
+ def port
31
+ response = ask('What is your Elasticsearch port? (Default: 9200) ', String)
32
+
33
+ response = '9200' if response == ''
34
+
35
+ response
36
+ end
37
+
38
+ def es_location_hash
39
+ {
40
+ host: host,
41
+ port: port
42
+ }
43
+ end
44
+
45
+ def elasticsearch_fs_location
46
+ ask('In what directory does Elasticsearch run? ', String)
47
+ end
48
+
49
+ def sudo_password
50
+ ask('What password should be used while updating Elasticsearch? ') { |q| q.echo = "x" }
51
+ end
52
+
53
+ def download_hash
54
+ {
55
+ base_url: 'download.elasticsearch.org',
56
+ version: version,
57
+ extension: extension
58
+ }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,134 @@
1
+ require 'minitest_helper'
2
+
3
+ module ElasticsearchUpdate
4
+ class TestDownloader < Downloader
5
+ def open(_url, &b)
6
+ b.call StringIO.new "d377e39343e5cc277104beee349e1578dc50f7f8 elasticsearch-1.4.2.deb"
7
+ end
8
+
9
+ def abort(string)
10
+ string
11
+ end
12
+ end
13
+
14
+ # The TestDownloader class below tests the Downloader class from the library
15
+ class TestDownloader
16
+ describe 'Downloader', 'Used to download and verify Elasticsearch file' do
17
+
18
+ it 'should initialize and correctly assign values.' do
19
+ hash =
20
+ {
21
+ base_url: 'download.elasticsearch.org',
22
+ version: '1.4.2',
23
+ extension: '.deb'
24
+ }
25
+
26
+ @downloader = TestDownloader.new(hash, false)
27
+ assert_kind_of ElasticsearchUpdate::Downloader, @downloader
28
+
29
+ @downloader.base.must_equal 'download.elasticsearch.org'
30
+ @downloader.extension.must_equal '.deb'
31
+ @downloader.version.must_equal '1.4.2'
32
+ @downloader.download_url.must_equal 'https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.deb'
33
+ @downloader.verify_url.must_equal 'https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.deb.sha1.txt'
34
+ end
35
+
36
+ it 'should retrieve the correct SHA1 value from Elasticsearch\'s website' do
37
+ hash =
38
+ {
39
+ base_url: 'download.elasticsearch.org',
40
+ version: '1.4.2',
41
+ extension: '.deb'
42
+ }
43
+
44
+ @mock_file = Minitest::Mock.new
45
+ @mock_file.expect(:read, 'd377e39343e5cc277104beee349e1578dc50f7f8 elasticsearch-1.4.2.deb')
46
+
47
+ Kernel.stub :open, nil, @mock_file do
48
+ @downloader = TestDownloader.new(hash, true)
49
+ @downloader.download_remote_sha1.must_equal 'd377e39343e5cc277104beee349e1578dc50f7f8'
50
+ end
51
+ end
52
+
53
+ it "should retrieve the verify the file's SHA1 value against Elasticsearch's" do
54
+ hash =
55
+ {
56
+ base_url: 'download.elasticsearch.org',
57
+ version: '1.4.2',
58
+ extension: '.deb'
59
+ }
60
+
61
+ @mock_file_obj = Minitest::Mock.new
62
+ @mock_result = 'd377e39343e5cc277104beee349e1578dc50f7f8'
63
+ @mock_file_obj.expect(:hexdigest, @mock_result)
64
+
65
+ @mock_file = Minitest::Mock.new
66
+ @mock_file.expect(:path, 'fake/path')
67
+
68
+ Digest::SHA1.stub :file, @mock_file_obj do
69
+ @downloader = TestDownloader.new(hash, true)
70
+ @downloader.update_file = @mock_file
71
+ @downloader.verify_update_file.must_equal true
72
+ end
73
+ end
74
+
75
+ it "should retrieve the verify the file's SHA1 value against Elasticsearch's" do
76
+ hash =
77
+ {
78
+ base_url: 'download.elasticsearch.org',
79
+ version: '1.4.2',
80
+ extension: '.deb'
81
+ }
82
+
83
+ @mock_file_obj = Minitest::Mock.new
84
+ @mock_result = '0'
85
+ @mock_file_obj.expect(:hexdigest, @mock_result)
86
+
87
+ @mock_file = Minitest::Mock.new
88
+ @mock_file.expect(:path, 'fake/path')
89
+
90
+ Digest::SHA1.stub :file, @mock_file_obj do
91
+ @downloader = TestDownloader.new(hash, true)
92
+ @downloader.update_file = @mock_file
93
+ @downloader.verify_update_file.must_equal 'File was not downloaded correctly. Please try again.'
94
+ end
95
+ end
96
+
97
+ it 'creates a tempfile to store the Elasticsearch deb file' do
98
+ hash =
99
+ {
100
+ base_url: 'download.elasticsearch.org',
101
+ version: '1.4.2',
102
+ extension: '.deb'
103
+ }
104
+
105
+ @downloader = TestDownloader.new(hash, true)
106
+ assert_kind_of Tempfile, @downloader.download_file(true)
107
+ end
108
+
109
+ it 'writes a downloaded file' do
110
+ hash =
111
+ {
112
+ base_url: 'download.elasticsearch.org',
113
+ version: '1.4.2',
114
+ extension: '.deb'
115
+ }
116
+
117
+ @mock_resp = Minitest::Mock.new
118
+ @mock_resp.expect(:read_body, 'Test')
119
+
120
+ @mock_http = Minitest::Mock.new
121
+ @mock_http.expect(:request_get, @mock_resp, [String])
122
+
123
+ @mock_file = Minitest::Mock.new
124
+ @mock_file.expect(:write, true, [String])
125
+ @mock_file.expect(:close, true)
126
+
127
+ @downloader = TestDownloader.new(hash, true)
128
+ Net::HTTP.stub :start, true, @mock_http do
129
+ @downloader.write_file_from_url(@mock_file, 'http://test.org')
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,92 @@
1
+ require 'minitest_helper'
2
+
3
+ module ElasticsearchUpdate
4
+ class TestElasticsearch < Elasticsearch
5
+ def system(command)
6
+ command
7
+ end
8
+ end
9
+ # The TestInstaller class below tests the Downloader class from the library
10
+ class TestElasticsearch
11
+ describe 'Elasticsearch', 'Used to interact with Elasticsearch' do
12
+
13
+ it 'should initialize without errors' do
14
+ @es_client = TestElasticsearch.new
15
+ assert_kind_of Elasticsearch, @es_client
16
+
17
+ @es_client.es_port.must_equal 9200
18
+ @es_client.es_host.must_equal 'localhost'
19
+ end
20
+
21
+ it 'should disable cluster allocation' do
22
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
23
+
24
+ @mock_http = Minitest::Mock.new
25
+ @mock_http.expect(:start, 200)
26
+
27
+ Net::HTTP.stub :new, @mock_http do
28
+ @es_client.cluster_routing_allocation('none').must_equal 200
29
+ end
30
+ end
31
+
32
+ it 'should enable cluster allocation' do
33
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
34
+
35
+ @mock_http = Minitest::Mock.new
36
+ @mock_http.expect(:start, 200)
37
+
38
+ Net::HTTP.stub :new, @mock_http do
39
+ @es_client.cluster_routing_allocation('all').must_equal 200
40
+ end
41
+ end
42
+
43
+ it 'should shutdown the local node' do
44
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
45
+
46
+ Net::HTTP.stub :post_form, 200 do
47
+ @es_client.shutdown_local_node.must_equal 200
48
+ end
49
+ end
50
+
51
+ it 'start elasticsearch service' do
52
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
53
+
54
+ result = @es_client.start_elasticsearch_service('test')
55
+
56
+ result.must_equal 'echo test | sudo -S service elasticsearch start'
57
+ end
58
+
59
+ it 'start elasticsearch binary' do
60
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
61
+
62
+ result = @es_client.start_elasticsearch_binary('/path/to/elasticsearch/')
63
+
64
+ result.must_equal '/path/to/elasticsearch/bin/elasticsearch'
65
+ end
66
+
67
+ it 'start elasticsearch service from the deb extension' do
68
+ @mock = Minitest::Mock.new
69
+ @mock.expect(:extension, '.deb')
70
+ @mock.expect(:sudo_password, 'test')
71
+
72
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
73
+
74
+ result = @es_client.start_elasticsearch(@mock)
75
+
76
+ result.must_equal 'echo test | sudo -S service elasticsearch start'
77
+ end
78
+
79
+ it 'start elasticsearch service from the rpm extension' do
80
+ @mock = Minitest::Mock.new
81
+ @mock.expect(:extension, '.rpm')
82
+ @mock.expect(:sudo_password, 'test')
83
+
84
+ @es_client = TestElasticsearch.new({ host: 'localhost', port: 9200 }, true)
85
+
86
+ result = @es_client.start_elasticsearch(@mock)
87
+
88
+ result.must_equal 'echo test | sudo -S service elasticsearch start'
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,65 @@
1
+ require 'minitest_helper'
2
+
3
+ module ElasticsearchUpdate
4
+ # The TestInstaller class below tests the Downloader class from the library
5
+ class TestInstaller < Minitest::Test
6
+ describe 'Installer', 'Used to install Elasticsearch file' do
7
+
8
+ it 'should initialize and correctly assign values.' do
9
+ @installer = ElasticsearchUpdate::Installer.new('test_password', '.deb', false)
10
+ assert_kind_of ElasticsearchUpdate::Installer, @installer
11
+
12
+ @installer.sudo_password.must_equal 'test_password'
13
+ @installer.extension.must_equal '.deb'
14
+ end
15
+
16
+ it 'should install the .deb file' do
17
+ @mock_file = Minitest::Mock.new
18
+ @mock_file.expect(:path, '/path/to/file.deb')
19
+
20
+ Kernel.stub :system, true do
21
+ @installer = ElasticsearchUpdate::Installer.new('test_password', '.deb', true)
22
+ response = @installer.install_file(@mock_file)
23
+
24
+ response.must_equal true
25
+ end
26
+ end
27
+
28
+ it 'should install the .rpm file' do
29
+ @mock_file = Minitest::Mock.new
30
+ @mock_file.expect(:path, '/path/to/file.rpm')
31
+
32
+ Kernel.stub :system, true do
33
+ @installer = ElasticsearchUpdate::Installer.new('test_password', '.rpm', true)
34
+ response = @installer.install_file(@mock_file)
35
+
36
+ response.must_equal true
37
+ end
38
+ end
39
+
40
+ it 'should install the .zip file' do
41
+ @mock_file = Minitest::Mock.new
42
+ @mock_file.expect(:path, '/path/to/file.rpm')
43
+
44
+ Kernel.stub :system, true do
45
+ @installer = ElasticsearchUpdate::Installer.new('test_password', '.zip', true)
46
+ response = @installer.install_file(@mock_file)
47
+
48
+ response.must_equal true
49
+ end
50
+ end
51
+
52
+ it 'should install the .rpm file' do
53
+ @mock_file = Minitest::Mock.new
54
+ @mock_file.expect(:path, '/path/to/file.rpm')
55
+
56
+ Kernel.stub :system, true do
57
+ @installer = ElasticsearchUpdate::Installer.new('test_password', '.tar.gz', true)
58
+ response = @installer.install_file(@mock_file)
59
+
60
+ response.must_equal true
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,94 @@
1
+ require 'minitest_helper'
2
+
3
+ module ElasticsearchUpdate
4
+ class TestWizard < Wizard
5
+ def ask(_question, _type = String, &_block)
6
+ 'Question asked.'
7
+ end
8
+
9
+ def choose(&_block)
10
+ @choice = '.deb'
11
+ end
12
+ end
13
+ # The TestInstaller class below tests the Downloader class from the library
14
+ class TestWizard
15
+ describe 'Wizard', 'Used to ask questions on the command line' do
16
+
17
+ it 'should initialize without errors' do
18
+ wizard = TestWizard.new
19
+ assert_kind_of Wizard, wizard
20
+ end
21
+
22
+ it "should ask for Elasticsearch's host" do
23
+ wizard = TestWizard.new
24
+
25
+ result = wizard.host
26
+
27
+ result.must_equal 'Question asked.'
28
+ end
29
+
30
+ it 'should ask for the desired Elasticsearch version' do
31
+ wizard = TestWizard.new
32
+
33
+ result = wizard.version
34
+
35
+ result.must_equal 'Question asked.'
36
+ end
37
+
38
+ it "should ask for Elasticsearch's port" do
39
+ wizard = TestWizard.new
40
+
41
+ result = wizard.port
42
+
43
+ result.must_equal 'Question asked.'
44
+ end
45
+
46
+ it "should ask for Elasticsearch's location info" do
47
+ wizard = TestWizard.new
48
+
49
+ result = wizard.es_location_hash
50
+
51
+ result.must_equal(host: 'Question asked.', port: 'Question asked.')
52
+ end
53
+
54
+ it "should ask for Elasticsearch's file system location" do
55
+ wizard = TestWizard.new
56
+
57
+ result = wizard.elasticsearch_fs_location
58
+
59
+ result.must_equal 'Question asked.'
60
+ end
61
+
62
+ it "should ask for the system's sudo password" do
63
+ wizard = TestWizard.new
64
+
65
+ result = wizard.sudo_password
66
+
67
+ result.must_equal 'Question asked.'
68
+ end
69
+
70
+ it 'should ask for the desired extension of the update file' do
71
+ wizard = TestWizard.new
72
+
73
+ @mock_menu = Minitest::Mock.new
74
+ @mock_menu.expect(:prompt, true)
75
+ @mock_menu.expect(:choice, '.deb')
76
+
77
+ TestWizard.stub :choose, '.deb', @mock_menu do
78
+ result = wizard.extension
79
+ result.must_equal '.deb'
80
+ end
81
+ end
82
+
83
+ it 'should create the download hash' do
84
+ wizard = TestWizard.new
85
+
86
+ result = wizard.download_hash
87
+
88
+ result.must_equal(base_url: 'download.elasticsearch.org',
89
+ version: 'Question asked.',
90
+ extension: '.deb')
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,9 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+ require 'minitest/autorun'
4
+ require 'elasticsearch_update'
5
+
6
+ # ElasticsearchUpdate houses all tests for the
7
+ # Elasticsearch Updater and all associated library files
8
+ module ElasticsearchUpdate
9
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch_update
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Kirsche
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.4.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: highline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.21
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.21
69
+ description: Updates the elasticsearch instance from a deb or rpm on the local machine.
70
+ Assumes elasticsearch is a service.
71
+ email:
72
+ - kev.kirsche@gmail.com
73
+ executables:
74
+ - elasticsearch_update
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/elasticsearch_update
85
+ - bin/update.rb
86
+ - elasticsearch_update.gemspec
87
+ - lib/elasticsearch_update.rb
88
+ - lib/elasticsearch_update/cli.rb
89
+ - lib/elasticsearch_update/downloader.rb
90
+ - lib/elasticsearch_update/elasticsearch.rb
91
+ - lib/elasticsearch_update/installer.rb
92
+ - lib/elasticsearch_update/version.rb
93
+ - lib/elasticsearch_update/wizard.rb
94
+ - test/elasticsearch_update/downloader_spec.rb
95
+ - test/elasticsearch_update/elasticsearch_spec.rb
96
+ - test/elasticsearch_update/installer_spec.rb
97
+ - test/elasticsearch_update/wizard_spec.rb
98
+ - test/minitest_helper.rb
99
+ homepage: https://github.com/kkirsche/elasticsearch_update
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Updates the elasticsearch instance on the local machine.
123
+ test_files:
124
+ - test/elasticsearch_update/downloader_spec.rb
125
+ - test/elasticsearch_update/elasticsearch_spec.rb
126
+ - test/elasticsearch_update/installer_spec.rb
127
+ - test/elasticsearch_update/wizard_spec.rb
128
+ - test/minitest_helper.rb