~fginther/ubuntu-test-cases/document-rtm-mp-testing

« back to all changes in this revision

Viewing changes to scripts/provision.sh

  • Committer: Leo Arias
  • Date: 2014-11-10 19:28:56 UTC
  • mfrom: (345 touch)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: leo.arias@canonical.com-20141110192856-rgpksx9n9j0b39yl
Merged with the touch branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
## This is the script jenkins should run to provision a device in the lab
 
4
 
 
5
set -e
 
6
 
 
7
BASEDIR=$(dirname $(readlink -f $0))
 
8
export PATH=${BASEDIR}/../utils/host:${PATH}
 
9
 
 
10
RESDIR=`pwd`/clientlogs
 
11
 
 
12
NETWORK_FILE="${NETWORK_FILE-${HOME}/.ubuntu-ci/wifi.conf}"
 
13
 
 
14
IMAGE_OPT="${IMAGE_OPT---bootstrap --developer-mode --channel ubuntu-touch/devel-proposed}"
 
15
UUID="${UUID-$(uuidgen -r)}"
 
16
PHABLET_PASSWORD="${PHABLET_PASSWORD-0000}"
 
17
 
 
18
usage() {
 
19
cat <<EOF
 
20
usage: $0 [-s ANDROID_SERIAL] [-n NETWORK_FILE] [-P ppa] [-p package] [-r revision] [-w]
 
21
 
 
22
Provisions the given device with the latest build
 
23
 
 
24
OPTIONS:
 
25
  -h    Show this message
 
26
  -s    Specify the serial of the device to install
 
27
  -n    Select network file
 
28
  -P    add the ppa to the target (can be repeated)
 
29
  -D    add a debian package dir to the target (can be repeated)
 
30
  -p    add the package to the target (can be repeated)
 
31
  -r    Specify the image revision to flash
 
32
  -w    make the system writeable (implied with -p and -P arguments)
 
33
 
 
34
EOF
 
35
}
 
36
 
 
37
image_info() {
 
38
        # mark the version we installed in /home/phablet/.ci-[uuid,flash-args]
 
39
        # adb shell messes up \n's with \r\n's so do the whole of the regex on the target
 
40
        IMAGEVER=$(adb shell "sudo system-image-cli -i | sed -n -e 's/version version: \([0-9]*\)/\1/p' -e 's/version ubuntu: \([0-9]*\)/\1/p' -e 's/version device: \([0-9]*\)/\1/p' | paste -s -d:")
 
41
        CHAN=$(adb shell "sudo system-image-cli -i | sed -n -e 's/channel: \(.*\)/\1/p' | paste -s -d:")
 
42
        REV=$(echo $IMAGEVER | cut -d: -f1)
 
43
        echo "$IMAGE_OPT" | grep -q "\-\-revision" || IMAGE_OPT="${IMAGE_OPT} --revision $REV"
 
44
        echo "$IMAGE_OPT" | grep -q "\-\-channel" || IMAGE_OPT="${IMAGE_OPT} --channel $CHAN"
 
45
        adb shell "echo '${IMAGEVER}' > /home/phablet/.ci-version"
 
46
        echo $UUID > $RESDIR/.ci-uuid
 
47
        adb push $RESDIR/.ci-uuid /home/phablet/
 
48
        cat > $RESDIR/.ci-flash-args <<EOF
 
49
$IMAGE_OPT
 
50
EOF
 
51
        adb push $RESDIR/.ci-flash-args /home/phablet/.ci-flash-args
 
52
        echo $CUSTOMIZE > $RESDIR/.ci-customizations
 
53
        adb push $RESDIR/.ci-customizations /home/phablet/.ci-customizations
 
54
}
 
55
 
 
56
log() {
 
57
        echo = $(date): $*
 
58
}
 
59
 
 
60
set_hwclock() {
 
61
        log "SETTING HWCLOCK TO CURRENT TIME"
 
62
        # Use ip for ntp.ubuntu.com in case resolving doesn't work yet
 
63
        adb-shell sudo ntpdate 91.189.94.4 || log "WARNING: could not set ntpdate"
 
64
        # hwclock sync has to happen after we set writable image
 
65
        adb-shell sudo hwclock -w || log "WARNING: could not sync hwclock"
 
66
        log "Current date on device is:"
 
67
        adb shell date
 
68
        log "Current hwclock on device is:"
 
69
        adb shell sudo hwclock
 
70
}
 
