Bluetooth: introduce script to help set up Raspberry Pi for AVL

The existing process to setup Raspberry Pi for AVL testing requires
extensive manual efforts. This script hopes to automate this process
to provide a simple and consistent setup process for AVL Raspberry Pis.

This script shall run on a Linux autotest serves with Internet access
and interact with an SSH-enabled Raspberry Pi.

BUG=b:182800461
TEST=sh raspberrypi_setup 100.100.100.100 test0000

Change-Id: Ic9c4db23c84c24e4c9b9ca7d77ca3a3b7f3e8019
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/chameleon/+/2761462
Tested-by: Michael Sun <michaelfsun@google.com>
Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Reviewed-by: Daniel Winkler <danielwinkler@google.com>
Commit-Queue: Michael Sun <michaelfsun@google.com>
diff --git a/utils/raspberrypi_setup b/utils/raspberrypi_setup
new file mode 100644
index 0000000..0a3aa35
--- /dev/null
+++ b/utils/raspberrypi_setup
@@ -0,0 +1,109 @@
+#!/bin/bash
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# A tool to help set up a new Raspberry Pi (with Raspberry Pi OS installed).
+# This script is meant to be executed OUTSIDE the chroot in a regular Linux
+# system environment.
+# Usage: raspberrypi_setup PI_IP_ADDR ROOT_PASSWORD IF_PI, where:
+#   PI_IP_ADDR: IP of the Raspberry Pi this script is setting up.
+#   ROOT_PASSWORD: The root password will set to the Raspberry Pi.
+#   IF_PI: This tells the script if it is initiated from the host, leave it
+#          blank while execute from the host (autotest server).
+# e.g., raspberrypi_setup 100.100.100.100 test0000
+
+SSH_KEY_TARBALL="https://chromium.googlesource.com/chromiumos/chromite/+archive/master/ssh_keys.tar.gz"
+
+PI_IP="$1"
+ROOT_PASSWD="$2"
+IF_PI="$3"
+
+prepare_host() {
+    if ping -W 10 -c 1 "${PI_IP}"; then
+        echo "Raspberry Pi Found - $(date)"
+        # Buffer time to allow SSH up after pinable.
+        sleep 5
+    else
+        echo "Failed to Ping Raspberry Pi"
+        exit 1
+    fi
+
+    echo "Install required packets"
+    apt-get -y install sshpass > /dev/null 2>&1
+
+    ssh-keygen -R pi@${PI_IP} > /dev/null 2>&1
+}
+
+prepare_pi() {
+    echo "Copy setup files to the target Raspberry Pi"
+    if ! sshpass -p 'raspberry' scp -o StrictHostKeyChecking=no \
+        ~/.ssh/ssh_keys.tar.gz $0 pi@"${PI_IP}":/tmp; then
+        echo "Failed copy setup files to the target Raspberry Pi"
+        exit 1
+    fi
+}
+
+setup_host() {
+    echo "Setup autotest server (local) SSH keys"
+
+    if ! wget -O ~/.ssh/ssh_keys.tar.gz "${SSH_KEY_TARBALL}" > /dev/null 2>&1;
+    then
+        echo "Failed to download SSH keys"
+        exit 1
+    fi
+    tar -xf ~/.ssh/ssh_keys.tar.gz -C ~/.ssh/
+    chmod 0600 ~/.ssh/testing_rsa
+
+    echo "Add Raspberry Pi to SSH config"
+    cat << EOF >> ~/.ssh/config
+HOST ${PI_IP}
+    User root
+    IdentityFile ~/.ssh/testing_rsa
+EOF
+
+    echo "Apply new SSH configuration"
+    ssh-agent > /dev/null 2>&1
+    ssh-add > /dev/null 2>&1
+    ssh-add -l > /dev/null 2>&1
+}
+
+setup_pi() {
+    PI_ROOT_PASSWORD="$1"
+
+    echo "Updating SSH settings"
+    systemctl enable ssh
+    sed -i 's/.*#PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
+    sed -i 's/.*#PubkeyAuthentication .*/PubkeyAuthentication yes/' \
+        /etc/ssh/sshd_config
+    sed -i 's/.*#PasswordAuthentication .*/PasswordAuthentication yes/' \
+        /etc/ssh/sshd_config
+    sed -i \
+        's~.*AuthorizedKeysFile.*~AuthorizedKeysFile .ssh/authorized_keys~' \
+        /etc/ssh/sshd_config
+
+    echo "Updating root password"
+    echo "${PI_ROOT_PASSWORD}\n${PI_ROOT_PASSWORD}" | passwd
+
+    echo "Setup Raspberry Pi (remote) SSH keys"
+    mkdir -p ~/.ssh
+
+    tar -xf /tmp/ssh_keys.tar.gz -C ~/.ssh/
+    chmod 0600 /root/.ssh/testing_rsa
+    mv ~/.ssh/testing_rsa.pub ~/.ssh/authorized_keys
+    rm /tmp/ssh_keys.tar.gz
+
+    echo "Restart Raspberry Pi SSH service"
+    service ssh restart
+}
+
+if [ -z "$IF_PI" ]; then
+    prepare_host
+    setup_host
+    prepare_pi
+    sshpass -p "raspberry" ssh pi@"${PI_IP}" \
+        "sudo sh /tmp/raspberrypi_setup "${1}" "${2}" yes"
+    echo "Raspberry Pi Setup Finished"
+else
+    setup_pi "$2"
+fi