-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_vm_script.sh
More file actions
executable file
·193 lines (173 loc) · 6.69 KB
/
docker_vm_script.sh
File metadata and controls
executable file
·193 lines (173 loc) · 6.69 KB
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
#!/bin/bash
device_serial="localhost:5555"
suspend_time=${suspend_time:-20}
SECONDS=0
stop=$(($SECONDS+60*$suspend_time))
# For starting the virtual device with a premade container
start_snapshot_container () {
#/usr/bin/toolbox
#alias docker-compose='docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "/home/yurigblaise:/home/yurigblaise" -w="/home/yurigblaise" docker/compose:1.24.0'
#sed -i '/.*emulator_emulator.*/{n;N;N;d}' ./docker/docker-compose-build.yaml
#sed -i "s|emulator_emulator|blaiseyuri/emu_avd_snapshot_p_x86_64|" ./docker/docker-compose-build.yaml
#cd js/jwt-provider
#wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
#python get-pip.py --user
#python -m pip install -r requirements.txt
#cp jwt_secrets_pub.jwks ../docker/certs/jwt_secrets_pub.jwks
#docker-compose -f ./docker/docker-compose-build.yaml -f ./docker/development.yaml up -d
snapshot_image=$1
curl https://codeload.github.com/google/android-emulator-container-scripts/tar.gz/master | tar -xz --strip=2 "android-emulator-container-scripts-master/js"
sed -i '/.*emulator_emulator.*/{n;N;N;d}' ./docker/docker-compose-build.yaml
sed -i "s/emulator_emulator/${snapshot_image}/" ./docker/docker-compose-build.yaml
dir=$PWD
sudo su -l $USER << EOF
echo $PWD
sudo chmod 666 /var/run/docker.sock
docker-compose -f $dir/docker/docker-compose-build.yaml -f $dir/docker/development.yaml up -d
EOF
}
start_container () {
sudo su -l $USER << EOF
echo "STARTING NESTED DOCKER CONTAINER"
cd ~/android-emulator-container-scripts
sudo chmod 666 /var/run/docker.sock
docker-compose -f ~/android-emulator-container-scripts/js/docker/docker-compose-build.yaml -f ~/android-emulator-container-scripts/js/docker/development.yaml up -d
EOF
}
try_adb_connect () {
SECONDS=0
adb_connected=false
until [ "$SECONDS" -gt "$stop" ] || "$adb_connected"; do
echo "attempting to connect..."
echo "$((($stop-$SECONDS)/60)) Minutes till restart"
! adb connect $device_serial | grep "Connection refused"
result_code=$?
if [ "$result_code" -eq 1 ]; then
echo "Adb connection not successful, retying..."
sleep 5
else
adb_connected=true
echo "Adb connection successful"
fi
done
if [ $SECONDS -gt $stop ]; then
echo "try_adb_connect has timed out after $SECONDS seconds allotted time was $stop seconds"
echo "Shutting down..."
sudo shutdown -h now
fi
}
docker_push() {
default_id=$(get_docker_id);
container_name=$2
dockerhub_account=$1
docker login
docker commit $default_id $container_name
echo "Commit completed tagging image and pushing"
docker image tag emulator_emulator "${dockerhub_account}/${container_name}"
echo "Docker image "${dockerhub_account}/${container_name}" tagged now being pushed"
docker push ${dockerhub_account}/${container_name}
}
get_docker_id() {
local docker_name="${1:-emulator}"
local id;
until [ ${#id} -gt 0 ] ; do
sleep 5
id=$(docker ps -qf name=$docker_name)
done;
echo $id
}
wait_docker_health () {
default_id=$(get_docker_id);
containername="${1:-$default_id}" #get name if null provide id
sleep 1;
SECONDS=0
health_status=$(docker inspect -f {{.State.Health.Status}} $containername)
echo $health_status
until [ "`docker inspect -f {{.State.Health.Status}} $containername`"=="healthy" ]; do
sleep 1;
echo "Device status is $health_status"
health_status=$(docker inspect -f {{.State.Health.Status}} $containername)
echo $health_status
done;
}
# Save snapshot in container image
save_snapshot() {
device_serial=${1:-$device_serial}
default_id=$(get_docker_id)
echo "Connected to container, saving snapshot"
docker exec $default_id /android/sdk/platform-tools/adb emu avd snapshot save container_snapshot
docker exec $default_id /android/sdk/platform-tools/adb emu avd snapshot pull container_snapshot /home/container_snapshot
}
load_snapshot () {
device_serial=${1:-$device_serial}
default_id=$(get_docker_id)
echo "Connected to container, loading snapshot"
docker exec $default_id /android/sdk/platform-tools/adb emu avd snapshot push container_snapshot /home/container_snapshot
docker exec $default_id /android/sdk/platform-tools/adb emu avd snapshot load container_snapshot
docker exec $default_id /android/sdk/platform-tools/adb emu avd snapshot list
}
check_emu_idle () {
device_serial=${1:-$device_serial}
idle_time_found=false
SECONDS=0
#attempt to get the idle time via adb if not found after stop, shutdown VM
until [ $SECONDS -gt $stop ] || "$idle_time_found"; do
idle_time=$(adb -s $device_serial shell dumpsys power | grep -i "lastUserActivityTime=" | grep -i "ms" | grep -oP '(?<=\()[0-9]+')
result_code=$?
echo "Result code: $result_code idle_time: $idle_time(ms)"
if [ "$result_code" -eq "0" ]; then
echo "Loop should finish"
idle_time_found=true
else
sleep 30
fi
done
if [ $SECONDS -gt $stop ]; then
echo "the function check_emu_idle has timed out after $SECONDS seconds allotted time was $stop seconds"
echo "Saving a snapshot then shutting down."
save_snapshot $device_serial
echo "Snapshot saved, shutting down..."
sudo shutdown -h now
fi
idle_minutes=$(($idle_time/60000))
echo "idle time: $idle_minutes minutes"
if [ "$idle_minutes" -gt "$suspend_time" ]; then
echo "Maximum idle time of $suspend_time minutes has been reached."
echo "Saving a snapshot then shutting down."
save_snapshot $device_serial
echo "Snapshot saved, shutting down..."
sudo shutdown -h now
fi
}
#Function to check if the emulator is idle in intervals
shutdown_on_idle () {
sleep_time=$(($suspend_time*60))
while true; do
check_emu_idle
sleep $sleep_time
done
}
device_power_on () {
device_serial=${1:-$device_serial}
set -o pipefail
adb -s $device_serial shell dumpsys connectivity | sed -e '/[0-9] NetworkAgentInfo.*CONNECTED/p' -n;
while [ $? -ne 0 ]; do
sleep 5
echo "waiting on device to power on"
adb -s $device_serial shell dumpsys connectivity | sed -e '/[0-9] NetworkAgentInfo.*CONNECTED/p' -n;
done
echo "Device is now on"
}
# Check if the function exists (bash specific)
if typeset -f "$1" > /dev/null
then
# call arguments verbatim
"$@"
#if only one parameter is passed ignore
else
# Show a helpful error
echo "'$1' is not a known function name" >&2
exit 1
fi
# try_adb_connect
# check_emu_idle