-
Notifications
You must be signed in to change notification settings - Fork 3
/
handle_request_raw_viollier
executable file
·131 lines (104 loc) · 3.88 KB
/
handle_request_raw_viollier
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
#!/bin/bash
configfile=viollier.conf
usage() { echo "Usage: $0 [ -f ] [-c <configfile>]" 1>&2; exit $1; }
# Helper
fail() {
printf '\e[31;1mArgh: %s\e[0m\n' "$1" 1>&2
[[ -n "$2" ]] && echo "$2" 1>&2
exit 1
}
oops() {
printf '\e[33;1mOops:%s\e[0m\n' "$1" 1>&2
}
title() {
printf '\e[34;1m======================\n%s\n======================\e[0m\n\n' "$1"
}
message() {
printf '\e[37;1m%s\t%s\e[0m\n' "$1" "$2"
}
status() {
printf '\e[36;1m%s\e[0m\n' "$1"
}
force=0
while getopts "c:fh" o; do
case "${o}" in
c) configfile=${OPTARG}
if [[ ! -r ${configfile} ]]; then
echo "Cannot read ${configfile}" 1>&2
usage 1
fi
;;
f) force=1 ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
shift $((OPTIND-1))
. ${configfile}
: ${fileserver:?}
# ${srvport}
: ${expname:?}
: ${basedir:=$(pwd)}
: ${download:?}
: ${sampleset:?}
target_base='raw_othercenters'
req_file='fastq_requests.tsv'
msg_file='request_results.txt'
[[ "${1}" == '-h' || "${1}" == '--help' ]] && usage 0
if [[ $( < ~/.netrc ) =~ machine[[:space:]]+${fileserver}[[:space:]]?login[[:space:]]+([^[:space:]]+) ]]; then
username="${BASH_REMATCH[1]}"
elif [[ $( < ~/.ssh/config ) =~ Host[[:space:]]+${fileserver}[[:space:]]+ ]]; then
username=
else
fail "cannot find login for machine ${fileserver} in ~/.netrc nor ~/.ssh/config"
fi
cd ${basedir}
# download new request file and compare with old one
if lftp -c "set xfer:clobber on; get1 sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}/${expname}/${target_base}/${req_file} -o ${download}/${target_base}/"; then
if (( force )); then
status "Forcing update"
# check content
elif [[ -e "${download}/${target_base}/${req_file}.old" ]] && diff -q "${download}/${target_base}/${req_file}"{,.old}; then
# same: nothing to do
status "No new uploads in ${req_file}"
grep -P '^(\([[:alpha:]]+|[[:space:]]+|summary)' ${download}/${target_base}/${msg_file}
exit 0
fi
else
# error
fail "Cannot download request file sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}/${expname}/${target_base}/${req_file}" "Please check that ${configfile} is correctly setup"
fi
# create result file
tee ${download}/${target_base}/${msg_file} <<MSG
starting on $(date --rfc-3339=seconds)
new ${req_file}, date $(date --rfc-3339=seconds -r "${download}/${target_base}/${req_file}")
validating input:
MSG
# validate input
validated_file='fastq_requests_validated.tsv'
if python ./validate_request_raw_viollier.py --req_file ${download}/${target_base}/${req_file} --out_file ${download}/${target_base}/${validated_file} --db_config ${basedir}/${dbconfigfile} 2>&1; then
printf '\e[36;1mValidation successful\e[0m\n' 1>&2
echo -e "successfully validated input\n"
else
# error
printf '\e[31;1mValidation failed!\e[0m' 1>&2
echo -e "error while trying to validate the request, see above\n"
fi | tee -a ${download}/${target_base}/${msg_file}
tee ${download}/${target_base}/${msg_file} <<MSG2
attempting upload:
MSG2
# attempt upload
subdir=$(date '+%Y-%m-%d' -r "${download}/${target_base}/${req_file}")
if ./upload_viollier_raw -q -c "${configfile}" -s "${subdir}" "${download}/${target_base}/${validated_file}" 2>&1; then
# upload success
printf '\e[36;1mSuccess\e[0m\n' 1>&2
echo -e "successfully uploaded to ${subdir}\n"
mv -v "${download}/${target_base}/${req_file}"{,.old}
mv -v "${download}/${target_base}/${validated_file}"{,.old}
else
# error
printf '\e[31;1mArgh:\e[0m' 1>&2
echo -e "error while trying to upload, see above\n"
fi | tee -a ${download}/${target_base}/${msg_file}
(echo $'\nsummary of troubles'; grep -oP '(?<=[\(])(zero yield|[[:alpha:]]+)' ${download}/${target_base}/${msg_file}|sort|uniq -c || echo $'\tno troubles found' ) | tee -a ${download}/${target_base}/${msg_file}
exec lftp -c "set xfer:clobber on; put ${download}/${target_base}/${msg_file} -o sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}/${expname}/${target_base}/"