-
Notifications
You must be signed in to change notification settings - Fork 13
/
dropbatch
executable file
·127 lines (113 loc) · 2.71 KB
/
dropbatch
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
#!/usr/bin/env bash
. lib.bash || exit
usage() {
echo "Usage: $progname [-n] [-w] HOST [FILE]"
echo "Usage: $progname -l [-n] [-w] [HOST]"
echo
echo "Place a script in ~/Dropbox to be run by another host's dropbatch.service"
echo
echo_opt "-l" "list all stored jobs and their output files"
echo_opt "-n" "place script directly on remote host via NFS"
echo_opt "-w" "wait and show job output when it appears"
echo
echo "Jobs submitted with the -w option are stored in a separate location; they"
echo "are only listed if -w is specified together with -l."
}
dolist=0
usenfs=0
wait=0
while getopts :lnw OPT; do
case $OPT in
l) dolist=1;;
n) usenfs=1;;
w) wait=1;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
host=${1%%.*}
script=$2
if [[ ! $host ]] && (( !dolist || usenfs )); then
vdie "missing hostname"
fi
if (( usenfs )); then
targetdir=/n/${host?}/Dropbox
else
targetdir=~/Dropbox
fi
if (( wait )); then
targetdir=$targetdir/.System/Batch
fi
if (( dolist )); then
for path in "$targetdir"/[a-z]*-[0-9]*.sh*; do
file=${path##*/}
if [[ $host && $file != "$host"-* ]]; then
continue
fi
if ! [[ $file =~ ^([a-z]+)-([0-9]+)\.sh(\.done|\.log)?$ ]]; then
continue
fi
jhost=${BASH_REMATCH[1]}
jtime=${BASH_REMATCH[2]}
jstatus=${BASH_REMATCH[3]}
jdate=$(date -d "@$jtime" +"%b %-d, %H:%M")
printf '%s\t%s\t%s\t%s\n' \
"$jtime" "$jdate" "$jhost" "$path"
done \
| sort -t $'\t' -k 1 -n \
| column -t -s $'\t' -N TIME,DATE,HOST,FILE -H TIME -O DATE,HOST,FILE
exit
fi
if [[ ! $script || $script == "-" ]]; then
tmp=$(mktemp /tmp/job.XXXXXXXX.sh)
echo '#!/bin/bash' > "$tmp"
if [[ ! $script && -t 0 ]]; then
${EDITOR:-vi} "$tmp"
else
cat > "$tmp"
fi
if ! grep -vqs "^#!" "$tmp"; then
vmsg "script is empty"
rm -f "$tmp"
exit
fi
script=$tmp
elif [[ ! -f $script ]]; then
vdie "script file '$script' not found"
elif [[ ! -s $script ]]; then
vmsg "script file is empty"
exit
fi
dest=$targetdir/$host-$(date +%s).sh
cp "$script" "$dest.tmp" &&
chmod u+x "$dest.tmp" &&
mv "$dest.tmp" "$dest" &&
rm "$script" || exit
vmsg "job $dest submitted"
if (( wait )); then
vmsg "waiting for job to be processed..."
time=0
if (( usenfs )); then
# For submissions over NFS, wait for the job to complete before
# checking -- doing it too quickly will populate negative stat
# cache which takes 30 seconds to expire.
time=3; sleep $time
fi
until [[ -e $dest.done ]]; do
printf '\rWaiting (%ds)...' $[time++]
sleep 1
done
printf '\n'
if [[ -e $dest.log ]]; then
vmsg "job finished"
else
vmsg "job finished, waiting for result..."
time=0
until [[ -e $dest.log ]]; do
if [[ -t 1 ]]; then
printf '\r%ds' $[time++]
fi
sleep 1
done
fi
${PAGER:-cat} "$dest.log"
fi