Last active
July 11, 2023 17:21
-
-
Save vipulgupta2048/f840f2771d6a4cf4c46fea9d3710afe6 to your computer and use it in GitHub Desktop.
MIGRATE THEM ALL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Script to migrate all local devices between balenaCloud environments. Steps: | |
# 1. Scan for devices (Needs balenaCLI to be installed) | |
# 2. SSH into devices and convert all devices to development OS variant | |
# 3. Once complete, migrate devices to new target environement using balena join | |
# 4. Shows migrated devices in the new target fleet | |
# 5. Optionally you can convert all devices back to production OS variant after migration | |
BALENA_SOURCE_FLEET_TOKEN= | |
BALENA_SOURCE_FLEET_URL=balena-cloud.com | |
BALENA_TARGET_FLEET_TOKEN= | |
BALENA_TARGET_FLEET_URL=newbalenamachine.owned.by.me | |
BALENA_TARGET_FLEET_SLUG=vipulgupta2048/newest-fleet | |
# Command to convert devices to dev mode | |
devmode_on="tmp=$(mktemp)&& jq '.developmentMode="true"' /mnt/boot/config.json > "$tmp" && mv "$tmp" /mnt/boot/config.json && exit" | |
echo "Let the migration begin" | |
# Scan local devices available - ALL LOCAL DEVICES WILL BE PICKED UP | |
addresses=`sudo balena scan --json | jq -r '.[].address'` | |
# Split the addresses into an array | |
IFS=$'\n' read -d '' -ra address_array <<< "$addresses" | |
echo "${address_array[@]}" | |
# Login to instance from where devices have to be migrated | |
BALENARC_BALENA_URL=$BALENA_SOURCE_FLEET_URL balena login --token $BALENA_SOURCE_FLEET_TOKEN | |
BALENARC_BALENA_URL=$BALENA_SOURCE_FLEET_URL balena whoami | |
echo "If this isn't correct, stop now... waiting 5 seconds" | |
sleep 5 | |
# Loop through the list of devices converting all to dev mode | |
for address in "${address_array[@]}"; do | |
echo "Converting $address to dev mode" | |
echo 'bash -c "$devmode_on"' | BALENARC_BALENA_URL=$BALENA_SOURCE_FLEET_URL balena ssh "$address" | |
done | |
############## Once all devices are converted to development | |
echo "********************************* Let the migration actually begin" | |
# Login to instance that you want to migrate | |
BALENARC_BALENA_URL=$BALENA_TARGET_FLEET_URL balena login --token $BALENA_TARGET_FLEET_TOKEN | |
BALENARC_BALENA_URL=$BALENA_TARGET_FLEET_URL balena whoami | |
echo "if this isn't correct, stop now... waiting 5 seconds" | |
sleep 5 | |
for address in "${address_array[@]}"; do | |
BALENARC_BALENA_URL=$BALENA_TARGET_FLEET_URL balena join $address --fleet $BALENA_TARGET_FLEET_SLUG --pollInterval 10 | |
echo "Migrated $address to $BALENA_TARGET_FLEET_SLUG" | |
done | |
BALENARC_BALENA_URL=$BALENA_TARGET_FLEET_URL balena devices | grep "$BALENA_TARGET_FLEET_SLUG" | |
## Problems that you might face | |
# SSH: Process exited with non-zero status code "255" | |
# Are the SSH keys correctly configured in balenaCloud? See: | |
# https://www.balena.io/docs/learn/manage/ssh-access/#add-an-ssh-key-to-balenacloud | |
# Are you accidentally using `sudo`? | |
# If you are seeing this ^^^ don't use sudo to start the script? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment