-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfunc.sh
executable file
·143 lines (127 loc) · 3.48 KB
/
func.sh
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
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Only one lock is allowed: biglock
# getLockFile() parameters
# $1 lock filename
# $2 timeout seconds
#set -x
getCurrLock() {
result=`ls $__LOCKDIR/*-$1.lock 2>/dev/null | head -n1`
while [ $? -ne 0 ]
do
result=`ls $__LOCKDIR/*-$1.lock 2>/dev/null| head -n1`
done
echo $result
}
getLockFile() {
lock=$1
__locked=0
__TS=`date +%s%N`
__LOCKDIR="/tmp"
__LOCKFILE="$__LOCKDIR/$__TS-$$-$lock.lock"
if [ $2 ]
then
__TIMEOUT=$2
else
__TIMEOUT=30
fi
if [ -e $__LOCKFILE ]
then
logger -t cloud "Process $0 pid $$ want to get ECLUSIVE LOCK $lock RECURSIVELY!"
psline=`ps u $$`
logger -t cloud "Failed job detail: $psline"
echo 0
return
fi
psline=`ps u $$`
echo $psline > $__LOCKFILE
if [ ! -e $__LOCKFILE ]
then
return
fi
for i in `seq 1 $(($__TIMEOUT * 10))`
do
currlock=$(getCurrLock $lock)
if [ $currlock -ef $__LOCKFILE ]
then
__locked=1
break
fi
sleep 0.1
if [ $((i % 10)) -eq 0 ]
then
logger -t cloud "Process $0 pid $$ waiting for the lock $lock for another 1 second"
fi
done
if [ $__locked -ne 1 ]
then
logger -t cloud "fail to acquire the lock $lock for process $0 pid $$ after $__TIMEOUT seconds time out!"
cmd=`cat $currlock 2>/dev/null`
if [ $? -eq 0 ]
then
logger -t cloud "waiting for process: $cmd"
else
logger -t cloud "didn't get info about process who we're waiting for"
fi
psline=`ps u $$`
logger -t cloud "Failed job detail: $psline"
rm $__LOCKFILE
fi
echo $__locked
}
# releaseLockFile() parameters
# $1 lock filename
# $2 locked(1) or not(0)
releaseLockFile() {
__LOCKDIR="/tmp"
__LOCKFILE="$__LOCKDIR/*-$$-$1.lock"
__locked=$2
if [ "$__locked" == "1" ]
then
rm $__LOCKFILE
fi
}
# releaseLockFile() parameters
# $1 exit value
# $2 lock filename
# $3 locked(1) or not(0)
unlock_exit() {
releaseLockFile $2 $3
exit $1
}
# calculate the ip & network mask
rangecalc(){
local IFS='.'
local -a oct mask ip
read -ra oct <<<"$1"
read -ra mask <<<"$2"
for i in {0..3}
do
ip+=( "$(( oct[i] & mask[i] ))" )
done
echo "${ip[*]}"
}
#get cidr of the nic
getcidr(){
local dev=$1
local mask=`ifconfig $dev|grep "Mask"|cut -d ":" -f 4`
local cidrsize=`ip addr show $dev|grep inet|head -n 1|awk '{print $2}'|cut -d '/' -f 2`
local ipaddr=`ip addr show $dev|grep inet|head -n 1|awk '{print $2}'|cut -d '/' -f 1`
local base=$(rangecalc $ipaddr $mask)
echo $base/$cidrsize
}