diff --git a/README.md b/README.md index 4f7395b..eb4198c 100755 --- a/README.md +++ b/README.md @@ -12,4 +12,6 @@ MUX, including the default separate Data Gateway service, see [docs/hdstack.md](docs/hdstack.md). See the [FreeDMR v1.5.0 release notes](docs/release-notes-v1.5.0.md) before -upgrading an existing container deployment. +upgrading an existing container deployment. Required deployment migrations are +performed by the documented upgrade script; pulling a container image alone +does not update mounted configuration. diff --git a/docker-configs/check-deployment-version.sh b/docker-configs/check-deployment-version.sh new file mode 100644 index 0000000..ed1c8cc --- /dev/null +++ b/docker-configs/check-deployment-version.sh @@ -0,0 +1,73 @@ +#!/bin/sh + +CONFIG_FILE="${1:-/opt/freedmr/freedmr.cfg}" +REQUIRED_VERSION=1 +UPGRADE_SCRIPT_URL='https://gitlab.hacknix.net/hacknix/FreeDMR/-/raw/master/docker-configs/upgrade-daprs-to-data-gateway.sh' + +installed_version=$(awk ' + BEGIN { prefix = "# FREEDMR_DEPLOYMENT_VERSION=" } + index($0, prefix) == 1 { + count++ + value = substr($0, length(prefix) + 1) + gsub(/[[:space:]]/, "", value) + if (value !~ /^[0-9]+$/) + invalid = 1 + } + END { + if (count == 0) + print 0 + else if (count == 1 && !invalid) + print value + else + exit 2 + } +' "$CONFIG_FILE" 2>/dev/null) +status=$? + +if [ "$status" -ne 0 ] +then + installed_version=invalid +fi + +case "$installed_version" in + ''|*[!0-9]*) upgrade_required=1 ;; + *) + if [ "$installed_version" -lt "$REQUIRED_VERSION" ] + then + upgrade_required=1 + else + upgrade_required=0 + fi + ;; +esac + +if [ "$upgrade_required" -eq 0 ] +then + exit 0 +fi + +cat >&2 <&2 + exit 0 + ;; +esac + +echo 'FreeDMR startup stopped until the deployment upgrade is applied.' >&2 +exit 78 diff --git a/docker-configs/docker-compose.yml b/docker-configs/docker-compose.yml index a57fb87..8914b7c 100644 --- a/docker-configs/docker-compose.yml +++ b/docker-configs/docker-compose.yml @@ -57,6 +57,9 @@ services: #- HDSTACK_BASEID=23400 # The separate bridge.py process is optional. - HDSTACK_BRIDGE=0 + # Deliberate custom deployments may bypass a required migration. + # The container logs a warning on every overridden startup. + #- FREEDMR_ALLOW_UNMIGRATED_START=1 # Override these only when using different mounted files. #- HDSTACK_BRIDGE_CONFIG=/opt/freedmr/freedmr-bridge.cfg #- HDSTACK_BRIDGE_RULES=/opt/freedmr/rules.py diff --git a/docker-configs/docker-compose_install.sh b/docker-configs/docker-compose_install.sh index 6d7616b..50bb147 100644 --- a/docker-configs/docker-compose_install.sh +++ b/docker-configs/docker-compose_install.sh @@ -293,6 +293,9 @@ sed -i "s|^[[:space:]]*- HDSTACK_BRIDGE=.*| - HDSTACK_BRIDGE=$HDSTACK sed -i "s|__FREEDMR_GATEWAY_FBP_RELATIONSHIPS__|$FREEDMR_GATEWAY_FBP_RELATIONSHIPS|" /etc/freedmr/docker-compose.yml sed -i "s|__FREEDMR_GATEWAY_APRS_LOGIN_CALL__|$FREEDMR_GATEWAY_APRS_LOGIN_CALL|" /etc/freedmr/docker-compose.yml sed -i "s|__FREEDMR_GATEWAY_APRS_PASSCODE__|$FREEDMR_GATEWAY_APRS_PASSCODE|" /etc/freedmr/docker-compose.yml +docker compose -f /etc/freedmr/docker-compose.yml config --quiet +sed -i '/^# FREEDMR_DEPLOYMENT_VERSION=/d' /etc/freedmr/freedmr.cfg +sed -i '1i# FREEDMR_DEPLOYMENT_VERSION=1' /etc/freedmr/freedmr.cfg chown -R 54000:54000 /etc/freedmr if [ -e /etc/cron.daily/lastheard ] diff --git a/docker-configs/entrypoint-proxy b/docker-configs/entrypoint-proxy index 47d40fc..0cc5733 100755 --- a/docker-configs/entrypoint-proxy +++ b/docker-configs/entrypoint-proxy @@ -17,6 +17,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ############################################################################### +/bin/sh /opt/freedmr/docker-configs/check-deployment-version.sh \ + /opt/freedmr/freedmr.cfg || exit $? + cd /opt/freedmr HDSTACK_COUNT="${HDSTACK:-}" diff --git a/docker-configs/upgrade-daprs-to-data-gateway.sh b/docker-configs/upgrade-daprs-to-data-gateway.sh index afa2a60..b558c4b 100755 --- a/docker-configs/upgrade-daprs-to-data-gateway.sh +++ b/docker-configs/upgrade-daprs-to-data-gateway.sh @@ -6,6 +6,9 @@ set -eu CONFIG_DIR=/etc/freedmr DRY_RUN=0 ASSUME_YES=0 +TARGET_DEPLOYMENT_VERSION=1 +LOCK_DIR= +WORK_DIR= usage() { @@ -13,7 +16,8 @@ usage() Usage: upgrade-daprs-to-data-gateway.sh [--config-dir DIR] [--dry-run] [--yes] Migrates a recognised legacy D-APRS service in docker-compose.yml to the native -FreeDMR Data Gateway and enables its FBP v5 relationship in freedmr.cfg. +FreeDMR Data Gateway and enables its FBP v5 relationship in freedmr.cfg. The +operation is idempotent and can resume an unambiguous partial migration. Existing server, HDStack and APRS values are read from freedmr.cfg and the legacy D-APRS service in docker-compose.yml. A new private FBP key is generated @@ -21,6 +25,12 @@ unless freedmr.cfg already contains a non-placeholder Data Gateway passphrase. USAGE } +cleanup() +{ + [ -z "$WORK_DIR" ] || rm -rf "$WORK_DIR" + [ -z "$LOCK_DIR" ] || rmdir "$LOCK_DIR" 2>/dev/null || true +} + fail() { echo "Upgrade aborted: $*" >&2 @@ -82,10 +92,22 @@ config_value() compose_environment_value() { - wanted="$1" - awk -v wanted="$wanted" ' + wanted_service="$1" + wanted="$2" + awk -v wanted_service="$wanted_service" -v wanted="$wanted" ' { line = $0 + sub(/[[:space:]]+$/, "", line) + if (line == " " wanted_service ":") { + in_service = 1 + next + } + if (in_service && + (line ~ /^ [A-Za-z0-9_.-]+:[[:space:]]*$/ || + line ~ /^[^[:space:]#][^:]*:[[:space:]]*$/)) + exit + if (!in_service) + next sub(/^[[:space:]]*-[[:space:]]*/, "", line) sub(/^[[:space:]]*/, "", line) if (index(line, wanted "=") == 1) { @@ -104,6 +126,42 @@ compose_environment_value() ' "$COMPOSE_FILE" } +deployment_version() +{ + awk ' + BEGIN { prefix = "# FREEDMR_DEPLOYMENT_VERSION=" } + index($0, prefix) == 1 { + count++ + value = substr($0, length(prefix) + 1) + gsub(/[[:space:]]/, "", value) + if (value !~ /^[0-9]+$/) + invalid = 1 + } + END { + if (count == 0) + print 0 + else if (count == 1 && !invalid) + print value + else + exit 2 + } + ' "$FREEDMR_CONFIG" +} + +json_string_value() +{ + field_name="$1" + printf '%s\n' "$2" | sed 's/}.*$/}/' | sed -n \ + "s/.*\"$field_name\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" +} + +json_number_value() +{ + field_name="$1" + printf '%s\n' "$2" | sed 's/}.*$/}/' | sed -n \ + "s/.*\"$field_name\"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p" +} + build_relationships() { count="$1" @@ -170,6 +228,14 @@ COMPOSE_FILE="$CONFIG_DIR/docker-compose.yml" [ -w "$FREEDMR_CONFIG" ] || fail "$FREEDMR_CONFIG is not writable" [ -w "$COMPOSE_FILE" ] || fail "$COMPOSE_FILE is not writable" +LOCK_DIR="$CONFIG_DIR/.freedmr-upgrade.lock" +if ! mkdir "$LOCK_DIR" 2>/dev/null +then + fail "another FreeDMR deployment upgrade appears to be running: $LOCK_DIR" +fi +trap cleanup EXIT +trap 'cleanup; exit 130' HUP INT TERM + grep -q '^\[GLOBAL\][[:space:]]*$' "$FREEDMR_CONFIG" || \ fail "$FREEDMR_CONFIG has no [GLOBAL] section" grep -q '^services:[[:space:]]*$' "$COMPOSE_FILE" || \ @@ -179,24 +245,36 @@ grep -q '^ freedmr:[[:space:]]*$' "$COMPOSE_FILE" || \ legacy_count=$(grep -Ec '^ D-APRS:[[:space:]]*$' "$COMPOSE_FILE" || true) native_count=$(grep -Ec '^ data-gateway:[[:space:]]*$' "$COMPOSE_FILE" || true) -[ "$native_count" -eq 0 ] || \ - fail "a native data-gateway service already exists; no files were changed" -[ "$legacy_count" -eq 1 ] || \ - fail "expected exactly one legacy D-APRS service, found $legacy_count" - -legacy_block=$(awk ' - /^ D-APRS:[[:space:]]*$/ { active = 1 } - active && NR > 1 && (/^ [A-Za-z0-9_.-]+:[[:space:]]*$/ || /^[^[:space:]#][^:]*:[[:space:]]*$/) && $0 !~ /^ D-APRS:/ { exit } - active { print } -' "$COMPOSE_FILE") -printf '%s\n' "$legacy_block" | grep -q 'container_name:[[:space:]]*D-APRS' || \ - fail "the D-APRS service is not the recognised legacy container shape" -printf '%s\n' "$legacy_block" | grep -q 'APRS_CALL' || \ - fail "the D-APRS service has no recognised APRS_CALL setting" -printf '%s\n' "$legacy_block" | grep -q 'APRS_PASSCODE' || \ - fail "the D-APRS service has no recognised APRS_PASSCODE setting" - -HDSTACK_COUNT=$(compose_environment_value HDSTACK) +[ "$legacy_count" -le 1 ] || fail "found more than one legacy D-APRS service" +[ "$native_count" -le 1 ] || fail "found more than one native data-gateway service" +if [ "$legacy_count" -eq 0 ] && [ "$native_count" -eq 0 ] +then + fail "neither a recognised legacy D-APRS service nor a native data-gateway service was found" +fi + +if [ "$legacy_count" -eq 1 ] +then + legacy_block=$(awk ' + /^ D-APRS:[[:space:]]*$/ { active = 1 } + active && NR > 1 && (/^ [A-Za-z0-9_.-]+:[[:space:]]*$/ || /^[^[:space:]#][^:]*:[[:space:]]*$/) && $0 !~ /^ D-APRS:/ { exit } + active { print } + ' "$COMPOSE_FILE") + printf '%s\n' "$legacy_block" | grep -q 'container_name:[[:space:]]*D-APRS' || \ + fail "the D-APRS service is not the recognised legacy container shape" + printf '%s\n' "$legacy_block" | grep -q 'APRS_CALL' || \ + fail "the D-APRS service has no recognised APRS_CALL setting" + printf '%s\n' "$legacy_block" | grep -q 'APRS_PASSCODE' || \ + fail "the D-APRS service has no recognised APRS_PASSCODE setting" +fi + +CURRENT_DEPLOYMENT_VERSION=$(deployment_version) || \ + fail "freedmr.cfg has an invalid or duplicate deployment-version marker" +if [ "$CURRENT_DEPLOYMENT_VERSION" -gt "$TARGET_DEPLOYMENT_VERSION" ] +then + fail "deployment version $CURRENT_DEPLOYMENT_VERSION is newer than this upgrader supports" +fi + +HDSTACK_COUNT=$(compose_environment_value freedmr HDSTACK) if [ -z "$HDSTACK_COUNT" ] then CPU_COUNT=$(getconf _NPROCESSORS_ONLN 2>/dev/null) || CPU_COUNT=2 @@ -219,7 +297,7 @@ then fail "HDSTACK must be an integer from 1 to 5" fi -BASE_ID=$(compose_environment_value HDSTACK_BASEID) +BASE_ID=$(compose_environment_value freedmr HDSTACK_BASEID) if [ -z "$BASE_ID" ] then BASE_ID=$(config_value GLOBAL SERVER_ID) @@ -234,20 +312,51 @@ then fail "the base server ID plus worker IDs exceeds the supported range" fi -APRS_CALL=$(compose_environment_value APRS_CALL) -[ -n "$APRS_CALL" ] || fail "the legacy D-APRS service has no usable APRS_CALL value" +if [ "$native_count" -eq 1 ] +then + APRS_CALL=$(compose_environment_value data-gateway FREEDMR_GATEWAY_APRS_LOGIN_CALL) + APRS_PASSCODE=$(compose_environment_value data-gateway FREEDMR_GATEWAY_APRS_PASSCODE) + NATIVE_RELATIONSHIPS=$(compose_environment_value data-gateway FREEDMR_GATEWAY_FBP_RELATIONSHIPS) + [ -n "$NATIVE_RELATIONSHIPS" ] || \ + fail "the native data-gateway service has no FBP relationship configuration" + NATIVE_FBP_KEY=$(json_string_value key "$NATIVE_RELATIONSHIPS") + NATIVE_GATEWAY_BIND_PORT=$(json_number_value bind_port "$NATIVE_RELATIONSHIPS") + NATIVE_GATEWAY_PEER_PORT=$(json_number_value peer_port "$NATIVE_RELATIONSHIPS") + [ -n "$NATIVE_FBP_KEY" ] || \ + fail "the native data-gateway relationship has no usable key" + [ -n "$NATIVE_GATEWAY_BIND_PORT" ] || \ + fail "the native data-gateway relationship has no usable bind port" + [ -n "$NATIVE_GATEWAY_PEER_PORT" ] || \ + fail "the native data-gateway relationship has no usable peer port" +else + APRS_CALL=$(compose_environment_value D-APRS APRS_CALL) + APRS_PASSCODE=$(compose_environment_value D-APRS APRS_PASSCODE) + NATIVE_RELATIONSHIPS= + NATIVE_FBP_KEY= + NATIVE_GATEWAY_BIND_PORT= + NATIVE_GATEWAY_PEER_PORT= +fi + +[ -n "$APRS_CALL" ] || fail "the reporting service has no usable APRS-IS login callsign" case "$APRS_CALL" in ''|*[!A-Za-z0-9-]*) fail "APRS-IS login callsign contains invalid characters" ;; esac [ "${#APRS_CALL}" -le 9 ] || fail "APRS-IS login callsign must not exceed 9 characters" -APRS_PASSCODE=$(compose_environment_value APRS_PASSCODE) [ -n "$APRS_PASSCODE" ] || \ - fail "the legacy D-APRS service has no usable APRS_PASSCODE value" + fail "the reporting service has no usable APRS-IS passcode" is_unsigned_integer "$APRS_PASSCODE" || fail "APRS-IS passcode must be numeric" FBP_KEY=$(config_value DATA-GATEWAY PASSPHRASE) -if [ -z "$FBP_KEY" ] || [ "$FBP_KEY" = "change-this" ] +if [ -n "$NATIVE_FBP_KEY" ] +then + if [ -n "$FBP_KEY" ] && [ "$FBP_KEY" != "change-this" ] && \ + [ "$FBP_KEY" != "$NATIVE_FBP_KEY" ] + then + fail "freedmr.cfg and the native data-gateway service use different FBP keys" + fi + FBP_KEY="$NATIVE_FBP_KEY" +elif [ -z "$FBP_KEY" ] || [ "$FBP_KEY" = "change-this" ] then FBP_KEY=$(od -An -N10 -tx1 /dev/urandom | tr -d ' \n') fi @@ -257,9 +366,21 @@ esac [ "${#FBP_KEY}" -le 20 ] || fail "the Data Gateway FBP key must not exceed 20 characters" GATEWAY_PEER_PORT=$(config_value DATA-GATEWAY PORT) +[ -n "$GATEWAY_PEER_PORT" ] || GATEWAY_PEER_PORT="$NATIVE_GATEWAY_PEER_PORT" [ -n "$GATEWAY_PEER_PORT" ] || GATEWAY_PEER_PORT=62041 +if [ -n "$NATIVE_GATEWAY_PEER_PORT" ] && \ + [ "$GATEWAY_PEER_PORT" != "$NATIVE_GATEWAY_PEER_PORT" ] +then + fail "freedmr.cfg and the native data-gateway service use different peer ports" +fi GATEWAY_BIND_PORT=$(config_value DATA-GATEWAY TARGET_PORT) +[ -n "$GATEWAY_BIND_PORT" ] || GATEWAY_BIND_PORT="$NATIVE_GATEWAY_BIND_PORT" [ -n "$GATEWAY_BIND_PORT" ] || GATEWAY_BIND_PORT=62031 +if [ -n "$NATIVE_GATEWAY_BIND_PORT" ] && \ + [ "$GATEWAY_BIND_PORT" != "$NATIVE_GATEWAY_BIND_PORT" ] +then + fail "freedmr.cfg and the native data-gateway service use different bind ports" +fi for gateway_port in "$GATEWAY_PEER_PORT" "$GATEWAY_BIND_PORT" do if ! is_unsigned_integer "$gateway_port" || \ @@ -276,14 +397,22 @@ done RELATIONSHIPS=$(build_relationships \ "$HDSTACK_COUNT" "$BASE_ID" "$FBP_KEY" \ "$GATEWAY_BIND_PORT" "$GATEWAY_PEER_PORT") +if [ "$native_count" -eq 1 ] && [ "$NATIVE_RELATIONSHIPS" != "$RELATIONSHIPS" ] +then + fail "the native data-gateway relationships do not match the configured HDStack layout" +fi WORK_DIR=$(mktemp -d "$CONFIG_DIR/.data-gateway-upgrade.XXXXXX") -trap 'rm -rf "$WORK_DIR"' EXIT PROPOSED_CONFIG="$WORK_DIR/freedmr.cfg" PROPOSED_COMPOSE="$WORK_DIR/docker-compose.yml" awk -v gateway_key="$FBP_KEY" \ -v gateway_port="$GATEWAY_PEER_PORT" \ - -v gateway_target_port="$GATEWAY_BIND_PORT" ' + -v gateway_target_port="$GATEWAY_BIND_PORT" \ + -v deployment_version="$TARGET_DEPLOYMENT_VERSION" ' + BEGIN { + print "# FREEDMR_DEPLOYMENT_VERSION=" deployment_version + } + /^# FREEDMR_DEPLOYMENT_VERSION=/ { next } function finish_section() { if (section == "GLOBAL" && !global_value_seen) print "DATA_GATEWAY: True" @@ -344,7 +473,9 @@ awk -v gateway_key="$FBP_KEY" \ } ' "$FREEDMR_CONFIG" > "$PROPOSED_CONFIG" -awk -v relationships="$RELATIONSHIPS" -v aprs_call="$APRS_CALL" -v aprs_passcode="$APRS_PASSCODE" ' +if [ "$native_count" -eq 0 ] +then + awk -v relationships="$RELATIONSHIPS" -v aprs_call="$APRS_CALL" -v aprs_passcode="$APRS_PASSCODE" ' function print_gateway() { print " data-gateway:" print " container_name: freedmr-data-gateway" @@ -392,6 +523,25 @@ awk -v relationships="$RELATIONSHIPS" -v aprs_call="$APRS_CALL" -v aprs_passcode print } ' "$COMPOSE_FILE" > "$PROPOSED_COMPOSE" +else + awk ' + { + if (skip_legacy) { + if ($0 ~ /^ [A-Za-z0-9_.-]+:[[:space:]]*$/ || + $0 ~ /^[^[:space:]#][^:]*:[[:space:]]*$/) { + skip_legacy = 0 + } else { + next + } + } + if ($0 ~ /^ D-APRS:[[:space:]]*$/) { + skip_legacy = 1 + next + } + print + } + ' "$COMPOSE_FILE" > "$PROPOSED_COMPOSE" +fi for required in \ 'DATA_GATEWAY: True' \ @@ -418,6 +568,13 @@ then fail "Docker Compose rejected the generated configuration" fi +if cmp -s "$FREEDMR_CONFIG" "$PROPOSED_CONFIG" && \ + cmp -s "$COMPOSE_FILE" "$PROPOSED_COMPOSE" +then + echo "FreeDMR deployment is already at version $TARGET_DEPLOYMENT_VERSION; no files were changed." + exit 0 +fi + if [ "$DRY_RUN" -eq 1 ] then echo "Dry run successful; no files were changed." @@ -437,7 +594,7 @@ then esac fi -stamp=$(date -u +%Y%m%dT%H%M%SZ) +stamp="$(date -u +%Y%m%dT%H%M%SZ)-$$" CONFIG_BACKUP="$FREEDMR_CONFIG.before-data-gateway.$stamp" COMPOSE_BACKUP="$COMPOSE_FILE.before-data-gateway.$stamp" [ ! -e "$CONFIG_BACKUP" ] || fail "backup already exists: $CONFIG_BACKUP" @@ -451,10 +608,17 @@ then chown --reference="$FREEDMR_CONFIG" "$PROPOSED_CONFIG" chown --reference="$COMPOSE_FILE" "$PROPOSED_COMPOSE" fi -mv "$PROPOSED_CONFIG" "$FREEDMR_CONFIG" -mv "$PROPOSED_COMPOSE" "$COMPOSE_FILE" +if ! mv "$PROPOSED_CONFIG" "$FREEDMR_CONFIG" +then + fail "could not install the upgraded FreeDMR configuration" +fi +if ! mv "$PROPOSED_COMPOSE" "$COMPOSE_FILE" +then + cp -p "$CONFIG_BACKUP" "$FREEDMR_CONFIG" + fail "could not install the upgraded Compose file; the FreeDMR configuration was restored" +fi -echo "FreeDMR Data Gateway migration complete." +echo "FreeDMR deployment upgrade to version $TARGET_DEPLOYMENT_VERSION complete." echo "Backups:" echo " $CONFIG_BACKUP" echo " $COMPOSE_BACKUP" diff --git a/docs/hdstack.md b/docs/hdstack.md index f590100..e5735cf 100644 --- a/docs/hdstack.md +++ b/docs/hdstack.md @@ -180,8 +180,10 @@ would address the FreeDMR container itself, not a separate gateway container. `upgrade-daprs-to-data-gateway.sh` migrates the documented legacy `D-APRS` Compose service to the native Data Gateway without replacing unrelated -FreeDMR configuration or Compose services. Download it, inspect it, and run a -dry run first: +FreeDMR configuration or Compose services. This deployment transition is not +performed by `docker pull`: current images require the corresponding deployment +version marker in the mounted `freedmr.cfg`. Download the script, inspect it, +and run a dry run before pulling and starting the new images: ```sh curl -fsSL \ @@ -194,19 +196,38 @@ sudo /tmp/upgrade-daprs-to-data-gateway.sh The script reads the server ID and any existing Data Gateway ports and non-placeholder passphrase from `freedmr.cfg`. It reads the HDStack settings -and legacy `APRS_CALL` and `APRS_PASSCODE` from `docker-compose.yml`. If -`HDSTACK` is -omitted, it applies the container's CPU-count-minus-one default, clamped to one -through five. A private FBP key is generated only when no usable one exists. -Missing or invalid existing values cause the migration to stop rather than -prompt for replacement configuration. +and APRS credentials from the legacy or native service in +`docker-compose.yml`. If `HDSTACK` is omitted, it applies the container's +CPU-count-minus-one default, clamped to one through five. A private FBP key is +generated only when no usable one exists. Missing, conflicting or invalid +existing values cause the migration to stop rather than guess. Before changing either file, the script validates the proposed Compose -configuration and asks for confirmation. It creates timestamped backups beside -`/etc/freedmr/freedmr.cfg` and `/etc/freedmr/docker-compose.yml`. It refuses an -unrecognised layout or an existing native `data-gateway` service rather than -guessing. The script does not pull images or restart containers; follow the -commands it prints after reviewing the generated files. +configuration and asks for confirmation. It uses a deployment lock and creates +timestamped backups beside `/etc/freedmr/freedmr.cfg` and +`/etc/freedmr/docker-compose.yml`. The operation is idempotent. It recognises a +complete native deployment and can resume when either the configuration or +Compose half of this migration was installed first. A complete native service +also takes precedence over a stale legacy service left beside it. Conflicting +keys, ports, malformed services and unrecognised layouts still stop for manual +review. The deployment marker is written only as part of a validated upgrade. + +The script does not pull images or restart containers; follow the commands it +prints after reviewing the generated files. If a current FreeDMR image is +started against an unversioned deployment, the container logs the required +upgrade and exits before starting FreeDMR. A sysop maintaining a deliberately +custom deployment may temporarily bypass that guard with: + +```yaml +environment: + - FREEDMR_ALLOW_UNMIGRATED_START=1 +``` + +The override logs a warning on every startup. It does not modify or advance the +deployment marker, and it does not assert that the custom configuration is +compatible. The guided installer validates its generated Compose file and +writes the same marker for new deployments. Merely copying an example +`freedmr.cfg` does not record a completed deployment upgrade. ## Session assignment diff --git a/tests/test_data_gateway_upgrade.py b/tests/test_data_gateway_upgrade.py index 780bdb4..ef7224d 100644 --- a/tests/test_data_gateway_upgrade.py +++ b/tests/test_data_gateway_upgrade.py @@ -10,6 +10,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SCRIPT = ROOT / "docker-configs" / "upgrade-daprs-to-data-gateway.sh" +DEPLOYMENT_CHECK = ROOT / "docker-configs" / "check-deployment-version.sh" class DataGatewayUpgradeTests(unittest.TestCase): @@ -139,6 +140,25 @@ class DataGatewayUpgradeTests(unittest.TestCase): [24501, 24502, 24503], ) + def test_ignores_legacy_variable_names_in_unrelated_service(self): + self.compose.write_text( + COMPOSE.replace("services:\n", "services:\n" + UNRELATED_SERVICE), + encoding="utf-8", + ) + + result = self.run_upgrade("--yes") + + self.assertEqual(result.returncode, 0, result.stderr) + compose = self.compose.read_text(encoding="utf-8") + self.assertIn(UNRELATED_SERVICE, compose) + self.assertIn("FREEDMR_GATEWAY_APRS_LOGIN_CALL: 'M0OLD'", compose) + relationships = self._relationships(compose) + self.assertEqual(len(relationships), 2) + self.assertEqual( + [item["peer_network_id"] for item in relationships], + [23401, 23402], + ) + def test_omitted_hdstack_uses_current_cpu_derived_default(self): self.compose.write_text( COMPOSE.replace(" - HDSTACK=2\n", ""), @@ -193,9 +213,114 @@ class DataGatewayUpgradeTests(unittest.TestCase): self.assertEqual(self.compose.read_bytes(), original_compose) self.assertEqual(list(self.config_dir.glob("*.before-data-gateway.*")), []) - def test_refuses_existing_native_service_without_changes(self): + def test_completed_upgrade_is_idempotent(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) + original_config = self.config.read_bytes() + original_compose = self.compose.read_bytes() + original_backups = sorted(self.config_dir.glob("*.before-data-gateway.*")) + + second = self.run_upgrade("--yes") + + self.assertEqual(second.returncode, 0, second.stderr) + self.assertIn("already at version 1", second.stdout) + self.assertEqual(self.config.read_bytes(), original_config) + self.assertEqual(self.compose.read_bytes(), original_compose) + self.assertEqual( + sorted(self.config_dir.glob("*.before-data-gateway.*")), + original_backups, + ) + + def test_ignores_native_variable_names_in_unrelated_service(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) self.compose.write_text( - COMPOSE.replace(" D-APRS:\n", " data-gateway:\n"), + self.compose.read_text(encoding="utf-8").replace( + "services:\n", + "services:\n" + UNRELATED_NATIVE_SERVICE, + ), + encoding="utf-8", + ) + original_config = self.config.read_bytes() + original_compose = self.compose.read_bytes() + + second = self.run_upgrade("--yes") + + self.assertEqual(second.returncode, 0, second.stderr) + self.assertIn("already at version 1", second.stdout) + self.assertEqual(self.config.read_bytes(), original_config) + self.assertEqual(self.compose.read_bytes(), original_compose) + + def test_resumes_after_configuration_was_installed_first(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) + migrated_config = self.config.read_bytes() + self.compose.write_text(COMPOSE, encoding="utf-8") + + resumed = self.run_upgrade("--yes") + + self.assertEqual(resumed.returncode, 0, resumed.stderr) + self.assertEqual(self.config.read_bytes(), migrated_config) + compose = self.compose.read_text(encoding="utf-8") + self.assertIn(" data-gateway:\n", compose) + self.assertNotIn(" D-APRS:\n", compose) + + def test_resumes_after_compose_was_installed_first(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) + migrated_compose = self.compose.read_bytes() + relationships = self._relationships(migrated_compose.decode("utf-8")) + expected_key = relationships[0]["key"] + self.config.write_text(CONFIG, encoding="utf-8") + + resumed = self.run_upgrade("--yes") + + self.assertEqual(resumed.returncode, 0, resumed.stderr) + self.assertEqual(self.compose.read_bytes(), migrated_compose) + parser = configparser.ConfigParser() + parser.read(self.config) + self.assertEqual(parser.get("DATA-GATEWAY", "PASSPHRASE"), expected_key) + self.assertTrue(parser.getboolean("DATA-GATEWAY", "ENABLED")) + self.assertIn( + "# FREEDMR_DEPLOYMENT_VERSION=1", + self.config.read_text(encoding="utf-8"), + ) + + def test_removes_stale_legacy_service_when_native_service_is_complete(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) + native = self.compose.read_text(encoding="utf-8") + self.compose.write_text( + native.replace( + " freedmrmonitor2:\n", + DAPRS_SERVICE + " freedmrmonitor2:\n", + ), + encoding="utf-8", + ) + + resumed = self.run_upgrade("--yes") + + self.assertEqual(resumed.returncode, 0, resumed.stderr) + compose = self.compose.read_text(encoding="utf-8") + self.assertEqual(compose.count(" data-gateway:\n"), 1) + self.assertNotIn(" D-APRS:\n", compose) + + def test_refuses_when_another_upgrade_holds_the_lock(self): + (self.config_dir / ".freedmr-upgrade.lock").mkdir() + + result = self.run_upgrade("--yes") + + self.assertEqual(result.returncode, 2) + self.assertIn("another FreeDMR deployment upgrade", result.stderr) + + def test_refuses_conflicting_native_relationship_layout(self): + first = self.run_upgrade("--yes") + self.assertEqual(first.returncode, 0, first.stderr) + self.compose.write_text( + self.compose.read_text(encoding="utf-8").replace( + '"peer_network_id":23402', + '"peer_network_id":99999', + ), encoding="utf-8", ) original_config = self.config.read_bytes() @@ -204,7 +329,7 @@ class DataGatewayUpgradeTests(unittest.TestCase): result = self.run_upgrade("--yes") self.assertEqual(result.returncode, 2) - self.assertIn("already exists", result.stderr) + self.assertIn("do not match the configured HDStack layout", result.stderr) self.assertEqual(self.config.read_bytes(), original_config) self.assertEqual(self.compose.read_bytes(), original_compose) @@ -234,6 +359,85 @@ class DataGatewayUpgradeTests(unittest.TestCase): return json.loads(match.group(1)) +class DeploymentVersionCheckTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory(dir=ROOT / "tests") + self.addCleanup(self.temporary.cleanup) + self.config = Path(self.temporary.name) / "freedmr.cfg" + + def run_check(self, *, override=None): + environment = os.environ.copy() + if override is not None: + environment["FREEDMR_ALLOW_UNMIGRATED_START"] = override + return subprocess.run( + ["/bin/sh", str(DEPLOYMENT_CHECK), str(self.config)], + check=False, + capture_output=True, + text=True, + env=environment, + ) + + def test_current_deployment_is_accepted_silently(self): + self.config.write_text( + "# FREEDMR_DEPLOYMENT_VERSION=1\n[GLOBAL]\n", + encoding="utf-8", + ) + + result = self.run_check() + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stderr, "") + + def test_unversioned_deployment_is_stopped_with_upgrade_message(self): + self.config.write_text("[GLOBAL]\n", encoding="utf-8") + + result = self.run_check() + + self.assertEqual(result.returncode, 78) + self.assertIn("deployment upgrade required", result.stderr) + self.assertIn("Installed deployment version: 0", result.stderr) + self.assertIn("upgrade-daprs-to-data-gateway.sh", result.stderr) + + def test_explicit_override_allows_unversioned_deployment(self): + self.config.write_text("[GLOBAL]\n", encoding="utf-8") + + result = self.run_check(override="1") + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("WARNING: starting with an unverified", result.stderr) + self.assertNotIn( + "FREEDMR_DEPLOYMENT_VERSION", + self.config.read_text(encoding="utf-8"), + ) + + def test_invalid_or_duplicate_marker_is_stopped(self): + self.config.write_text( + "# FREEDMR_DEPLOYMENT_VERSION=1\n" + "# FREEDMR_DEPLOYMENT_VERSION=1\n" + "[GLOBAL]\n", + encoding="utf-8", + ) + + result = self.run_check() + + self.assertEqual(result.returncode, 78) + self.assertIn("Installed deployment version: invalid", result.stderr) + + def test_startup_check_and_upgrader_require_the_same_version(self): + checker = DEPLOYMENT_CHECK.read_text(encoding="utf-8") + upgrader = SCRIPT.read_text(encoding="utf-8") + required = re.search(r"^REQUIRED_VERSION=([0-9]+)$", checker, re.MULTILINE) + target = re.search( + r"^TARGET_DEPLOYMENT_VERSION=([0-9]+)$", + upgrader, + re.MULTILINE, + ) + + self.assertIsNotNone(required) + self.assertIsNotNone(target) + self.assertEqual(required.group(1), target.group(1)) + + CONFIG = """\ [GLOBAL] SERVER_ID: 23400 @@ -272,6 +476,41 @@ ENABLED: True MODE: MASTER """ +DAPRS_SERVICE = """\ + D-APRS: + container_name: D-APRS + environment: + - APRS_CALL=M0OLD + - APRS_PASSCODE=54321 + image: legacy-daprs:test + restart: "unless-stopped" + networks: + app_net: + ipv4_address: 172.16.238.40 + +""" + +UNRELATED_SERVICE = """\ + unrelated: + image: unrelated:test + environment: + - HDSTACK=5 + - HDSTACK_BASEID=99900 + - APRS_CALL=WRONG + - APRS_PASSCODE=99999 + +""" + +UNRELATED_NATIVE_SERVICE = """\ + unrelated-native: + image: unrelated:test + environment: + FREEDMR_GATEWAY_APRS_LOGIN_CALL: 'WRONG' + FREEDMR_GATEWAY_APRS_PASSCODE: '99999' + FREEDMR_GATEWAY_FBP_RELATIONSHIPS: 'not-the-gateway-value' + +""" + COMPOSE = """\ version: '2.4' services: @@ -285,17 +524,7 @@ services: app_net: ipv4_address: 172.16.238.10 - D-APRS: - container_name: D-APRS - environment: - - APRS_CALL=M0OLD - - APRS_PASSCODE=54321 - image: legacy-daprs:test - restart: "unless-stopped" - networks: - app_net: - ipv4_address: 172.16.238.40 - +""" + DAPRS_SERVICE + """\ freedmrmonitor2: container_name: freedmrmonitor2 image: monitor:test diff --git a/tests/test_hdstack.py b/tests/test_hdstack.py index 1ae3286..0cfcf6e 100644 --- a/tests/test_hdstack.py +++ b/tests/test_hdstack.py @@ -475,6 +475,7 @@ class HDStackContainerTests(unittest.TestCase): self.assertIn('HDSTACK_BRIDGE_CONFIG', entrypoint) self.assertIn('HDSTACK_BRIDGE_RULES', entrypoint) self.assertIn('/^\\[GLOBAL\\]/', entrypoint) + self.assertIn('check-deployment-version.sh', entrypoint) def test_standard_compose_enables_cpu_derived_hdstack_and_skeleton_mounts(self): compose = (DOCKER_CONFIGS / 'docker-compose.yml').read_text(encoding='utf-8') @@ -504,6 +505,7 @@ class HDStackContainerTests(unittest.TestCase): self.assertIn("FREEDMR_GATEWAY_INGRESS_MODE: fbp", compose) self.assertIn("FREEDMR_GATEWAY_FBP_RELATIONSHIPS:", compose) self.assertIn("/etc/freedmr/json/:/data/:ro", compose) + self.assertIn("FREEDMR_ALLOW_UNMIGRATED_START=1", compose) def test_installer_records_guided_hdstack_and_bridge_choices(self): installer = ( @@ -531,6 +533,8 @@ class HDStackContainerTests(unittest.TestCase): self.assertIn('TARGET_IP: freedmr-data-gateway', installer) self.assertIn('s/^SERVER_ID:.*/SERVER_ID: $HDSTACK_BASEID/', installer) self.assertIn('s/^SERVER_ID:.*/SERVER_ID: $HDSTACK_BRIDGE_ID/', installer) + self.assertIn('docker compose -f /etc/freedmr/docker-compose.yml config --quiet', installer) + self.assertIn("1i# FREEDMR_DEPLOYMENT_VERSION=1", installer) self.assertNotIn('/etc/freedmr/.env', installer) def test_installer_uses_current_debian_docker_repository(self):