|
11 | 11 | # * Git
|
12 | 12 | # * Ruby >= 1.9.3
|
13 | 13 | # * Rails >= 5
|
14 |
| -# * Java >= 8 (for Elasticsearch) |
15 | 14 | #
|
16 | 15 | # Usage:
|
17 | 16 | # ------
|
|
22 | 21 |
|
23 | 22 | require 'uri'
|
24 | 23 | require 'net/http'
|
25 |
| - |
26 |
| -at_exit do |
27 |
| - pid = File.read("#{destination_root}/tmp/pids/elasticsearch.pid") rescue nil |
28 |
| - if pid |
29 |
| - say_status "Stop", "Elasticsearch", :yellow |
30 |
| - run "kill #{pid}" |
31 |
| - end |
32 |
| -end |
| 24 | +require 'json' |
33 | 25 |
|
34 | 26 | $elasticsearch_url = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
|
35 | 27 |
|
36 |
| -# ----- Check & download Elasticsearch ------------------------------------------------------------ |
| 28 | +# ----- Check for Elasticsearch ------------------------------------------------------------------- |
37 | 29 |
|
38 |
| -cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url)) rescue nil |
39 |
| -cluster_info = JSON.parse(cluster_info) if cluster_info |
| 30 | +required_elasticsearch_version = '6' |
40 | 31 |
|
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 |
| 32 | +docker_command =<<-CMD.gsub(/\s{1,}/, ' ').strip |
| 33 | + docker run \ |
| 34 | + --name elasticsearch-rails-searchapp \ |
| 35 | + --publish 9200:9200 \ |
| 36 | + --env "discovery.type=single-node" \ |
| 37 | + --env "cluster.name=elasticsearch-rails" \ |
| 38 | + --env "cluster.routing.allocation.disk.threshold_enabled=false" \ |
| 39 | + --rm \ |
| 40 | + docker.elastic.co/elasticsearch/elasticsearch-oss:6.3.0 |
| 41 | +CMD |
47 | 42 |
|
48 |
| - COMMAND = <<-COMMAND.gsub(/^ /, '') |
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}" } |
53 |
| - COMMAND |
54 |
| - |
55 |
| - puts "\n" |
56 |
| - say_status "ERROR", "Elasticsearch not running!\n", :red |
57 |
| - puts '-'*80 |
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" |
60 |
| - COMMAND.each_line { |l| say_status '', "$ #{l}" } |
61 |
| - puts |
62 |
| - say_status '', "(To uninstall, just remove the generated application directory.)" |
63 |
| - puts '-'*80, '' |
64 |
| - |
65 |
| - if yes?("Install Elasticsearch?", :bold) |
66 |
| - puts |
67 |
| - say_status "Install", "Elasticsearch", :yellow |
68 |
| - |
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 |
| - |
77 |
| - commands = COMMAND.split("\n") |
78 |
| - exec = commands.pop |
79 |
| - inside("vendor") do |
80 |
| - commands.each { |command| run command } |
81 |
| - run "(#{exec})" # Launch Elasticsearch in subshell |
82 |
| - 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 |
91 |
| - end |
92 |
| -end unless ENV['RAILS_NO_ES_INSTALL'] |
| 43 | +begin |
| 44 | + cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url)) |
| 45 | +rescue Errno::ECONNREFUSED => e |
| 46 | + say_status "ERROR", "Cannot connect to Elasticsearch on <#{$elasticsearch_url}>\n\n", :red |
| 47 | + say_status "", "The application requires an Elasticsearch cluster running, " + |
| 48 | + "but no cluster has been found on <#{$elasticsearch_url}>." |
| 49 | + say_status "", "The easiest way of launching Elasticsearch is by running it with Docker (https://www.docker.com/get-docker):\n\n" |
| 50 | + say_status "", docker_command + "\n" |
| 51 | + exit(1) |
| 52 | +rescue StandardError => e |
| 53 | + say_status "ERROR", "#{e.class}: #{e.message}", :red |
| 54 | + exit(1) |
| 55 | +end |
| 56 | + |
| 57 | +cluster_info = JSON.parse(cluster_info) |
| 58 | + |
| 59 | +unless cluster_info['version'] |
| 60 | + say_status "ERROR", "Cannot determine Elasticsearch version from <#{$elasticsearch_url}>", :red |
| 61 | + say_status "", JSON.dump(cluster_info), :red |
| 62 | + exit(1) |
| 63 | +end |
| 64 | + |
| 65 | +if cluster_info['version']['number'] < required_elasticsearch_version |
| 66 | + say_status "ERROR", |
| 67 | + "The application requires Elasticsearch version #{required_elasticsearch_version} or higher, found version #{cluster_info['version']['number']}.\n\n", :red |
| 68 | + say_status "", "The easiest way of launching Elasticsearch is by running it with Docker (https://www.docker.com/get-docker):\n\n" |
| 69 | + say_status "", docker_command + "\n" |
| 70 | + exit(1) |
| 71 | +end |
93 | 72 |
|
94 | 73 | # ----- Application skeleton ----------------------------------------------------------------------
|
95 | 74 |
|
|
144 | 123 |
|
145 | 124 | # ----- Auxiliary gems ----------------------------------------------------------------------------
|
146 | 125 |
|
147 |
| -gem 'mocha', group: 'test', require: 'mocha/api' |
| 126 | +gem 'mocha', group: 'test' |
148 | 127 | gem 'rails-controller-testing', group: 'test'
|
149 | 128 |
|
150 | 129 | # ----- Remove CoffeeScript, Sass and "all that jazz" ---------------------------------------------
|
|
160 | 139 | say_status "Rubygems", "Adding Elasticsearch libraries into Gemfile...\n", :yellow
|
161 | 140 | puts '-'*80, ''; sleep 0.75
|
162 | 141 |
|
163 |
| -gem 'elasticsearch', git: 'git://github.com/elasticsearch/elasticsearch-ruby.git' |
164 |
| -gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git' |
165 |
| -gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git' |
| 142 | +gem 'elasticsearch', git: 'https://github.com/elasticsearch/elasticsearch-ruby.git' |
| 143 | +gem 'elasticsearch-model', git: 'https://github.com/elasticsearch/elasticsearch-rails.git' |
| 144 | +gem 'elasticsearch-rails', git: 'https://github.com/elasticsearch/elasticsearch-rails.git' |
166 | 145 |
|
167 | 146 |
|
168 | 147 | git add: "Gemfile*"
|
|
0 commit comments