-
Notifications
You must be signed in to change notification settings - Fork 1
/
startRemote.sh
executable file
·208 lines (178 loc) · 4.29 KB
/
startRemote.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /bin/bash
#signature
signature='
$$\ $$\ $$\ $$\ $$$$$$\ $$$$$$\
$$ | $$ |$$$\ $$ |$$ __$$\ $$ __$$\
$$ |$$ / $$$$\ $$ |\__/ $$ |$$ / \__|
$$$$$ / $$ $$\$$ | $$$$$$ |$$ |
$$ $$< $$ \$$$$ |$$ ____/ $$ |
$$ |\$$\ $$ |\$$$ |$$ | $$ | $$\
$$ | \$$\ $$ | \$$ |$$$$$$$$\ \$$$$$$ |
\__| \__|\__| \__|\________| \______/
----------------------------------------
'
# input discription
# n : number of games running simultaneously
# ssp (optional) : servers start port - first server will be run on
# this port and the number will be increased for next server and so on
# spd (optional) : servers port difference - the port number will be
# increased by sdp from each server to the next
#global variables
declare -i n
declare -i ssp
declare -i spd
teamsDIR=""
resultDIR=""
DIR=`dirname $0`
declare -i lineOfResults
declare -i lineOfGames=`wc -l $DIR/data/Games.txt | awk '{ print $1 }'`
declare -i firstLines
#methods
initialize() {
read -t 5 -p "enter path of teams directory: " teamsDIR
if [[ $teamsDIR = "" ]]; then
echo "$DIR/teams"
fi
###########
read -t 5 -p "enter path of results directory: " resultDIR
if [[ $resultDIR = "" ]]; then
echo "$DIR/results"
fi
############
read -t 5 -p "enter servers start port: " ssp
if [[ $ssp -eq 0 ]]; then
ssp=6000
echo "ssp=6000"
fi
###########
read -t 5 -p "enter servers port difference: " spd
if [[ $spd -eq 0 ]]; then
spd=10
echo "spd=10"
fi
}
writePathToFile() {
if [[ $resultDIR = "" ]] ; then
resultDIR=$DIR/results
fi
if [[ $teamsDIR = "" ]] ; then
teamsDIR=$DIR/teams
fi
firstChar=${resultDIR:0:1}
if [[ $firstChar = "." ]] ; then
resultDIR=${resultDIR:1}
resultDIR=`pwd`$resultDIR
fi
echo $teamsDIR > $DIR/data/path.txt
echo $resultDIR >> $DIR/data/path.txt
}
countN() {
input=$1
declare -i m=0
declare -i count
while IFS= read -r line
do
count=0
for word in $line
do
W[count]=$word
count+=1
done
if [[ $count -gt 1 ]] && [[ $count -lt 4 ]] && [[ ${W[0]:0:1} != "#" ]]
then
m=`expr ${W[1]} + $m`
fi
done < $input
n=$m
}
findLineOfResults() {
if [ -e $resultDIR/Results.txt ] ; then
lineOfResults=`wc -l $resultDIR/Results.txt | awk '{ print $1 }'`
else
lineOfResults=0
fi
}
bar() {
local items=$1
local total=$2
local size=$3
per=$(($items*$size/$total % $size))
left=$(($size-$per))
chars=$(local s=$(printf "%${per}s"); echo "${s// /=}")
echo -ne "[$chars>";
printf "%${left}s"
echo -ne ']\r'
}
clearbar() {
local size=$1
printf " %s${size} "
echo -ne "\r"
}
progressBar() {
declare -i preLine=-1
findLineOfResults
firstLines=$lineOfResults
lineOfResults=0
bar 0 100 50
lineOfGames=`wc -l $DIR/data/Games.txt | awk '{ print $1 }'`
while ! [ $lineOfGames -eq $lineOfResults ] ; do
findLineOfResults
lineOfResults=$lineOfResults-$firstLines
lineOfGames=`wc -l $DIR/data/Games.txt | awk '{ print $1 }'`
declare -i percent=$lineOfResults
percent=$percent*100
percent=$percent/$lineOfGames
if ! [ $lineOfResults -eq $preLine ] ; then
echo -n $percent %
bar $percent 100 50
fi
preLine=$lineOfResults
done
echo "[=======================================================>"
echo "Done!"
}
#main method
main() {
echo "$signature"
echo "start" $$ > $DIR/proc.txt
sed -i -r '/^\s*$/d' $DIR/data/Games.txt
initialize
countN "$DIR/data/remoteAddresses.txt"
writePathToFile
echo ""
echo "Updating remote servers."
$DIR/updateRemote.sh
echo "Remote servers updated."
# running runOnRemote script for each set of games (n times) (each server and port)
declare -i m=0
declare -i lim
input="$DIR/data/remoteAddresses.txt"
declare -i count
declare -i i
declare -i port=$ssp
declare -i remote_index=1
while IFS= read -r line
do
count=0
for word in $line
do
W[count]=$word
count+=1
done
if [[ $count -gt 1 ]] && [[ $count -lt 4 ]] && [[ ${W[0]:0:1} != "#" ]]
then
lim=`expr ${W[1]}`
port=$ssp
for (( i=0; i < lim; i++ )); do
$DIR/RemoteSubScritps/runOnRemote.sh $remote_index $port $n $m &
port=$port+$spd
m+=1
done
fi
remote_index+=1
done < $input
progressBar
rm $DIR/proc.txt
}
#
main