Skip to content

Commit a1c8804

Browse files
author
Tom Barnes
committed
Change leasing LEASE_PID var to LEASE_ID (it doesn't need to be a PID)
1 parent b8d51d8 commit a1c8804

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

src/integration-tests/bash/lease.sh

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,35 @@ cat << EOF
2929
3030
Usage:
3131
32-
Obtain : lease.sh -o pid [-t timeout]
33-
Renew : lease.sh -r pid
34-
Delete : lease.sh -d pid
32+
Obtain : lease.sh -o id [-t timeout]
33+
Renew : lease.sh -r id
34+
Delete : lease.sh -d id
3535
Force Delete : lease.sh -f
3636
Show : lease.sh -s
3737
Help : lease.sh -h
3838
3939
The user is expected to call this script multiple times throughout
4040
the ownership of a lease:
4141
42-
1) First, call 'lease.sh -o pid' to try obtain a lease. If
42+
1) First, call 'lease.sh -o id' to try obtain a lease. If
4343
this fails, fail your script, since failure may mean some other
4444
owner has ownership. This call will block up to 30 minutes
4545
while trying to get the lease (tunable via -t), and will grab
4646
the lease if no other process owns it or no other process has touched
47-
it for a period of 10 minutes.
47+
it for a period of 10 minutes. 'id' should be some unique
48+
string suitable for use as a filename - a pid is sufficient
49+
or a `uuidgen` value.
4850
49-
2) While running, call 'lease.sh -r pid' every few minutes
51+
2) While running, call 'lease.sh -r id' every few minutes
5052
to renew the lease and retain ownership. Fail if this fails,
5153
since that means you may have lost the lease. Be careful to call
5254
this within 10 minutes of last obtaining or renewing the lease,
5355
otherwise the lease expires and another process is free to obtain
54-
ownership. Use the same value of 'pid' as was specified in step (1).
56+
ownership. Use the same value of 'id' as was specified in step (1).
5557
56-
3) Finally, call 'lease.sh -d pid' to delete and give up your
58+
3) Finally, call 'lease.sh -d id' to delete and give up your
5759
lease. This will fail if you are not the current owner. Use the same
58-
value of 'pid' as was specified in step (1). If you forget to
60+
value of 'id' as was specified in step (1). If you forget to
5961
call it, no one else will be able to get the lease for up to 10
6062
minutes.
6163
@@ -120,8 +122,8 @@ function main {
120122
# Save command line for error messages...
121123
COMMAND_LINE="$0 $*"
122124

123-
# Local PID that owns the K8S lease. This is passed in on the command line.
124-
LOCAL_PID=""
125+
# Local ID that owns the K8S lease. This is passed in on the command line.
126+
LOCAL_ID=""
125127

126128
local mode=""
127129

@@ -137,23 +139,23 @@ function main {
137139
;;
138140
-o) if [ "$mode" = "" ] && [ ! "$2" = "" ]; then
139141
mode="obtainLease"
140-
LOCAL_PID="${2}"
142+
LOCAL_ID="${2}"
141143
shift
142144
else
143145
mode="commandLineError"
144146
fi
145147
;;
146148
-r) if [ "$mode" = "" ] && [ ! "$2" = "" ]; then
147149
mode="renewLease"
148-
LOCAL_PID="${2}"
150+
LOCAL_ID="${2}"
149151
shift
150152
else
151153
mode="commandLineError"
152154
fi
153155
;;
154156
-d) if [ "$mode" = "" ] && [ ! "$2" = "" ]; then
155157
mode="deleteRemoteLeaseSafe"
156-
LOCAL_PID="${2}"
158+
LOCAL_ID="${2}"
157159
shift
158160
else
159161
mode="commandLineError"
@@ -191,7 +193,7 @@ function main {
191193
shift
192194
done
193195

194-
LOCAL_ROOT="${LOCAL_ROOT}/lease/pid-${LOCAL_PID}"
196+
LOCAL_ROOT="${LOCAL_ROOT}/lease/id-${LOCAL_ID}"
195197

196198
# Execute command line
197199

@@ -240,7 +242,7 @@ function getLocalLease {
240242
function makeLocalLease {
241243
#
242244
# Make a candidate lease file in ${LOCAL_ROOT}/[expired/]${LOCAL_FILE}
243-
# Embed pid, hostname, timestamp, etc to make it easy to identify the owner.
245+
# Embed id, hostname, timestamp, etc to make it easy to identify the owner.
244246
# returns non-zero if fails
245247
# Usage: Pass "expired" as $1 to create a lease that immediately expires
246248
# (its timestamp value will be 0) This file will be located
@@ -262,7 +264,7 @@ function makeLocalLease {
262264
timestamp=0
263265
date=`date '+%m-%d-%YT%H:%M:%S'`
264266
host=$HOST
265-
pid=$LOCAL_PID
267+
id=$LOCAL_ID
266268
user=$USER
267269
EOF
268270
else
@@ -271,7 +273,7 @@ EOF
271273
timestamp=`date +%s`
272274
date=`date '+%m-%d-%YT%H:%M:%S'`
273275
host=$HOST
274-
pid=$LOCAL_PID
276+
id=$LOCAL_ID
275277
user=$USER
276278
EOF
277279
fi

src/integration-tests/bash/run.sh

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,17 @@
101101
# BRANCH_NAME Git branch name.
102102
# Default is determined by calling 'git branch'.
103103
#
104-
# LEASE_PID Set to a PID to (A) periodically renew a lease on
104+
# LEASE_ID Set to a unique value to (A) periodically renew a lease on
105105
# the k8s cluster that indicates that no other
106106
# run.sh should attempt to use the cluster, and (B)
107107
# delete this lease when run.sh completes.
108108
# If "true" the caller must previously
109109
# obtain the lease external to the run.sh
110-
# using "lease.sh -o $LEASE_PID", where PID corresponds
110+
# using "lease.sh -o $LEASE_ID", where ID corresponds
111111
# to a process that's expected to continue
112112
# throughout the run (typically the parent
113-
# process).
113+
# process) or some uuid potentially generated by uuidgen
114+
# command or some-such..
114115
#
115116
# The following additional overrides are currently only used when
116117
# WERCKER=true:
@@ -205,31 +206,31 @@ $1
205206
}
206207

207208

208-
# Attempt to renew the k8s lease if $LEASE_PID is set. This should be called
209+
# Attempt to renew the k8s lease if $LEASE_ID is set. This should be called
209210
# every few minutes throughout the run, and is currently called at the start
210-
# of each test. See LEASE_PID instructions above for instructions about
211+
# of each test. See LEASE_ID instructions above for instructions about
211212
# how to obtain the lease prior to calling run.sh.
212213
function renewLease {
213-
if [ ! "$LEASE_PID" = "" ]; then
214+
if [ ! "$LEASE_ID" = "" ]; then
214215
# RESULT_DIR may not have been created yet, so use /tmp
215216
local outfile=/tmp/acc_test_renew_lease.out
216-
$SCRIPTPATH/lease.sh -r $LEASE_PID > $outfile 2>&1
217+
$SCRIPTPATH/lease.sh -r "$LEASE_ID" > $outfile 2>&1
217218
if [ $? -ne 0 ]; then
218219
trace "Lease renew error:"
219220
echo "" >> $outfile
220-
echo "ERROR: Could not renew lease on k8s cluster for LEASE_PID=$LEASE_PID." >> $outfile
221-
echo "Used '$SCRIPTPATH/lease.sh -r $LEASE_PID' to try renew the lease." >> $outfile
221+
echo "ERROR: Could not renew lease on k8s cluster for LEASE_ID=\"$LEASE_ID\"." >> $outfile
222+
echo "Used '$SCRIPTPATH/lease.sh -r \"$LEASE_ID\"' to try renew the lease." >> $outfile
222223
echo "Some of the potential reasons for this failure are that another run" >> $outfile
223224
echo "may have obtained the lease, the lease may have been externally " >> $outfile
224225
echo "deleted, or the caller of run.sh may have forgotten to obtain the" >> $outfile
225-
echo "lease before calling run.sh (using 'lease.sh -o $LEASE_PID'). " >> $outfile
226+
echo "lease before calling run.sh (using 'lease.sh -o \"$LEASE_ID\"'). " >> $outfile
226227
echo "To force delete a lease no matter who owns the lease," >> $outfile
227228
echo "call 'lease.sh -f' or 'kubernetes delete cm acceptance-test-lease'" >> $outfile
228229
echo "(this should only be done when sure there's no current run.sh " >> $outfile
229230
echo "that owns the lease). To view the current lease holder," >> $outfile
230231
echo "use 'lease.sh -s'. To disable this lease check, do not set" >> $outfile
231-
echo "the LEASE_PID environment variable. For more information, see" >> $outfile
232-
echo "LEASE_PID in the instructions embedded at the top of run.sh." >> $outfile
232+
echo "the LEASE_ID environment variable. For more information, see" >> $outfile
233+
echo "LEASE_ID in the instructions embedded at the top of run.sh." >> $outfile
233234
echo "" >> $outfile
234235
cat $outfile
235236
fail "Could not renew lease on k8s cluster"
@@ -383,7 +384,7 @@ function state_dump {
383384
local PV_ROOT="`cat /tmp/test_suite.pv_root`"
384385
local PROJECT_ROOT="`cat /tmp/test_suite.project_root`"
385386
local SCRIPTPATH="$PROJECT_ROOT/src/integration-tests/bash"
386-
local LEASE_PID="`cat /tmp/test_suite.lease_pid`"
387+
local LEASE_ID="`cat /tmp/test_suite.lease_id`"
387388

388389
if [ ! -d "$RESULT_DIR" ]; then
389390
trace State dump exiting early. RESULT_DIR \"$RESULT_DIR\" does not exist or is not a directory.
@@ -439,9 +440,9 @@ function state_dump {
439440
trace Job failed. See ${outfile}.
440441
fi
441442

442-
if [ ! "$LEASE_PID" = "" ]; then
443+
if [ ! "$LEASE_ID" = "" ]; then
443444
# release the lease if we own it
444-
${SCRIPTPATH}/lease.sh -d $LEASE_PID > ${RESULT_DIR}/release_lease.out 2>&1
445+
${SCRIPTPATH}/lease.sh -d "$LEASE_ID" > ${RESULT_DIR}/release_lease.out 2>&1
445446
if [ "$?" = "0" ]; then
446447
trace Lease released.
447448
else
@@ -2448,7 +2449,7 @@ function test_suite_init {
24482449
WEBLOGIC_IMAGE_PULL_SECRET_NAME \
24492450
WERCKER \
24502451
JENKINS \
2451-
LEASE_PID;
2452+
LEASE_ID;
24522453
do
24532454
trace "Customizable env var before: $varname=${!varname}"
24542455
done
@@ -2464,7 +2465,7 @@ function test_suite_init {
24642465
[ ! "$?" = "0" ] && fail "Error: Could not determine branch. Run script from within a git repo".
24652466
fi
24662467

2467-
export LEASE_PID=${LEASE_PID}
2468+
export LEASE_ID="${LEASE_ID}"
24682469

24692470
# The following customizable exports are currently only customized by WERCKER
24702471
export IMAGE_TAG_OPERATOR=${IMAGE_TAG_OPERATOR:-`echo "test_${BRANCH_NAME}" | sed "s#/#_#g"`}
@@ -2490,7 +2491,7 @@ function test_suite_init {
24902491
WEBLOGIC_IMAGE_PULL_SECRET_NAME \
24912492
WERCKER \
24922493
JENKINS \
2493-
LEASE_PID;
2494+
LEASE_ID;
24942495
do
24952496
trace "Customizable env var after: $varname=${!varname}"
24962497
done
@@ -2513,7 +2514,7 @@ function test_suite_init {
25132514
echo "${RESULT_ROOT}" > /tmp/test_suite.result_root
25142515
echo "${PV_ROOT}" > /tmp/test_suite.pv_root
25152516
echo "${PROJECT_ROOT}" > /tmp/test_suite.project_root
2516-
echo "${LEASE_PID}" > /tmp/test_suite.lease_pid
2517+
echo "${LEASE_ID}" > /tmp/test_suite.lease_id
25172518

25182519
# Declare we're in a test. We did not declare at the start
25192520
# of the function to ensure that any env vars that might

wercker.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,26 @@ integration-test:
113113
kubectl version
114114
115115
# obtain an exclusive k8s cluster lease using the 'lease.sh' helper script
116-
# - first set LEASE_PID to a PID
116+
# - first set LEASE_ID to a unique value
117117
# - then try obtain the lease, block up to 100 minutes (wercker pipeline should timeout before then)
118-
# - finally, run.sh will periodically try renew the lease as it runs (using $LEASE_PID)
118+
# - finally, run.sh will periodically try renew the lease as it runs (using $LEASE_ID)
119119
# - if run.sh fails when it tries to renew the lease (as something else took it, etc), it will exit early
120120
# - when run.sh exits, it will try release the lease if it's still the owner...
121-
export LEASE_PID=$$
121+
export LEASE_ID="`uuidgen`-pid$$"
122+
echo @@
123+
echo @@ "Obtaining lease!"
122124
echo @@
123125
echo @@ "About to block up to the 100 minutes trying to get exclusive access to the kubernetes cluster."
124126
echo @@ "If this blocks unexpectedly and you are sure that the kubernetes cluster isn't in use by "
125127
echo @@ "another Wercker pipeline, you can force the lease to free up via 'kubectl delete cm acceptance-test-lease'."
126-
echo @@ "See LEASE_PID in run.sh for details about this heuristic."
127-
echo @@ "PID=$LEASE_PID host=$HOST date=`date` user=$USER."
128+
echo @@ "See LEASE_ID in run.sh for details about this heuristic."
129+
echo @@ "LEASE_ID=$LEASE_ID host=$HOST date=`date` user=$USER."
130+
echo @@
128131
echo @@ "Current lease owner (if any):"
129132
$WERCKER_SOURCE_DIR/src/integration-tests/bash/lease.sh -s
133+
echo @@
130134
echo @@ "About to try obtain lease:"
131-
$WERCKER_SOURCE_DIR/src/integration-tests/bash/lease.sh -o $LEASE_PID -t $((100 * 60))
135+
$WERCKER_SOURCE_DIR/src/integration-tests/bash/lease.sh -o "$LEASE_ID" -t $((100 * 60))
132136
echo @@
133137
134138
# create pull secrets
@@ -182,7 +186,7 @@ integration-test:
182186
# release lease in case run.sh failed to release it
183187
# (the following command only releases the release after confirming this pipeline still owns it)
184188
# TBD Calling this somehow seems to fail the wercker run
185-
# $WERCKER_SOURCE_DIR/src/integration-tests/bash/lease.sh -d $LEASE_PID > /tmp/junk 2>&1
189+
# $WERCKER_SOURCE_DIR/src/integration-tests/bash/lease.sh -d "$LEASE_ID" > /tmp/junk 2>&1
186190
187191
# clean up
188192
yum clean all

0 commit comments

Comments
 (0)