-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdx86-lock
executable file
·190 lines (169 loc) · 3.54 KB
/
pdx86-lock
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
#!/bin/sh
# Clone the repository into a temporary directory
# Checkout the mutex branch
# Read and update the LOCK file if possible with PDX86_ID
# Store a lock lease time in hours in the commit message
# Remove the temporary repository
# Report on the result
PROG_NAME="${0##*/}"
PROG_DIR="${0%/*}"
. $PROG_DIR/pdx86-config
#PDX86_REPO=ssh://[email protected]/srv/git/pdx86-tools.git
[email protected]:dvhart/pdx86-tools.git
LOCKFILE="LOCK"
LEASE_HOURS=2
FORCE=false
CMD="lock"
ERR=1
clean_up() {
rm -rf $PDX86_DIR
}
clean_up_trap() {
clean_up
exit $ERR
}
trap clean_up HUP INT TERM
clone() {
git clone -b mutex $PDX86_REPO $PDX86_DIR 2> /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: failed to clone $REMOTE repository ($PDX86_REPO)"
clean_up
exit $ERR
fi
cd $PDX86_DIR
VAL=`cat $LOCKFILE`
}
usage() {
echo "Usage: $PROG_NAME [OPTIONS]"
echo ""
echo "With no arguments, attempt to acquire the lock with a default 2 hour lease"
echo ""
echo " -f force the lock or unlock operation"
echo " (has no effect if you own the lock)"
echo " -h display this help and exit"
echo " -n do nothing"
echo " -s show status of the lock, but do not acquire or free it"
echo " -t NUM specify a lease time of NUM hours, defaults to 2 hours"
echo " -u release the lock"
}
push() {
git commit -a -m "$1" > /dev/null 2>&1 && git push 2> /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: failed to push LOCK change"
clean_up
exit $ERR
fi
}
owner() {
if [ -z "$VAL" ]; then
echo "Lock is free"
return
fi
echo "Lock held by $VAL:"
git log --pretty=format:"%h %s%n%cr" -1 -- LOCK
LOCK_TIME=$(date -d "$(git log --pretty=format:%cI origin/mutex -1 -- LOCK)" +%s)
CUR_TIME=$(date +%s)
ELAPSED_TIME=$(($CUR_TIME-$LOCK_TIME))
LAST_LEASE=$((3600*$(git log --pretty=format:%s -1 -- LOCK | sed -e "s/.*For \([0-9][0-9]*\) hours.*/\1/")))
if [ $? -ne 0 ]; then
echo "No lease time provided"
elif [ $ELAPSED_TIME -lt $LAST_LEASE ]; then
echo "Time remaining on lease: $(($(($LAST_LEASE-$ELAPSED_TIME))/60)) minutes"
else
echo "Lease expired: $(($(($ELAPSED_TIME-$LAST_LEASE))/60)) minutes ago"
fi
exit $ERR
}
lock() {
if [ -z "$VAL" ]; then
echo "$PDX86_ID" > $LOCKFILE
push "Lock acquired by $PDX86_ID (For $LEASE_HOURS hours)"
echo "Lock acquired by $PDX86_ID (For $LEASE_HOURS hours)"
elif $FORCE; then
echo "$PDX86_ID" > $LOCKFILE
push "Lock forcibly acquired by $PDX86_ID (For $LEASE_HOURS hours)"
echo "Lock forcibly acquired by $PDX86_ID (For $LEASE_HOURS hours)"
else
owner
fi
}
unlock() {
if [ -z "$VAL" ]; then
echo "Lock already free"
elif [ "$VAL" = "$PDX86_ID" ]; then
echo "" > $LOCKFILE
push "Lock released by $PDX86_ID"
echo "Lock released"
elif $FORCE; then
echo "" > $LOCKFILE
push "Lock forcibly released by $PDX86_ID"
echo "Lock forcibly released"
else
owner
clean_up
exit $ERR
fi
}
if [ -z "$PDX86_ID" ]; then
echo "ERROR: PDX86_ID environment variable is not set"
exit $ERR
fi
PDX86_DIR=$(mktemp -d --suffix=.$PROG_NAME)
if [ ! -d "$PDX86_DIR" ]; then
echo "ERROR: failed to create temp directory"
exit $ERR
fi
while true; do
case "$1" in
"-f")
FORCE=true
;;
"-h")
CMD="help"
;;
"-n")
CMD="nothing"
;;
"-s")
CMD="status"
;;
"-t")
shift
LEASE_HOURS="$1"
;;
"-u")
CMD="unlock"
;;
*)
break
;;
esac
shift
done
case "$CMD" in
"help")
usage
;;
"nothing")
;;
"lock")
clone
lock
;;
"status")
clone
owner
;;
"unlock")
clone
unlock
;;
*)
echo "Unknown CMD: $CMD"
usage
clean_up
exit $ERR
;;
esac
clean_up
exit 0