1 | 6 | |
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 | |
package com.jcabi.mysql.maven.plugin; |
31 | |
|
32 | |
import com.jcabi.aspects.Loggable; |
33 | |
import com.jcabi.aspects.Tv; |
34 | |
import com.jcabi.log.Logger; |
35 | |
import com.jcabi.log.VerboseProcess; |
36 | |
import com.jcabi.log.VerboseRunnable; |
37 | |
import java.io.File; |
38 | |
import java.io.IOException; |
39 | |
import java.io.OutputStreamWriter; |
40 | |
import java.io.PrintWriter; |
41 | |
import java.util.Arrays; |
42 | |
import java.util.Collection; |
43 | |
import java.util.LinkedList; |
44 | |
import java.util.concurrent.Callable; |
45 | |
import java.util.concurrent.ConcurrentHashMap; |
46 | |
import java.util.concurrent.ConcurrentMap; |
47 | |
import java.util.concurrent.TimeUnit; |
48 | |
import javax.validation.constraints.NotNull; |
49 | |
import lombok.EqualsAndHashCode; |
50 | |
import lombok.ToString; |
51 | |
import org.apache.commons.io.FileUtils; |
52 | |
import org.apache.commons.lang3.CharEncoding; |
53 | |
import org.apache.commons.lang3.StringUtils; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 0 | @ToString |
66 | 0 | @EqualsAndHashCode(of = "processes") |
67 | |
@Loggable(Loggable.INFO) |
68 | |
@SuppressWarnings({ "PMD.DoNotUseThreads", "PMD.TooManyMethods" }) |
69 | 8 | public final class Instances { |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private static final String DATA_SUB_DIR = "data"; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private static final String NO_DEFAULTS = "--no-defaults"; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private static final String DEFAULT_USER = "root"; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private static final String DEFAULT_PASSWORD = "root"; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
@SuppressWarnings("PMD.AvoidUsingHardCodedIP") |
95 | |
private static final String DEFAULT_HOST = "127.0.0.1"; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 8 | private final transient ConcurrentMap<Integer, Process> processes = |
101 | |
new ConcurrentHashMap<Integer, Process>(0); |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 8 | private transient boolean clean = true; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public void start(@NotNull final Config config, @NotNull final File dist, |
121 | |
@NotNull final File target, final boolean deldir, final File socket) |
122 | |
throws IOException { |
123 | 16 | this.setClean(target, deldir); |
124 | 8 | synchronized (this.processes) { |
125 | 8 | if (this.processes.containsKey(config.port())) { |
126 | 0 | throw new IllegalArgumentException( |
127 | |
String.format("port %d is already busy", config.port()) |
128 | |
); |
129 | |
} |
130 | 8 | final Process proc = this.process(config, dist, target, socket); |
131 | 8 | this.processes.put(config.port(), proc); |
132 | 8 | Runtime.getRuntime().addShutdownHook( |
133 | |
new Thread( |
134 | 8 | new Runnable() { |
135 | |
@Override |
136 | |
public void run() { |
137 | 4 | Instances.this.stop(config.port()); |
138 | 0 | } |
139 | |
} |
140 | |
) |
141 | |
); |
142 | 8 | } |
143 | 8 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public void stop(final int port) { |
150 | 24 | synchronized (this.processes) { |
151 | 8 | final Process proc = this.processes.remove(port); |
152 | 8 | if (proc != null) { |
153 | 8 | proc.destroy(); |
154 | |
} |
155 | 8 | } |
156 | 8 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public boolean reusedExistingDatabase() { |
164 | 6 | return !this.clean; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private Process process(@NotNull final Config config, |
178 | |
final File dist, final File target, final File socketfile) |
179 | |
throws IOException { |
180 | 8 | final File temp = this.prepareFolders(target); |
181 | |
final File socket; |
182 | 8 | if (socketfile == null) { |
183 | 8 | socket = new File(target, "mysql.sock"); |
184 | |
} else { |
185 | 0 | socket = socketfile; |
186 | |
} |
187 | 8 | final ProcessBuilder builder = this.builder( |
188 | |
dist, |
189 | |
"bin/mysqld", |
190 | |
Instances.NO_DEFAULTS, |
191 | |
String.format("--user=%s", System.getProperty("user.name")), |
192 | |
"--general_log", |
193 | |
"--console", |
194 | |
"--innodb_buffer_pool_size=64M", |
195 | |
"--innodb_log_file_size=64M", |
196 | |
"--log_warnings", |
197 | |
"--innodb_use_native_aio=0", |
198 | |
String.format("--binlog-ignore-db=%s", config.dbname()), |
199 | |
String.format("--basedir=%s", dist), |
200 | |
String.format("--lc-messages-dir=%s", new File(dist, "share")), |
201 | |
String.format("--datadir=%s", this.data(dist, target)), |
202 | |
String.format("--tmpdir=%s", temp), |
203 | |
String.format("--socket=%s", socket), |
204 | |
String.format("--pid-file=%s", new File(target, "mysql.pid")), |
205 | |
String.format("--port=%d", config.port()) |
206 | |
).redirectErrorStream(true); |
207 | 8 | builder.environment().put("MYSQL_HOME", dist.getAbsolutePath()); |
208 | 8 | for (final String option : config.options()) { |
209 | 1 | if (!StringUtils.isBlank(option)) { |
210 | 1 | builder.command().add(String.format("--%s", option)); |
211 | |
} |
212 | 1 | } |
213 | 8 | final Process proc = builder.start(); |
214 | 8 | final Thread thread = new Thread( |
215 | |
new VerboseRunnable( |
216 | 16 | new Callable<Void>() { |
217 | |
@Override |
218 | |
public Void call() throws Exception { |
219 | 8 | new VerboseProcess(proc).stdoutQuietly(); |
220 | 7 | return null; |
221 | |
} |
222 | |
} |
223 | |
) |
224 | |
); |
225 | 8 | thread.setDaemon(true); |
226 | 8 | thread.start(); |
227 | 8 | this.waitFor(socket, config.port()); |
228 | 8 | if (this.clean) { |
229 | 7 | this.configure(config, dist, socket); |
230 | |
} |
231 | 8 | return proc; |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private File prepareFolders(final File target) throws IOException { |
241 | 8 | if (this.clean && target.exists()) { |
242 | 7 | FileUtils.deleteDirectory(target); |
243 | 7 | Logger.info(this, "deleted %s directory", target); |
244 | |
} |
245 | 8 | if (!target.exists() && target.mkdirs()) { |
246 | 7 | Logger.info(this, "created %s directory", target); |
247 | |
} |
248 | 8 | final File temp = new File(target, "temp"); |
249 | 8 | if (!temp.exists() && !temp.mkdirs()) { |
250 | 0 | throw new IllegalStateException( |
251 | |
"Error during temporary folder creation" |
252 | |
); |
253 | |
} |
254 | 8 | return temp; |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
private File data(final File dist, final File target) throws IOException { |
265 | 8 | final File dir = new File(target, DATA_SUB_DIR); |
266 | 8 | if (!dir.exists()) { |
267 | 7 | final File cnf = new File(target, "my-default.cnf"); |
268 | 7 | FileUtils.writeStringToFile( |
269 | |
cnf, |
270 | |
"[mysql]\n# no defaults..." |
271 | |
); |
272 | 7 | new VerboseProcess( |
273 | |
this.builder( |
274 | |
dist, |
275 | |
"scripts/mysql_install_db", |
276 | |
String.format("--defaults-file=%s", cnf), |
277 | |
"--force", |
278 | |
"--innodb_use_native_aio=0", |
279 | |
String.format("--datadir=%s", dir), |
280 | |
String.format("--basedir=%s", dist) |
281 | |
) |
282 | |
).stdoutQuietly(); |
283 | |
} |
284 | 8 | return dir; |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
private File waitFor(final File socket, final int port) throws IOException { |
295 | 8 | final long start = System.currentTimeMillis(); |
296 | 8 | long age = 0L; |
297 | |
while (true) { |
298 | 29 | if (socket.exists()) { |
299 | 8 | Logger.info( |
300 | |
this, |
301 | |
"socket %s is available after %[ms]s of waiting", |
302 | |
socket, age |
303 | |
); |
304 | 8 | break; |
305 | |
} |
306 | 21 | if (SocketHelper.isOpen(port)) { |
307 | 0 | Logger.info( |
308 | |
this, |
309 | |
"port %s is available after %[ms]s of waiting", |
310 | |
port, age |
311 | |
); |
312 | 0 | break; |
313 | |
} |
314 | |
try { |
315 | 21 | TimeUnit.SECONDS.sleep(1L); |
316 | 0 | } catch (final InterruptedException ex) { |
317 | 0 | Thread.currentThread().interrupt(); |
318 | 0 | throw new IllegalStateException(ex); |
319 | 21 | } |
320 | 21 | age = System.currentTimeMillis() - start; |
321 | 21 | if (age > TimeUnit.MINUTES.toMillis((long) Tv.FIVE)) { |
322 | 0 | throw new IOException( |
323 | |
Logger.format( |
324 | |
"socket %s is not available after %[ms]s of waiting", |
325 | |
socket, age |
326 | |
) |
327 | |
); |
328 | |
} |
329 | |
} |
330 | 8 | return socket; |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
private void configure(@NotNull final Config config, |
341 | |
final File dist, final File socket) |
342 | |
throws IOException { |
343 | 7 | new VerboseProcess( |
344 | |
this.builder( |
345 | |
dist, |
346 | |
"bin/mysqladmin", |
347 | |
Instances.NO_DEFAULTS, |
348 | |
String.format("--port=%d", config.port()), |
349 | |
String.format("--user=%s", Instances.DEFAULT_USER), |
350 | |
String.format("--socket=%s", socket), |
351 | |
String.format("--host=%s", Instances.DEFAULT_HOST), |
352 | |
"password", |
353 | |
Instances.DEFAULT_PASSWORD |
354 | |
) |
355 | |
).stdoutQuietly(); |
356 | 7 | final Process process = |
357 | |
this.builder( |
358 | |
dist, |
359 | |
"bin/mysql", |
360 | |
String.format("--port=%d", config.port()), |
361 | |
String.format("--user=%s", Instances.DEFAULT_USER), |
362 | |
String.format("--password=%s", Instances.DEFAULT_PASSWORD), |
363 | |
String.format("--socket=%s", socket) |
364 | |
).start(); |
365 | 7 | final PrintWriter writer = new PrintWriter( |
366 | |
new OutputStreamWriter( |
367 | |
process.getOutputStream(), CharEncoding.UTF_8 |
368 | |
) |
369 | |
); |
370 | 7 | writer.print("CREATE DATABASE "); |
371 | 7 | writer.print(config.dbname()); |
372 | 7 | writer.println(";"); |
373 | 7 | if (!Instances.DEFAULT_USER.equals(config.user())) { |
374 | 2 | writer.println( |
375 | |
String.format( |
376 | |
"CREATE USER '%s'@'%s' IDENTIFIED BY '%s';", |
377 | |
config.user(), |
378 | |
Instances.DEFAULT_HOST, |
379 | |
config.password() |
380 | |
) |
381 | |
); |
382 | 2 | writer.println( |
383 | |
String.format( |
384 | |
"GRANT ALL ON %s.* TO '%s'@'%s';", |
385 | |
config.dbname(), |
386 | |
config.user(), |
387 | |
Instances.DEFAULT_HOST |
388 | |
) |
389 | |
); |
390 | |
} |
391 | 7 | writer.close(); |
392 | 7 | new VerboseProcess(process).stdoutQuietly(); |
393 | 7 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
private ProcessBuilder builder(final File dist, final String name, |
403 | |
final String... cmds) { |
404 | 29 | String label = name; |
405 | 29 | final Collection<String> commands = new LinkedList<String>(); |
406 | 29 | final File exec = new File(dist, label); |
407 | 29 | if (exec.exists()) { |
408 | |
try { |
409 | 29 | exec.setExecutable(true); |
410 | 0 | } catch (final SecurityException sex) { |
411 | 0 | throw new IllegalStateException(sex); |
412 | 29 | } |
413 | |
} else { |
414 | 0 | label = String.format("%s.exe", name); |
415 | 0 | if (!new File(dist, label).exists()) { |
416 | 0 | label = String.format("%s.pl", name); |
417 | 0 | commands.add("perl"); |
418 | |
} |
419 | |
} |
420 | 29 | commands.add(new File(dist, label).getAbsolutePath()); |
421 | 29 | commands.addAll(Arrays.asList(cmds)); |
422 | 29 | Logger.info(this, "$ %s", StringUtils.join(commands, " ")); |
423 | 29 | return new ProcessBuilder() |
424 | |
.command(commands.toArray(new String[commands.size()])) |
425 | |
.directory(dist); |
426 | |
} |
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
private void setClean(final File target, final boolean deldir) { |
435 | 8 | if (new File(target, DATA_SUB_DIR).exists() && !deldir) { |
436 | 1 | Logger.info(this, "reuse existing database %s", target); |
437 | 1 | this.clean = false; |
438 | |
} else { |
439 | 7 | this.clean = true; |
440 | |
} |
441 | 8 | Logger.info(this, "reuse existing database %s", !this.clean); |
442 | 8 | } |
443 | |
|
444 | |
} |