71
 
 
72
retry() {
 
73
        timeout=$1
 
74
        shift
 
75
        loops=$1
 
76
        shift
 
77
        cmd=$*
 
78
        loopcnt=0
 
79
        while true; do
 
80
                $cmd && break || {
 
81
                        if [ $loopcnt -lt $loops ] ; then
 
82
                                loopcnt=$[$loopcnt+1]
 
83
                                echo "Retry [$loopcnt/$loops] after $timeout seconds..."
 
84
                                sleep $timeout
 
85
                        else
 
86
                                echo Failed on \'$cmd\' after $loops retries
 
87
                                exit 1
 
88
                        fi
 
89
                }
 
90
        done
 
91
}
 
92
 
 
93
reboot_bootloader() {
 
94
        # In CI, we've seen cases whre 'adb reboot bootloader' will just
 
95
        # reboot the device and not enter the bootloader. Adding another
 
96
        # reboot and retrying was found to be a successful workaround:
 
97
        # https://bugs.launchpad.net/ubuntu/+source/android-tools/+bug/1359488
 
98
        #
 
99
        # We only want to do this if we know ANDROID_SERIAL. Attempting
 
100
        # to guess might end up flashing the wrong device.
 
101
 
 
102
        log "Attempting adb reboot bootloader"
 
103
        adb reboot bootloader
 
104
        if [ -n "${ANDROID_SERIAL}" ] ; then
 
105
                # Entering the bootloader should take < 10 seconds, add some
 
106
                # padding for device variance.
 
107
                sleep 30
 
108
                if ! fastboot devices | grep -q "${ANDROID_SERIAL}"; then
 
109
                        log "Device not in fastboot after adb reboot bootloader"
 
110
                        # After a failed 'reboot bootloader' attempt, a reboot
 
111
                        # is used to get the device back to a saner state.
 
112
                        adb reboot
 
113
                        return 1
 
114
                fi
 
115
        fi
 
116
        return 0
 
117
}
 
118
 
 
119
full_flash() {
 
120
        log "FLASHING DEVICE"
 
121
        # Use a 60 second retry loop for reboot_bootloader.
 
122
        # If the attempt failed, it may take nearly 60 seconds to complete
 
123
        # the reboot cycle to get the device back to a sane state.
 
124
        retry 60 3 reboot_bootloader
 
125
        # Use a 10 second retry loop for ubuntu-device-flash.
 
126
        # Most failures appear to be transient and work with an immediate
 
127
        # retry.
 
128
        retry 10 3 ubuntu-device-flash touch --password $PHABLET_PASSWORD $IMAGE_OPT
 
129
        adb wait-for-device
 
130
        sleep 60  #give the system a little time
 
131
}
 
132
 
 
133
while getopts i:s:n:P:D:p:r:wh opt; do
 
134
        case $opt in
 
135
        h)
 
136
                usage
 
137
                exit 0
 
138
                ;;
 
139
        n)
 
140
                NETWORK_FILE=$OPTARG
 
141
                ;;
 
142
        s)
 
143
                export ANDROID_SERIAL=$OPTARG
 
144
                ;;
 
145
        i)
 
146
                IMAGE_TYPE=$OPTARG
 
147
                ;;
 
148
        w)
 
149
                # making this a non-zero length string enables the logic
 
150
                CUSTOMIZE=" "
 
151
                ;;
 
152
        P)
 
153
                CUSTOMIZE="$CUSTOMIZE --ppa $OPTARG"
 
154
                ;;
 
155
        D)
 
156
                CUSTOMIZE="$CUSTOMIZE --package-dir $OPTARG"
 
157
                ;;
 
158
        p)
 
159
                CUSTOMIZE="$CUSTOMIZE -p $OPTARG"
 
160
                ;;
 
161
        r)
 
