You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

68 lines
3.0 KiB

#!/bin/bash -e
# This is a helper script to automatically deploy firmware binaries to a remote running esp-ota-update-server
# It is configured by a project specific env-file which needs to be sourced before calling this script
# TODO:
# - implement sanity check for config vars
## Settings you may want to adjust
ESP_UPDATER_FS_PATH=/home/sqozz/esp_ota_updater # FS path to esp-ota-update-server on remote server (no trailing slash!)
FIRMWARE_FS_PATH=$ESP_UPDATER_FS_PATH/firmwares/ # FS path to the firmwares. Only used to construct full path (REMOTE_BIN_FILE) later on
CONFIG_FS_PATH=$ESP_UPDATER_FS_PATH/config.json # FS path to the config of esp-ota-update-server. Unsed to adjust the hash of the deployed binary
LOCAL_BIN_FILE="$PROJECT_NAME.ino.nodemcu.bin" # name of the binary to flash - this is the default for arduino nodemcu binaries
ssh-add -l 2> /dev/null > /dev/null
if [ $? -eq 2 ]; then
echo "ssh-agent is highly advisable for this script! Continuing anyway but you're going to be asked for you password quiet often."
fi
echo "=== PROJECT SETTINGS ==="
echo "Updateserver: $USER@$UPDATE_SERVER"
echo "Project: $PROJECT_NAME"
echo "Branch: $FIRMWARE_BRANCH"
echo ""
# calculate local hash
echo -n "Calculating hash for $LOCAL_BIN_FILE"
LOCAL_HASH=$(md5sum $LOCAL_BIN_FILE | awk '{ print $1 }')
echo -e "\b -> $LOCAL_HASH… done."
# calculate remote hash
BIN_FS_PATH=$PROJECT_NAME/$FIRMWARE_BRANCH/$FIRMWARE_BRANCH.bin
REMOTE_BIN_FILE=$FIRMWARE_FS_PATH$BIN_FS_PATH
echo -n "Calculating hash for $USER@$UPDATE_SERVER:$REMOTE_BIN_FILE"
REMOTE_HASH=$(ssh $USER@$UPDATE_SERVER "md5sum $REMOTE_BIN_FILE" | awk '{ print $1 }')
echo -e "\b -> $REMOTE_HASH… done."
# only upload if the binary is not the same
if [ "$LOCAL_HASH" = "$REMOTE_HASH" ]; then
echo "Firmware already exists on update server, skipping upload…"
exit 10
fi
echo -n "Uploading $LOCAL_BIN_FILE to $USER@$UPDATE_SERVER:$REMOTE_BIN_FILE"
scp $LOCAL_BIN_FILE $USER@$UPDATE_SERVER:$REMOTE_BIN_FILE > /dev/null
echo "done."
# we should never end up here if the transfer fails. This is just to be 100% sure
REMOTE_HASH=$(ssh $USER@$UPDATE_SERVER "md5sum $REMOTE_BIN_FILE" | awk '{ print $1 }')
echo "Remote hash after upload: $REMOTE_HASH"
if [ "$LOCAL_HASH" != "$REMOTE_HASH" ]; then
echo "Remote hash $REMOTE_HASH does not match local hash $LOCAL_HASH after upload!"
exit 20
fi
# backup
echo -n "Creating config.json backup to config.json.old… "
ssh $USER@$UPDATE_SERVER "cp $CONFIG_FS_PATH $CONFIG_FS_PATH.old"
echo "done."
# if you pipe this directly into config.json it will result in a race and an erased/empty config
# therefore we write it into a temp-config first
echo -n "Creating updated config.json.new… "
ssh $USER@$UPDATE_SERVER "cat $CONFIG_FS_PATH | jq '.firmwares.$PROJECT_NAME.$FIRMWARE_BRANCH.hash = \"$REMOTE_HASH\"' > $CONFIG_FS_PATH.new"
echo "done."
# and finally: deploy
echo -n "Deploying updated config.json.new to config.json… "
ssh $USER@$UPDATE_SERVER "cp $CONFIG_FS_PATH.new $CONFIG_FS_PATH"
echo "done."