chiark / gitweb /
Merge branch 'master' into 'master'
[fdroidserver.git] / docker / install_agent.py
1 #!/usr/bin/env python2
2
3 import os
4 from subprocess import call, check_output
5 from time import sleep
6
7 FNULL = open(os.devnull, 'w')
8
9 print("Ensuring device is online")
10 call("adb wait-for-device", shell=True)
11
12 print("Installing the drozer agent")
13 print("If the device just came online it is likely the package manager hasn't booted.")
14 print("Will try multiple attempts to install.")
15 print("This may need tweaking depending on hardware.")
16
17
18 attempts = 0
19 time_to_sleep = 30
20
21 while attempts < 8:
22     output = check_output('adb shell "pm list packages"', shell=True)
23     print("Checking whether the package manager is up...")
24     if "Could not access the Package Manager" in output:
25         print("Nope. Sleeping for 30 seconds and then trying again.")
26         sleep(time_to_sleep)
27     else:
28         break
29
30 time_to_sleep = 5
31 attempts = 0
32
33 while attempts < 5:
34     sleep(time_to_sleep)
35     try:
36         install_output = check_output("adb install /home/drozer/drozer-agent.apk", shell=True)
37     except Exception:
38         print("Failed. Trying again.")
39         attempts += 1
40     else:
41         attempts += 1
42         if "Error: Could not access the Package Manager" not in install_output:
43             break
44
45 print("Install attempted. Checking everything worked")
46
47 pm_list_output = check_output('adb shell "pm list packages"', shell=True)
48
49 if "com.mwr.dz" not in pm_list_output:
50     print(install_output)
51     exit("APK didn't install properly. Exiting.")
52
53 print("Installed ok.")
54
55 print("Starting the drozer agent main activity: com.mwr.dz/.activities.MainActivity")
56 call('adb shell "am start com.mwr.dz/.activities.MainActivity"', shell=True, stdout=FNULL)
57
58 print("Starting the service")
59 # start the service
60 call("python /home/drozer/enable_service.py", shell=True, stdout=FNULL)
61
62 print("Forward dem ports mon.")
63 call("adb forward tcp:31415 tcp:31415", shell=True, stdout=FNULL)