162
                IMAGE_OPT="$IMAGE_OPT --revision $OPTARG"
 
163
                ;;
 
164
 
 
165
        esac
 
166
done
 
167
 
 
168
if [ -z $ANDROID_SERIAL ] ; then
 
169
        # ensure we only have one device attached
 
170
        lines=$(adb devices | wc -l)
 
171
        if [ $lines -gt 3 ] ; then
 
172
                echo "ERROR: More than one device attached, please use -s option"
 
173
                echo
 
174
                usage
 
175
                exit 1
 
176
        fi
 
177
fi
 
178
 
 
179
if [ ! -f $NETWORK_FILE ] && [ -z $USE_EMULATOR ] ; then
 
180
        echo "ERROR: NETWORK_FILE, $NETWORK_FILE, not found"
 
181
        exit 1
 
182
fi
 
183
 
 
184
set -x
 
185
[ -d $RESDIR ] && rm -rf $RESDIR
 
186
mkdir -p $RESDIR
 
187
 
 
188
if [ -z $USE_EMULATOR ] ; then
 
189
        full_flash
 
190
else
 
191
        log "CREATING EMULATOR"
 
192
        ubuntu-emulator destroy --yes $ANDROID_SERIAL || true
 
193
        sudo ubuntu-emulator create $ANDROID_SERIAL $IMAGE_OPT
 
194
        ${BASEDIR}/reboot-and-wait
 
195
fi
 
196
 
 
197
if [ -z $USE_EMULATOR ] ; then
 
198
        log "SETTING UP WIFI"
 
199
        retry 60 5 adb-shell 'sudo -iu phablet env |grep UPSTART_SESSION=unix'
 
200
        retry 60 5 phablet-network -n $NETWORK_FILE
 
201
fi
 
202
 
 
203
phablet-config welcome-wizard --disable
 
204
 
 
205
if [ -n "$CUSTOMIZE" ] ; then
 
206
        log "CUSTOMIZING IMAGE"
 
207
        phablet-config writable-image -r ${PHABLET_PASSWORD} $CUSTOMIZE
 
208
fi
 
209
 
 
210
log "SETTING UP SUDO"
 
211
adb shell "echo ${PHABLET_PASSWORD} |sudo -S bash -c 'echo phablet ALL=\(ALL\) NOPASSWD: ALL > /etc/sudoers.d/phablet && chmod 600 /etc/sudoers.d/phablet'"
 
212
 
 
213
# FIXME: Can't do this through phablet-config for now because it needs auth
 
214
# phablet-config edges-intro --disable
 
215
adb shell "sudo dbus-send --system --print-reply --dest=org.freedesktop.Accounts /org/freedesktop/Accounts/User32011 org.freedesktop.DBus.Properties.Set string:com.canonical.unity.AccountsService string:demo-edges variant:boolean:false"
 
216
 
 
217
if [ -n "${SKIP_CLICK}" ]; then
 
218
        log "SKIPPING CLICK PACKAGE SETUP AS REQUESTED"
 
219
else
 
220
        log "SETTING UP CLICK PACKAGES"
 
221
        CLICK_TEST_OPTS=""
 
222
        channel_name=$(adb shell "sudo system-image-cli -i | sed -n -e 's/channel: \(.*\)/\1/p' | paste -s -d:")
 
223
        # Before running phablet-click-test setup, we need to make sure the
 
224
        # session is available
 
225
        retry 60 5 adb-shell 'sudo -iu phablet env |grep UPSTART_SESSION=unix'
 
226
 
 
227
        # FIXME: workaround for phablet-click-test-setup to pull the right sources
 
228
        if [[ $channel_name == *rtm* ]] ; then
 
229
                CLICK_TEST_OPTS="--distribution ubuntu-rtm --series 14.09"
 
230
        fi
 
231
        phablet-click-test-setup $CLICK_TEST_OPTS
 
232
fi
 
233
 
 
234
# get our target-based utilities into our PATH
 
235
adb push ${BASEDIR}/../utils/target /home/phablet/bin
 
236
 
 
237
image_info
 
238
 
 
239
set_hwclock