forked from budtmo/docker-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappium.sh
376 lines (329 loc) · 9.23 KB
/
appium.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
types=($TYPES)
echo "Available types: ${types[@]}"
echo "Selected type of deployment: $TYPE, Template file: $TEMPLATE"
function prepare_geny_cloud() {
contents=$(cat $TEMPLATE)
# LogIn
echo "Log In"
gmsaas auth login "${USER}" "${PASS}"
# Start device(s)
created_instances=()
echo "Creating device(s) based on given json file..."
for row in $(echo "${contents}" | jq -r '.[] | @base64'); do
get_value() {
echo ${row} | base64 --decode | jq -r ${1}
}
template=$(get_value '.template')
device=$(get_value '.device')
port=$(get_value '.port')
if [[ $device != null ]]; then
echo "Starting \"$device\" with template name \"$template\"..."
instance_uuid=$(gmsaas instances start "${template}" "${device}")
else
echo "Starting Device with Random name..."
random_device_name=$(python3 -c 'import uuid; print(str(uuid.uuid4()).upper())')
instance_uuid=$(gmsaas instances start "${template}" "${random_device_name}")
fi
echo "Instance-ID: \"$instance_uuid\""
created_instances+=("${instance_uuid}")
if [[ $port != null ]]; then
echo "Connect device on port \"$port\"..."
gmsaas instances adbconnect "${instance_uuid}" --adb-serial-port "${port}"
else
echo "Connect device on port..."
gmsaas instances adbconnect "${instance_uuid}"
fi
done
# Store created instances in a file
echo "All created instances: ${created_instances[@]}"
echo "${created_instances[@]}" > "${INSTANCES_PATH}"
}
function prepare_geny_aws() {
contents=$(cat $TEMPLATE)
# Creating aws tf file(s)
echo "Creating tf file(s)"
index=1
port=5555
for row in $(echo "${contents}" | jq -r '.[] | @base64'); do
get_value() {
echo ${row} | base64 --decode | jq -r ${1}
}
region=$(get_value '.region')
android_version=$(get_value '.android_version')
instance=$(get_value '.instance')
ami=$(get_value '.AMI')
sg=$(get_value '.SG')
subnet_id=$(get_value '.subnet_id')
if [[ $subnet_id == null ]]; then
subnet_id=""
fi
echo $region
echo $android_version
echo $instance
echo $ami
echo $sg
echo $subnet_id
#TODO: remove this dirty hack
if [[ $android_version == null ]]; then
echo "[HACK] Version cannot be empty! version will be added!"
android_version="6.0"
fi
#Custom Security Group
if [[ $sg != null ]]; then
echo "Custom security group is found!"
security_group=""
is_array=$(echo "${sg}" | jq 'if type=="array" then true else false end')
if [ $is_array == "true" ]; then
echo "New security group with given rules will be created"
for i in $(echo "${sg}" | jq -r '.[] | @base64'); do
get_value() {
echo ${i} | base64 --decode | jq -r ${1}
}
type=$(get_value '.type')
configs=$(get_value '.configurations')
for c in $(echo "${configs}" | jq -r '.[] | @base64'); do
get_value() {
echo ${c} | base64 --decode | jq -r ${1}
}
from_port=$(get_value '.from_port')
to_port=$(get_value '.to_port')
protocol=$(get_value '.protocol')
cidr_blocks=$(get_value '.cidr_blocks')
security_group+=$(cat <<_EOF
$type {
from_port = $from_port
to_port = $to_port
protocol = "$protocol"
cidr_blocks = ["$cidr_blocks"]
}
_EOF
)
done
done
else
#TODO: remove this dirty hack
echo "Given security group will be used!"
is_array="false"
security_group=$(cat <<_EOF
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
_EOF
)
fi
else
echo "Custom security is not found! It will use default security group!"
security_group=$(cat <<_EOF
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 51000
to_port = 51100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 51000
to_port = 51100
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
_EOF
)
fi
aws_tf_content=$(cat <<_EOF
variable "aws_region_$index" {
type = "string"
default = "$region"
}
variable "android_version_$index" {
type = "string"
default = "$android_version"
}
variable "instance_type_$index" {
type = "string"
default = "$instance"
}
variable "subnet_id_$index" {
type = "string"
default = "$subnet_id"
}
provider "aws" {
alias = "provider_$index"
region = "\${var.aws_region_$index}"
}
resource "aws_security_group" "geny_sg_$index" {
provider = "aws.provider_$index"
$security_group
}
data "aws_ami" "geny_aws_$index" {
provider = "aws.provider_$index"
most_recent = true
filter {
name = "name"
values = ["genymotion-ami-\${var.android_version_$index}-*"]
}
owners = ["679593333241"] #Genymotion
}
resource "aws_key_pair" "geny_key_$index" {
provider = "aws.provider_$index"
public_key = "\${file("~/.ssh/id_rsa.pub")}"
}
resource "aws_instance" "geny_aws_$index" {
provider = "aws.provider_$index"
ami="\${data.aws_ami.geny_aws_$index.id}"
instance_type = "\${var.instance_type_$index}"
subnet_id = "\${var.subnet_id_$index}"
vpc_security_group_ids=["\${aws_security_group.geny_sg_$index.name}"]
key_name = "\${aws_key_pair.geny_key_$index.key_name}"
tags {
Name = "DockerAndroid-\${data.aws_ami.geny_aws_$index.id}"
}
count = 1
provisioner "remote-exec" {
connection {
type = "ssh"
user = "shell"
private_key = "\${file("~/.ssh/id_rsa")}"
}
script = "/root/enable_adb.sh"
}
}
output "public_dns_$index" {
value = "\${aws_instance.geny_aws_$index.public_dns}"
}
_EOF
)
echo "$aws_tf_content" > /root/aws_tf_$index.tf
if [[ $ami != null ]]; then
echo "Using given AMI!"
sed -i "s/.*ami=.*/ ami=\"$ami\"/g" /root/aws_tf_$index.tf
else
echo "Custom AMI is not found. It will use the latest AMI!"
fi
if [[ $sg != null ]] && [[ $is_array == "false" ]]; then
echo "Using given security group: $sg"
sed -i "s/.*vpc_security_group_ids=.*/ vpc_security_group_ids=[\"$sg\"]/g" /root/aws_tf_$index.tf
fi
echo "---------------------------------------------------------"
((index++))
((port++))
done
# Deploy EC2 instance(s)
echo "Deploy EC2 instance(s) on AWS with Genymotion image based on given json file..."
./terraform init
./terraform plan
./terraform apply -auto-approve
# Workaround to connect adb remotely because there is a issue by using local-exec
time_sleep=5
interval_sleep=1
echo "Connect to adb remotely"
for ((i=index;i>=1;i--)); do
dns=$(./terraform output public_dns_$i)
((sleep ${interval_sleep} && adb connect localhost:${port}) > /dev/null & ssh -i ~/.ssh/id_rsa -o ServerAliveInterval=60 -o StrictHostKeyChecking=no -q -NL ${port}:localhost:5555 shell@${dns}) &
((port--))
time_sleep=$((time_sleep+interval_sleep))
done
echo "It will wait for ${time_sleep} until all device(s) to be connected"
sleep ${time_sleep}
adb devices
echo "Process is completed"
}
function run_appium() {
echo "Preparing appium-server..."
CMD="appium --log $APPIUM_LOG"
if [ "$CONNECT_TO_GRID" = true ]; then
NODE_CONFIG_JSON="/root/src/nodeconfig.json"
/root/generate_config.sh $NODE_CONFIG_JSON
CMD+=" --nodeconfig $NODE_CONFIG_JSON"
fi
if [ "$RELAXED_SECURITY" = true ]; then
CMD+=" --relaxed-security"
fi
echo "Preparation is done"
TERM="xterm -T AppiumServer -n AppiumServer -e $CMD"
$TERM
}
function ga(){
if [ "$GA" = true ]; then
echo "Collecting data for improving the project"
description="PROCESSOR: ${SYS_IMG}; VERSION: ${ANDROID_VERSION}; DEVICE: ${DEVICE}; APPIUM: ${APPIUM}; SELENIUM: ${CONNECT_TO_GRID}; MOBILE_TEST: ${MOBILE_WEB_TEST}"
random_user=$(cat /proc/version 2>&1 | sed -e 's/ /_/g' | sed -e 's/[()]//g' | sed -e 's/@.*_gcc_version/_gcc/g' | sed -e 's/__/_/g' | sed -e 's/Linux_version_//g' | sed -e 's/generic_build/genb/g')
random_user="${APP_RELEASE_VERSION}_${random_user}"
payload=(
--data v=${GA_API_VERSION}
--data aip=1
--data tid="${GA_TRACKING_ID}"
--data cid="${random_user}"
--data t="event"
--data ec="${APP_TYPE}"
--data ea="${random_user}"
--data el="${description}"
--data an="docker-android"
--data av="${APP_RELEASE_VERSION}"
)
curl ${GA_ENDPOINT} "${payload[@]}" --silent
else
echo "Nothing to do"
fi
}
function saltstack(){
if [ ! -z "${SALT_MASTER}" ]; then
echo "ENV SALT_MASTER it not empty, salt-minion will be prepared"
echo "master: ${SALT_MASTER}" >> /etc/salt/minion
salt-minion &
echo "salt-minion is running..."
else
echo "SaltStack is disabled"
fi
}
ga
saltstack
if [ "$REAL_DEVICE" = true ]; then
echo "Using real device"
run_appium
elif [ "$GENYMOTION" = true ]; then
echo "Using Genymotion"
echo "${types[@]}"
case $TYPE in
"${types[0]}" )
echo "Using Genymotion-Cloud (SaaS)"
prepare_geny_cloud
run_appium
;;
"${types[1]}" )
echo "Using Genymotion-AWS"
prepare_geny_aws
run_appium
;;
esac
else
echo "Using Emulator"
python3 -m src.app
fi