Skip to content

Commit 0df1a1f

Browse files
committed
[RAILS] Updated the installation process in the "01-basic" application template
1 parent 7aa03e8 commit 0df1a1f

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

elasticsearch-rails/lib/rails/templates/01-basic.rb

+44-17
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#
1111
# * Git
1212
# * Ruby >= 1.9.3
13-
# * Rails >= 4
14-
# * Java >= 7 (for Elasticsearch)
13+
# * Rails >= 5
14+
# * Java >= 8 (for Elasticsearch)
1515
#
1616
# Usage:
1717
# ------
@@ -31,31 +31,32 @@
3131
end
3232
end
3333

34-
run "touch tmp/.gitignore"
35-
36-
append_to_file ".gitignore", "vendor/elasticsearch-1.0.1/\n"
34+
$elasticsearch_url = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
3735

38-
git :init
39-
git add: "."
40-
git commit: "-m 'Initial commit: Clean application'"
36+
# ----- Check & download Elasticsearch ------------------------------------------------------------
4137

42-
# ----- Download Elasticsearch --------------------------------------------------------------------
38+
cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url)) rescue nil
39+
cluster_info = JSON.parse(cluster_info) if cluster_info
4340

44-
ELASTICSEARCH_URL = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
41+
if cluster_info.nil? || cluster_info['version']['number'] < '5'
42+
# Change the port when incompatible Elasticsearch version is running on localhost:9200
43+
if $elasticsearch_url == 'http://localhost:9200' && cluster_info && cluster_info['version']['number'] < '5'
44+
$change_port = '9280'
45+
$elasticsearch_url = "http://localhost:#{$change_port}"
46+
end
4547

46-
unless (Net::HTTP.get(URI.parse(ELASTICSEARCH_URL)) rescue false)
4748
COMMAND = <<-COMMAND.gsub(/^ /, '')
48-
curl -# -O "http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.0.1.tar.gz"
49-
tar -zxf elasticsearch-1.0.1.tar.gz
50-
rm -f elasticsearch-1.0.1.tar.gz
51-
./elasticsearch-1.0.1/bin/elasticsearch -d -p #{destination_root}/tmp/pids/elasticsearch.pid
49+
curl -# -O "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.1.tar.gz"
50+
tar -zxf elasticsearch-5.2.1.tar.gz
51+
rm -f elasticsearch-5.2.1.tar.gz
52+
./elasticsearch-5.2.1/bin/elasticsearch -d -p #{destination_root}/tmp/pids/elasticsearch.pid #{$change_port.nil? ? '' : "-E http.port=#{$change_port}" }
5253
COMMAND
5354

5455
puts "\n"
5556
say_status "ERROR", "Elasticsearch not running!\n", :red
5657
puts '-'*80
57-
say_status '', "It appears that Elasticsearch is not running on this machine."
58-
say_status '', "Is it installed? Do you want me to install it for you with this command?\n\n"
58+
say_status '', "It appears that Elasticsearch 5 is not running on this machine."
59+
say_status '', "Is it installed? Do you want me to install and run it for you with this command?\n\n"
5960
COMMAND.each_line { |l| say_status '', "$ #{l}" }
6061
puts
6162
say_status '', "(To uninstall, just remove the generated application directory.)"
@@ -65,15 +66,41 @@
6566
puts
6667
say_status "Install", "Elasticsearch", :yellow
6768

69+
java_info = `java -version 2>&1`
70+
71+
unless java_info.match /1\.[8-9]/
72+
puts
73+
say_status "ERROR", "Required Java version (1.8) not found, exiting...", :red
74+
exit(1)
75+
end
76+
6877
commands = COMMAND.split("\n")
6978
exec = commands.pop
7079
inside("vendor") do
7180
commands.each { |command| run command }
7281
run "(#{exec})" # Launch Elasticsearch in subshell
7382
end
83+
84+
# Wait for Elasticsearch to be up...
85+
#
86+
system <<-COMMAND
87+
until $(curl --silent --head --fail #{$elasticsearch_url} > /dev/null 2>&1); do
88+
printf '.'; sleep 1
89+
done
90+
COMMAND
7491
end
7592
end unless ENV['RAILS_NO_ES_INSTALL']
7693

94+
# ----- Application skeleton ----------------------------------------------------------------------
95+
96+
run "touch tmp/.gitignore"
97+
98+
append_to_file ".gitignore", "vendor/elasticsearch-5.2.1/\n"
99+
100+
git :init
101+
git add: "."
102+
git commit: "-m 'Initial commit: Clean application'"
103+
77104
# ----- Add README --------------------------------------------------------------------------------
78105

79106
puts

0 commit comments

Comments
 (0)