-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUpgradeUtilDriver.java
359 lines (300 loc) · 13.5 KB
/
UpgradeUtilDriver.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
/*
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree.
*/
package org.fcrepo.upgrade.utils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.riot.RDFLanguages;
import java.io.File;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static org.fcrepo.upgrade.utils.Config.VALID_DIGEST_ALGORITHMS;
import static org.slf4j.LoggerFactory.getLogger;
/**
* @author dbernstein
* @since 2019-08-05
*/
public class UpgradeUtilDriver {
private static final org.slf4j.Logger logger = getLogger(UpgradeUtilDriver.class);
public static final Map<FedoraVersion, Set<FedoraVersion>> VALID_MIGRATION_PATHS = new HashMap<>();
static {
VALID_MIGRATION_PATHS.put(FedoraVersion.V_4_7_5, Set.of(FedoraVersion.V_5));
VALID_MIGRATION_PATHS.put(FedoraVersion.V_5, Set.of(FedoraVersion.V_6));
}
private UpgradeUtilDriver() {
// Prevent public instantiation
}
/**
* The main entry point
*
* @param args from the command line
*/
public static void main(final String[] args) {
final UpgradeUtilDriver driver = new UpgradeUtilDriver();
try {
driver.run(args);
} catch (final Exception e) {
logger.error("Error performing upgrade: {}", e.getMessage());
logger.debug("Stacktrace: ", e);
}
}
private void run(final String[] args) {
final var configOptions = options();
final var config = parseOptions(configOptions, args);
try {
//start the upgrade
final var manager = UpgradeManagerFactory.create(config);
manager.start();
} catch (final Exception e) {
logger.error("Upgrade failed.", e);
printHelpAndExit(format("Upgrade failed: %s -> See log for details.", e.getMessage()), configOptions);
}
}
private Config parseOptions(final Options configOptions, final String[] args) {
// first see if they've specified a config file
final CommandLineParser parser = new DefaultParser();
final CommandLine cmd;
try {
cmd = parser.parse(configOptions, args);
} catch (final ParseException e) {
printHelpAndExit(e.getMessage(), configOptions);
throw new RuntimeException("I am unreachable");
}
logger.info("Command line parameters: ");
for(final var option : cmd.getOptions()) {
logger.info(" {}: {}", option.getLongOpt(), option.getValues());
}
final File inputDir = new File(cmd.getOptionValue("i"));
if (!inputDir.exists()) {
printHelpAndExit(format("input directory %s does not exist.", inputDir.getAbsolutePath()), configOptions);
}
final var commandArgs = cmd.getArgs();
if(commandArgs.length > 0) {
final var errorMessage =
new StringBuilder(format("The following argument(s) were not expected: %s .", commandArgs));
errorMessage.append(" Please ensure all arguments are associated with a valid command-line flag.");
printHelpAndExit(errorMessage.toString(),options());
}
final String outputDirStr = cmd.getOptionValue("o");
final File outputDir;
if (outputDirStr == null) {
final var date = Calendar.getInstance().getTime();
final var dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
final var strCurrentTime = dateFormat.format(date);
outputDir = new File("output_" + strCurrentTime);
outputDir.mkdirs();
} else {
outputDir = new File(outputDirStr);
outputDir.mkdirs();
if (!outputDir.exists()) {
printHelpAndExit(format("output directory %s could not be created.", outputDir.getAbsolutePath()),
configOptions);
}
}
logger.info("input directory: {}", inputDir.getAbsolutePath());
logger.info("output directory: {}", outputDir.getAbsolutePath());
final var config = new Config();
config.setSourceVersion(FedoraVersion.fromString(cmd.getOptionValue("s")));
config.setTargetVersion(FedoraVersion.fromString(cmd.getOptionValue("t")));
config.setInputDir(inputDir);
config.setOutputDir(outputDir);
if (cmd.hasOption("source-rdf")) {
config.setSrcRdfLang(RDFLanguages.contentTypeToLang(cmd.getOptionValue("source-rdf")));
}
if (cmd.hasOption("threads")) {
config.setThreads(Integer.valueOf(cmd.getOptionValue("threads")));
}
if (config.getTargetVersion().equals(FedoraVersion.V_6)) {
//base URI only used when migrating to F6
config.setBaseUri(cmd.getOptionValue("base-uri"));
}
if (cmd.hasOption("digest-algorithm")) {
final var algo = cmd.getOptionValue("digest-algorithm");
if (!VALID_DIGEST_ALGORITHMS.contains(algo)) {
printHelpAndExit(format("invalid digest algorithm (%s) provided, must be one of %s",
algo, String.join(", ", VALID_DIGEST_ALGORITHMS)), configOptions);
}
config.setDigestAlgorithm(cmd.getOptionValue("digest-algorithm"));
}
config.setFedoraUser(cmd.getOptionValue("migration-user"));
config.setFedoraUserAddress(cmd.getOptionValue("migration-user-address"));
config.setWriteToS3(cmd.hasOption("write-to-s3"));
config.setS3Region(cmd.getOptionValue("s3-region"));
config.setS3Endpoint(cmd.getOptionValue("s3-endpoint"));
config.setS3PathStyleAccess(cmd.hasOption("s3-path-style-access"));
config.setS3AccessKey(cmd.getOptionValue("s3-access-key"));
config.setS3SecretKey(cmd.getOptionValue("s3-secret-key"));
config.setS3Bucket(cmd.getOptionValue("s3-bucket"));
config.setS3Prefix(cmd.getOptionValue("s3-prefix"));
if (cmd.getOptionValue('R') != null) {
config.setResourceInfoFile(Paths.get(cmd.getOptionValue('R')));
}
if (config.getTargetVersion() == FedoraVersion.V_6) {
if (config.getBaseUri() == null) {
printHelpAndExit("base-uri must be specified when migrating to Fedora 6", configOptions);
}
}
if (config.isWriteToS3() && StringUtils.isBlank(config.getS3Bucket())) {
printHelpAndExit("s3-bucket must be specified when writing to S3", configOptions);
}
return config;
}
private Options options() {
// Help option
final Options configOptions = new org.apache.commons.cli.Options();
configOptions.addOption(Option.builder("h")
.longOpt("help")
.hasArg(false)
.desc("Print these options")
.required(false)
.build());
configOptions.addOption(Option.builder("i")
.longOpt("input-dir")
.hasArg(true)
.desc("The path to the directory containing a Fedora 4.7.x or Fedora 5.x export")
.required(true)
.build());
configOptions.addOption(Option.builder("o")
.longOpt("output-dir")
.hasArg(true)
.desc("The path to the directory where upgraded resources will be written. " +
"Default value: output_<yyyyMMdd-HHmmss>. " +
"For example: output_20200101-075901")
.required(false)
.build());
configOptions.addOption(Option.builder("s")
.longOpt("source-version")
.hasArg(true)
.desc(format(
"The version of Fedora that was the source of the export. Valid values: %s",
join(VALID_MIGRATION_PATHS.keySet())))
.required(true)
.build());
configOptions.addOption(Option.builder("t")
.longOpt("target-version")
.hasArg(true)
.desc(format("The version of Fedora to which you are upgrading. Valid values: %s",
join(VALID_MIGRATION_PATHS.values().stream()
.flatMap(x -> x.stream()).collect(Collectors.toSet()))))
.required(true)
.build());
// F6 migration opts
configOptions.addOption(Option.builder("r")
.longOpt("source-rdf")
.hasArg(true)
.desc("The RDF language used in the Fedora export. Default: " + Config.DEFAULT_SRC_RDF_LANG.getName())
.required(false)
.build());
configOptions.addOption(Option.builder("u")
.longOpt("base-uri")
.hasArg(true)
.desc("Fedora's base URI. For example, http://localhost:8080/rest")
.required(false)
.build());
configOptions.addOption(Option.builder("p")
.longOpt("threads")
.hasArg(true)
.desc("The number of threads to use. Default: the number of available cores")
.required(false)
.build());
configOptions.addOption(Option.builder("d")
.longOpt("digest-algorithm")
.hasArg(true)
.desc("The digest algorithm to use in OCFL. Default: " + Config.DEFAULT_DIGEST_ALGORITHM)
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("migration-user")
.hasArg(true)
.desc("The user to attribute OCFL versions to. Default: " + Config.DEFAULT_USER)
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("migration-user-address")
.hasArg(true)
.desc("The address of the user OCFL versions are attributed to. Default: "
+ Config.DEFAULT_USER_ADDRESS)
.required(false)
.build());
// S3 options
configOptions.addOption(Option.builder()
.longOpt("write-to-s3")
.hasArg(false)
.desc("Enables writing migrated Fedora 6 data to S3 rather than the local filesystem")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-bucket")
.hasArg(true)
.desc("The S3 bucket to write to, required when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-prefix")
.hasArg(true)
.desc("The S3 prefix to locate the OCFL repo in, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-region")
.hasArg(true)
.desc("The AWS region, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-endpoint")
.hasArg(true)
.desc("The AWS endpoint URL, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-path-style-access")
.hasArg(false)
.desc("The S3 access style, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-access-key")
.hasArg(true)
.desc("The AWS access key, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder()
.longOpt("s3-secret-key")
.hasArg(true)
.desc("The AWS secret key, optionally use when writing to S3")
.required(false)
.build());
configOptions.addOption(Option.builder("R")
.longOpt("resource-info-file")
.hasArg(true)
.desc("The path the file that contains a list of resources to be processed")
.required(false)
.build());
return configOptions;
}
private Object join(final Collection<FedoraVersion> versions) {
return versions.stream().map(FedoraVersion::getStringValue).collect(Collectors.joining(","));
}
private static void printHelpAndExit(final String errorMessage, final Options options) {
final HelpFormatter formatter = new HelpFormatter();
System.err.println(errorMessage);
formatter.printHelp("java -jar fcrepo-upgrade-util-<version>.jar", options);
System.exit(1);
}
}