Skip to content

Commit

Permalink
πŸš‘ Fix yq for the v4 syntax (#234)
Browse files Browse the repository at this point in the history
* πŸš‘ Fix yq for the v4 syntax

* Shellcheck warning
  • Loading branch information
frenck authored Aug 11, 2021
1 parent a96c384 commit 2cab448
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions adguard/rootfs/etc/cont-init.d/adguard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Handles configuration
# ==============================================================================
readonly CONFIG="/data/adguard/AdGuardHome.yaml"
declare port
declare schema_version
declare -a hosts
declare part
Expand All @@ -17,25 +16,24 @@ if ! bashio::fs.file_exists "${CONFIG}"; then
cp /etc/adguard/AdGuardHome.yaml "${CONFIG}"
fi

port=$(bashio::addon.port "53/udp")
yq write --inplace "${CONFIG}" \
dns.port "${port}" \
port=$(bashio::addon.port "53/udp") \
yq e --inplace '.dns.port = env(port)' "${CONFIG}" \
|| bashio::exit.nok 'Failed updating AdGuardHome DNS port'


# Bump schema version in case this is an upgrade path
schema_version=$(yq read "${CONFIG}" schema_version)
schema_version=$(yq e '.schema_version // ""' "${CONFIG}")
if bashio::var.has_value "${schema_version+}"; then
if (( schema_version == 7 )); then
# Clean up old interface bind formats
yq delete --inplace "${CONFIG}" dns.bind_host
yq write --inplace "${CONFIG}" schema_version 8
yq --inplace e 'del(.dns.bind_host)' "${CONFIG}"
yq --inplace e '.schema_version = 8' "${CONFIG}"
fi

# Warn if this is an upgrade from below schema version 7, skip further process
if (( schema_version < 7 )); then
# Ensure dummy value exists so AdGuard doesn't kill itself during migration
yq write --inplace "${CONFIG}" dns.bind_host "127.0.0.1"
yq --inplace e '.dns.bind_host = "127.0.0.1"' "${CONFIG}"
bashio::warning
bashio::warning "AdGuard Home needs to update its configuration schema"
bashio::warning "you might need to restart he add-on once more to complete"
Expand All @@ -46,7 +44,7 @@ if bashio::var.has_value "${schema_version+}"; then
else
# No idea what the schema is, might be an old config?
# Ensure dummy value exists so AdGuard doesn't kill itself during migration
yq write --inplace "${CONFIG}" dns.bind_host "127.0.0.1"
yq --inplace e '.dns.bind_host = "127.0.0.1"' "${CONFIG}"
fi

# Collect IP addresses
Expand All @@ -55,7 +53,7 @@ hosts+=($(bashio::network.ipv6_address))
hosts+=($(bashio::addon.ip_address))

# Add interface to bind to, to AdGuard Home
yq delete --inplace "${CONFIG}" dns.bind_hosts
yq --inplace e '.dns.bind_hosts = []' "${CONFIG}"
for host in "${hosts[@]}"; do
# Empty host value? Skip it
if ! bashio::var.has_value "${host}"; then
Expand Down Expand Up @@ -84,7 +82,7 @@ for host in "${hosts[@]}"; do
fi
fi

yq write --inplace "${CONFIG}" \
dns.bind_hosts[+] "${host%/*}" \
host="${host}" yq --inplace e \
'.dns.bind_hosts += [env(host)]' "${CONFIG}" \
|| bashio::exit.nok 'Failed updating AdGuardHome host'
done

0 comments on commit 2cab448

Please sign in to comment.