~paulliu/unity8/lp1378469_MessageMenu

« back to all changes in this revision

Viewing changes to run_on_device

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
CODE_DIR=shell
 
3
USER=phablet
 
4
USER_ID=32011
 
5
PASSWORD=phablet
 
6
PACKAGE=unity8
 
7
BINARY=unity8
 
8
TARGET_IP=${TARGET_IP-127.0.0.1}
 
9
TARGET_SSH_PORT=${TARGET_SSH_PORT-2222}
 
10
TARGET_DEBUG_PORT=3768
 
11
RUN_OPTIONS=-qmljsdebugger=port:$TARGET_DEBUG_PORT
 
12
SETUP=false
 
13
GDB=false
 
14
SUDO="echo $PASSWORD | sudo -S"
 
15
NUM_JOBS='$(( `grep -c ^processor /proc/cpuinfo` + 1 ))'
 
16
 
 
17
usage() {
 
18
    echo "usage: run_on_device [OPTIONS]\n"
 
19
    echo "Script to setup a build environment for the shell and sync build and run it on the device\n"
 
20
    echo "OPTIONS:"
 
21
    echo "  -s, --setup   Setup the build environment"
 
22
    echo ""
 
23
    echo "IMPORTANT:"
 
24
    echo " * Make sure to have the networking and PPAs setup on the device beforehand (phablet-deploy-networking && phablet-ppa-fetch)."
 
25
    echo " * Execute that script from a directory containing a branch the shell code."
 
26
    exit 1
 
27
}
 
28
 
 
29
exec_with_ssh() {
 
30
    ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t $USER@$TARGET_IP -p $TARGET_SSH_PORT "bash -ic \"$@\""
 
31
}
 
32
 
 
33
exec_with_adb() {
 
34
    adb shell chroot /data/ubuntu /usr/bin/env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin "$@"
 
35
}
 
36
adb_root() {
 
37
    adb root
 
38
    adb wait-for-device
 
39
}
 
40
 
 
41
install_ssh_key() {
 
42
    ssh-keygen -R $TARGET_IP
 
43
    HOME_DIR=/data/ubuntu/home/phablet
 
44
    adb push ~/.ssh/id_rsa.pub $HOME_DIR/.ssh/authorized_keys
 
45
    adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh
 
46
    adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh/authorized_keys
 
47
    adb shell chmod 700 $HOME_DIR/.ssh
 
48
    adb shell chmod 600 $HOME_DIR/.ssh/authorized_keys
 
49
}
 
50
 
 
51
setup_adb_forwarding() {
 
52
    adb forward tcp:$TARGET_SSH_PORT tcp:22
 
53
    adb forward tcp:$TARGET_DEBUG_PORT tcp:$TARGET_DEBUG_PORT
 
54
}
 
55
 
 
56
install_dependencies() {
 
57
    exec_with_adb apt-get -y install openssh-server
 
58
    exec_with_ssh $SUDO apt-get -y install build-essential rsync bzr ccache gdb ninja-build
 
59
    exec_with_ssh $SUDO add-apt-repository -y ppa:canonical-qt5-edgers/qt5-proper
 
60
    exec_with_ssh $SUDO add-apt-repository -s -y ppa:phablet-team/ppa
 
61
    exec_with_ssh $SUDO apt-get update
 
62
    exec_with_ssh $SUDO apt-get -y build-dep $PACKAGE
 
63
}
 
64
 
 
65
sync_code() {
 
66
    bzr export --uncommitted --format=dir /tmp/$CODE_DIR
 
67
    rsync -crlOzv -e "ssh -p $TARGET_SSH_PORT -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" /tmp/$CODE_DIR/ $USER@$TARGET_IP:$CODE_DIR/
 
68
    rm -rf /tmp/$CODE_DIR
 
69
}
 
70
 
 
71
build() {
 
72
    exec_with_ssh PATH=/usr/lib/ccache:$PATH "cd $CODE_DIR/ && PATH=/usr/lib/ccache:$PATH ./build"
 
73
}
 
74
 
 
75
run() {
 
76
    SERVICEFILE="/data/ubuntu/etc/device-services"
 
77
    adb shell "grep $BINARY $SERVICEFILE"
 
78
    CONTAINS_SHELL=$?
 
79
    if [ $CONTAINS_SHELL -eq 0 ]; then
 
80
        echo "Modifying $SERVICEFILE on device. Old contents will be saved in $SERVICEFILE.backup"
 
81
        adb shell cp $SERVICEFILE $SERVICEFILE.backup
 
82
        adb shell sed -i /$BINARY/d $SERVICEFILE
 
83
        adb shell "chroot /data/ubuntu /usr/bin/service ubuntu-session restart"
 
84
    fi
 
85
    adb shell pkill $BINARY
 
86
    if $GDB; then
 
87
        exec_with_ssh "cd $CODE_DIR/ && ./run --gdb --nomousetouch -- $RUN_OPTIONS"
 
88
    else
 
89
        exec_with_ssh "cd $CODE_DIR && ./run --nomousetouch -- $RUN_OPTIONS"
 
90
    fi
 
91
    if [ $CONTAINS_SHELL -eq 0 ]; then
 
92
        echo "Restoring $SERVICEFILE on device from $SERVICEFILE.backup"
 
93
        adb shell cp $SERVICEFILE.backup $SERVICEFILE
 
94
        adb shell "chroot /data/ubuntu /usr/bin/service ubuntu-session restart"
 
95
    fi
 
96
}
 
97
 
 
98
set -- `getopt -n$0 -u -a --longoptions="setup,gdb,help" "sgh" "$@"`
 
99
 
 
100
# FIXME: giving incorrect arguments does not call usage and exit
 
101
while [ $# -gt 0 ]
 
102
do
 
103
    case "$1" in
 
104
       -s|--setup)   SETUP=true;;
 
105
       -g|--gdb)     GDB=true;;
 
106
       -h|--help)    usage;;
 
107
       --)           shift;break;;
 
108
    esac
 
109
    shift
 
110
done
 
111
 
 
112
 
 
113
adb_root
 
114
[ "${TARGET_IP}" = "127.0.0.1" ] && setup_adb_forwarding
 
115
 
 
116
if $SETUP; then
 
117
    echo "Setting up environment for building shell.."
 
118
    install_ssh_key
 
119
    install_dependencies
 
120
    sync_code
 
121
else
 
122
    echo "Transferring code.."
 
123
    sync_code
 
124
    echo "Building.."
 
125
    build
 
126
    echo "Running.."
 
127
    run
 
128
fi
 
129