-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathandroid-stf-api.py
360 lines (284 loc) · 15.1 KB
/
android-stf-api.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#! /usr/bin/env python3
import getopt
import json
import os
import requests
import subprocess
import sys
from pathlib import Path
def main(argv):
global returned_data, response
SERVER_URL = 'http://stf:7100'
ADB_PATH = '/usr/local/android-sdk-linux/platform-tools/adb'
# use this one to debug on Win10/Cygwin '/cygdrive/c/ProgramData/chocolatey/bin/adb'
ADB_KEY_LOCATION = "/home/jenkins/.android/adbkey.pub" \
# use this one to debug on Win10/Cygwin /cygdrive/c/Users/youruser/.android/adbkey.pub"
STF_API_TOKEN = ''
ANDROID_VERSION = ''
CMD = ''
if ((len(sys.argv) != 6)):
print("DeviceFarmer STF script argument list is not long enough, please have look at usage")
print("Usage: android-stf-test.py --token <token> --version <android_version> connect/disconnect")
sys.exit(1)
try:
opts, args = getopt.getopt(argv, "token:version:connect:disconnect",
["token=", "version=", "connect", "disconnect"])
except getopt.GetoptError:
print("Usage: android-stf-test.py --token <token> --version <android_version> connect/disconnect")
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print("help: android-stf-test.py --token <token> --version <android_version> --connect <true/false>")
sys.exit(0)
elif opt in ("-token", "--token"):
STF_API_TOKEN = arg
elif opt in ("-version", "--version"):
ANDROID_VERSION = arg
elif opt in ("-connect", "--connect"):
CMD = arg
CMD = sys.argv[5]
print(
"-> Triggered command line arguments STF_API_TOKEN: " + STF_API_TOKEN + " - ANDROID_VERSION: " + ANDROID_VERSION + " - CMD: " + CMD + "\n")
if (str(CMD) == "connect"):
url = SERVER_URL + "/api/v1/devices"
token = str(STF_API_TOKEN)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
print("-> Searching for corresponding DeviceFarmer STF devices with version " + ANDROID_VERSION + "...")
try:
response = requests.get(url, headers=headers)
returned_data = json.loads(response.text)
except requests.exceptions.ConnectTimeout as err:
print('ERROR: Connection timeout occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
except requests.exceptions.HTTPError as err:
print('ERROR: Server error occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
STF_DEVICE_SERIAL = ''
for row in returned_data['devices']:
try:
if (row['using'] == False) and (row['present'] == True):
version = row['version'];
if version == str(ANDROID_VERSION):
STF_DEVICE_SERIAL = row['serial']
break;
except KeyError:
print(" WARNING: One device doesn't reference a 'version', skipping...")
if (str(STF_DEVICE_SERIAL) == str()):
print("ERROR: Did not find any currently free device matching with the android version " + ANDROID_VERSION)
exit(0)
else:
print(
" SUCCESS: Found a device corresponding to the android version " + ANDROID_VERSION + " with serial " + STF_DEVICE_SERIAL + "\n")
print("-> Sending our local ADB key, just in case...")
url = SERVER_URL + "/api/v1/user/adbPublicKeys"
token = str(STF_API_TOKEN)
adbKeyLocation = str(ADB_KEY_LOCATION)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
adbKey = {"publickey": Path(adbKeyLocation).read_text()}
data = json.dumps(adbKey)
try:
response = requests.post(url, data=data, headers=headers)
if response.status_code != 200:
print("ERROR : code: " + str(response.status_code))
content = json.loads(response.text)
print("-> Description: " + content['description'])
exit(1)
except requests.exceptions.ConnectTimeout:
print('ERROR: Connection timeout occurred. Please retry later')
print('--> Details : {content}'.format(content=err.response.content))
exit(1)
except requests.exceptions.HTTPError as err:
print('ERROR: Server error occurred. Please retry later')
print('--> Details : {content}'.format(content=err.response.content))
exit(1)
answer = json.loads(response.text)
if answer['success'] == False:
print("ERROR: DeviceFarmer STF service error.")
remote = json.loads(str(answer.val))
DESCR = remote['description']
print("-> Details: " + DESCR)
exit(1)
else:
print(" SUCCESS: Key with path " + adbKeyLocation + " got imported" + "\n")
print("-> Booking the device with serial: " + STF_DEVICE_SERIAL)
url = SERVER_URL + "/api/v1/user/devices"
token = str(STF_API_TOKEN)
serial = str(STF_DEVICE_SERIAL)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
serialNo = {"serial": serial}
data = json.dumps(serialNo)
try:
response = requests.post(url, data=data, headers=headers)
if response.status_code != 200:
print("ERROR : code: " + str(response.status_code))
content = json.loads(response.text)
print("-> Description: " + content['description'])
exit(1)
except requests.exceptions.ConnectTimeout:
print('ERROR: Connection timeout occurred. Please retry later')
print('--> Details : {content}'.format(content=err.response.content))
exit(1)
except requests.exceptions.HTTPError as err:
print('ERROR: Server error occurred. Please retry later')
print('--> Details : {content}'.format(content=err.response.content))
exit(1)
answer = json.loads(response.text)
if (answer['success'] == False):
print("ERROR: DeviceFarmer STF service error.")
remote = json.loads(str(answer.val))
DESCR = remote['description']
print("-> Details: " + DESCR)
exit(1)
else:
print(" SUCCESS: Device with serial " + STF_DEVICE_SERIAL + " is reserved" + "\n")
print("-> Retrieving device details...")
serial = str(STF_DEVICE_SERIAL)
url = SERVER_URL + "/api/v1/user/devices/" + serial
token = str(STF_API_TOKEN)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
try:
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("ERROR : code: " + str(response.status_code))
content = json.loads(response.text)
print("-> Description: " + content['description'])
exit(1)
except requests.exceptions.ConnectTimeout:
print('ERROR: Connection timeout occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
except requests.exceptions.HTTPError as err:
print('ERROR: Server error occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
if response.status_code == 200:
INFO = json.loads(response.text)
MANUFACTURER = INFO['device']['manufacturer']
MODEL = INFO['device']['model']
VERSION = INFO['device']['version']
ABI = INFO['device']['abi']
REMOTE_URL = INFO['device']['remoteConnectUrl']
print(" SUCCESS: details of the device, MANUFACTURER:" + str(MANUFACTURER) + " - MODEL:" + str(
MODEL) + " - VERSION:" + str(VERSION) + " - ABI:" + str(ABI) + "\n")
print("-> Searching for the ADB connection for the device...")
serial = str(STF_DEVICE_SERIAL)
url = SERVER_URL + "/api/v1/user/devices/" + serial + "/remoteConnect"
token = str(STF_API_TOKEN)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
serialNo = {"serial": serial}
data = json.dumps(serialNo)
try:
response = requests.post(url, data=data, headers=headers)
if response.status_code != 200:
print("ERROR : code: " + str(response.status_code))
content = json.loads(response.text)
print("-> Description: " + content['description'])
exit(1)
except requests.exceptions.ConnectTimeout:
print('ERROR: Connection timeout occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
except requests.exceptions.HTTPError as err:
print('ERROR: Server error occurred. Please retry later')
print('-> Details : {content}'.format(content=err.response.content))
exit(1)
RESPONSE = json.loads(response.text)
STATUS = RESPONSE['success']
STF_ADB_ADDRESS = ''
if (str(STATUS) == "True"):
STF_ADB_ADDRESS = RESPONSE['remoteConnectUrl']
print(" SUCCESS : ADB remoteConnectUrl is reached (" + str(STF_ADB_ADDRESS) + ")" + "\n")
try:
# print ("-> Killing ADB server... ")
# killadb_status = os.system(ADB_PATH+" kill-server > /dev/null ")
# print ("-> Starting ADB server... ")
# startadb_status = os.system(ADB_PATH+" start-server > /dev/null ")
# if (killadb_status == 0 and startadb_status == 0):
# print (" SUCCESS: ADB successfully restarted "+"\n")
# print("-> Downloading granted adb keys ADB device (" + str(STF_ADB_ADDRESS) + ")...")
# adbPrivKeyDownload = os.system(
# "curl -s -o /home/jenkins/.android/adbkey https://kazan.atosworldline.com/share/data/technical-user-kazan-mobile/docker/ressources/adbkey")
# adbPrubKeyDownload = os.system(
# "curl -s -o /home/jenkins/.android/adbkey.pub https://kazan.atosworldline.com/share/data/technical-user-kazan-mobile/docker/ressources/adbkey.pub")
# if (adbPrivKeyDownload == 0 and adbPrubKeyDownload == 0):
# print(" SUCCESS: Download of adbkey and abkey.pub " + "\n")
# else:
# print("ERROR: download ADB keys from kazan mobile sharedspace " + STF_ADB_ADDRESS)
# exit(1)
print("-> Connecting to ADB device (" + str(STF_ADB_ADDRESS) + ")...")
result = os.system(ADB_PATH + " -e connect " + str(STF_ADB_ADDRESS) + " > /dev/null ")
if (result == 0):
print(" SUCCESS: Connecting ADB device with address: (" + str(STF_ADB_ADDRESS) + ") " + "\n")
else:
print("ERROR: connecting to ADB for device with address: " + STF_ADB_ADDRESS)
exit(1)
except subprocess.CalledProcessError as ex:
print("ERROR: connecting to ADB for device with address: " + STF_ADB_ADDRESS)
exit(1)
else:
print("ERROR: ADB information request failed")
remote = json.loads(str(RESPONSE.val))
DESCR = remote['description']
print(DESCR)
exit(1)
if (str(CMD) == "disconnect"):
print("--> Searching for serial numbers of devices used by you from DeviceFarmer STF...")
url = SERVER_URL + "/api/v1/user/devices/"
token = str(STF_API_TOKEN)
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
try:
response = requests.get(url, headers=headers)
except requests.exceptions.ConnectTimeout:
print('Oops. Connection timeout occurred, please try again later!')
except requests.exceptions.HTTPError as err:
print('Oops. HTTP Error occurred, please try again later')
print('Response is: {content}'.format(content=err.response.content))
returned_data = json.loads(response.text)
try:
for row in returned_data['devices']:
STF_DEVICE_SERIAL = row['serial']
if not STF_DEVICE_SERIAL:
print("ERROR: Did not find any serial number of the device used by you, failed to disconnect")
exit(0)
print(" SUCCESS : Found device linked with serial number: " + STF_DEVICE_SERIAL)
print("\n" + "-> Disconnecting device ADB session from DeviceFarmer STF...")
url = SERVER_URL + "/api/v1/user/devices/" + STF_DEVICE_SERIAL + "/remoteConnect"
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
response = requests.delete(url, headers=headers)
RESPONSE = json.loads(response.text)
STATUS = RESPONSE['success']
if str(STATUS) == "True":
print(" SUCCESS : The device ADB session is removed")
DESC = RESPONSE['description']
else:
DESC = RESPONSE['description']
print("ERROR: while disconnecting device ADB session with serial :" + STF_DEVICE_SERIAL + DESC)
print("error details - " + DESC)
exit(1)
print("\n" + "-> Unreserved the device by user session...")
url = SERVER_URL + "/api/v1/user/devices/" + STF_DEVICE_SERIAL
headers = {'Content-type': 'application/json', 'Authorization': "Bearer " + token}
response = requests.delete(url, headers=headers)
RESPONSE = json.loads(response.text)
STATUS = RESPONSE['success']
if str(STATUS) == "True":
DESC = RESPONSE['description']
print(" SUCCESS : User device is unreserved" + "\n")
killserver = os.system(ADB_PATH + " kill-server > /dev/null")
exit(0)
else:
DESC = RESPONSE['description']
print("ERROR: User device failed to be unreserved ")
print("error details - " + DESC + "\n")
exit(1)
except requests.exceptions.ReadTimeout:
print('Oops. Connection ReadTimeout occurred, try again later!')
except requests.exceptions.HTTPError as err:
print('Oops. HTTP Error occurred, try again later')
print('Response is: {content}'.format(content=err.response.content))
print("ERROR: Did not find any devices for disconnect...")
exit(0)
if __name__ == "__main__":
main(sys.argv[1:])