forked from blobbsen/androidFarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
androidFarm.sh
executable file
·262 lines (198 loc) · 5.9 KB
/
androidFarm.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
# author: [email protected]
# version: beta
# date: 03/05/2015
# custom variables, must change
jenkinsMaster="http://192.168.1.101:8080"
base=`echo pwd`
# specify credentials with administer and generic read permission,
# in order to read the status of jenkins slave instances
user=""
password=""
# arrays for processing
name=()
secret=()
serial=()
# counter for while loop
counter=0
#### functions ####
getData() {
# reset
counter=0
while read line; do
name[$counter]=`echo $line | awk '{split($0,a," "); print a[1]}'`
secret[$counter]=`echo $line | awk '{split($0,a," "); print a[2]}'`
serial[$counter]=`echo $line | awk '{split($0,a," "); print a[3]}'`
pid[$counter]=`echo $line | awk '{split($0,a," "); print a[4]}'`
((counter++))
done < ${base}/devices.conf
}
evaluate () {
((counter--))
for ((i=0;i<=${counter};i++)); do
# checking status
deviceIsConnected=$(isConnected ${serial[$i]})
if [ "$deviceIsConnected" == "true" ]; then
# check if slave needs to be started
OUTPUT=`ls ${base}/temp/ | grep ${name[i]}`
if [ ${#OUTPUT} -lt 1 ]; then
echo ""
echo ""
echo ""
echo "############ `date` ############"
echo ""
echo "device for slave: ${name[i]} connected, starting its slave instance..."
echo ""
echo "---------- log of 'java -jar slave.jar' --------------------------"
startSlave $i
echo ""
#echo "---------- checking status of slave --------------------"
#slaveStatus=$(getStatus ${name[$i]})
#echo "--------------------------------------------------------------------"
#echo ""
#if [ "$slaveStatus" == "offline" ]; then
# echo "starting slave instance for ${name[i]} failed !!!"
#else
# echo "starting slave instance for ${name[i]} successful"
#fi
fi
else
# check if shut down for slave is necessary
OUTPUT=`ls ${base}/temp/ | grep ${name[i]}`
if [ ${#OUTPUT} -gt 0 ]; then
echo ""
echo ""
echo ""
echo "############ `date` ############"
echo ""
echo "device for slave: ${name[i]} disconnected, killing its slave instance..."
echo ""
# cutting pid from folder name (chars after last occurence of "_")
pid=${OUTPUT##*_}
kill $pid >> ${base}/temp/garbage
echo ""
#sleep 10
#echo "---------- checking status of slave --------------------"
#slaveStatus=$(getStatus ${name[$1]})
#echo "--------------------------------------------------------------------"
#echo ""
#if [ "$slaveStatus" == "offline" ]; then
echo "killing slave instance for ${name[i]} successful"
rmdir ${base}/temp/${OUTPUT}
#else
# echo "killing slave instance for ${name[i]} failed !!!"
#fi
echo ""
fi
fi
done
}
## slave specific functions
startSlave () {
# TO DO: implement startSlave.sh script inside this one
pidTemp=$( bash ${base}/startSlave.sh ${jenkinsMaster} ${name[$1]} ${secret[$1]} ${base} )
sleep 30
echo "$(cat ${base}/temp/workAround.txt)"
echo "--------------------------------------------------------------------"
# deleting workAround.txt for next loop
rm ${base}/temp/workAround.txt
# ganz anders und dann wieder mit ordnern
# creating instance blakeks
mkdir ${base}/temp/${name[$1]}_${pidTemp}
}
getStatus () {
cd ${base}/temp
# so jenkins got some time to refresh its content!!!! must be there
sleep 20
if [ "$user" == "" ]; then
wget --no-check-certificate ${jenkinsMaster}/computer/
else
wget --no-check-certificate --auth-no-challenge --http-user=${user} --http-password=${password} ${jenkinsMaster}/computer/
fi
webpage=`cat ${base}/temp/index.html`
rm ${base}/temp/index.html
cd ${base}
if [[ $webpage == *"${name[$1]}</a> (offline)<"* ]]; then
echo "offline";
else
echo "online";
fi
}
## adb specific function
isConnected () {
OUTPUT=`adb devices | grep $1`
if [ ${#OUTPUT} -lt 1 ]; then
echo "false"
else
echo "true"
fi
}
######################## script ##########################
# command specific processes #
if [ "$1" == "start" ]; then
if [ "$2" != "notInitial" ]; then
echo ""
echo "-------------- starting androidFarm daemon ---------------"
# check if setup is needed
mkdir -p ${base}/master
mkdir -p ${base}/slaves
mkdir -p ${base}/temp
# deleting temporary files
rm -rf ${base}/temp/*
fi
getData
evaluate
# delay
sleep 10
bash ${base}/androidFarm.sh start notInitial
fi
if [ "$1" == "add" ]; then
echo ""
echo "---------------------- androidFarm -----------------------"
echo ""
echo "adding androidSlave $2 with serial $4"
# TO DO: create slave directory and softlinking tools dir of androidFarm master
mkdir ${base}/slaves/$2
ln -s ${base}/master/tools ${base}/slaves/$2/tools
newDevice="$2 $3 $4"
# add it to the devices.conf
echo $newDevice >> ${base}/devices.conf
echo ""
echo "----------------------------------------------------------"
echo ""
fi
if [ "$1" == "list" ]; then
# TO DO: list of new devices. einfach drei arrays machen und fertig.
# der eine wird gegen das andere gepushed.
echo ""
echo "---------------------- androidFarm -----------------------"
echo ""
echo "--- list of connected devices ---"
echo `adb devices`
# just works with nexus devices
#
# to do: try to save in variable and echo instead of cat
#
#adb devices -l > ${base}/temp/adbList
#ARRAY=( $(cat ${base}/temp/adbList | tr '\n' ' ') )
#rm ${base}/temp/adbList
#for (( i=4; i<${#ARRAY[@]}; i++ )); do
# temp="${ARRAY[$(($i+4))]}"
# if [ "$temp" == "" ]; then
# temp="unauthorised device"
# fi
# echo "${temp} serial: ${ARRAY[$i]}"
# i=$(($i+5))
#done
echo ""
echo "--- list of known devices ---"
knownDevices=( $(cat ${base}/devices.conf | tr '\n' ' ') )
for (( i=0; i<${#knownDevices[@]}; i++ )); do
temp2=$(($i+2))
echo "name: ${knownDevices[i]} serial: ${knownDevices[$temp2]}"
i=$(($i+2))
done
echo ""
echo "----------------------------------------------------------"
echo ""
fi