Last active
August 29, 2015 14:06
-
-
Save almozavr/aed1a1d94773fb55b411 to your computer and use it in GitHub Desktop.
Start(resume)/stop virtual machine by serial number and connect/disconnect with adb. Could be useful to control remote CI Genymotion instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#=====EXAMPLE OF USAGE=============== | |
# $ ./scripts/gm.sh -r 526d4be6-9964-4a84-83fd-31af8029cf44 | |
# $ ./gradlew clean connectedAndroidTest | |
# $ ./scripts/gm.sh -s 526d4be6-9964-4a84-83fd-31af8029cf44 | |
#==================================== | |
#=========== | |
# VM helpers | |
function vm_get_ip_or_wait(){ | |
local ip="value"; # geustproperty value if vm is not connected | |
while [ $ip == "value" ] ; do | |
sleep 0.5; | |
ip=`VBoxManage guestproperty get $VM androvm_ip_management | awk '{print $2}'`; | |
done; | |
echo $ip | |
} | |
function vm_status(){ | |
VBoxManage list vms -l | grep $VM -A 100 | grep since | sed -e 's/\ \ //g' -e 's/\ (since.*$//g' | cut -d: -f2- | |
} | |
function vm_running(){ | |
if [[ $(vm_status_contains 'running') ]]; then | |
true | |
else | |
false | |
fi | |
} | |
function vm_paused(){ | |
if [[ $(vm_status_contains 'paused') ]]; then | |
true | |
else | |
false | |
fi | |
} | |
function vm_stopped(){ | |
if [[ $(vm_status_contains "powered off") ]]; then | |
true | |
elif [[ $(vm_status_contains 'aborted') ]]; then | |
true | |
else | |
false | |
fi | |
} | |
function vm_status_contains(){ | |
echo $(vm_status) | grep "$1" | |
} | |
#============== | |
# GM start/stop functions | |
function start_or_resume_gm(){ | |
echo "GM :: Trying to start or resume if needed" | |
echo "GM :: current status - $(vm_status)" | |
if vm_paused; then | |
echo "GM :: Trying to resume" | |
VBoxManage controlvm $VM resume | |
elif vm_stopped; then | |
echo "GM :: Trying to start..." | |
VBoxHeadless -s $VM -v off & | |
else | |
echo 'Noting to do - already running' | |
fi | |
while !(vm_running) ; do | |
sleep 1; | |
done | |
echo "GM final status :: $(vm_status)" | |
echo 'ADB :: Connecting...' | |
local ip=$(vm_get_ip_or_wait) | |
adb connect $ip | |
# sleep 5 | |
} | |
function pause_gm(){ | |
echo 'GM :: trying to pause if needed' | |
echo 'ADB :: Disconnecting...' | |
local ip=$(vm_get_ip_or_wait) | |
adb disconnect $ip | |
sleep 1 | |
echo "GM :: current status - $(vm_status)" | |
if !(vm_paused) ; then | |
echo "GM :: Trying to pause..." | |
`VBoxManage controlvm $VM pause` | |
echo "GM :: final status :: $(vm_status)" | |
else | |
echo 'Noting to do - already paused' | |
fi | |
} | |
########################## | |
# Entry point | |
while getopts r:s: flag; do | |
VM=${OPTARG}; | |
case "${flag}" in | |
r) start_or_resume_gm ;; | |
s) pause_gm ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment