/
SymmetricAdmin.java
517 lines (446 loc) · 20.5 KB
/
SymmetricAdmin.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.h2.util.StringUtils;
import org.jumpmind.security.ISecurityService;
import org.jumpmind.security.SecurityConstants;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.IPurgeService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.service.ITriggerRouterService;
import org.jumpmind.util.JarBuilder;
/**
* Perform administration tasks with SymmetricDS.
*/
public class SymmetricAdmin extends AbstractCommandLauncher {
@SuppressWarnings("unused")
private static final Log log = LogFactory.getLog(SymmetricAdmin.class);
private static final String CMD_RELOAD_NODE = "reload-node";
private static final String CMD_RELOAD_TABLE = "reload-table";
private static final String CMD_EXPORT_BATCH = "export-batch";
private static final String CMD_IMPORT_BATCH = "import-batch";
private static final String CMD_RUN_PURGE = "run-purge";
private static final String CMD_ENCRYPT_TEXT = "encrypt-text";
private static final String CMD_CREATE_WAR = "create-war";
private static final String CMD_CREATE_SYM_TABLES = "create-sym-tables";
private static final String CMD_EXPORT_SYM_TABLES = "export-sym-tables";
private static final String CMD_OPEN_REGISTRATION = "open-registration";
private static final String CMD_SYNC_TRIGGERS = "sync-triggers";
private static final String CMD_EXPORT_PROPERTIES = "export-properties";
private static final String CMD_UNINSTALL = "uninstall";
private static final String CMD_SEND_SQL = "send-sql";
private static final String CMD_SEND_SCRIPT = "send-script";
private static final String CMD_SEND_SCHEMA = "send-schema";
private static final String[] NO_ENGINE_REQUIRED = { CMD_EXPORT_PROPERTIES };
private static final String OPTION_NODE = "node";
private static final String OPTION_CATALOG = "catalog";
private static final String OPTION_SCHEMA = "schema";
private static final String OPTION_WHERE = "where";
private static final String OPTION_FORCE = "force";
private static final String OPTION_NODE_GROUP = "node-group";
private static final int WIDTH = 80;
private static final int PAD = 3;
public SymmetricAdmin(String app, String argSyntax, String messageKeyPrefix) {
super(app, argSyntax, messageKeyPrefix);
}
public static void main(String[] args) throws Exception {
new SymmetricAdmin("symadmin", "<subcommand> [options] [args]", "SymAdmin.Option.")
.execute(args);
}
@Override
protected boolean printHelpIfNoOptionsAreProvided() {
return true;
}
@Override
protected boolean requiresPropertiesFile() {
return true;
}
@Override
protected void printHelp(CommandLine line, Options options) {
String[] args = line.getArgs();
if (args.length == 0) {
System.out.println("Error: no subcommand was given. Use '" + app
+ " help' for a list of subcommands.\n");
} else if (args.length > 1 && args[0].equals(HELP)) {
printHelpCommand(line);
} else {
System.out.println(app + " version " + Version.version());
System.out.println("Perform administration tasks with SymmetricDS.\n");
System.out
.println("Usage: symadmin <subcommand> --engine [engine.name] [options] [args]");
System.out
.println(" symadmin <subcommand> --properties [properties file] [options] [args]");
System.out
.println("Type 'symadmin help <subcommand>' for help on a specific subcommand.\n");
System.out.println("Available subcommands:");
PrintWriter pw = new PrintWriter(System.out);
printHelpLine(pw, CMD_OPEN_REGISTRATION);
printHelpLine(pw, CMD_RELOAD_NODE);
printHelpLine(pw, CMD_RELOAD_TABLE);
printHelpLine(pw, CMD_EXPORT_BATCH);
printHelpLine(pw, CMD_IMPORT_BATCH);
printHelpLine(pw, CMD_RUN_PURGE);
printHelpLine(pw, CMD_ENCRYPT_TEXT);
printHelpLine(pw, CMD_CREATE_WAR);
printHelpLine(pw, CMD_CREATE_SYM_TABLES);
printHelpLine(pw, CMD_EXPORT_SYM_TABLES);
printHelpLine(pw, CMD_SYNC_TRIGGERS);
printHelpLine(pw, CMD_EXPORT_PROPERTIES);
printHelpLine(pw, CMD_SEND_SQL);
printHelpLine(pw, CMD_SEND_SCHEMA);
printHelpLine(pw, CMD_SEND_SCRIPT);
printHelpLine(pw, CMD_UNINSTALL);
pw.flush();
}
}
private void printHelpLine(PrintWriter pw, String cmd) {
String text = StringUtils.pad(" " + cmd, 23, " ", true)
+ Message.get("SymAdmin.Cmd." + cmd);
new HelpFormatter().printWrapped(pw, 79, 25, text);
}
private void printHelpCommand(CommandLine line) {
String[] args = line.getArgs();
if (args.length > 1) {
String cmd = args[1];
HelpFormatter format = new HelpFormatter();
PrintWriter writer = new PrintWriter(System.out);
Options options = new Options();
format.printWrapped(writer, WIDTH,
"Usage: " + app + " " + cmd + " " + Message.get("SymAdmin.Usage." + cmd) + "\n");
format.printWrapped(writer, WIDTH, Message.get("SymAdmin.Help." + cmd));
if (cmd.equals(CMD_SEND_SQL) || cmd.equals(CMD_SEND_SCHEMA)
|| cmd.equals(CMD_RELOAD_TABLE) || cmd.equals(CMD_SEND_SCRIPT)) {
addOption(options, "n", OPTION_NODE, true);
addOption(options, "g", OPTION_NODE_GROUP, true);
}
if (cmd.equals(CMD_RELOAD_TABLE)) {
addOption(options, "c", OPTION_CATALOG, true);
addOption(options, "s", OPTION_SCHEMA, true);
addOption(options, "w", OPTION_WHERE, true);
}
if (cmd.equals(CMD_SYNC_TRIGGERS)) {
addOption(options, "f", OPTION_FORCE, false);
}
if (options.getOptions().size() > 0) {
format.printWrapped(writer, WIDTH, "\nOptions:");
format.printOptions(writer, WIDTH, options, PAD, PAD);
}
if (!ArrayUtils.contains(NO_ENGINE_REQUIRED, cmd)) {
format.printWrapped(writer, WIDTH, "\nEngine options:");
options = new Options();
super.buildOptions(options);
format.printOptions(writer, WIDTH, options, PAD, PAD);
format.printWrapped(writer, WIDTH, "\nCrypto options:");
options = new Options();
buildCryptoOptions(options);
format.printOptions(writer, WIDTH, options, PAD, PAD);
}
writer.flush();
}
}
@Override
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, "n", OPTION_NODE, true);
addOption(options, "g", OPTION_NODE_GROUP, true);
addOption(options, "c", OPTION_CATALOG, true);
addOption(options, "s", OPTION_SCHEMA, true);
addOption(options, "w", OPTION_WHERE, true);
addOption(options, "f", OPTION_FORCE, false);
buildCryptoOptions(options);
}
@SuppressWarnings("unchecked")
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
List<String> args = line.getArgList();
String cmd = args.remove(0);
configureCrypto(line);
if (cmd.equals(CMD_EXPORT_PROPERTIES)) {
exportDefaultProperties(line, args);
return true;
} else if (cmd.equals(CMD_CREATE_WAR)) {
generateWar(line, args);
return true;
} else if (cmd.equals(CMD_EXPORT_SYM_TABLES)) {
exportSymTables(line, args);
return true;
} else if (cmd.equals(CMD_RUN_PURGE)) {
runPurge(line, args);
return true;
} else if (cmd.equals(CMD_OPEN_REGISTRATION)) {
openRegistration(line, args);
return true;
} else if (cmd.equals(CMD_RELOAD_NODE)) {
reloadNode(line, args);
return true;
} else if (cmd.equals(CMD_EXPORT_BATCH)) {
exportBatch(line, args);
return true;
} else if (cmd.equals(CMD_SYNC_TRIGGERS)) {
syncTrigger(line, args);
return true;
} else if (cmd.equals(CMD_CREATE_SYM_TABLES)) {
createSymTables();
return true;
} else if (cmd.equals(CMD_IMPORT_BATCH)) {
importBatch(line, args);
return true;
} else if (cmd.equals(CMD_ENCRYPT_TEXT)) {
encryptText(line, args);
return true;
} else if (cmd.equals(CMD_SEND_SQL)) {
sendSql(line, args);
return true;
} else if (cmd.equals(CMD_UNINSTALL)) {
uninstall(line, args);
return true;
} else if (cmd.equals(CMD_RELOAD_TABLE)) {
reloadTable(line, args);
return true;
} else if (cmd.equals(CMD_SEND_SCHEMA)) {
sendSchema(line, args);
return true;
} else if (cmd.equals(CMD_SEND_SCRIPT)) {
sendScript(line, args);
return true;
}
return false;
}
private String popArg(List<String> args, String argName) {
if (args.size() == 0) {
System.out.println("ERROR: Expected argument for: " + argName);
System.exit(1);
}
return args.remove(0);
}
private void runPurge(CommandLine line, List<String> args) {
IPurgeService purgeService = getSymmetricEngine().getPurgeService();
boolean all = args.contains("all") || args.size() == 0;
if (args.contains("outgoing") || all) {
purgeService.purgeOutgoing(true);
}
if (args.contains("incoming") || all) {
purgeService.purgeIncoming(true);
}
if (args.contains("data-gaps") || all) {
purgeService.purgeDataGaps(true);
}
}
private void exportBatch(CommandLine line, List<String> args) throws Exception {
IDataExtractorService dataExtractorService = getSymmetricEngine().getDataExtractorService();
String nodeId = popArg(args, "Node ID");
String batchId = popArg(args, "Batch ID");
OutputStreamWriter writer = getWriter(args);
dataExtractorService.extractBatchRange(writer, nodeId, Long.valueOf(batchId),
Long.valueOf(batchId));
writer.close();
}
private void importBatch(CommandLine line, List<String> args) throws Exception {
IDataLoaderService service = getSymmetricEngine().getDataLoaderService();
InputStream in = null;
if (args.size() == 0) {
in = System.in;
} else {
in = new FileInputStream(args.get(0));
}
service.loadDataFromPush(getSymmetricEngine().getNodeService().findIdentity(), in,
System.out);
System.out.flush();
in.close();
}
private void encryptText(CommandLine line, List<String> args) {
String plainText = popArg(args, "Text");
ISecurityService service = getSymmetricEngine().getSecurityService();
System.out.println(SecurityConstants.PREFIX_ENC + service.encrypt(plainText));
}
private void openRegistration(CommandLine line, List<String> args) {
String nodeGroupId = popArg(args, "Node Group ID");
String externalId = popArg(args, "External ID");
IRegistrationService registrationService = getSymmetricEngine().getRegistrationService();
registrationService.openRegistration(nodeGroupId, externalId);
System.out.println(String.format(
"Opened registration for node group of '%s' external ID of '%s'", nodeGroupId,
externalId));
}
private void reloadNode(CommandLine line, List<String> args) {
String nodeId = popArg(args, "Node ID");
IDataService dataService = getSymmetricEngine().getDataService();
String message = dataService.reloadNode(nodeId, false);
System.out.println(message);
}
private void syncTrigger(CommandLine line, List<String> args) throws IOException {
boolean genAlways = line.hasOption(OPTION_FORCE);
File file = null;
if (args.size() > 0) {
file = new File(args.get(0));
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
}
ITriggerRouterService triggerService = getSymmetricEngine().getTriggerRouterService();
StringBuilder sqlBuffer = new StringBuilder();
triggerService.syncTriggers(sqlBuffer, genAlways);
if (file != null) {
FileUtils.writeStringToFile(file, sqlBuffer.toString());
}
}
private void generateWar(CommandLine line, List<String> args) throws Exception {
String warFileName = popArg(args, "Filename");
final File workingDirectory = new File("../.war");
FileUtils.deleteDirectory(workingDirectory);
FileUtils.copyDirectory(new File("../web"), workingDirectory);
FileUtils.copyDirectory(new File("../conf"), new File(workingDirectory, "WEB-INF/classes"));
if (propertiesFile != null && propertiesFile.exists()) {
FileUtils.copyFile(propertiesFile, new File(workingDirectory,
"WEB-INF/classes/symmetric.properties"));
}
JarBuilder builder = new JarBuilder(workingDirectory, new File(warFileName),
new File[] { workingDirectory }, Version.version());
builder.build();
FileUtils.deleteDirectory(workingDirectory);
}
private void exportSymTables(CommandLine line, List<String> args) throws IOException {
OutputStreamWriter os = getWriter(args);
os.write(getSymmetricEngine().getSymmetricDialect().getCreateSymmetricDDL());
os.close();
}
private void exportDefaultProperties(CommandLine line, List<String> args) throws IOException {
OutputStreamWriter os = getWriter(args);
BufferedReader is = new BufferedReader(new InputStreamReader(
SymmetricAdmin.class.getResourceAsStream("/symmetric-default.properties"),
Charset.defaultCharset()));
String str = is.readLine();
while (str != null) {
os.write(str);
os.write(System.getProperty("line.separator"));
str = is.readLine();
}
is.close();
os.close();
}
private void createSymTables() {
getSymmetricEngine().setupDatabase(true);
}
private void uninstall(CommandLine line, List<String> args) {
getSymmetricEngine().uninstall();
}
private void sendSql(CommandLine line, List<String> args) {
String tableName = popArg(args, "Table Name");
String sql = popArg(args, "SQL");
String catalogName = line.getOptionValue(OPTION_CATALOG);
String schemaName = line.getOptionValue(OPTION_SCHEMA);
for (Node node : getNodes(line)) {
System.out.println("Sending SQL to node '" + node.getNodeId() + "'");
getSymmetricEngine().getDataService().sendSQL(node.getNodeId(), catalogName,
schemaName, tableName, sql, false);
}
}
private void reloadTable(CommandLine line, List<String> args) {
String tableName = popArg(args, "Table Name");
String catalogName = line.getOptionValue(OPTION_CATALOG);
String schemaName = line.getOptionValue(OPTION_SCHEMA);
for (Node node : getNodes(line)) {
System.out.println("Reloading table to node '" + node.getNodeId() + "'");
if (line.hasOption(OPTION_WHERE)) {
getSymmetricEngine().getDataService().reloadTable(node.getNodeId(), catalogName,
schemaName, tableName, line.getOptionValue(OPTION_WHERE));
} else {
getSymmetricEngine().getDataService().reloadTable(node.getNodeId(), catalogName,
schemaName, tableName);
}
}
}
private void sendSchema(CommandLine line, List<String> args) {
String tableName = popArg(args, "Table Name");
String catalog = line.getOptionValue(OPTION_CATALOG);
String schema = line.getOptionValue(OPTION_SCHEMA);
Collection<Node> nodes = getNodes(line);
for (Node node : nodes) {
getSymmetricEngine().getDataService().sendSchema(node.getNodeId(), catalog, schema,
tableName, false);
}
}
private void sendScript(CommandLine line, List<String> args) throws Exception {
String scriptName = popArg(args, "Script Name");
String scriptData = FileUtils.readFileToString(new File(scriptName));
for (Node node : getNodes(line)) {
System.out.println("Sending script to node '" + node.getNodeId() + "'");
getSymmetricEngine().getDataService().sendScript(node.getNodeId(), scriptData, false);
}
}
private Collection<Node> getNodes(CommandLine line) {
if (line.hasOption(OPTION_NODE_GROUP)) {
return getSymmetricEngine().getNodeService().findEnabledNodesFromNodeGroup(
line.getOptionValue(OPTION_NODE_GROUP));
} else if (line.hasOption(OPTION_NODE)) {
Collection<Node> nodes = new ArrayList<Node>();
String nodeId = line.getOptionValue(OPTION_NODE);
Node node = getSymmetricEngine().getNodeService().findNode(nodeId);
if (node == null) {
System.out.println("ERROR: Unable to find node '" + nodeId + "'");
System.exit(1);
}
nodes.add(node);
return nodes;
} else {
System.out.println("ERROR: Must specify one option: " + OPTION_NODE + ", "
+ OPTION_NODE_GROUP);
System.exit(1);
}
return null;
}
private OutputStreamWriter getWriter(List<String> args) throws IOException {
OutputStreamWriter os = null;
if (args.size() == 0) {
os = new OutputStreamWriter(System.out);
} else {
File file = new File(args.get(0));
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
os = new FileWriter(file);
}
return os;
}
}