/
droonga-engine-configure
executable file
·216 lines (185 loc) · 5.61 KB
/
droonga-engine-configure
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env ruby
#
# Copyright (C) 2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require "optparse"
require "fileutils"
require "yaml"
require "droonga/path"
require "droonga/command/droonga_engine"
require "droonga/safe_file_writer"
require "droonga/service_installation"
options = {
:quiet => nil,
:clear => nil,
:reset_config => nil,
:reset_catalog => nil,
}
configuration = Droonga::Command::DroongaEngine::Configuration.new
parser = OptionParser.new
parser.on("--quiet", "Run with no prompt.") do |host|
options[:quiet] = true
end
parser.on("--clear", "Clear any existing data.") do |host|
options[:clear] = true
end
parser.on("--reset-config", "Regenerate the configuration file \"droonga-engine.yaml\".") do |host|
options[:reset_config] = true
end
parser.on("--reset-catalog", "Regenerate the \"catalog.json\".") do |host|
options[:reset_catalog] = true
end
configuration.add_command_line_options(parser)
parser.parse!(ARGV)
def unjoin(configuration)
system("droonga-engine-unjoin",
"--host", configuration.host,
:out => "/dev/null",
:err => "/dev/null")
end
def input(message, default_value=nil)
print "#{message} [#{default_value}]: "
response = gets.strip
if response.empty?
default_value
else
response
end
end
def confirmed?(message)
while true
print "#{message} (y/N): "
response = gets
case response
when /\Ay/i
return true
when /\An/i, /^$/
return false
end
end
end
def path_from_base_dir(path)
path = path.to_s
base = "#{Droonga::Path.base.to_s}/"
if path.start_with?(base)
path.sub(base, "")
else
path
end
end
service_installation = Droonga::ServiceInstallation.new
service_installation.ensure_using_service_base_directory
running = false
begin
if service_installation.running?
if !options[:quiet]
puts("The droonga-engine service is now running.")
puts("Before reconfiguration, the service is going to be stopped " +
"and this node will be unjoined from the cluster.")
unless confirmed?("Are you sure you want to continue reconfiguration?")
exit(false)
end
end
running = true
end
rescue Droonga::NotInstalledAsService::MissingPidFilePath
puts("Not installed as a service yet.")
end
unless service_installation.have_write_permission?
puts("You have no permission to write files under " +
"<#{Droonga::Path.base.to_s}>.")
puts("Try again with right permission.")
exit(false)
end
data_files = [
Droonga::Path.databases,
Droonga::Path.state,
]
have_data = data_files.any?(&:exist?)
options[:clear] = false unless have_data
if !options[:quiet] and options[:clear].nil?
options[:clear] = confirmed?("Do you want all data to be cleared?")
end
options[:reset_config] = true unless Droonga::Path.config.exist?
if !options[:quiet] and options[:reset_config].nil?
options[:reset_config] = confirmed?("Do you want the configuration file " +
"\"droonga-engine.yaml\" to be regenerated?")
end
options[:reset_catalog] = true unless Droonga::Path.catalog.exist?
if !options[:quiet] and options[:reset_catalog].nil?
options[:reset_catalog] = confirmed?("Do you want the file \"catalog.json\" " +
"to be regenerated?")
end
if running
unjoin(configuration)
service_installation.stop
end
if options[:clear]
data_files.each do |file|
FileUtils.rm_rf(file.to_s)
end
end
if options[:reset_config] or options[:reset_catalog]
if configuration.have_given_host? or options[:quiet]
host = configuration.host
else
host = input("host", configuration.host)
end
if configuration.have_given_port? or options[:quiet]
port = configuration.port
else
port = input("port", configuration.port).to_i
end
if configuration.have_given_tag? or options[:quiet]
tag = configuration.tag
else
tag = input("tag", configuration.tag)
end
end
if options[:reset_config]
if configuration.have_given_log_level? or options[:quiet]
log_level = configuration.log_level
else
log_level = input("log level", configuration.log_level)
end
new_configuration = {
"host" => host,
"port" => port,
"tag" => tag,
"daemon" => true,
"log_file" => path_from_base_dir(Droonga::Path.default_log_file),
"log_level" => log_level,
}
Droonga::SafeFileWriter.write(Droonga::Path.config,
YAML.dump(new_configuration))
end
if options[:reset_catalog]
system("droonga-engine-catalog-generate",
"--output", Droonga::Path.catalog.to_s,
"--hosts", host,
"--port", port.to_s,
"--tag", tag)
end
if options[:reset_config] or options[:reset_catalog]
service_installation.ensure_correct_file_permission(Droonga::Path.base)
end
if running
successfully_started = service_installation.start
unless successfully_started
puts("The droonga-engine service is still stopped.")
puts("You need to start the service again manually.")
end
end
exit(true)