/
LinuxMachine.java
1746 lines (1598 loc) · 81.2 KB
/
LinuxMachine.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* #%L
* Netarchivesuite - deploy
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.deploy;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Element;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.utils.FileUtils;
/**
* A LinuxMachine is the instance of the abstract machine class, which runs the operating system Linux or another Unix
* dependent operation system. This class only contains the operating system specific functions.
*/
public class LinuxMachine extends Machine {
public static final String HERITRIX_1_CLASSNAME = "org.archive.crawler.Heritrix";
protected LinkedHashMap<String, String> bundles = new LinkedHashMap<>();
protected LinkedHashMap<String, String> certificates = new LinkedHashMap<String, String>();
/**
* The constructor. Starts by initialising the parent abstract class, then sets the operating system dependent
* variables.
*
* @param subTreeRoot The XML root element.
* @param parentSettings The Settings to be inherited from the PhysicalLocation, where this machine is placed.
* @param param The machine parameters to be inherited from the PhysicalLocation.
* @param netarchiveSuiteSource The name of the NetarchiveSuite package file. Must end with '.zip'.
* @param securityPolicy The security policy file, to be copied into machine directory.
* @param dbFile The name of the database file.
* @param arcdbFile The name of the archive file.
* @param resetDir Whether the temporary directory should be reset.
* @param externalJarFolder The folder containing the external jar library files.
* @param logoFile user specific logo png file.
* @param menulogoFile user specific menulogo png file.
* @param deployConfiguration The general deployment configuration.
* @param bitmagDir The folder containing the bitmagasin confiuration.
*/
public LinuxMachine(Element subTreeRoot, XmlStructure parentSettings, Parameters param,
String netarchiveSuiteSource, File slf4JConfig, File securityPolicy, File dbFile,
File arcdbFile, boolean resetDir, File externalJarFolder, File logoFile, File menulogoFile,
DeployConfiguration deployConfiguration, File bitmagDir) {
super(subTreeRoot, parentSettings, param, netarchiveSuiteSource, slf4JConfig, securityPolicy, dbFile,
arcdbFile, resetDir, externalJarFolder, logoFile, menulogoFile, bitmagDir);
// set operating system
operatingSystem = Constants.OPERATING_SYSTEM_LINUX_ATTRIBUTE;
scriptExtension = Constants.SCRIPT_EXTENSION_LINUX;
String[] bundlesArr;
String[] certificatesArr;
String srcStr;
String dstStr;
int idx;
for (Application app : applications) {
if (app.isBundledHarvester()) {
bundlesArr = app.getSettingsValues(Constants.SETTINGS_HARVEST_HERITRIX3_BUNDLE_LEAF);
if ((bundlesArr == null || bundlesArr.length == 0)
&& deployConfiguration.getDefaultBundlerZip() != null) {
bundlesArr = new String[] { deployConfiguration.getDefaultBundlerZip().getAbsolutePath() };
} else if ((bundlesArr == null || bundlesArr.length == 0 || bundlesArr[0].length()==0)
&& deployConfiguration.getDefaultBundlerZip() == null) {
throw new IllegalArgumentException("A Heritrix bundler needs to be defined for H3 controllers, "
+ "either directly in the deploy configuration or from the command line with the -B option.");
}
certificatesArr = app.getSettingsValues(Constants.SETTINGS_HARVEST_HERITRIX3_CERTIFICATE_LEAF);
if (bundlesArr != null && bundlesArr.length > 0 && certificatesArr != null) {
for (int i = 0; i < bundlesArr.length; ++i) {
srcStr = bundlesArr[i];
if (!bundles.containsKey(srcStr)) {
idx = srcStr.lastIndexOf('/');
if (idx != -1) {
dstStr = srcStr.substring(idx + 1);
} else {
dstStr = srcStr;
}
dstStr = machineParameters.getInstallDirValue() + "/" + getEnvironmentName() + "/" + dstStr;
bundles.put(srcStr, dstStr);
System.out.println(srcStr + " -> " + dstStr);
}
}
for (int i = 0; i < certificatesArr.length; ++i) {
srcStr = certificatesArr[i];
if (!certificates.containsKey(srcStr)) {
idx = srcStr.lastIndexOf('/');
if (idx != -1) {
dstStr = srcStr.substring(idx + 1);
} else {
dstStr = srcStr;
}
dstStr = machineParameters.getInstallDirValue() + "/" + getEnvironmentName() + "/" + dstStr;
certificates.put(srcStr, dstStr);
System.out.println(srcStr + " -> " + dstStr);
}
}
XmlStructure appSettings = app.getSettings();
Element heritrixBundleElement =
appSettings.getSubChild(Constants.SETTINGS_HARVEST_HERITRIX3_BUNDLE_LEAF);
if (heritrixBundleElement == null) {
appSettings.getSubChild(Constants.SETTINGS_HERITRIX3_BRANCH).addElement("bundle");
heritrixBundleElement =
appSettings.getSubChild(Constants.SETTINGS_HARVEST_HERITRIX3_BUNDLE_LEAF);
heritrixBundleElement.setText((String)bundles.values().toArray()[0]);
} else {
heritrixBundleElement.setText(bundles.get(heritrixBundleElement.getText()));
}
Element h3KeystoreElement = appSettings
.getSubChild(Constants.SETTINGS_HARVEST_HERITRIX3_CERTIFICATE_LEAF);
if (h3KeystoreElement != null) {
String h3KeystoreName = certificates.get(h3KeystoreElement.getText());
if (h3KeystoreName != null) {
h3KeystoreElement.setText(h3KeystoreName);
}
}
}
}
}
}
protected static final class osInstallScriptTpl {
protected static final String[] mainScript = {
// echo copying null.zip to:kb-test-adm-001.kb.dk
"echo copying ${netarchiveSuiteFileName} to:${name}",
// scp null.zip dev@kb-test-adm-001.kb.dk:/home/dev
"scp ${netarchiveSuiteFileName} ${machineUserLogin}:${installDirValue}",
// echo deleting dev@kb-test-adm-001.kb.dk:/home/dev/TEST/lib
"echo deleting ${machineUserLogin}:${installDirValue}/${environmentName}/lib",
// ssh dev@kb-test-adm-001.kb.dk rm -rf /home/dev/TEST/lib
"ssh ${machineUserLogin} \"rm -rf ${installDirValue}/${environmentName}/lib\"",
// echo unzipping null.zip at:kb-test-adm-001.kb.dk
"echo unzipping ${netarchiveSuiteFileName} at:${name}",
// ssh dev@kb-test-adm-001.kb.dk unzip -q -o /home/dev/null.zip -d /home/dev/TEST
"ssh ${machineUserLogin} \"unzip -q -o ${installDirValue}/${netarchiveSuiteFileName} -d ${installDirValue}/${environmentName}\"",
// update Logos.
"${osUpdateLogos}",
// create other directories.
"${osInstallScriptCreateDir}",
// echo preparing for copying of settings and scripts
"echo preparing for copying of settings and scripts",
// For overriding jmxremote.password give user all rights.
// ssh machine: "if [ -e conf/jmxremote.password ];
// then chmod u+rwx conf/jmxremote.password; fi; "
"ssh ${machineUserLogin} \" cd ~; if [ -e ${installDirValue}/${environmentName}/conf/jmxremote.password ]; then chmod u+rwx ${installDirValue}/${environmentName}/conf/jmxremote.password; fi; \"",
// For overriding jmxremote.access give user all rights.
// ssh machine: "if [ -e conf/jmxremote.access ];
// then chmod u+rwx conf/jmxremote.access; fi; "
"ssh ${machineUserLogin} \" cd ~; if [ -e ${installDirValue}/${environmentName}/conf/jmxremote.access ]; then chmod u+rwx ${installDirValue}/${environmentName}/conf/jmxremote.access; fi; \"",
// echo copying settings and scripts
"echo copying settings and scripts",
// scp -r kb-test-adm-001.kb.dk/* dev@kb-test-adm-001.kb.dk:/home/dev/TEST/conf/
"scp -r ${name}/* ${machineUserLogin}:${installDirValue}/${environmentName}/conf/",
// INSTALL EXTERNAL JAR FILES.
"${osInstallExternalJarFiles}",
// APPLY HARVEST DEFINITION DATABASE!
"${osInstallDatabase}",
// APPLY ARCHIVE DATABASE!
"${osInstallArchiveDatabase}",
// echo make scripts executable
"echo make scripts executable",
// Allow only user to be able to deal with these files
// (go=-rwx,u=+rwx) = 700.
// ssh dev@kb-test-adm-001.kb.dk "chmod 700 /home/dev/TEST/conf/*.sh "
"ssh ${machineUserLogin} \"chmod 700 ${installDirValue}/${environmentName}/conf/*.sh \"",
// HANDLE JMXREMOTE PASSWORD AND ACCESS FILE.
"${JMXremoteFilesCommand}",
// END OF SCRIPT
"${heritrix3Files}"
};
protected static final String scpFile = "scp ${srcFileName} ${machineUserLogin}:${dstFileName}";
}
@Override
protected String osInstallScript() {
Map<String, String> env = new HashMap<String, String>();
StringBuilder sb = new StringBuilder();
Iterator<Entry<String, String>> iter;
String tmpStr;
env.put("machineUserLogin", machineUserLogin());
iter = bundles.entrySet().iterator();
Entry<String, String> entry;
while (iter.hasNext()) {
entry = iter.next();
env.put("srcFileName", entry.getKey());
env.put("dstFileName", entry.getValue());
tmpStr = Template.untemplate(osInstallScriptTpl.scpFile, env, true);
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(tmpStr);
}
iter = certificates.entrySet().iterator();
while (iter.hasNext()) {
entry = iter.next();
env.put("srcFileName", entry.getKey());
env.put("dstFileName", entry.getValue());
tmpStr = Template.untemplate(osInstallScriptTpl.scpFile, env, true);
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(tmpStr);
}
env.clear();
env.put("netarchiveSuiteFileName", netarchiveSuiteFileName);
env.put("name", hostname);
env.put("machineUserLogin", machineUserLogin());
env.put("installDirValue", machineParameters.getInstallDirValue());
env.put("environmentName", getEnvironmentName());
env.put("osUpdateLogos", osUpdateLogos());
env.put("osInstallScriptCreateDir", osInstallScriptCreateDir());
env.put("osInstallExternalJarFiles", osInstallExternalJarFiles());
env.put("osInstallDatabase", osInstallDatabase());
env.put("osInstallArchiveDatabase", osInstallArchiveDatabase());
env.put("JMXremoteFilesCommand", getJMXremoteFilesCommand());
env.put("heritrix3Files", sb.toString());
String str = Template.untemplate(osInstallScriptTpl.mainScript, env, true, "\n");
return str;
}
protected static final class osKillScriptTpl {
protected static final String[] mainScript = {
"ssh ${machineUserLogin} \". /etc/profile; ${installDirValue}/${environmentName}/conf/killall.sh\";"
};
}
@Override
protected String osKillScript() {
Map<String, String> env = new HashMap<String, String>();
env.put("machineUserLogin", machineUserLogin());
env.put("environmentName", getEnvironmentName());
env.put("installDirValue", machineParameters.getInstallDirValue());
String str = Template.untemplate(osKillScriptTpl.mainScript, env, true, "\n");
return str;
}
protected static final class osStartScriptTpl {
protected static final String[] mainScript = {
"ssh ${machineUserLogin} \". /etc/profile;. ~/.bash_profile; ${installDirValue}/${environmentName}/conf/startall.sh; sleep 5; cat ${installDirValue}/${environmentName}/*.log\""
};
}
/**
* Creates the operation system specific starting script for this machine.
* <p>
* pseudo code: - ssh maclogin ". /etc/profile; conf/startall.sh; sleep 5; cat install/*.log"
* <p>
* where: maclogin = login for machine (username@machine). conf = path to /conf directory. install = path to install
* directory.
*
* @return Operation system specific part of the startscript.
*/
@Override
protected String osStartScript() {
Map<String, String> env = new HashMap<String, String>();
env.put("machineUserLogin", machineUserLogin());
env.put("environmentName", getEnvironmentName());
env.put("installDirValue", machineParameters.getInstallDirValue());
String str = Template.untemplate(osStartScriptTpl.mainScript, env, true, "\n");
return str;
}
@Override
protected String getInstallDirPath() {
return machineParameters.getInstallDirValue() + Constants.SLASH + getEnvironmentName();
}
@Override
protected String getConfDirPath() {
return getInstallDirPath() + Constants.CONF_DIR_LINUX;
}
/**
* Creates the local path to the lib dir.
*
* @return The path to the lib dir for ssh.
*/
protected String getLocalLibDirPath() {
return Constants.LIB_DIR_LINUX;
}
/**
* This function creates the script to kill all applications on this machine. The scripts calls all the kill script
* for each application. It also runs the script for killing any external database.
* <p>
* pseudo code: - echo Killing all applications at machine: mac - if [ -e ./kill_app.sh ] - ./kill_app.sh - fi - ...
* <p>
* where: mac = machine name. app = application name. ... = the same for other applications.
*
* @param directory The directory for this machine (use global variable?).
* @throws IOFailure If an error occurred during the creation of the local killall script.
*/
@Override
protected void createOSLocalKillAllScript(File directory) throws IOFailure {
// create the kill all script file
File killAllScript = new File(directory, Constants.SCRIPT_NAME_KILL_ALL + scriptExtension);
try {
// Initialise script
PrintWriter killPrinter = new PrintWriter(killAllScript, getTargetEncoding());
try {
killPrinter.println(ScriptConstants.ECHO_KILL_ALL_APPS + Constants.COLON + Constants.SPACE
+ Constants.APOSTROPHE + hostname + Constants.APOSTROPHE);
killPrinter.println(ScriptConstants.BIN_BASH_COMMENT);
killPrinter.println(ScriptConstants.CD + Constants.SPACE + getConfDirPath());
// insert path to kill script for all applications
for (Application app : applications) {
// Constructing filename
String appScript = Constants.DOT + Constants.SLASH + Constants.SCRIPT_NAME_LOCAL_KILL
+ app.getIdentification() + scriptExtension;
// check if file exists
killPrinter.println(ScriptConstants.LINUX_IF_EXIST + Constants.SPACE + appScript + Constants.SPACE
+ ScriptConstants.LINUX_THEN + Constants.SPACE);
killPrinter.println(ScriptConstants.MULTI_SPACE_6 + appScript);
killPrinter.println(ScriptConstants.FI);
}
// kill the harvest database, if any, after the applications
killPrinter.print(callKillHarvestDatabase());
// kill the admin database, if any, after the applications
killPrinter.print(callKillArchiveDatabase());
} finally {
// close script
killPrinter.close();
}
} catch (IOException e) {
String msg = "Problems creating local kill all script. ";
log.error(msg, e);
throw new IOFailure(msg, e);
}
}
/**
* This function creates the script to start all applications on this machine. The scripts calls all the start
* script for each application. It also runs the script for starting any external database.
* <p>
* pseudo code: - echo Starting all applications at machine: mac - if [ -e ./start_app.sh ] - ./start_app.sh - fi -
* ...
* <p>
* where: mac = machine name. app = application name. ... = the same for other applications.
*
* @param directory The directory for this machine (use global variable?).
* @throws IOFailure If an error occurred during the creation of the local startall script.
*/
@Override
protected void createOSLocalStartAllScript(File directory) throws IOFailure {
// create the start all script file
File startAllScript = new File(directory, Constants.SCRIPT_NAME_START_ALL + scriptExtension);
try {
// Initialise script
PrintWriter startPrinter = new PrintWriter(startAllScript, getTargetEncoding());
try {
startPrinter.println(ScriptConstants.BIN_BASH_COMMENT);
startPrinter.println(ScriptConstants.CD + Constants.SPACE + getConfDirPath());
// start the harvest database, if any, before the applications.
startPrinter.print(callStartHarvestDatabase());
// start the admin database, if any, before the applications.
startPrinter.print(callStartArchiveDatabase());
startPrinter.println(ScriptConstants.ECHO_START_ALL_APPS + Constants.COLON + Constants.SPACE
+ Constants.APOSTROPHE + hostname + Constants.APOSTROPHE);
// insert path to start script for each applications
for (Application app : applications) {
// make name of file
String appScript = Constants.DOT + Constants.SLASH + Constants.SCRIPT_NAME_LOCAL_START
+ app.getIdentification() + scriptExtension;
// check if file exists
startPrinter.println(ScriptConstants.LINUX_IF_EXIST + Constants.SPACE + appScript + Constants.SPACE
+ ScriptConstants.LINUX_THEN + Constants.SPACE);
startPrinter.println(ScriptConstants.MULTI_SPACE_6 + appScript);
startPrinter.println(ScriptConstants.FI);
}
} finally {
// close script
startPrinter.close();
}
} catch (IOException e) {
String msg = "Problems creating local start all script. ";
log.trace(msg, e);
throw new IOFailure(msg, e);
}
}
/**
* Creates the kill scripts for all the applications.
* <p>
* The script starts by finding all running processes of the application. If it finds any processes, it kills them.
* <p>
* The kill_app.sh should have the following structure:
* <p>
* - echo Killing linux application. - #!/bin/bash - PIDS = $(ps -wwfe | grep fullapp | grep -v grep | grep
* path\settings_app.xml | awk "{print \\$2}") - if [ -n "$PIDS" ]; then - kill -9 $PIDS; - fi
* <p>
* Also, if a heritrix process is started, the following is added: - PIDS = $(ps -wwfe | grep heritrix | grep -v
* grep | grep path\settings_app.xml | awk "{print \\$2}") - if [ -n "$PIDS" ]; then - kill -9 $PIDS; - fi
* <p>
* where: path = the path to the ./conf directory. fullapp = the full application name with class path. app = the id
* of the application (name + instanceId). heritrix = the heritrix class path.
*
* @param directory The directory for this machine (use global variable?).
* @throws IOFailure If an error occured during the creation of the kill application script file.
*/
@Override
protected void createApplicationKillScripts(File directory) throws IOFailure {
// go through all applications and create their kill script
for (Application app : applications) {
File appKillScript = new File(directory, Constants.SCRIPT_NAME_LOCAL_KILL + app.getIdentification()
+ scriptExtension);
try {
// make print writer for writing to file
PrintWriter appPrint = new PrintWriter(appKillScript, getTargetEncoding());
try {
// echo Killing linux application.
appPrint.println(ScriptConstants.ECHO_KILL_LINUX_APPLICATION + Constants.COLON + Constants.SPACE
+ app.getIdentification());
// #!/bin/bash
appPrint.println(ScriptConstants.BIN_BASH_COMMENT);
// First try an ordinary kill, wait 2 seconds. Then test,
// if process is still around. If it is make a hard kill
// (-9)
// PIDS = $(ps -wwfe | grep fullapp | grep -v grep | grep
// path\settings_app.xml | awk "{print \\$2}")
appPrint.println(ScriptConstants.getLinuxPIDS(app.getTotalName(), getConfDirPath(),
app.getIdentification()));
// if [ -n "$PIDS" ]; then
appPrint.println(ScriptConstants.LINUX_IF_N_EXIST + Constants.SPACE + Constants.QUOTE_MARK
+ ScriptConstants.PIDS + Constants.QUOTE_MARK + Constants.SPACE
+ ScriptConstants.LINUX_N_THEN);
appPrint.println(ScriptConstants.KILL_PIDS + Constants.SEMICOLON);
// fi
appPrint.println(ScriptConstants.FI);
appPrint.println();
appPrint.println(ScriptConstants.SLEEP_2);
appPrint.println();
// Set PIDS
appPrint.println(ScriptConstants.getLinuxPIDS(app.getTotalName(), getConfDirPath(),
app.getIdentification()));
// IF
appPrint.println(ScriptConstants.LINUX_IF_N_EXIST + Constants.SPACE + Constants.QUOTE_MARK
+ ScriptConstants.PIDS + Constants.QUOTE_MARK + Constants.SPACE
+ ScriptConstants.LINUX_N_THEN);
// kill -9 $PIDS;
appPrint.println(ScriptConstants.KILL_9_PIDS + Constants.SEMICOLON);
// fi
appPrint.println(ScriptConstants.FI);
// If the application contains a heritrix instance,
// then make script for killing the heritrix process.
String[] heritrixJmxPort = app.getSettingsValues(Constants.SETTINGS_HARVEST_HERITRIX_JMX_PORT);
if (heritrixJmxPort != null && heritrixJmxPort.length > 0) {
// log if more than one jmx port defined for heritrix.
if (heritrixJmxPort.length > 1) {
log.trace(heritrixJmxPort.length + " number of jmx-ports for a heritrix " + "harvester.");
}
appPrint.println();
// - PIDS = $(ps -wwfe | grep heritrix | grep -v grep
// | grep path\settings_app.xml | awk "{print \\$2}")
appPrint.println(ScriptConstants.getLinuxPIDS(HERITRIX_1_CLASSNAME, getConfDirPath(),
app.getIdentification()));
// - if [ -n "$PIDS" ]; then
appPrint.println(ScriptConstants.LINUX_IF_N_EXIST + Constants.SPACE + Constants.QUOTE_MARK
+ ScriptConstants.PIDS + Constants.QUOTE_MARK + Constants.SPACE
+ ScriptConstants.LINUX_N_THEN);
// first make a ordinary kill, wait 2 seconds, then make a
// hard kill -9
appPrint.println(ScriptConstants.KILL_PIDS + Constants.SEMICOLON);
// - fi
appPrint.println(ScriptConstants.FI);
appPrint.println();
// wait 2 seconds
appPrint.println(ScriptConstants.SLEEP_2);
appPrint.println();
// See if Process is still around
appPrint.println(ScriptConstants.getLinuxPIDS(HERITRIX_1_CLASSNAME, getConfDirPath(),
app.getIdentification()));
// if still around
appPrint.println(ScriptConstants.LINUX_IF_N_EXIST + Constants.SPACE + Constants.QUOTE_MARK
+ ScriptConstants.PIDS + Constants.QUOTE_MARK + Constants.SPACE
+ ScriptConstants.LINUX_N_THEN);
// - kill -9 $PIDS;
appPrint.println(ScriptConstants.KILL_9_PIDS + Constants.SEMICOLON);
// - fi
appPrint.println(ScriptConstants.FI);
}
} finally {
// close file
appPrint.close();
}
} catch (IOException e) {
String msg = "Problems creating application kill script: ";
log.error(msg, e);
throw new IOFailure(msg, e);
}
}
}
/**
* Creates the start scripts for all the applications.
* <p>
* The application should only be started, if it is not running already. The script starts by finding all running
* processes of the application. If any processes are found, a new application should not be started. Otherwise
* start the application.
* <p>
* The start_app.sh should have the following structure:
* <p>
* - echo Starting linux application: app - cd path - #!/bin/bash - PIDS = $(ps -wwfe | grep fullapp | grep -v grep
* | grep path\settings_app.xml | awk "{print \\$2}") - if [ -n "$PIDS" ]; then - echo Application already running.
* - else - export CLASSPATH = cp:$CLASSPATH; - JAVA - fi
* <p>
* where: path = the path to the install directory. fullapp = the full name application with java path. app = the
* name of the application. cp = the classpaths for the application. JAVA = the command to run the java application.
*
* @param directory The directory for this machine (use global variable?).
* @throws IOFailure If an error occurred during the creation of the start application script file.
*/
@Override
protected void createApplicationStartScripts(File directory) throws IOFailure {
// go through all applications and create their start script
for (Application app : applications) {
if (app.getTotalName().contains("GUI")) {
createHarvestDatabaseUpdateScript(directory, true);
}
File appStartScript = new File(directory, Constants.SCRIPT_NAME_LOCAL_START + app.getIdentification()
+ scriptExtension);
try {
// make print writer for writing to file
PrintWriter appPrint = new PrintWriter(appStartScript, getTargetEncoding());
try {
// #!/bin/bash
appPrint.println(ScriptConstants.ECHO_START_LINUX_APP + Constants.COLON + Constants.SPACE
+ app.getIdentification());
// cd path
appPrint.println(ScriptConstants.CD + Constants.SPACE + app.installPathLinux());
// PIDS = $(ps -wwfe | grep fullapp | grep -v grep | grep
// path\settings_app.xml | awk "{print \\$2}")
appPrint.println(ScriptConstants.getLinuxPIDS(app.getTotalName(), getConfDirPath(),
app.getIdentification()));
// if [ -n "$PIDS" ]; then
appPrint.println(ScriptConstants.LINUX_IF_N_EXIST + Constants.SPACE + Constants.QUOTE_MARK
+ ScriptConstants.PIDS + Constants.QUOTE_MARK + Constants.SPACE
+ ScriptConstants.LINUX_N_THEN);
// echo Application already running.
appPrint.println(ScriptConstants.ECHO_APP_ALREADY_RUNNING);
// else
appPrint.println(ScriptConstants.ELSE);
// export CLASSPATH = cp;
appPrint.println(ScriptConstants.MULTI_SPACE_4 + ScriptConstants.EXPORT_CLASSPATH
+ osGetClassPath(app) + ScriptConstants.VALUE_OF_CLASSPATH + Constants.SEMICOLON);
// JAVA
String securityManagement = "";
if (app.getTotalName().contains(ScriptConstants.BITARCHIVE_APPLICATION_NAME)) {
securityManagement = Constants.SPACE + Constants.DASH + ScriptConstants.OPTION_SECURITY_MANAGER
+ Constants.SPACE + Constants.DASH + ScriptConstants.OPTION_SECURITY_POLICY
+ getConfDirPath() + Constants.SECURITY_POLICY_FILE_NAME;
}
String bitmagOptions = "";
if (app.getTotalName().contains("HarvestControllerApplication") && bitmagasinFolder != null) {
bitmagOptions = Constants.SPACE
+ Constants.DASH
+ ScriptConstants.OPTION_BITMAG_SETTINGS_DIR
+ getConfDirPath()
+ Constants.BITMAG_CLIENT_CONFIG_DIR
+ Constants.SPACE
+ ScriptConstants.OPTION_BITMAG_CERTKEY_FILENAME
+ Constants.BITMAG_CERTKEY_FILE
+ Constants.SPACE;
}
appPrint.println(ScriptConstants.MULTI_SPACE_4
+ ScriptConstants.JAVA
+ Constants.SPACE
+ app.getMachineParameters().writeJavaOptions()
+ Constants.SPACE
+ Constants.DASH
+ ScriptConstants.OPTION_SETTINGS
+ getConfDirPath()
+ Constants.PREFIX_SETTINGS
+ app.getIdentification()
+ Constants.EXTENSION_XML_FILES
// TODO check to see if inheritedSlf4jConfigFile is not null
+ Constants.SPACE + Constants.DASH + ScriptConstants.OPTION_LOGBACK_CONFIG
+ getConfDirPath() + Constants.LOGBACK_PREFIX + app.getIdentification()
+ Constants.EXTENSION_XML_FILES
+ securityManagement
+ bitmagOptions
+ Constants.SPACE + app.getTotalName() + Constants.SPACE + ScriptConstants.LINUX_DEV_NULL
+ Constants.SPACE + Constants.SCRIPT_NAME_LOCAL_START + app.getIdentification()
+ Constants.EXTENSION_LOG_FILES + Constants.SPACE
+ ScriptConstants.LINUX_ERROR_MESSAGE_TO_1);
// fi
appPrint.println(ScriptConstants.FI);
} finally {
// close file
appPrint.close();
}
} catch (IOException e) {
String msg = "Problems creating application start script. ";
log.trace(msg, e);
throw new IOFailure(msg, e);
}
}
}
@Override
protected String osGetClassPath(Application app) {
StringBuilder res = new StringBuilder();
// get all the classpaths
for (Element cp : app.getMachineParameters().getClassPaths()) {
res.append(getInstallDirPath() + Constants.SLASH + cp.getText().trim() + Constants.COLON);
}
return res.toString();
}
@Override
protected String osInstallDatabase() {
StringBuilder res = new StringBuilder();
String databaseDir = machineParameters.getHarvestDatabaseDirValue();
// Do not install if no proper database directory.
if (databaseDir == null || databaseDir.isEmpty()) {
return Constants.EMPTY;
}
// copy to final destination if database argument.
if (databaseFile != null) {
// echo Copying database
res.append(ScriptConstants.ECHO_COPYING_DATABASE);
res.append(Constants.NEWLINE);
// scp database.jar user@machine:dbDir/db
res.append(ScriptConstants.SCP + Constants.SPACE);
res.append(databaseFile.getPath());
res.append(Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.COLON);
res.append(getInstallDirPath());
res.append(Constants.SLASH);
res.append(Constants.HARVEST_DATABASE_BASE_PATH);
res.append(Constants.NEWLINE);
}
// unzip database.
res.append(ScriptConstants.ECHO_UNZIPPING_DATABASE);
res.append(Constants.NEWLINE);
// ssh user@machine "
// cd dir; if [ -d databaseDir ]; then echo ;
// else mkdir databaseDir; fi; if [ $(ls -A databaseDir) ];
// then echo ERROR MESSAGE: DIR NOT EMPTY;
// else unzip -q -o dbDir/db -d databaseDir/.; fi; exit;
// "
res.append(ScriptConstants.SSH + Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.SPACE + Constants.QUOTE_MARK + ScriptConstants.CD + Constants.SPACE);
res.append(getInstallDirPath());
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.LINUX_IF_DIR_EXIST + Constants.SPACE);
res.append(databaseDir);
res.append(Constants.SPACE + ScriptConstants.LINUX_THEN + Constants.SPACE + ScriptConstants.ECHO
+ Constants.SPACE);
res.append(ScriptConstants.DATABASE_ERROR_PROMPT_DIR_NOT_EMPTY);
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.ELSE + Constants.SPACE
+ ScriptConstants.LINUX_UNZIP_COMMAND + Constants.SPACE);
res.append(Constants.HARVEST_DATABASE_BASE_PATH);
res.append(Constants.SPACE + ScriptConstants.SCRIPT_DIR + Constants.SPACE);
res.append(databaseDir);
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.FI + Constants.SEMICOLON + Constants.SPACE
+ ScriptConstants.EXIT + Constants.SEMICOLON + Constants.SPACE + Constants.QUOTE_MARK);
res.append(Constants.NEWLINE);
return res.toString();
}
@Override
protected String osInstallExternalJarFiles() {
if (jarFolder == null) {
return Constants.EMPTY;
}
StringBuilder res = new StringBuilder();
// Comment about copying files.
res.append(ScriptConstants.ECHO_INSTALLING_EXTERNAL_JAR_FILES);
res.append(Constants.NEWLINE);
// if [ -d folder ]; then scp folder machine:installdir/external; fi;
res.append(ScriptConstants.LINUX_IF_DIR_EXIST + Constants.SPACE);
res.append(jarFolder.getPath());
res.append(Constants.SPACE + ScriptConstants.LINUX_THEN + Constants.SPACE + ScriptConstants.SCP
+ Constants.SPACE + ScriptConstants.DASH_R + Constants.SPACE);
res.append(jarFolder.getPath());
res.append(Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.COLON);
res.append(getInstallDirPath());
res.append(Constants.SLASH + Constants.EXTERNAL_JAR_DIRECTORY + Constants.SEMICOLON + Constants.SPACE
+ ScriptConstants.FI + Constants.SEMICOLON);
res.append(Constants.NEWLINE);
return res.toString();
}
@Override
protected String osInstallArchiveDatabase() {
String adminDatabaseDir = machineParameters.getArchiveDatabaseDirValue();
// Do not install if no proper archive database directory.
if (adminDatabaseDir == null || adminDatabaseDir.isEmpty()) {
return Constants.EMPTY;
}
// Initialise the StringBuilder containing the resulting script.
StringBuilder res = new StringBuilder();
// copy to final destination if database argument.
if (arcDatabaseFile != null) {
// echo Copying database
res.append(ScriptConstants.ECHO_COPYING_ARCHIVE_DATABASE);
res.append(Constants.NEWLINE);
// scp database.jar user@machine:dbDir/bpdb
res.append(ScriptConstants.SCP + Constants.SPACE);
res.append(arcDatabaseFile.getPath());
res.append(Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.COLON);
res.append(getInstallDirPath());
res.append(Constants.SLASH);
// Now the two databases are in different directories
res.append(Constants.ARCHIVE_DATABASE_BASE_PATH);
res.append(Constants.NEWLINE);
}
// unzip database.
res.append(ScriptConstants.ECHO_UNZIPPING_ARCHIVE_DATABASE);
res.append(Constants.NEWLINE);
// ssh user@machine "
// cd dir; if [ -d bpDatabaseDir ]; then echo ;
// else mkdir bpDatabaseDir; fi; if [ $(ls -A bpDatabaseDir) ];
// then echo ERROR MESSAGE: DIR NOT EMPTY;
// else unzip -q -o dbDir/bpdb -d databaseDir/.; fi; exit;
// "
res.append(ScriptConstants.SSH + Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.SPACE + Constants.QUOTE_MARK + ScriptConstants.CD + Constants.SPACE);
res.append(getInstallDirPath());
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.LINUX_IF_DIR_EXIST + Constants.SPACE);
res.append(adminDatabaseDir);
res.append(Constants.SPACE + ScriptConstants.LINUX_THEN + Constants.SPACE + ScriptConstants.ECHO
+ Constants.SPACE);
res.append(ScriptConstants.DATABASE_ERROR_PROMPT_DIR_NOT_EMPTY);
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.ELSE + Constants.SPACE
+ ScriptConstants.LINUX_UNZIP_COMMAND + Constants.SPACE);
res.append(Constants.ARCHIVE_DATABASE_BASE_PATH);
res.append(Constants.SPACE + ScriptConstants.SCRIPT_DIR + Constants.SPACE);
res.append(adminDatabaseDir);
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.FI + Constants.SEMICOLON + Constants.SPACE
+ ScriptConstants.EXIT + Constants.SEMICOLON + Constants.SPACE + Constants.QUOTE_MARK);
res.append(Constants.NEWLINE);
return res.toString();
}
/**
* Creates the specified directories in the deploy-configuration file.
* <p>
* Structure - ssh login cd path; DIRS; CLEANDIR; exit;
* <p>
* where: login = username@machine. path = path to install directory. DIRS = the way to create directories. CLEANDIR
* = the command to clean the tempDir (if chosen as optional)
* <p>
* The install creation of DIR has the following structure for directory dir: if [ ! -d dir ]; then mkdir dir; fi;
*
* @return The script for creating the directories.
*/
@Override
protected String osInstallScriptCreateDir() {
StringBuilder res = new StringBuilder();
res.append(ScriptConstants.ECHO_CREATING_DIRECTORIES);
res.append(Constants.NEWLINE);
res.append(ScriptConstants.SSH + Constants.SPACE);
res.append(machineUserLogin());
res.append(Constants.SPACE + Constants.QUOTE_MARK + ScriptConstants.CD + Constants.SPACE);
res.append(getInstallDirPath());
res.append(Constants.SEMICOLON + Constants.SPACE);
// go through all directories.
String dir;
// get archive.bitpresevation.baseDir directory.
dir = settings.getLeafValue(Constants.SETTINGS_ARCHIVE_BP_BASEDIR_LEAF);
if (dir != null && !dir.isEmpty() && !dir.equalsIgnoreCase(Constants.DOT)) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, false));
}
// get archive.arcrepository.baseDir directory.
dir = settings.getLeafValue(Constants.SETTINGS_ARCHIVE_ARC_BASEDIR_LEAF);
if (dir != null && !dir.isEmpty() && !dir.equalsIgnoreCase(Constants.DOT)) {
res.append(scriptCreateDir(dir, false));
}
// get tempDir directory.
dir = settings.getLeafValue(Constants.SETTINGS_TEMPDIR_LEAF);
if (dir != null && !dir.isEmpty() && !dir.equalsIgnoreCase(Constants.DOT)) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, resetTempDir));
}
// get the application specific directories.
res.append(getAppDirectories());
res.append(ScriptConstants.EXIT + Constants.SEMICOLON + Constants.SPACE + Constants.QUOTE_MARK
+ Constants.NEWLINE);
return res.toString();
}
@Override
protected String scriptCreateDir(String dir, boolean clean) {
StringBuilder res = new StringBuilder();
res.append(ScriptConstants.LINUX_IF_NOT_DIR_EXIST + Constants.SPACE);
res.append(dir);
res.append(Constants.SPACE + ScriptConstants.LINUX_THEN + Constants.SPACE + ScriptConstants.MKDIR
+ Constants.SPACE);
res.append(dir);
if (clean) {
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.ELSE_REMOVE + Constants.SPACE);
res.append(dir);
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.MKDIR + Constants.SPACE);
res.append(dir);
}
res.append(Constants.SEMICOLON + Constants.SPACE + ScriptConstants.FI + Constants.SEMICOLON + Constants.SPACE);
return res.toString();
}
/**
* Function for creating the directories along the path until the end directory. Does not create the end directory.
*
* @param dir The path to the directory.
* @return The script for creating the directory.
*/
protected String createPathToDir(String dir) {
StringBuilder res = new StringBuilder();
String[] pathDirs = dir.split(Constants.REGEX_SLASH_CHARACTER);
StringBuilder path = new StringBuilder();
// only make directories along path to last directory,
// don't create end directory.
for (int i = 0; i < pathDirs.length - 1; i++) {
// don't make directory of empty path.
if (!pathDirs[i].isEmpty()) {
path.append(pathDirs[i]);
res.append(scriptCreateDir(path.toString(), false));
}
path.append(Constants.SLASH);
}
return res.toString();
}
@Override
protected String getAppDirectories() {
StringBuilder res = new StringBuilder();
String[] dirs;
for (Application app : applications) {
// get archive.fileDir directories.
dirs = app.getSettingsValues(Constants.SETTINGS_BITARCHIVE_BASEFILEDIR_LEAF);
if (dirs != null && dirs.length > 0) {
for (String dir : dirs) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, false));
for (String subdir : Constants.BASEFILEDIR_SUBDIRECTORIES) {
res.append(scriptCreateDir(dir + Constants.SLASH + subdir, false));
}
}
}
// get harvester.harvesting.serverDir directories.
dirs = app.getSettingsValues(Constants.SETTINGS_HARVEST_SERVERDIR_LEAF);
if (dirs != null && dirs.length > 0) {
for (String dir : dirs) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, false));
}
}
// get the viewerproxy.baseDir directories.
dirs = app.getSettingsValues(Constants.SETTINGS_VIEWERPROXY_BASEDIR_LEAF);
if (dirs != null && dirs.length > 0) {
for (String dir : dirs) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, false));
}
}
// get the common.tempDir directories. But only those,
// which are not the same as the machine common.tempDir.
dirs = app.getSettingsValues(Constants.SETTINGS_TEMPDIR_LEAF);
if (dirs != null && dirs.length > 0) {
String machineDir = settings.getLeafValue(Constants.SETTINGS_TEMPDIR_LEAF);
for (String dir : dirs) {
// Don't make machine temp dir twice.
if (!dir.equals(machineDir)) {
res.append(createPathToDir(dir));
res.append(scriptCreateDir(dir, resetTempDir));
}
}
}
}
return res.toString();
}
/**
* Dummy function on linux machine. This is only used for windows machines!
*
* @param dir The directory to put the file.
*/
@Override
protected void createInstallDirScript(File dir) {
// Do nothing. Dummy function on linux machine.
}
@Override
protected String changeFileDirPathForSecurity(String path) {
path += Constants.SLASH + Constants.SECURITY_FILE_DIR_TAG + Constants.SLASH;
return path.replace(Constants.SLASH, ScriptConstants.SECURITY_DIR_SEPARATOR);
}
@Override
protected String getJMXremoteFilesCommand() {
String accessFilePath;
String passwordFilePath;
String[] options;
// retrieve the access file path.
options = settings.getLeafValues(Constants.SETTINGS_COMMON_JMX_ACCESSFILE);
// extract the path, if any. Else set default.
if (options.length == 0) {
accessFilePath = Constants.JMX_ACCESS_FILE_PATH_DEFAULT;
} else {
accessFilePath = options[0];
// warn if more than one access file is defined.
if (options.length > 1) {
log.debug(Constants.MSG_WARN_TOO_MANY_JMXREMOTE_FILE_PATHS);
}
}
// retrieve the password file path.
options = settings.getLeafValues(Constants.SETTINGS_COMMON_JMX_PASSWORDFILE);
// extract the path, if any. Else set default.
if (options.length == 0) {
passwordFilePath = Constants.JMX_PASSWORD_FILE_PATH_DEFAULT;
} else {
passwordFilePath = options[0];
// warn if more than one access file is defined.
if (options.length > 1) {
log.debug(Constants.MSG_WARN_TOO_MANY_JMXREMOTE_FILE_PATHS);
}
}
// initialise the resulting command string.
StringBuilder res = new StringBuilder();
// echo make password files readonly
res.append(ScriptConstants.ECHO_MAKE_PASSWORD_FILES);
res.append(Constants.NEWLINE);