Skip to content

Commit

Permalink
add service name to copy
Browse files Browse the repository at this point in the history
  • Loading branch information
MelleKoningSGG committed Mar 6, 2024
1 parent a45bc79 commit 2815df1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
39 changes: 39 additions & 0 deletions balena-copy-device.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# check 2 parameters
if [ $# -ne 2 ]; then
echo "Error: Please provide two arguments: source device name and destination device name."
exit 1
fi

# check balena cli installation
if ! [ -x "$(command -v balena)" ]; then
echo "Error: Balena CLI tool is not installed. Please install it by following the instructions at https://www.balena.io/docs/reference/cli/"
exit 1
fi

# Get environment variables from the source device and store them in a JSON file
balena envs --device $1 --json > envs.json

# Add each environment variable to the destination device
for env in $(jq -c '.[]' envs.json); do
name=$(echo "$env" | jq -r '.name')
value=$(echo "$env" | jq -r '.value')
servicename=$(echo "$env" | jq -r '.serviceName')

if [ -z "$value" ]; then
value="none"
fi

if [ -z "$name" ]; then
name="none"
fi
echo "Copying variable $name to device $2 and service $servicename in progress..."
# Escape special characters in value
value_escaped=$(printf '%s\n' "$value" | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/"/\\"/g')

# Add variable to destination device using retrieved values
balena env add "$name" "$value_escaped" --device "$2" --service "$servicename"
done

echo "Environment variables successfully copied from device $1 to device $2."
9 changes: 5 additions & 4 deletions balena-copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ balena envs --fleet $1 --json > envs.json
for env in $(jq -c '.[]' envs.json); do
name=$(echo "$env" | jq -r '.name')
value=$(echo "$env" | jq -r '.value')

servicename=$(echo "$env" | jq -r '.serviceName')

if [ -z "$value" ]; then
value="none"
fi

if [ -z "$name" ]; then
name="none"
fi
echo "Copying variable $name to fleet $2 in progress..."
echo "Copying variable $name to fleet $2 and service $servicename in progress..."
# Escape special characters in value
value_escaped=$(printf '%s\n' "$value" | sed -e 's/\\/\\\\/g; s/\//\\\//g; s/"/\\"/g')

# Add variable to destination fleet using retrieved values
balena env add "$name" "$value_escaped" --fleet "$2"
balena env add "$name" "$value_escaped" --fleet "$2" --service "$servicename"
done

echo "Environment variables successfully copied from fleet $1 to fleet $2."
echo "Environment variables successfully copied from fleet $1 to fleet $2."

0 comments on commit 2815df1

Please sign in to comment.