forked from CJohnsonADAH/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn
executable file
·1057 lines (891 loc) · 39.3 KB
/
adpn
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
#!/bin/bash
#
# adpn: master script for scripted ADPNet operations.
#
# @version 2022.0512
SCRIPTPATH="$( which "$0" )"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$( dirname "${SCRIPTPATH}" )"
source "${SCRIPTDIR}/adpn-define-aliases"
source "${SCRIPTDIR}/adpn-lockss-daemon-aliases"
source "${SCRIPTDIR}/adpn-workflow-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
preserve ADPNet Member: prepare an AU for preservation and notify config manager
verify Node Manager: accept an AU from Member, test, and confirm accessibility from your node
accept Config Manager: accept a verified AU from Node, test, and insert into titlesdb
promote Config Manager: publish an accepted Member's AU to the entire network
help Display usage notes for the ${SCRIPT} script or for a given command
Exit codes:
0 = success (successful retrieval and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
declare -a _CMDLINE=("$0")
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND & SHARED SETTINGS ##########################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # @see adpn-define-aliases
declare -a _FORCE_CONTEXT=( "--context=${SCRIPT_CMD_NAME}" )
declare -a _ADD_CONTEXT=()
[[ -z "${_PARAM[context]}" ]] && _ADD_CONTEXT=( "${_FORCE_CONTEXT[@]}" )
declare -a CONTEXT_SW=( "${_ADD_CONTEXT[@]}" )
[[ -n "${DDBG}" ]] && CONTEXT_SW+=( "${DDBG}" )
[[ -n "${VV}" ]] && CONTEXT_SW+=( "${VV}" )
[[ -n "${QQ}" ]] && CONTEXT_SW+=( "${QQ}" )
if [[ ! -z "${_PARAM[version]}" ]] ; then
if [[ -z "${CMD}" ]] ; then
CMD="version"
else
"${ME}" version
fi
fi
if [[ ! -z "${_PARAM[help]}" ]] ; then
CMD="help"
_ARGV=( "help" "${_ARGV[@]}" )
fi
adpn_debug 10,"${SCRIPT}","${CMD}",enter "${LINENO}" "entered, cmd=%s, cmdline='%s'" "${CMD}" "$( join_by "', '" "${_CMDLINE[@]:2}" )" 1>&2
##########################################################################################
## COMMUNICATIONS: check for information to access encrypted persistent data #############
##########################################################################################
export ADPN_STASH_FILE
export ADPN_STASH_KEY
ADPN_STASH_CLOSE=""
declare -A _NO_RECURSE=( [stash]=stash [setup]=stash [version]=stash [help]=stash )
if [[ stash =~ ^(${_NO_RECURSE[$CMD]})$ ]] ; then
# avoid infinite recursion, double-execution, unwanted errors, etc.
ADPN_STASH_JSON=""
elif [[ -z "${ADPN_STASH_FILE}" || -z "${ADPN_STASH_KEY}" ]] ; then
# stash open: this will output some variable settings
adpn_debug 900,"${SCRIPT}","${CMD}",recurse "${LINENO}" "checking for an open encrypted stash, cmd=%s" "${CMD}"
eval $( "${ADPN_STASH}" "${_ADD_CONTEXT[@]}" open --quiet --if-needed )
fi
##########################################################################################
### SCRIPT: EXECUTE PIPELINE ############################################################
##########################################################################################
case "${CMD}" in
"")
adpn_notice "${LINENO}" "command expected (for example: '${SCRIPT} preserve', '${SCRIPT} accept', etc.)"
adpn_notice "${LINENO}" "%s" "${__HELP__}"
EXITCODE=255
;;
"version")
# @method adpn version
# Displays current version of the adpn script.
#
# Usage: adpn version
#
# @version 2019.0808
VERSION=$(grep "^# @version" $0 | head --lines=1 | cut --only-delimited --fields=3- --delimiter=" ")
printf "%s version %s\n" "${SCRIPT}" "${VERSION}"
EXITCODE=0
;;
"help")
# @method adpn help
# Displays usage notes for the adpn script or a given adpn script command
#
# Usage: adpn help [<CMD>]
#
# CMD is an adpn script pipeline command, e.g. "preserve", "ingest", "publish"
# If omitted, display usage notes for the adpn script as a whole.
#
# @version 2019.0715
HELPCMD="${_ARGV[2]}"
if [[ -z "${HELPCMD}" ]] ; then
echo "${__DOC__}"
EXITCODE=0
else
adpn_help_notes "${ME}" "${HELPCMD}" "${CMD}"
EXITCODE=$?
fi
;;
"test")
# @method adpn test
#
# Usage: adpn test [<TESTCODE>] [<CMD>] [<ARGS>]...
#
# TESTCODE is a code identifying the test data packet to use. For a list of available
# tests, use:
#
# adpn test
#
# CMD is an adpn script pipeline command, e.g "preserve", "ingest", "publish"
# ARGS are the arguments for that pipeline command.
#
# @version 2021.0910
TESTDIR="${SCRIPTDIR}/json/tests"
if [[ "${_ARGV[2]}" =~ ^(adpnprop|adpn_parameter_from|adpn_getpassword_from|adpn_opened_get_labels|adpn_check_au_status|adpn_plugin_parameter_from_json|adpn_notice|adpn_workflow_get_step)$ ]] ; then
TEST_FUNCTION="${_ARGV[2]}"
if [[ -n "${_PARAM[pipe]}" ]] ; then
PIPED_INPUT="$( cat "-" )"
SETTING_VALUE="$( printf "%s" "${PIPED_INPUT}" | "${TEST_FUNCTION}" "${_ARGV[@]:3}" )" ; EXITCODE="$?"
else
SETTING_VALUE="$( "${TEST_FUNCTION}" "${_ARGV[@]:3}" )" ; EXITCODE="$?"
fi
adpn_notice "${LINENO}" "%s: exit=%d, value='%s'" "${TEST_FUNCTION}" "${EXITCODE}" "${SETTING_VALUE}"
elif [[ ! -z "${_CMDLINE[2]}" ]] ; then
TESTFILE=${TESTDIR}/${_CMDLINE[2]}.json
if [[ -r "${TESTFILE}" ]] ; then
"${_CMDLINE[0]}" "${_CMDLINE[@]:3}" ${TESTFILE}
EXITCODE="$?"
else
adpn_notice "${LINENO}" "Use '%s [<TESTCODE>]'." "${SCRIPT} ${CMD}"
printf "\n" 1>&2
printf "Available tests:\n" 1>&2
cd $TESTDIR && ls ./*.json | sed 's/^.\//- /' | sed 's/[.]json$//' 1>&2
EXITCODE=255
fi
else
adpn_notice "${LINENO}" "Use '%s [<TESTCODE>]'." "${SCRIPT} ${CMD}"
printf "\n" 1>&2
printf "Available tests:\n" 1>&2
cd $TESTDIR && ls ./*.json | sed 's/^.\//- /' | sed 's/[.]json$//' 1>&2
EXITCODE=255
fi
;;
"setup")
# @method adpn setup
# Perform initial setup for adpn script in a new environment.
#
# Usage: adpn setup [<CMD>] [--for=<CONTEXT>]
#
# --for=preserve sets up adpn for use on your workstation to submit AUs
# --for=verify sets up adpn for use on your preservation node to verify AUs
# --for=ingest sets up adpn for use on the props server to ingest/publish AUs
# --for=publish synonym for --for=ingest
#
# @version 2021.0406
declare -a ADS_ARGV=()
[[ -z "${_ARGV[2]}" ]] && ADS_ARGV+=( "all" ) || ADS_ARGV+=( "${_ARGV[@]:2}" )
ADS_ARGV+=( "${_SWITCHES[@]}" )
[[ -z "${_PARAM[context]}" ]] && ADS_ARGV+=( "${_ADD_CONTEXT[@]}" )
[[ -z "${_PARAM[script]}" ]] && ADS_ARGV+=( "--script=${SCRIPTPATH}" )
adpn-do-setup "${ADS_ARGV[@]}" ; EXITCODE="$?"
;;
"property")
# @method adpn property
# Display or set property values from the configuration file (adpnet.json)
#
# @see adpn-property-do --help
adpn-property-do "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}"
EXITCODE="$?"
;;
"stash")
# @method adpn stash
# Display or set property values from the temporary encrypted stash file
#
# @see adpn-stash-do --help
adpn_debug 500,adpn,stash "${LINENO}" "stash request: %s" "${_CMD_REST[*]}"
adpn-stash-do "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}"
EXITCODE="$?"
;;
"receive")
# @method adpn receive
#
# @version 2022.0512
DROP_DEST=$( adpn_parameter_from '--remote' '.drop-server/ssh' )
DROP_PATH_T=$( adpn_parameter_from '.stage/base_dir/%s' )
DROP_USER_T=$( adpn_parameter_from '.drop-server/ssh_template' )
DROP_PATH_USER=$( adpn_parameter_from '--from' '.stage/user' )
DROP_PATH_SUBDIR=$( adpn_parameter_from '--subdirectory' '--directory' "${_ARGV[2]}" )
DROP_PATH_BASE="$( printf "${DROP_PATH_T}" "${DROP_PATH_USER}" )"
DROP_PATH="$( printf "%s/%s" "${DROP_PATH_BASE%/}" "${DROP_PATH_SUBDIR}" )"
DROP_USER_FULL="$( printf "${DROP_USER_T}" "${DROP_PATH_USER}" )"
adpn-package-do describe --remote="${DROP_DEST}" "${DROP_PATH}" | adpn notify "${_ADD_CONTEXT[@]}" --from="${DROP_USER_FULL}" --subdirectory="${DROP_PATH_SUBDIR}" --action="receive the staged package of"
;;
"describe")
# @method adpn describe
# ADPNet Member: Get a JSON packet describing
#
# @version 2021.0820
declare -a APD_CMD=( "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" )
adpn-package-do describe "${APD_CMD[@]}" || EXITCODE=$?
;;
"package")
# @method adpn package
# ADPNet Member: Package a directory of files into a LOCKSS Archival Unit (AU).
#
# Usage: adpn package [<LOCALPATH>] [<OPTIONS>]
#
# LOCALPATH is a directory on the local workstation containing the files for the
# Archival Unit (AU) to prepare.
#
# @version 2021.0918
declare -a APD_CMD=( "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" )
[[ "${_ARGV[2]}" =~ ^(enclose|validate|describe)$ ]] || APD_CMD=( enclose "${APD_CMD[@]}" )
adpn-package-do "${APD_CMD[@]}" || EXITCODE="$?"
;;
"stage")
# @method adpn stage
# ADPNet Member: Stage AU files on the local workstation to the staging server.
#
# Usage: adpn stage [<LOCALPATH>] [<STAGING>] [<OPTIONS>]...
#
# --subdirectory=<SLUG> destination subdirectory on staging server
# --au_title=<TITLE> human-readable title of this AU
#
# LOCALPATH is a directory on the local workstation containing the files for the
# Archival Unit (AU) to preserve. E.g.: "w:\WPA\Folder 01"
#
# STAGING is an URL specifying the FTP server and the path to the staging area to
# stage the AU files. E.g.: "sftp://adah@drop.adpn.org/adah/drop_au_content_in_here/"
# If omitted, content will be staged to the location provided by the stage/base
# configuration setting (@see adpn property get stage/base)
#
# @version 2021.0707
ASC_OUTPUT="$(mktemp)"
declare -a ASC_ARGV=( "${_ARGV[@]:2}" )
declare -A ASC_SWITCHES=( )
for SWITCH in "${!_SWITCHES[@]}" ; do
case "${SWITCH}" in
"output")
# DROP IT -- WE'LL SET IT OURSELVES BELOW
;;
*)
ASC_SWITCHES[${SWITCH}]="${_SWITCHES[$SWITCH]}"
;;
esac
done
adpn-stage-content.py --output=application/json \
"${ASC_SWITCHES[@]}" \
"${ASC_ARGV[@]}" "${CONTEXT_SW[@]}" > "${ASC_OUTPUT}"
ASC_ERRCODE="$?"
if [[ "${ASC_ERRCODE}" -lt 255 ]] ; then
JSON_PACKET="$( adpn_get_json_packets "${ASC_OUTPUT}" )"
printf "%s" "${JSON_PACKET}"
fi
rm "${ASC_OUTPUT}"
EXITCODE="${ASC_ERRCODE}"
;;
"reclaim")
"${ME}" pipe --remove_labels="WAITING: Confirm AU" --add_labels="DONE: Confirm AU,TODO: Unstage" -- confirm --preserved "${_CMD_REST[@]}" && \
"${ME}" pipe -- flag --key=pub_down --value=true "${_CMD_REST[@]}" && \
"${ME}" daemon refresh && \
"${ME}" pipe -- confirm --down "${_CMD_REST[@]}" && \
"${ME}" pipe --remove_labels="TODO: Unstage" --add_labels="DONE: Unstage" -- unstage "${_CMD_REST[@]}"
;;
"unstage")
# @method adpn unstage
# ADPNet Member: Unstage AU files from the staging server and replace with placeholder text
#
# Usage: adpn unstage [-|<JSONFILE>] [<OPTIONS>]...
#
# @version 2021.0817
declare -A TEMPFILES=()
TEMPFILES[STAGER_INPUT]="$( mktemp )"
TEMPFILES[STAGER_OUTPUT]="$( mktemp )"
TEMPFILES[STAGER_DIR]="$( mktemp -d )"
adpn_read_json_packet_source "${_ARGV[2]}" "${SCRIPT_CMD_NAME}" "${V}" | adpn_get_json_packets | adpn_select_json_packet "staged" "-" "tail -n 1" "unstaged" > "${TEMPFILES[STAGER_INPUT]}"
EXITCODE="$?"
SUBDIRECTORY="$( adpn_parameter_from "--subdirectory" "--directory" )"
[[ -z "${SUBDIRECTORY}" ]] && SUBDIRECTORY="$( cat "${TEMPFILES[STAGER_INPUT]}" | adpn_plugin_parameter_from_json "subdirectory" )"
[[ -z "${SUBDIRECTORY}" ]] && SUBDIRECTORY="$( cat "${TEMPFILES[STAGER_INPUT]}" | adpn_plugin_parameter_from_json "directory" )"
[[ -z "${SUBDIRECTORY}" ]] && adpn_debug 100 "${LINENO}" "No Subdirectory parameter in: %s" "$( cat "${TEMPFILES[STAGER_INPUT]}" )"
UNSTAGED_BY="$( adpn_get_user_email )"
[[ -n "${V}" ]] && adpn_notice "${LINENO}" "Generating README.text for unstaged AU."
README_TXT="$( cat "${TEMPFILES[STAGER_INPUT]}" | adpn-do-notify --step=unstaged --subdirectory="${SUBDIRECTORY}" "${CONTEXT_SW[@]}" --template:unstaged_by="${UNSTAGED_BY}" )"
SUB_SUBDIRECTORY="$( printf "%s/%s" "${TEMPFILES[STAGER_DIR]}" "${SUBDIRECTORY}" )"
mkdir -p "${SUB_SUBDIRECTORY}"
printf "%s" "${README_TXT}" > "${SUB_SUBDIRECTORY}/README.text"
adpn_debug 40,adpn,unstage "${LINENO}" "README.text contents: <<<EOF\n%s\nEOF;" "$( cat "${SUB_SUBDIRECTORY}/README.text" )"
declare -a ASC_ARGV=( "${_ARGV[@]:3}" )
declare -A ASC_SWITCHES=( )
for SWITCH in "${!_SWITCHES[@]}" ; do
case "${SWITCH}" in
"output"|"local")
# DROP IT -- WE'LL SET IT OURSELVES BELOW
;;
*)
ASC_SWITCHES[${SWITCH}]="${_SWITCHES[$SWITCH]}"
;;
esac
done
ASC_SWITCHES[unstage]="--unstage"
ASC_SWITCHES[output]="--output=application/json"
ASC_SWITCHES[local]="$( printf -- "--local=%s" "${SUB_SUBDIRECTORY}" )"
ASC_SWITCHES[subdirectory]="--subdirectory=${SUBDIRECTORY}"
[[ -n "${V}" ]] && adpn_notice "${LINENO}" "Unstaging files from staging area. %s" "$( declare -p ASC_SWITCHES | tr '\n' ' ' )"
cat "${TEMPFILES[STAGER_INPUT]}" | adpn-stage-content.py "${ASC_SWITCHES[@]}" \
"${ASC_ARGV[@]}" "${CONTEXT_SW[@]}" > "${TEMPFILES[STAGER_OUTPUT]}"
ASC_ERRCODE="$?"
if [[ "${ASC_ERRCODE}" -lt 255 ]] ; then
declare -a JSON_OVERLAYS=()
JSON_OVERLAYS+=( "$( adpn_get_json_packets "${TEMPFILES[STAGER_OUTPUT]}" )" )
JSON_PACKET="$( printf "%s\n" "${JSON_OVERLAYS[@]}" | adpn-json.py --cascade --output=application/json --prolog )"
printf "\n%s\n" "${JSON_PACKET}"
fi
for TEMPFILE in "${TEMPFILES[@]}" ; do
rm -r "${TEMPFILE}"
done
EXITCODE="${ASC_ERRCODE}"
;;
"notify")
# @method adpn notify
# ADPNet Member: Post a notification of a staged AU for the ADPNet Config Manager
# to accept from the staging server.
#
# Usage: printf "%s" "<JSON_INPUT>" | adpn notify [--from=<FROM>] [--subdirectory=<SLUG>]
#
# --from=<FROM> ID of the staging area that this AU should be harvested from
# --subdirectory=<DIR> AU subdirectory on staging server
#
# JSON_INPUT is the JSON-formatted metadata packet produced by a script command like
# `adpn stage` after packaging and staging the AU on an ADPNet staging server. E.g.:
#
# ~~~
# JSON PACKET: { "Ingest Title": "...", "File Size": "...", "From Peer": "ADAH", [...] }
# ~~~
#
# @version 2021.0709
adpn-do-notify "${CONTEXT_SW[@]}" "${_CMD_REST[@]}" || EXITCODE="$?"
;;
"preserve")
# @method adpn preserve
# ADPNet Member: Stage AU files on the local workstation to the staging area,
# test connection to staging server, collect data, and verify plugin parameters;
# then generate JSON and notify network admin that content is ready to ingest.
#
# Usage: adpn preserve [<LOCALPATH>] [<STAGING>] [<OPTIONS>]...
#
# --subdirectory=<SLUG> destination subdirectory on staging server
# --au_title=<TITLE> human-readable title of this AU
# --from=<PEER> code for origin node (AUB, ADAH, etc.)
#
# LOCALPATH is a directory on the local workstation containing the files for the
# Archival Unit (AU) to preserve. E.g.: "w:\WPA\Folder 01"
#
# STAGING is an URL specifying the FTP server and the path to the staging area to
# stage the AU files. E.g.: "sftp://adah@drop.adpn.org/adah/drop_au_content_in_here/"
# If omitted, content will be staged to the location provided by the stage/base
# configuration setting (@see adpn property get stage/base)
#
# Output: a report containing AU data, file size information, plugin parameters
# and a JSON packet for use by adpn ingest and adpn publish by Config Manager.
# Displayed to stdout; if adpn-do-notify can post a notification for the Config Manager
# this is filled in to the notification.
#
# @version 2021.0707
declare -a AU_LOCALPATHS=( "${_ARGV[@]:2}" )
AU_LOCALPATHS+=( "${PWD}" )
AU_LOCALPATHS=( "${AU_LOCALPATHS[@]/#/\\}" )
AU_LOCALPATH="$( adpn_parameter_from "--local" "${AU_LOCALPATHS[@]}" )"
AU_LOCALPATH="$( readlink -f "${AU_LOCALPATH}" )"
AU_LOCALBASE="$( basename "${AU_LOCALPATH}" )"
AU_DEFAULTBASE="${AU_LOCALBASE}"
if [[ -x ~/.adpn/to-staging-directory ]] ; then
AU_ALTBASE="$( printf "%s" "${AU_LOCALPATH}" | ~/.adpn/to-staging-directory )" && AU_LOCALBASE="${AU_ALTBASE}"
fi
MANIFEST_JSON="$( "${ME}" describe "${AU_LOCALPATH}" )"
[[ -n "${MANIFEST_JSON}" ]] && MANIFEST_JSON="$( printf "%s" "${MANIFEST_JSON}" | adpn-json.py --output=application/json )" || MANIFEST_JSON="{}"
[[ -n "${_PARAM[local]}" ]] && N=2 || N=3
declare -a AU_STAGINGAREAS=( "${_ARGV[@]:$N}" )
AU_STAGINGAREAS=( "${AU_STAGINGAREAS[@]/#/\\}" )
STAGING_AREA="$( adpn_parameter_from "--remote" "${AU_STAGINGAREAS[@]}" ".stage/base" )"
FROM="$( adpn_parameter_from "--from" "--publisher" "--peer" ".publisher" ".peer" "<From (<PUBLISHER>): " )"
SUBDIRECTORY="$( adpn_parameter_from "--subdirectory" "--directory" ".Directory name<${MANIFEST_JSON}" "<Directory name [${AU_LOCALBASE}]: " "${AU_LOCALBASE}")"
DEFAULT_AU="$( printf "AU from %s" "${SUBDIRECTORY}" )"
if [[ -x ~/.adpn/to-au-title ]] ; then
AU_DEFAULT_TITLE_ALT="$( printf "%s" "${AU_LOCALPATH}" | ~/.adpn/to-au-title )" && DEFAULT_AU="${AU_DEFAULT_TITLE_ALT}"
fi
AU_TITLE="$( adpn_parameter_from "--au_title" ".AU Package<${MANIFEST_JSON}" "<AU Title [${DEFAULT_AU}]: " "\\${DEFAULT_AU}")"
AU_INSTITUTION="$( adpn_parameter_from "--institution" ".institution" )"
[[ -z "${V}" ]] || adpn_notice "${LINENO}" "1. Packaging files into LOCKSS Archival Unit (AU)."
declare -a ADPN_PACKAGE_CMD=( "${ME}" "package" "${AU_LOCALPATH}" )
ADPN_PACKAGE_CMD+=( "${CONTEXT_SW[@]}" )
ADPN_PACKAGE_CMD+=( --au_title="${AU_TITLE}" --subdirectory="${SUBDIRECTORY}" )
[[ -n "${_SWITCHES[skip]}" ]] && ADPN_PACKAGE_CMD+=( "${_SWITCHES[skip]}" )
adpn_debug 20,adpn,preserve,package "${LINENO}" '$ %s' "$( printf "%q " "${ADPN_PACKAGE_CMD[@]}" )"
JSON_PACKET="$(
"${ADPN_PACKAGE_CMD[@]}"
)" ; EXITCODE="$?"
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "%s package FAILED.\n" "${SCRIPT}"
else
[[ -z "${V}" ]] || adpn_notice "${LINENO}" "2. Staging content to drop server."
declare -a ADPN_STAGE_CMD=( "${ME}" "stage" )
ADPN_STAGE_CMD+=( "${CONTEXT_SW[@]}" )
ADPN_STAGE_CMD+=( "${STAGING_AREA}" )
ADPN_STAGE_CMD+=( --au_title="${AU_TITLE}" --subdirectory="${SUBDIRECTORY}" )
[[ -n "${_SWITCHES[skip]}" ]] && ADPN_STAGE_CMD+=( "${_SWITCHES[skip]}" )
adpn_debug 50,adpn,preserve,stage "${LINENO}" 'printf "%%s" "%s" | [...]' "${JSON_PACKET}"
adpn_debug 20,adpn,preserve,stage "${LINENO}" '[...] | %s' "$( printf "%q " "${ADPN_STAGE_CMD[@]}" )"
JSON_PACKET=$(
printf "%s" "${JSON_PACKET}" \
| "${ADPN_STAGE_CMD[@]}"
) ; EXITCODE="$?"
fi
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "%s stage FAILED." "${SCRIPT}"
else
DROP_ID=$( adpn_parameter_from ".Staged To<${JSON_PACKET}" "\\${FROM}" )
[[ -z "${V}" ]] || adpn_notice "${LINENO}" "3. Notifying ADPNet Tech of staged AU."
declare -a _LABEL_SW=()
[[ -n "${_SWITCHES[labels]}" ]] && _LABEL_SW+=( "${_SWITCHES[labels]}" )
[[ -n "${_SWITCHES[remove_labels]}" ]] && _LABEL_SW+=( "${_SWITCHES[remove_labels]}" )
[[ -n "${_SWITCHES[add_labels]}" ]] && _LABEL_SW+=( "${_SWITCHES[add_labels]}" )
printf "%s" "${JSON_PACKET}" | "${SCRIPTPATH}" notify --from="${DROP_ID}" --subdirectory="${SUBDIRECTORY}" "${CONTEXT_SW[@]}" ; ADN_ERRCODE="$?"
EXITCODE="${ADN_ERRCODE}"
fi
[[ "${EXITCODE}" -gt 0 ]] && adpn_notice "${LINENO}" "%s notify FAILED." "${SCRIPT}"
;;
"verify")
# @method adpn verify
# Peer Manager, Config Manager: accept JSON data specifying a new AU for ingest
# test connection to the staging server and verify parameters
#
# Usage: adpn verify [-|<JSONFILE>] [<OPTIONS>]
#
# Options:
# --from=<PUB> code for origin publisher (AUB, ADAH, etc.); overrides JSON data
# --to=<PEER> node to publish to (AUB1, AUB2, ADAH, ...; ALL=entire network)
# --peer=<PEER> synonym for --to=<PEER>
#
# @version 2021.0807
on_node_do adpn-daemon-do verify "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" \
|| EXITCODE="$?"
;;
"accept"|"post"|"ingest")
# @method adpn accept
# Config Manager: accept JSON data specifying a new AU for ingest,
# test connection to the staging server and verify parameters; then
# insert into local titlesdb and confirm insertion.
#
# Usage: adpn accept [-|<JSONFILE>] [<OPTIONS>]...
#
# Options:
# --from=<PEER> code for origin node (AUB, ADAH, etc.); overrides JSON data
# --for=ALL|<PEER> node to publish to (AUB, ADAH...; ALL=entire network)
# --to=ALL|<PEER> synonym for --for=<NODE>
# --dry-run output SQL script for titlesdb insert but do not execute
# --sqldump=<SQLFILE> output SQL script for titlesdb to this file
#
# @version 2021.0708
# @method adpn post
# Config Manager: synonym for `adpn accept`
#
# @version 2021.0708
# @method adpn ingest
# Config Manager: synonym for `adpn accept`
#
# @version 2021.0420
on_props_do adpn-titlelist-do post "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" \
|| EXITCODE="$?"
;;
"promote"|"publish")
# @method adpn promote
# Config Manager: accept JSON data specifying a new AU for ingest,
# test connection to the staging server and verify parameters; then
# insert into titlesdb for whole network and confirm insertion.
#
# Usage: adpn publish [-|<JSONFILE>] [<OPTIONS>]...
#
# Options:
# --from=<PEER> code for origin node (AUB, ADAH, etc.); overrides JSON data
# --dry-run output SQL script for titlesdb insert but do not execute
# --sqldump=<SQLFILE> output SQL script for titlesdb to this file
#
# @version 2021.0420
# @method adpn publish
# Config Manager: synonym for `adpn promote`
#
# @version 2021.0420
on_props_do adpn-titlelist-do promote "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" \
|| EXITCODE="$?"
;;
"announce")
# @method adpn announce
# Config Manager: announce a new AU to the network.
#
# Usage: adpn announe [-|<JSONFIlE>] [<OPTIONS>]...
#
# Options:
# --pipeline
# --pipe-to
#
# @version 2022.0524
PIPELINE="$( adpn_parameter_from "--pipeline" "published:announced" )"
if [[ -n "${PIPELINE}" ]] ; then
PIPEDFROM=$( printf "%s" "${PIPELINE}" | cut --field=1 --delimiter=":" )
PIPEDTO=$( printf "%s" "${PIPELINE}" | cut --field=2 --delimiter=":" )
adpn_debug 2 "${LINENO}" "SWITCH='%s', PIPELINE='%s': PIPEDFROM='%s', PIPEDTO='%s'\n" "${_SWITCHES[pipeline]}" "${PIPELINE}" "${PIPEDFROM}" "${PIPEDTO}" 1>&2
fi
AN_INPUT=$( mktemp )
AN_OUTPUT=$( mktemp )
adpn_debug 10,adpn,announce "${LINENO}" "Try to get JSON: adpn_read_json_packet_source\n"
adpn_read_json_packet_source "${_ARGV[2]}" "${SCRIPT_CMD_NAME}" "${V}" | adpn_get_json_packets | adpn_select_json_packet "${PIPEDFROM}" "-" "head -n 1" "${PIPEDTO}" > "${AN_INPUT}"
declare -a _PIPE_CMD
_PIPE_TO="$( adpn_parameter_from "--pipe-to" "cat" | tr '[:space:]' ' ' )"
IFS=' ' read -ra _PIPE_CMD <<<"${_PIPE_TO}"
AN_LABEL="$(adpn_parameter_from "--label" "Announce AU to ADPNet Node Managers" )"
[[ -n "${AN_LABEL}" ]] && AN_LABEL="$( printf '**%s:** ' "${AN_LABEL%%+(:)}" )"
AN_IN_ORDER_TO="$( adpn_parameter_from "--in-order-to" "to post a notification to the ADPNet Slack channel" )"
[[ -n "${AN_IN_ORDER_TO}" ]] && AN_IN_ORDER_TO="$( printf ' to %s' "${AN_IN_ORDER_TO%%.}" )"
declare -a _SUM1=( "${SCRIPT}" "${_ARGV[1]}" )
PIPE_CMDLINE="$( printf "%q " "${_CMDLINE[@]}" )"
declare -a AN_TEMPLATE=(
'--step=published'
'--template=announce'
"$( printf -- "--template:cmd=%s" "${_SUM1[*]}" )"
"$( printf -- "--template:cmdline=%s" "${_CMDLINE[*]}" )"
"$( printf -- "--template:label=%s" "${AN_LABEL}" )"
"$( printf -- "--template:executed_by=%s" "$( adpn_get_user_email )" )"
"$( printf -- "--template:start_time=%s" "$( date )" )"
"$( printf -- "--template:in_order_to=%s" "${AN_IN_ORDER_TO}" )"
"$( printf -- "--template:pipe_cmdline=%s" "${PIPE_CMDLINE%%*( )}" )"
)
"${ADPN}" notify "${CONTEXT_SW[@]}" "${AN_TEMPLATE[@]}" < "${AN_INPUT}" > "${AN_OUTPUT}" ; EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
"${_PIPE_CMD[@]}" < "${AN_OUTPUT}" ; EXITCODE="$?"
fi
rm "${AN_INPUT}" "${AN_OUTPUT}"
EXITCODE="$?"
;;
"validate")
adpn-package-do validate "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" \
|| EXITCODE="$?"
;;
"confirm")
# @method adpn confirm
# Node Manager: confirm the status of an ingested AU from the LOCKSS daemon using a LOCKSS API request
#
# Usage: adpn confirm [^|<GITLAB REFERENCE>|<JSONFILE>|-] [<OPTIONS>]
#
# Options:
# --added test whether this AU has been added to the LOCKSS preservation node
# --crawled test whether this AU has been fully and successfully crawled yet by LOCKSS
# --preserved test whether this AU has been harvested and preserved by other LOCKSS nodes
# --down test whether this AU has been marked as "down" prior to unstaging
#
# If one or more test conditions are provided in options, then the command will return exit code
# 0=all test conditions were met; 1=one or more tests failed.
#
# @version 2021.0924
declare -a TEMP_CLEANUP=()
declare -a _CONFIRMED_TESTS=()
_PIPELINE_FROM=""
[[ -n "${_PARAM[added]}" ]] && _CONFIRMED_TESTS+=( "${_PARAM[added]}" ) && _PIPELINE_FROM=verified
[[ -n "${_PARAM[crawled]}" ]] && _CONFIRMED_TESTS+=( "${_PARAM[crawled]}" ) && _PIPELINE_FROM=ingested
[[ -n "${_PARAM[preserved]}" ]] && _CONFIRMED_TESTS+=( "${_PARAM[preserved]}" ) && _PIPELINE_FROM=published
[[ -n "${_PARAM[down]}" ]] && _CONFIRMED_TESTS+=( "${_PARAM[down]}" ) && _PIPELINE_FROM="confirmed-preserved"
[[ "${#_ARGV[@]}" -gt 3 ]] && _CONFIRMED_TESTS+=( "${_ARGV[@]:3}" )
_PIPELINE_TO="$( join_by "-" "confirmed" "${_CONFIRMED_TESTS[@]}" )"
adpn_set_pipeline_step "${_PIPELINE_FROM}:${_PIPELINE_TO}" "confirm"
DST_INPUT="$(mktemp)" ; TEMP_CLEANUP+=( "${DST_INPUT}" )
DST_OUTPUT="$(mktemp)" ; TEMP_CLEANUP+=( "${DST_OUTPUT}" )
adpn_read_json_packet_source "${_ARGV[2]}" "${SCRIPT_CMD_NAME}" "${V}" | adpn_get_json_packets | adpn_select_json_packet "${PIPEDFROM}" "-" "tail -n 1" "${PIPEDTO}" > "${DST_INPUT}"
EXITCODE="$?"
if [[ -s "${DST_INPUT}" ]] ; then
declare -a DST_SW=( "table" )
adpn_debug 01 "${LINENO}" '$ cat "%s" | adpn_packet2daemonstatustable "%s" > "%s"\n' "${DST_INPUT}" "$( join_by '" "' "${AIT_SW[@]}" )" "${DST_OUTPUT}"
LOCKSS_KEY="$( cat "${DST_INPUT}" | adpn_packet2lockssid )"
cat "${DST_INPUT}" | adpn_packet2daemonstatustable "${DST_SW[@]}" > "${DST_OUTPUT}" ; EXITCODE="$?"
declare -a DST_GREP_OPT=( "Volume" )
declare -a DST_GREP_TESTS=( )
[[ -n "${_PARAM[added]}" ]] && DST_GREP_OPT+=( "Disk Usage" "Status" "Last Completed Crawl" "Last Crawl" "Last Crawl Result" ) && DST_GREP_TESTS+=( 'Status=~Waiting for [A-Za-z]+' 'LastCompletedPoll=~.*' )
[[ -n "${_PARAM[crawled]}" ]] && DST_GREP_OPT+=( "Disk Usage" "Status" "Last Completed Crawl" "Last Crawl" "Last Crawl Result" ) && DST_GREP_TESTS+=( 'Status=~(Waiting for Poll|[0-9.]*[%] Agreement)' 'LastCompletedCrawl=>0' 'LastCompletedPoll=~.*' )
[[ -n "${_PARAM[preserved]}" ]] && DST_GREP_OPT+=( "Disk Usage" "Status" "Last Completed Crawl" "Last Crawl Result" "Last Completed Poll" "Last Poll Result" ) && DST_GREP_TESTS+=( 'Status=100.00% Agreement' 'LastCompletedCrawl=>0' 'LastCompletedPoll=>0' )
[[ -n "${_PARAM[down]}" ]] && DST_GREP_OPT+=( "Disk Usage" "Status" "Last Completed Crawl" "Last Crawl" "Last Crawl Result" "Available From Publisher" ) && DST_GREP_TESTS+=( "AvailableFromPublisher=No" )
[[ "${#_ARGV[@]}" -gt 3 ]] && DST_GREP_OPT+=( "${_ARGV[@]:3}" )
[[ "${#DST_GREP_OPT[@]}" -le 1 ]] && DST_GREP_OPT+=( ".*" )
declare -a DST_GREP=( grep -E "^($( join_by "|" "${DST_GREP_OPT[@]}" ))=" "${DST_OUTPUT}" )
declare -a DST_FILTER=( cat - )
declare -a DST_OUTPUT=( cat - )
declare -a DST_GREP_TESTS_HUMAN_READABLE=()
if [[ -n "${_PARAM[preserved]}${_PARAM[added]}${_PARAM[crawled]}${_PARAM[down]}" ]] ; then
DST_OUTPUT=( adpn_check_au_status "${_SWITCHES[@]}" )
push_input_loop
for TEST in "${DST_GREP_TESTS[@]}" ; do
DST_OUTPUT+=( "$( printf -- "--test:%s" "${TEST}" )" )
DST_GREP_TESTS_HUMAN_READABLE+=( "$( printf "%s" "${TEST}" | cut --delimiter="=" --field=1 )" )
done
pop_input_loop
DST_FILTER=( sed -E -e 's/\t/ /' -e 's/^([^=]+)=(.*)$/\1\t\2/' )
elif [[ "${_PARAM[output]}" == "text/tab-separated-values" ]] ; then
DST_FILTER=( sed -E -e 's/\t/ /' -e 's/^([^=]+)=(.*)$/\1\t\2/' )
else
DST_OUTPUT=( sed -E -e '$a\\n' )
fi
"${DST_GREP[@]}" | "${DST_FILTER[@]}" | "${DST_OUTPUT[@]}" ; EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 && "${_PARAM[passthru]}" ]] ; then
declare -a AC_TESTS=()
[[ -n "${_PARAM[crawled]}" ]] && AC_TESTS+=( "crawled" )
[[ -n "${_PARAM[preserved]}" ]] && AC_TESTS+=( "preserved" )
[[ -n "${_PARAM[down]}" ]] && AC_TESTS+=( "down" )
AC_S_TESTS="$( join_by "/" "${AC_TESTS[@]}" )"
declare -a JSON_OVERLAYS=(
"$( cat "${DST_INPUT}" )"
"$( adpn-json.py --output=application/json --key="Ingest Step" --value="confirmed-${AC_S_TESTS}" --prolog )"
"$( adpn-json.py --output=application/json --key="AU LOCKSS Key" --value="${LOCKSS_KEY}" --prolog )"
)
printf "\n"
printf "%s\n" "${JSON_OVERLAYS[@]}" | adpn-json.py --cascade --output="application/json" --prolog
printf "\n"
fi
fi
for TMP in "${TEMP_CLEANUP[@]}" ; do
rm "${TMP}"
done
em_cross_mark="$( printf "\u274C" )"
em_heavy_check="$( printf "\u2714" )"
GREP_TESTS_HUMAN_READABLE="$( join_by ", " "${DST_GREP_TESTS_HUMAN_READABLE[@]}" )"
[[ -z "${QQ}" && "${EXITCODE}" -eq 0 ]] && printf "\n" && adpn_notice "${LINENO}" "%s OK: This AU met all of the test conditions for %s." "${em_heavy_check}" "${GREP_TESTS_HUMAN_READABLE}"
[[ "${EXITCODE}" -gt 0 ]] && printf "\n" 1>&2 && adpn_notice "${LINENO}" "%s FAILED: This AU failed to meet one or more of the test conditions (%s)." "${em_cross_mark}" "${GREP_TESTS_HUMAN_READABLE}"
;;
"commit")
# @method adpn commit
# Config Manager: generate new static titlesDb XML file and commit changes to version control
#
# Usage: adpn commit [<OPTIONS>]...
#
# Options:
# --batch commit all changes to the XML file immediately without user input
# --interactive display each change in the XML file and request user confirmation
# --message a one-line description of the nature or purpose of the changes made
#
# @version 2021.0826
adpn-titlelist-do commit "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" || EXITCODE="$?"
;;
"flag")
# @method adpn flag
# Config Manager: add or remove a parameter to an AU in titlesdb.
#
# Usage: adpn flag [-|<JSONFILE>] [<OPTIONS>]...
#
# Options:
# --to=<PEER> code for destination feed (AUB, ADAH, etc.); overrides JSON data
# --key=<KEY> key name for the parameter (crawl_proxy, pub_down, ...)
# --value=<VALUE> value to set on the parameter (localhost:8080, true, ...)
# --delete remove the parameter with this name
# --dry-run output SQL script for titlesdb insert but do not execute
#
# @version 2021.0526
MYSQL_HOST="$( adpnprop 'mysql/host' )"
SSH_REMOTE="$( adpnprop 'props-server/ssh' )"
if [[ -n "${MYSQL_HOST}" ]] ; then
adpn-do-flag "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
elif [[ -n "${SSH_REMOTE}" ]] ; then
adpn-do-flag --remote="${SSH_REMOTE}" "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
fi
;;
"gitlab")
# @method adpn gitlab
# Perform tasks based on interacting with the Gitlab API, typically for pipelining
# JSON data packets or posting announcements.
#
# @see adpn-gitlab-do --help
#
# @version 2021.0910
adpn-gitlab-do "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
;;
"tell")
# @method adpn tell
# A gateway command for interacting with service APIs, typically for pipelining JSON
# data packets or posting announcements.
#
# Usage: adpn tell [<SERVICE>] to [get|post|<CMD>] [<OPTIONS>]...
#
# @version 2022.0526
adpn-do-tell "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
;;
"workflow")
adpn-workflow-do "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
;;
"pipe")
declare -a _CMD1=( "${ADPN}" "${CONTEXT_SW[@]}" --passthru "${_ARGV[@]:2}" )
declare -a _CMDLINE1=( "${SCRIPT}" "${_ARGV[@]:2}" )
declare -a _SUM1=( "${SCRIPT}" "${_ARGV[2]}" )
declare -A TMPFILES=( [PIPE_OUT]="$( mktemp )" )
# verify - Verify Access to AU from %(peer-to)s Preservation Node
# accept - Accept AU and Make Visible to %(peer-to)s Preservation Node
AN_LABEL="$(adpn_parameter_from "--label" "<(${SCRIPT_CMD_NAME}) Issue Note Title (if any): " )"
[[ -n "${AN_LABEL}" ]] && AN_LABEL="$( printf '**%s:** ' "${AN_LABEL%%+(:)}" )"
AN_IN_ORDER_TO="$( adpn_parameter_from "--in-order-to" "<(${SCRIPT_CMD_NAME}) I'm using the '${_SUM1[*]}' command in order to: " )"
[[ -n "${AN_IN_ORDER_TO}" ]] && AN_IN_ORDER_TO="$( printf ' to %s' "${AN_IN_ORDER_TO%%.}" )"
PIPE_CMDLINE="$( printf "%q " "${_CMDLINE[@]}" )"
declare -a AN_TEMPLATE=(
'--step=output'
'--template=output'
"$( printf -- "--template:cmd=%s" "${_SUM1[*]}" )"
"$( printf -- "--template:cmdline=%s" "${_CMDLINE1[*]}" )"
"$( printf -- "--template:label=%s" "${AN_LABEL}" )"
"$( printf -- "--template:executed_by=%s" "$( adpn_get_user_email )" )"
"$( printf -- "--template:start_time=%s" "$( date )" )"
"$( printf -- "--template:in_order_to=%s" "${AN_IN_ORDER_TO}" )"
"$( printf -- "--template:pipe_cmdline=%s" "${PIPE_CMDLINE%%*( )}" )"
)
declare -a _LABEL_SW=()
if [[ -n "${_PARAM[remove_labels]}" ]] ; then
_LABEL_SW+=( "$( printf -- '--remove_labels=%s' "${_PARAM[remove_labels]}" )" )
fi
if [[ -n "${_PARAM[add_labels]}" ]] ; then
_LABEL_SW+=( "$( printf -- '--add_labels=%s' "${_PARAM[add_labels]}" )" )
fi
[[ -z "${QQ}" ]] && adpn_notice "${LINENO}" '$ %s' "${_CMDLINE1[*]}"
adpn_debug 100,adpn,pipe,"${SUM1[1]}" "${LINENO}" 'exact$ %s %s' "${_CMD1[0]}" "$( printf "%q " "${_CMD1[@]:1}" )"
if [[ -z "${QQ}" ]] ; then
"${_CMD1[@]}" | tee "${TMPFILES[PIPE_OUT]}"
EXITCODE="${PIPESTATUS[0]}"
else
"${_CMD1[@]}" > "${TMPFILES[PIPE_OUT]}"
EXITCODE="$?"
fi
if [[ -n "${_PARAM[force]}" || "${EXITCODE}" -eq 0 ]] ; then
[[ -z "${QQ}" ]] && adpn_notice "${LINENO}" '| %s' "$( printf -- "%q " "${ME}" "notify" "${CONTEXT_SW[@]}" "${AN_TEMPLATE[@]}" "${_LABEL_SW[@]}" )"
cat "${TMPFILES[PIPE_OUT]}" | convertto_plain_text | "${ME}" notify "${CONTEXT_SW[@]}" "${AN_TEMPLATE[@]}" "${_LABEL_SW[@]}"
EXITCODE="$?"
else
[[ -n "${Q}" ]] && OUTPUT_NOTE=" Output below:" || OUTPUT_NOTE=""
adpn_notice "${LINENO}" 'FAILED: piped command `%s` returned EXIT CODE %d.%s' "${_CMD1[*]}" "${EXITCODE}" "${OUTPUT_NOTE}"
[[ -n "${OUTPUT_NOTE}" ]] && printf "~~~\n%s\n~~~\n" "$( cat "${TMPFILES[PIPE_OUT]}" )" 1>&2
fi
for FILE in "${TMPFILES[@]}" ; do
rm "${FILE}"
done
;;
"staging")
adpn-do-staging "${CONTEXT_SW[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
;;
"serve")
# @method adpn serve
# Preservation Node Manager: Helps retrieve content preserved on a LOCKSS box.
#
# @version 2021.0319
# STUB -- currently nowhere near complete
adpn_notice "${LINENO}" "This feature is still under development and not yet ready for use."
EXITCODE=255
# This is not yet ready for prime time.
#HTTP_USER=$( adpn_parameter_from "--daemon-user" ".daemon/user" )
#HTTP_PASS=$( adpn_parameter_from "--daemon-pass" ".daemon/pass" )
#if [[ -n "${HTTP_PASS}" ]] ; then
# HTTP_PASS="$( adpn_getpassword_from "${HTTP_PASS}" )"
#fi
#
#if [[ -z "${HTTP_USER}" ]] ; then
# read -p "LOCKSS Daemon User: " HTTP_USER
#fi
#if [[ -z "${HTTP_PASS}" ]] ; then
# read -sp "LOCKSS Daemon Password: " HTTP_PASS
#fi
#
#VERSION=1
#LOCKSS_BOX="$( adpn_parameter_from "--daemon" "localhost:8081" )"
#BASE_URL="$( adpn_parameter_from "--drop-server" ".stage/base_url" "http://drop.adpn.org/drop-server/adah/drop_au_content_in_here/" )"
#DIRECTORY="${_PARAM[directory]}"
#FILE="${_PARAM[file]}"
#
#FULL_URL=$( printf "%s%s/%s" "${BASE_URL}" "${DIRECTORY}" "${FILE}" )
#ENCODED_URL=$( rawurlencode "${FULL_URL}" )
#
#AU_ID_BASE='edu|auburn|adpn|directory|AuburnDirectoryPlugin&base_url~%s&directory~%s'
#AU_ID=$( printf "${AU_ID_BASE}" $(rawurlencode "${BASE_URL}" 2) $(rawurlencode "${DIRECTORY}" 2) )
#ENCODED_AU_ID=$( rawurlencode "${AU_ID}" )
#
#declare -a HT_PARAMS=()
#
#PARAM_URL="$( printf "url=%s" "${ENCODED_URL}" )"
#PARAM_AUID="$( printf "auid=%s" "${ENCODED_AU_ID}" )"
#PARAM_VERSION="$( printf "version=%d" "${VERSION}" )"
#
#HT_PARAMS+=( "${PARAM_URL}" )
#HT_PARAMS+=( "${PARAM_AUID}" )
#HT_PARAMS+=( "${PARAM_VERSION}" )
#
#HT_ARGS=$( join_by "&" "${HT_PARAMS[@]}" )
#
#OUTPUT_FILENAME="$( rawurlencode "${FILE}" )"
#wget "http://${LOCKSS_BOX}/ServeContent?${HT_ARGS}" --http-user="${HTTP_USER}" --http-password="${HTTP_PASS}" -O "${OUTPUT_FILENAME}"
;;
"rebalance")
# @method adpn rebalance
# Config Manager: Helps rebalance cached AUs on a LOCKSS cache.
#
# Usage:
#
# adpn rebalance
# adpn rebalance script
# adpn rebalance destination
# adpn rebalance lockdown
# adpn rebalance sync
# adpn rebalance reregister
#
# @version 2021.0302
adpn-do-rebalance "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" ; EXITCODE="$?"
;;
"publisher")
# @method adpn publisher
# Config Manager: Helps review and manage the list of AU publishers on a LOCKSS props server.
#
# @see adpn-do-publisher --help
#
# @version 2021.0302
on_props_do adpn-do-publisher "${_ADD_CONTEXT[@]}" "${_CMD_REST[@]}" \
|| EXITCODE="$?"