This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.rb
68 lines (56 loc) · 1.52 KB
/
import.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'json'
require 'net/http'
require 'uri'
# Upload files in a folder to Elasticsearch using http PUT
class Provision
def self.call(input)
Provision.new(input).provision
end
def initialize(input)
@input = input
end
def provision
$stdout.puts "Provisioning '#{ES_BASE_URI}'..."
files = Dir.glob("#{input}/**/*.json", File::FNM_DOTMATCH).sort
files.each { |file| import file }
$stdout.puts 'Provisioning done.'
end
private
ES_BASE_URI = ENV.fetch('ES_BASE_URI') { 'http://localhost:9200' }.freeze
attr_reader :input
def import(file)
content = File.read file
path = to_path(file)
response = upload(content, path)
log response, path
end
HEADER = { 'Content-Type' => 'application/json' }.freeze
def upload(content, path)
uri = URI.parse "#{ES_BASE_URI}/#{path}"
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri, HEADER)
request.body = content
http.request(request)
end
FILE_TO_URI = {
'_x_' => '*',
'_;_' => ':'
}.freeze
def to_path(file_name)
path = file_name
path.slice!("#{input}/")
path.slice!('.json')
FILE_TO_URI.each { |k, v| path.sub!(k, v) }
path
end
def log(response, path)
http_status = response.code.to_i
if http_status < 200 || http_status > 299
$stdout.puts "Could not upload '#{path}'", response.to_s, response.body
raise Net::HTTPBadResponse
else
$stdout.puts "Uploaded '#{path}'."
end
end
end
Provision.call(ARGV[0] || 'import')