-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart.sh
44 lines (35 loc) · 1.63 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
echo "--- Hostname ---"
# Check we can hit the supervisor API
if [[ -z "$BALENA_SUPERVISOR_ADDRESS" || -z "$BALENA_SUPERVISOR_ADDRESS" ]]; then
echo "Could not set hostname: BALENA_SUPERVISOR_ADDRESS and BALENA_SUPERVISOR_ADDRESS were not provided."
echo "Add 'io.balena.features.supervisor-api' label to allow interacting with the supervisor."
echo "See https://www.balena.io/docs/reference/supervisor/supervisor-api/#http-api-reference for details."
exit 0
fi
# Get current hostname
CURRENT_HOSTNAME=$(curl -sL "$BALENA_SUPERVISOR_ADDRESS/v1/device/host-config?apikey=$BALENA_SUPERVISOR_API_KEY" | jq -r '.network.hostname')
echo "Current hostname: $CURRENT_HOSTNAME"
# Use device short uuid if hostname is set to "UUID" or "uuid"
if [[ "${SET_HOSTNAME}" == "uuid" || "${SET_HOSTNAME}" == "UUID" ]]; then
SET_HOSTNAME="${BALENA_DEVICE_UUID:0:7}"
fi
# Skip if no SET_HOSTNAME
# We can't use HOSTNAME as the user input because alpine already prepopulates it with the container's hostname which is different than the host
if [[ -z "$SET_HOSTNAME" ]]; then
echo "Skipping hostname set: SET_HOSTNAME was not provided."
exit 0
fi
echo "Target hostname: $SET_HOSTNAME"
# Skip if target hostname already applied
if [[ "$CURRENT_HOSTNAME" == "$SET_HOSTNAME" ]]; then
echo "Skipping hostname set: target matches current hostname."
exit 0
fi
# Set target hostname
echo "Setting target hostname..."
curl -sL -X PATCH --header "Content-Type:application/json" \
--data '{"network": {"hostname": "'$SET_HOSTNAME'"}}' \
"$BALENA_SUPERVISOR_ADDRESS/v1/device/host-config?apikey=$BALENA_SUPERVISOR_API_KEY"
echo -e "\nHostname updated!"
exit 0