Skip to content

Commit

Permalink
Add the option to deploy with ssh instead of adb.
Browse files Browse the repository at this point in the history
Also some small updates
  • Loading branch information
wouter182 committed May 16, 2021
1 parent cd78127 commit 66662ee
Showing 1 changed file with 114 additions and 18 deletions.
132 changes: 114 additions & 18 deletions crossbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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."
Expand All @@ -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 () {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -752,19 +807,35 @@ 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"
exec_device chmod +x /tmp/askpass.sh

# 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
Expand All @@ -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"
Expand Down Expand Up @@ -896,6 +971,9 @@ while [ "$1" != "" ]; do
--parallel)
PARALLEL_BUILD=$VALUE
;;
--ssh)
DEPLOY_IP=$VALUE
;;
--help)
display_help
exit 0
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -1136,6 +1229,9 @@ else
variables
deploy_to_device
;;
undeploy)
undeploy_on_device
;;
*)
display_help
echo ""
Expand Down

0 comments on commit 66662ee

Please sign in to comment.