2017-07-03 20:50:37 +01:00
#!/bin/bash
2018-08-11 18:38:47 +01:00
types = ( $TYPES )
echo " Available types: ${ types [@] } "
echo " Selected type of deployment: $TYPE , Template file: $TEMPLATE "
2018-05-29 01:04:30 +01:00
function prepare_geny_cloud( ) {
2018-08-11 18:38:47 +01:00
contents = $( cat $TEMPLATE )
2018-06-22 00:16:40 +01:00
2019-06-07 08:28:48 +01:00
# LogIn
echo "Log In"
gmsaas auth login " ${ USER } " " ${ PASS } "
2018-05-29 01:04:30 +01:00
# Start device(s)
2019-06-07 08:28:48 +01:00
created_instances = ( )
2018-05-29 01:04:30 +01:00
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' )
2018-06-19 00:20:15 +01:00
port = $( get_value '.port' )
2019-06-07 08:28:48 +01:00
2019-11-20 14:29:41 +00:00
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
2019-06-07 08:28:48 +01:00
echo " Instance-ID: \" $instance_uuid \" "
created_instances += ( " ${ instance_uuid } " )
2018-06-19 00:20:15 +01:00
if [ [ $port != null ] ] ; then
2019-06-07 08:28:48 +01:00
echo " Connect device on port \" $port \"... "
gmsaas instances adbconnect " ${ instance_uuid } " --adb-serial-port " ${ port } "
2018-06-19 00:20:15 +01:00
else
2019-06-07 08:28:48 +01:00
echo "Connect device on port..."
gmsaas instances adbconnect " ${ instance_uuid } "
2018-06-19 00:20:15 +01:00
fi
2018-05-29 01:04:30 +01:00
done
2019-06-07 08:28:48 +01:00
# Store created instances in a file
echo " All created instances: ${ created_instances [@] } "
echo " ${ created_instances [@] } " > " ${ INSTANCES_PATH } "
2018-05-29 01:04:30 +01:00
}
2018-08-11 18:38:47 +01:00
function prepare_geny_aws( ) {
contents = $( cat $TEMPLATE )
# Creating aws tf file(s)
echo "Creating tf file(s)"
index = 1
2018-08-30 12:35:03 +01:00
port = 5555
2018-08-11 18:38:47 +01:00
for row in $( echo " ${ contents } " | jq -r '.[] | @base64' ) ; do
get_value( ) {
echo ${ row } | base64 --decode | jq -r ${ 1 }
}
2018-11-14 12:56:41 +00:00
region = $( get_value '.region' )
android_version = $( get_value '.android_version' )
instance = $( get_value '.instance' )
ami = $( get_value '.AMI' )
sg = $( get_value '.SG' )
2019-08-08 22:49:37 +01:00
subnet_id = $( get_value '.subnet_id' )
if [ [ $subnet_id = = null ] ] ; then
subnet_id = ""
fi
2018-11-14 12:56:41 +00:00
echo $region
echo $android_version
echo $instance
echo $ami
echo $sg
2019-08-08 22:49:37 +01:00
echo $subnet_id
2018-08-11 18:38:47 +01:00
2018-11-19 12:01:18 +00:00
#TODO: remove this dirty hack
2018-11-14 12:56:41 +00:00
if [ [ $android_version = = null ] ] ; then
2018-11-08 11:54:20 +00:00
echo "[HACK] Version cannot be empty! version will be added!"
android_version = "6.0"
fi
2018-11-14 12:56:41 +00:00
#Custom Security Group
if [ [ $sg != null ] ] ; then
echo "Custom security group is found!"
security_group = ""
2018-08-11 18:38:47 +01:00
2018-11-19 12:01:18 +00:00
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 }
}
2018-08-11 18:38:47 +01:00
2018-11-19 12:01:18 +00:00
type = $( get_value '.type' )
configs = $( get_value '.configurations' )
2018-08-11 18:38:47 +01:00
2018-11-19 12:01:18 +00:00
for c in $( echo " ${ configs } " | jq -r '.[] | @base64' ) ; do
get_value( ) {
echo ${ c } | base64 --decode | jq -r ${ 1 }
}
2018-11-14 12:56:41 +00:00
2018-11-19 12:01:18 +00:00
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
2018-11-14 12:56:41 +00:00
$type {
from_port = $from_port
to_port = $to_port
protocol = " $protocol "
cidr_blocks = [ " $cidr_blocks " ]
}
_EOF
2018-11-19 12:01:18 +00:00
)
done
2018-11-14 12:56:41 +00:00
done
2018-11-19 12:01:18 +00:00
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
2018-11-14 12:56:41 +00:00
else
echo "Custom security is not found! It will use default security group!"
security_group = $( cat <<_EOF
2018-08-11 18:38:47 +01:00
ingress {
2018-09-26 16:38:28 +01:00
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" ]
2018-08-11 18:38:47 +01:00
}
2018-11-05 13:49:59 +00:00
egress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = [ "0.0.0.0/0" ]
}
2018-11-14 12:56:41 +00:00
_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 "
}
2019-08-08 22:49:37 +01:00
variable " subnet_id_ $index " {
type = "string"
default = " $subnet_id "
}
2018-11-14 12:56:41 +00:00
provider "aws" {
alias = " provider_ $index "
region = " \${var.aws_region_ $index } "
}
resource "aws_security_group" " geny_sg_ $index " {
provider = " aws.provider_ $index "
$security_group
2018-08-20 10:46:09 +01:00
}
data "aws_ami" " geny_aws_ $index " {
provider = " aws.provider_ $index "
most_recent = true
filter {
name = "name"
values = [ " genymotion-ami-\${var.android_version_ $index }-* " ]
}
2018-08-21 08:49:10 +01:00
owners = [ "679593333241" ] #Genymotion
2018-08-11 18:38:47 +01:00
}
2018-08-24 15:14:04 +01:00
resource "aws_key_pair" " geny_key_ $index " {
provider = " aws.provider_ $index "
2018-08-27 13:30:51 +01:00
public_key = "\${file(" ~/.ssh/id_rsa.pub")}"
2018-08-24 15:14:04 +01:00
}
2018-08-11 18:38:47 +01:00
resource "aws_instance" " geny_aws_ $index " {
provider = " aws.provider_ $index "
2018-11-07 15:29:14 +00:00
ami = " \${data.aws_ami.geny_aws_ $index .id} "
2018-08-11 18:38:47 +01:00
instance_type = " \${var.instance_type_ $index } "
2019-08-08 22:49:37 +01:00
subnet_id = " \${var.subnet_id_ $index } "
2018-11-19 12:01:18 +00:00
vpc_security_group_ids = [ " \${aws_security_group.geny_sg_ $index .name} " ]
2018-08-24 15:14:04 +01:00
key_name = " \${aws_key_pair.geny_key_ $index .key_name} "
2018-08-11 18:38:47 +01:00
tags {
2018-09-21 12:57:31 +01:00
Name = " DockerAndroid-\${data.aws_ami.geny_aws_ $index .id} "
2018-08-11 18:38:47 +01:00
}
count = 1
2018-08-29 12:02:44 +01:00
provisioner "remote-exec" {
connection {
type = "ssh"
user = "shell"
private_key = "\${file(" ~/.ssh/id_rsa")}"
}
script = "/root/enable_adb.sh"
}
2018-08-11 18:38:47 +01:00
}
output " public_dns_ $index " {
2018-08-30 12:36:53 +01:00
value = " \${aws_instance.geny_aws_ $index .public_dns} "
2018-08-11 18:38:47 +01:00
}
_EOF
)
echo " $aws_tf_content " > /root/aws_tf_$index .tf
2018-11-07 15:29:14 +00:00
if [ [ $ami != null ] ] ; then
2018-11-19 12:01:18 +00:00
echo "Using given AMI!"
sed -i " s/.*ami=.*/ ami=\" $ami \"/g " /root/aws_tf_$index .tf
2018-11-07 15:29:14 +00:00
else
echo "Custom AMI is not found. It will use the latest AMI!"
fi
2018-11-19 12:01:18 +00:00
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
2018-11-14 12:56:41 +00:00
echo "---------------------------------------------------------"
2018-11-07 15:29:14 +00:00
2018-08-11 18:38:47 +01:00
( ( index++) )
2018-08-30 12:35:03 +01:00
( ( port++) )
2018-08-11 18:38:47 +01:00
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
2018-08-24 15:14:04 +01:00
2018-08-30 12:35:03 +01:00
# 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 )
2018-11-22 11:17:17 +00:00
( ( 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 } ) &
2018-08-30 12:35:03 +01:00
( ( 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"
2018-08-11 18:38:47 +01:00
}
2018-05-29 01:04:30 +01:00
function run_appium( ) {
echo "Preparing appium-server..."
CMD = " appium --log $APPIUM_LOG "
2018-06-20 22:30:54 +01:00
if [ " $CONNECT_TO_GRID " = true ] ; then
2018-05-29 01:04:30 +01:00
NODE_CONFIG_JSON = "/root/src/nodeconfig.json"
/root/generate_config.sh $NODE_CONFIG_JSON
CMD += " --nodeconfig $NODE_CONFIG_JSON "
fi
2018-06-20 22:30:54 +01:00
if [ " $RELAXED_SECURITY " = true ] ; then
CMD += " --relaxed-security"
fi
2019-08-14 11:28:04 +01:00
echo "Preparation is done"
TERM = " xterm -T AppiumServer -n AppiumServer -e $CMD "
$TERM
2018-05-29 01:04:30 +01:00
}
2018-10-26 14:00:46 +01:00
function ga( ) {
if [ " $GA " = true ] ; then
echo "Collecting data for improving the project"
2018-11-05 13:28:36 +00:00
description = " PROCESSOR: ${ SYS_IMG } ; VERSION: ${ ANDROID_VERSION } ; DEVICE: ${ DEVICE } ; APPIUM: ${ APPIUM } ; SELENIUM: ${ CONNECT_TO_GRID } ; MOBILE_TEST: ${ MOBILE_WEB_TEST } "
2018-10-26 14:00:46 +01:00
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
}
2019-03-01 11:46:50 +00:00
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
}
2018-10-26 14:54:04 +01:00
ga
2019-03-01 11:46:50 +00:00
saltstack
2018-06-22 00:16:40 +01:00
if [ " $REAL_DEVICE " = true ] ; then
2018-06-12 00:05:35 +01:00
echo "Using real device"
2018-05-29 01:04:30 +01:00
run_appium
2018-06-22 00:16:40 +01:00
elif [ " $GENYMOTION " = true ] ; then
2018-06-12 00:05:35 +01:00
echo "Using Genymotion"
2018-08-11 18:38:47 +01:00
echo " ${ types [@] } "
case $TYPE in
2018-09-04 09:34:49 +01:00
" ${ types [0] } " )
2018-11-29 08:06:17 +00:00
echo "Using Genymotion-Cloud (SaaS)"
2018-08-11 18:38:47 +01:00
prepare_geny_cloud
run_appium
2018-09-04 09:34:49 +01:00
; ;
" ${ types [1] } " )
echo "Using Genymotion-AWS"
prepare_geny_aws
run_appium
; ;
esac
2017-07-03 20:50:37 +01:00
else
2018-06-12 00:05:35 +01:00
echo "Using Emulator"
2018-05-29 01:04:30 +01:00
python3 -m src.app
2017-07-03 20:50:37 +01:00
fi