diff --git a/packages/migrations/src/index.ts b/packages/migrations/src/index.ts index 78c50cf6e..c86d4737e 100644 --- a/packages/migrations/src/index.ts +++ b/packages/migrations/src/index.ts @@ -1,8 +1,7 @@ import { migrateUserActionLogs } from "./migrateUserActionLogs.js"; import { removeLegacyDockerAssets } from "./removeLegacyDockerAssets.js"; -import { addAliasToRunningContainers } from "./addAliasToRunningContainers.js"; +import { removeDnsAndAddAlias } from "./removeDnsAndAddAlias.js"; import { pruneUserActionLogs } from "./pruneUserActionLogs.js"; -import { removeDnsFromComposeFiles } from "./removeDnsFromComposeFiles.js"; import { migrateDockerNetworkIpRange } from "./migrateDockerNetworkIpRange/index.js"; import { recreateContainersIfLegacyDns } from "./recreateContainersIfLegacyDns.js"; import { ensureCoreComposesHardcodedIpsRange } from "./ensureCoreComposesHardcodedIpsRange.js"; @@ -62,15 +61,6 @@ export async function executeMigrations( }) ); - await removeDnsFromComposeFiles().catch((e) => - migrationErrors.push({ - migration: "remove bind DNS from docker compose files", - coreVersion: "0.2.82", - name: "MIGRATION_ERROR", - message: e - }) - ); - await ensureCoreComposesHardcodedIpsRange().catch((e) => migrationErrors.push({ migration: "ensure core composes files has correct hardcoded IPs in range", @@ -106,7 +96,7 @@ export async function executeMigrations( }) ); - await addAliasToRunningContainers().catch((e) => + await removeDnsAndAddAlias().catch((e) => migrationErrors.push({ migration: "add docker alias to running containers", coreVersion: "0.2.80", diff --git a/packages/migrations/src/addAliasToRunningContainers.ts b/packages/migrations/src/removeDnsAndAddAlias.ts similarity index 87% rename from packages/migrations/src/addAliasToRunningContainers.ts rename to packages/migrations/src/removeDnsAndAddAlias.ts index d4671925d..0bbf87fd2 100644 --- a/packages/migrations/src/addAliasToRunningContainers.ts +++ b/packages/migrations/src/removeDnsAndAddAlias.ts @@ -8,7 +8,8 @@ import { dockerComposeUp, dockerNetworkReconnect, listPackageContainers, - getNetworkContainerConfig + getNetworkContainerConfig, + listPackages } from "@dappnode/dockerapi"; import { gte, lt, clean } from "semver"; import { getDockerComposePath, getIsMonoService, getPrivateNetworkAliases, shell } from "@dappnode/utils"; @@ -26,11 +27,36 @@ const dncoreNetworkName = params.DOCKER_PRIVATE_NETWORK_NAME; * "service1.example.dappnode" if the package is multiservice * "service1.example.dappnode" and "example.dappnode" if the package is multiservice and has in manifest mainservice */ -export async function addAliasToRunningContainers(): Promise { +export async function removeDnsAndAddAlias(): Promise { + const packages = await listPackages(); + for (const pkg of packages) removeDnsFromPackageComposeFile(pkg.dnpName, pkg.isCore); + const containers = await listPackageContainers(); await addAliasToGivenContainers(containers); } +export function removeDnsFromPackageComposeFile(dnpName: string, isCore: boolean): void { + const compose = new ComposeFileEditor(dnpName, isCore); + const services = compose.services(); + + for (const serviceName of Object.keys(services)) { + try { + const composeService = services[serviceName].get(); + // check composeService has the key dns + + if (Object.prototype.hasOwnProperty.call(composeService, "dns")) { + logs.info(`Removing DNS from ${serviceName} in ${dnpName} compose file`); + // setting undefined a yaml property might result into an error afterwards making js-yaml + // adding the following value to the undefined `Error parsing YAML: unknown tag !` + delete composeService.dns; + compose.write(); + } + } catch (e) { + logs.error(`Error removing DNS from ${serviceName} in ${dnpName} compose file`, e); + } + } +} + export async function addAliasToGivenContainers(containers: PackageContainer[]): Promise { for (const container of containers) { try { diff --git a/packages/migrations/src/removeDnsFromComposeFiles.ts b/packages/migrations/src/removeDnsFromComposeFiles.ts deleted file mode 100644 index 6d0aec346..000000000 --- a/packages/migrations/src/removeDnsFromComposeFiles.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { listPackages } from "@dappnode/dockerapi"; -import { ComposeFileEditor } from "@dappnode/dockercompose"; -import { logs } from "@dappnode/logger"; - -/** - * Migration to allow removal of current Bind functionality - * - * DNS resolution should be handled by the Docker DNS, not by Bind package - * - * For every service in every compose file, we must make sure it does not include - * the dns configuration - */ -export async function removeDnsFromComposeFiles(): Promise { - const packages = await listPackages(); - - for (const pkg of packages) { - removeDnsFromPackageComposeFile(pkg.dnpName, pkg.isCore); - } -} - -export function removeDnsFromPackageComposeFile(dnpName: string, isCore: boolean): void { - const compose = new ComposeFileEditor(dnpName, isCore); - const services = compose.services(); - - for (const serviceName of Object.keys(services)) { - const composeService = services[serviceName].get(); - // check composeService has the key dns - - if (Object.prototype.hasOwnProperty.call(composeService, "dns")) { - logs.info(`Removing DNS from ${serviceName} in ${dnpName} compose file`); - // setting undefined a yaml property might result into an error afterwards making js-yaml - // adding the following value to the undefined `Error parsing YAML: unknown tag !` - delete composeService.dns; - compose.write(); - } - } -}