From 66662ee001115a0c628d614c557a7c665727e3be Mon Sep 17 00:00:00 2001 From: wouter182 Date: Fri, 14 May 2021 00:05:12 +0200 Subject: [PATCH] Add the option to deploy with ssh instead of adb. Also some small updates --- crossbuilder | 132 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 114 insertions(+), 18 deletions(-) diff --git a/crossbuilder b/crossbuilder index 93def4b..2c5151d 100755 --- a/crossbuilder +++ b/crossbuilder @@ -38,6 +38,7 @@ set -e RED='\033[0;31m' GREEN='\033[0;32m' +WHITE='\033[1;37m' LIGHT_RED='\033[1;31m' LIGHT_GREEN='\033[1;32m' NC='\033[0m' @@ -64,7 +65,8 @@ display_help () { echo " build - Build a package in a container (will create it if needed)." echo " clean - Clean up the artifacts generated by a build." echo " deploy - Deploy built packages to a connected device." -# echo " undeploy - Remove previously deployed packages from a connected device." + echo " stop - Stop a container." + echo " undeploy - (under development, not working yet) Remove previously deployed packages from a connected device." echo "" echo "Options:" # echo " --verbose - Print verbose information." @@ -78,12 +80,58 @@ display_help () { echo " --no-deb - Do not build Debian packages and uses rsync to deploy the build artifacts." echo " --deploy-path - When deploying with --no-deb (rsync), installation path for the build artifacts [defaults to /]." echo " --parallel - Set parallelism of the build. Defaults to the number of logical core + 1." + echo " --ssh - Deploy packages over ssh instead of adb, add IP." } +deploy_method () { + if [ "$DEPLOY_IP" != "" ] ; then + DEPLOY_METHOD="ssh" + DEPLOY_USER="phablet" + else + DEPLOY_METHOD="adb" + fi +} + exec_device () { - #echo adb shell "$@" - adb shell "$@" + deploy_method + if [ "$DEPLOY_METHOD" = "ssh" ] ; then + SSH_ARGS="" + if [ "$DEPLOY_KEY" != "" ]; then + SSH_ARGS="SSH_ARGS -i $DEPLOY_KEY" + fi + ssh $SSH_ARGS $DEPLOY_USER@$DEPLOY_IP "$@" + else + adb shell "$@" + fi +} + +copy_to_device () { + deploy_method + if [ "$DEPLOY_METHOD" = "ssh" ] ; then + SSH_ARGS="" + if [ "$DEPLOY_KEY" != "" ]; then + SSH_ARGS="$SSH_ARGS -i $DEPLOY_KEY" + fi + DEPLOY_USER="phablet" + scp $SSH_ARGS $1 $DEPLOY_USER@$DEPLOY_IP:$2 + else + adb push $2 + fi +} + +copy_from_device () { + deploy_method + if [ "$DEPLOY_METHOD" = "ssh" ] ; then + SSH_ARGS="" + if [ "$DEPLOY_KEY" != "" ]; then + SSH_ARGS="$SSH_ARGS -i $DEPLOY_KEY" + fi + DEPLOY_USER="phablet" + scp $SSH_ARGS $DEPLOY_USER@$DEPLOY_IP:$1 . + else + adb pull $1 + fi } exec_container_root () { @@ -98,14 +146,19 @@ exec_container () { lxc exec $LXD_CONTAINER -- su -l -c "cd $SOURCE_PATH_CONTAINER; $command" $USERNAME } +stop_container () { + lxc stop $LXD_CONTAINER --force +} + variables () { DEBS_TARBALL_ROOT=debs if [ -n "$NEW_BUILD" ] ; then - NEW_PACKAGE_VERSION=$PACKAGE_VERSION"local~"`date +%s` + NEW_PACKAGE_VERSION=$PACKAGE_VERSION"-local~"`date -u +%Y%m%d%H%M%S` else LATEST_DEBS_TARBALL=$(ls -1 --sort=time $DEBS_TARBALL_ROOT_*.tar 2> /dev/null | head -n1) NEW_PACKAGE_VERSION=$(echo $LATEST_DEBS_TARBALL | sed "s/$DEBS_TARBALL_ROOT\_//" | sed "s/\.tar//") fi + echo "${POSITIVE_COLOR}Build package version: $NEW_PACKAGE_VERSION" DEBS_TARBALL="$DEBS_TARBALL_ROOT"_"$NEW_PACKAGE_VERSION.tar" LXD_IMAGE_NO_DOT=$(echo $LXD_IMAGE | sed "s|[./]|-|g") @@ -675,7 +728,7 @@ check_for_device_network() { NETWORK_UP=0 for i in `seq 1 5` do - if adb shell ping -c1 -w1 google.com | grep PING > /dev/null 2>&1 ; then + if exec_device ping -c1 -w1 google.com | grep PING > /dev/null 2>&1 ; then NETWORK_UP=1 break fi @@ -695,7 +748,7 @@ deploy_deb () { echo "${POSITIVE_COLOR}Transferring Debian packages to device.${NC}" # tranfer debian packages to device exec_device mkdir -p /tmp/repo - adb push $DEBS_TARBALL /tmp/repo/ + copy_to_device $DEBS_TARBALL /tmp/repo exec_device "cd /tmp/repo && tar xvf /tmp/repo/$DEBS_TARBALL && rm -f /tmp/repo/$DEBS_TARBALL_ROOT*" # install debian packages on device @@ -710,24 +763,26 @@ deploy_deb () { check_for_device_network echo "${POSITIVE_COLOR}Upgrading packages already installed on device with newly built ones.${NC}" # create local deb repository on device + echo "${POSITIVE_COLOR}Creating local deb repository on device${WHITE}" rm -f $CREATE_REPO_SCRIPT - if ! adb pull /tmp/repo/$CREATE_REPO_SCRIPT 2> /dev/null ; then - adb push $SCRIPT_DIR/$CREATE_REPO_SCRIPT /tmp/repo/ + if ! copy_from_device /tmp/repo/$CREATE_REPO_SCRIPT 2> /dev/null ; then + copy_to_device $SCRIPT_DIR/$CREATE_REPO_SCRIPT /tmp/repo/ exec_device /tmp/repo/$CREATE_REPO_SCRIPT /tmp/repo exec_device "printf 'deb file:/tmp/repo/ /\n' > /tmp/repo/sources.list" exec_device "cp /etc/apt/sources.list /tmp/repo/all.list" exec_device "cat /tmp/repo/sources.list >> /tmp/repo/all.list" - SERIES=$(adb shell lsb_release -cs | tr -d '\r') + SERIES=$(exec_device lsb_release -cs | tr -d '\r') exec_device "printf 'Package: *\nPin: release o=local\nPin-Priority: 2000\n\nPackage: *\nPin: release a=$SERIES*\nPin-Priority: 50' | SUDO_ASKPASS=/tmp/askpass.sh sudo -A tee /etc/apt/preferences.d/localrepo.pref" # exec_device SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get update else exec_device /tmp/repo/$CREATE_REPO_SCRIPT /tmp/repo fi; - exec_device "SUDO_ASKPASS=/tmp/askpass.sh sudo -A sed -i '/Pin-Priority/c\Pin-Priority: 50' /etc/apt/preferences.d/extra-ppas.pref" + exec_device "SUDO_ASKPASS=/tmp/askpass.sh sudo -A sed -i '/Pin-Priority/c\Pin-Priority: 50' /etc/apt/preferences.d/localrepo.pref" exec_device SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get update -o Dir::Etc::sourcelist="/tmp/repo/sources.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" exec_device SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get dist-upgrade -o Dir::Etc::sourcelist="/tmp/repo/all.list" --yes --force-yes - exec_device "SUDO_ASKPASS=/tmp/askpass.sh sudo -A sed -i '/Pin-Priority/c\Pin-Priority: 1001' /etc/apt/preferences.d/extra-ppas.pref" + exec_device "SUDO_ASKPASS=/tmp/askpass.sh sudo -A sed -i '/Pin-Priority/c\Pin-Priority: 3000' /etc/apt/preferences.d/localrepo.pref" + echo "${POSITIVE_COLOR}Debian packages deployed, post deployment can be started if required${WHITE}" fi; } @@ -752,11 +807,27 @@ deploy_make_install () { } deploy_to_device () { - DEVICE_STATE=`adb get-state` - if [ "$DEVICE_STATE" != "device" ] ; then - echo "${ERROR_COLOR}No device connected to deploy to.${NC}" - exit 1 - fi; + if [ "$DEPLOY_IP" != "" ] ; then + if test -z "$DEPLOY_IP" ; then + echo "${POSITIVE_COLOR}If a config named $DEPLOY_CONF existed, the packages would be deployed to a device.${NC}" + exit 0 + fi + + ping -c 1 $DEPLOY_IP > /dev/null + DEVICE_STATE=$? + if [ $DEVICE_STATE -ne 0 ] ; then + echo "${ERROR_COLOR}No device connected to deploy to.${NC}" + exit 1 + else + echo "Device with IP: $DEPLOY_IP could be reached" + fi + else + DEVICE_STATE=`adb get-state` + if [ "$DEVICE_STATE" != "device" ] ; then + echo "${ERROR_COLOR}No device connected to deploy to.${NC}" + exit 1 + fi + fi # setup sudo on device exec_device "printf '#\041/bin/sh\necho $DEVICE_PASSWORD' >/tmp/askpass.sh" @@ -764,7 +835,7 @@ deploy_to_device () { # check password is correct exec_device SUDO_ASKPASS=/tmp/askpass.sh sudo -A touch /tmp/password_ok - if adb pull /tmp/password_ok ; then + if copy_from_device /tmp/password_ok ; then rm -f password_ok exec_device SUDO_ASKPASS=/tmp/askpass.sh sudo -A rm -f /tmp/password_ok else @@ -783,13 +854,17 @@ deploy_to_device () { # execute post deploy if test -e $SOURCE_PATH_LOCAL/$POST_DEPLOY_SCRIPT ; then echo "${POSITIVE_COLOR}Execute project specific post deploy script ($POST_DEPLOY_SCRIPT).${NC}" - adb push $SOURCE_PATH_LOCAL/$POST_DEPLOY_SCRIPT /tmp + copy_to_device $SOURCE_PATH_LOCAL/$POST_DEPLOY_SCRIPT /tmp exec_device sh /tmp/$(basename $POST_DEPLOY_SCRIPT) else echo "${POSITIVE_COLOR}If a script named $POST_DEPLOY_SCRIPT existed, it would be executed on device after every deploy.${NC}" fi } +undeploy_on_device () { + echo "Undeployment on device is not working yet" +} + MISSING_PACKAGES= if ! which dpkg > /dev/null ; then MISSING_PACKAGES="dpkg" @@ -896,6 +971,9 @@ while [ "$1" != "" ]; do --parallel) PARALLEL_BUILD=$VALUE ;; + --ssh) + DEPLOY_IP=$VALUE + ;; --help) display_help exit 0 @@ -915,6 +993,18 @@ while [ "$1" != "" ]; do esac done +deploy_method +if [ "$DEPLOY_METHOD" = "ssh" ] ; then + DEVICE_ARCH=$(exec_device "dpkg --print-architecture" 2>&1| tr -d '\r') + if echo "$DEVICE_ARCH" | grep -vq "not found"; then + TARGET_ARCH=$DEVICE_ARCH + TARGET_UBUNTU=$(exec_device "lsb_release --release --short" | tr -d '\r') + DEPLOY=true + fi +fi; + +echo "${POSITIVE_COLOR}Ubuntu version device: $TARGET_UBUNTU with architecture: $TARGET_ARCH" + if [ -z "$LXD_IMAGE" ] ; then LXD_IMAGE=ubports-sdk:ubuntu-sdk-$TARGET_UBUNTU-$HOST_ARCH-$TARGET_ARCH-dev fi @@ -951,6 +1041,7 @@ detect_package () { if test -e debian/changelog ; then PACKAGE=`dpkg-parsechangelog --show-field Source` PACKAGE_VERSION=`dpkg-parsechangelog --show-field Version` + echo "Detected package: $PACKAGE with version: $PACKAGE_VERSION" fi } @@ -959,6 +1050,8 @@ check_changelog_exists () { echo "${ERROR_COLOR}No debian/changelog found in $PWD.${NC}" echo "If the source code is not available it can be automatically downloaded from the repositories using the 'source' command." exit 1 + else + echo "Debian/changelog found in $PWD.${NC}" fi } @@ -1136,6 +1229,9 @@ else variables deploy_to_device ;; + undeploy) + undeploy_on_device + ;; *) display_help echo ""