1#!/bin/bash
2#
3# Copyright 2016 Google Inc. All Rights Reserved.
4#
5# This is a generic ChromeOS package/image test setup script. It is meant to
6# be used for either the object file or package bisection tools. This script
7# does one of the following depending on what ${BISECT_MODE} is set to:
8#
9# 1) ${BISECT_MODE} is PACKAGE_MODE:
10#   build_image is called and generates a new ChromeOS image using whatever
11#   packages are currently in the build tree. This image is then pushed to the
12#   remote machine using flash over ethernet (or usb flash if ethernet flash
13#   fails).
14#
15# 2) ${BISECT_MODE} is OBJECT_MODE:
16#   emerge is called for ${BISECT_PACKAGE} and generates a build for said
17#   package. This package is then deployed to the remote machine and the machine
18#   is rebooted. If deploying fails then a new ChromeOS image is built from
19#   scratch and pushed to the machine like in PACKAGE_MODE.
20#
21# This script is intended to be used by binary_search_state.py, as
22# part of the binary search triage on ChromeOS objects and packages. It should
23# return '0' if the setup succeeds; and '1' if the setup fails (the image
24# could not build or be flashed).
25#
26
27export PYTHONUNBUFFERED=1
28
29source common/common.sh
30
31usb_flash()
32{
33  echo
34  echo "Insert a usb stick into the current machine"
35  echo "Note: The cros flash will take time and doesn't give much output."
36  echo "      Be patient. If your usb access light is flashing it's working."
37  sleep 1
38  read -p "Press enter to continue" notused
39
40  cros flash --board=${BISECT_BOARD} --clobber-stateful usb:// ~/trunk/src/build/images/${BISECT_BOARD}/latest/chromiumos_test_image.bin
41
42  echo
43  echo "Flash to usb complete!"
44  echo "Plug the usb into your chromebook and install the image."
45  echo "Refer to the ChromiumOS Developer's Handbook for more details."
46  echo "http://www.chromium.org/chromium-os/developer-guide#TOC-Boot-from-your-USB-disk"
47  while true; do
48    sleep 1
49    read -p "Was the installation of the image successful? " choice
50    case $choice in
51        [Yy]*) return 0;;
52        [Nn]*) return 1;;
53        *) echo "Please answer y or n.";;
54    esac
55  done
56}
57
58ethernet_flash()
59{
60  echo
61  echo "Please ensure your Chromebook is up and running Chrome so"
62  echo "cros flash may run."
63  echo "If your Chromebook has a broken image you can try:"
64  echo "1. Rebooting your Chromebook 6 times to install the last working image"
65  echo "2. Alternatively, running the following command on the Chromebook"
66  echo "   will also rollback to the last working image:"
67  echo "   'update_engine_client --rollback --nopowerwash --reboot'"
68  echo "3. Flashing a new image through USB"
69  echo
70  sleep 1
71  read -p $'Press enter to continue and retry the ethernet flash' notused
72  cros flash --board=${BISECT_BOARD} --clobber-stateful ${BISECT_REMOTE} ~/trunk/src/build/images/${BISECT_BOARD}/latest/chromiumos_test_image.bin
73}
74
75reboot()
76{
77  ret_val=0
78  pushd ~/trunk/src/scripts > /dev/null
79  set -- --remote=${BISECT_REMOTE}
80  . ./common.sh || ret_val=1
81  . ./remote_access.sh || ret_val=1
82  TMP=$(mktemp -d)
83  FLAGS "$@" || ret_val=1
84  remote_access_init || ret_val=1
85  remote_reboot || ret_val=1
86  popd > /dev/null
87
88  return $ret_val
89}
90
91echo
92echo "INSTALLATION BEGIN"
93echo
94
95if [[ "${BISECT_MODE}" == "OBJECT_MODE" ]]; then
96  echo "EMERGING ${BISECT_PACKAGE}"
97  CLEAN_DELAY=0 emerge-${BISECT_BOARD} -C ${BISECT_PACKAGE}
98  emerge-${BISECT_BOARD} ${BISECT_PACKAGE}
99  emerge_status=$?
100
101  if [[ ${emerge_status} -ne 0 ]] ; then
102    echo "emerging ${BISECT_PACKAGE} returned a non-zero status: $emerge_status"
103    exit 1
104  fi
105
106  echo
107  echo "DEPLOYING"
108  echo "cros deploy ${BISECT_REMOTE} ${BISECT_PACKAGE}"
109  cros deploy ${BISECT_REMOTE} ${BISECT_PACKAGE} --log-level=info
110  deploy_status=$?
111
112  if [[ ${deploy_status} -eq 0 ]] ; then
113    echo "Deploy successful. Rebooting device..."
114    reboot
115    if [[ $? -ne 0 ]] ; then
116      echo
117      echo "Could not automatically reboot device!"
118      read -p "Please manually reboot device and press enter to continue" notused
119    fi
120    exit 0
121  fi
122
123  echo "Deploy failed! Trying build_image/cros flash instead..."
124  echo
125fi
126
127echo "BUILDING IMAGE"
128pushd ~/trunk/src/scripts
129./build_image test --board=${BISECT_BOARD} --noenable_rootfs_verification --noeclean
130build_status=$?
131popd
132
133if [[ ${build_status} -eq 0 ]] ; then
134    echo
135    echo "FLASHING"
136    echo "Pushing built image onto device."
137    echo "cros flash --board=${BISECT_BOARD} --clobber-stateful ${BISECT_REMOTE} ~/trunk/src/build/images/${BISECT_BOARD}/latest/chromiumos_test_image.bin"
138    cros flash --board=${BISECT_BOARD} --clobber-stateful ${BISECT_REMOTE} ~/trunk/src/build/images/${BISECT_BOARD}/latest/chromiumos_test_image.bin
139    cros_flash_status=$?
140    while [[ ${cros_flash_status} -ne 0 ]] ; do
141        while true; do
142          echo
143          echo "cros flash has failed! From here you can:"
144          echo "1. Flash through USB"
145          echo "2. Retry flashing over ethernet"
146          echo "3. Abort this installation and skip this image"
147          echo "4. Abort this installation and mark test as failed"
148          sleep 1
149          read -p "Which method would you like to do? " choice
150          case $choice in
151              1) usb_flash && break;;
152              2) ethernet_flash && break;;
153              3) exit 125;;
154              4) exit 1;;
155              *) echo "Please answer 1, 2, 3, or 4.";;
156          esac
157        done
158
159        cros_flash_status=$?
160    done
161else
162    echo "build_image returned a non-zero status: ${build_status}"
163    exit 1
164fi
165
166exit 0
167