From 768c78bacffbcd34a651089654ad007e7053019c Mon Sep 17 00:00:00 2001 From: Marc Font Date: Fri, 22 Sep 2023 17:54:01 +0200 Subject: [PATCH] rebase to develop --- packages/dappmanager/src/domains.ts | 2 +- .../migrations/addAliasToRunningContainers.ts | 35 +++++++------------ .../addAliasToRunningContainers.test.ts | 15 ++++---- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/packages/dappmanager/src/domains.ts b/packages/dappmanager/src/domains.ts index 255287f699..f0a37b7706 100644 --- a/packages/dappmanager/src/domains.ts +++ b/packages/dappmanager/src/domains.ts @@ -21,7 +21,7 @@ export function stripBadDomainChars(s: string): string { * - "other.public.dappnode.eth" > "other" */ export function shortUniqueDappnodeEns(dnpName: string): string { - for (const s of [".public.dappnode.eth",".dnp.dappnode.eth", ".dappnode.eth", ".eth"]) + for (const s of [".dnp.dappnode.eth", ".dappnode.eth", ".eth"]) if (dnpName.endsWith(s)) dnpName = dnpName.slice(0, -s.length); return stripCharacters(dnpName); } diff --git a/packages/dappmanager/src/modules/migrations/addAliasToRunningContainers.ts b/packages/dappmanager/src/modules/migrations/addAliasToRunningContainers.ts index c0de34292b..e9c61352f1 100644 --- a/packages/dappmanager/src/modules/migrations/addAliasToRunningContainers.ts +++ b/packages/dappmanager/src/modules/migrations/addAliasToRunningContainers.ts @@ -2,7 +2,7 @@ import { ComposeNetwork, ComposeServiceNetwork } from "@dappnode/types"; import Dockerode from "dockerode"; import { uniq } from "lodash-es"; import { PackageContainer } from "@dappnode/common"; -import { getPrivateNetworkAlias } from "../../domains.js"; +import { getPrivateNetworkAliases } from "../../domains.js"; import { logs } from "@dappnode/logger"; import { params } from "@dappnode/params"; import { parseComposeSemver } from "../../utils/sanitizeVersion.js"; @@ -44,22 +44,15 @@ export async function addAliasToRunningContainers(): Promise { export async function addAliasToGivenContainers(containers: PackageContainer[]): Promise { for (const container of containers) { - // Gets the main alias for the container. - // If mono service, it will be "dappnodename.dappnode" - // If multiservice, it will be "service.dappnodename.dappnode" - const aliasesToMigrate = [getPrivateNetworkAlias(container)]; - - // Adds "root" alias to multiservice container if it is the main service - // "root" alias is always "dappnodename.dappnode", as if it was a mono service - if (isMainServiceOfMultiServicePackage(container)) { - aliasesToMigrate.push(getPrivateNetworkAlias({ dnpName: container.dnpName, serviceName: '' })); - } + const isMain = container.isMain ?? false; // Set a default value of false if isMain is undefined + const service = { serviceName: container.serviceName, dnpName: container.dnpName, isMain } + const aliases = getPrivateNetworkAliases(service) // Adds aliases to the compose file that generated the container - migrateCoreNetworkAndAliasInCompose(container, aliasesToMigrate); + migrateCoreNetworkAndAliasInCompose(container, aliases); // Adds aliases to the container network - for (const alias of aliasesToMigrate) { + for (const alias of aliases) { const currentEndpointConfig = await getDnCoreNetworkContainerConfig(container.containerName); if (!hasAlias(currentEndpointConfig, alias)) { const updatedConfig = updateEndpointConfig(currentEndpointConfig, alias); @@ -108,10 +101,6 @@ export function migrateCoreNetworkAndAliasInCompose( version: params.MINIMUM_COMPOSE_VERSION }; - // This removes the old network "network", might not be necessary anymore - if (composeNetwork || serviceNetwork) { - compose.services()[container.serviceName].removeNetwork(params.DNP_PRIVATE_NETWORK_NAME_FROM_CORE); - } // This adds the new network with the new aliases into the compose file compose.services()[container.serviceName].addNetwork( @@ -123,12 +112,12 @@ export function migrateCoreNetworkAndAliasInCompose( compose.write(); } -function isMainServiceOfMultiServicePackage(container: PackageContainer): boolean { - const compose = new ComposeFileEditor(container.dnpName, container.isCore); - const services = compose.services(); // Invoke the services function - if (Object.keys(services).length > 1 && container.isMain) return true; - return false; -} +// function isMainServiceOfMultiServicePackage(container: PackageContainer): boolean { +// const compose = new ComposeFileEditor(container.dnpName, container.isCore); +// const services = compose.services(); // Invoke the services function +// if (Object.keys(services).length > 1 && container.isMain) return true; +// return false; +// } function updateEndpointConfig(currentEndpointConfig: Dockerode.NetworkInfo | null, alias: string) { return { diff --git a/packages/dappmanager/test/unit/modules/docker/addAliasToRunningContainers.test.ts b/packages/dappmanager/test/unit/modules/docker/addAliasToRunningContainers.test.ts index d9c3fda067..9aa0274c0f 100644 --- a/packages/dappmanager/test/unit/modules/docker/addAliasToRunningContainers.test.ts +++ b/packages/dappmanager/test/unit/modules/docker/addAliasToRunningContainers.test.ts @@ -4,15 +4,12 @@ import { expect } from "chai"; import { PackageContainer } from "@dappnode/common"; import { addAliasToGivenContainers } from "../../../../src/modules/migrations/addAliasToRunningContainers"; import { mockContainer, shellSafe } from "../../../testUtils.js"; -import params from "../../../../src/params.js"; - -params.REPO_DIR = `${process.cwd()}/test/unit/modules/docker/test-alias/`; -params.DNP_PRIVATE_NETWORK_NAME = "dncore_network"; +import { params } from "../../../../../params/src/index.js"; const DNP_NAME = "logger.dnp.dappnode.eth"; const DNP_NAME_MONO = "logger-mono.dnp.dappnode.eth"; -const TEST_ALIAS_PATH = `${process.cwd()}/test/unit/modules/docker/test-alias/${DNP_NAME}`; -const TEST_ALIAS_PATH_MONO = `${process.cwd()}/test/unit/modules/docker/test-alias/${DNP_NAME_MONO}`; +const TEST_ALIAS_PATH = `dnp_repo/${DNP_NAME}`; +const TEST_ALIAS_PATH_MONO = `dnp_repo/${DNP_NAME_MONO}`; const DNCORE_NETWORK = params.DNP_PRIVATE_NETWORK_NAME; @@ -20,7 +17,7 @@ const monoContainer: PackageContainer = { ...mockContainer, containerName: "DAppNodeTest-logger.dnp.dappnode.eth", dnpName: `${DNP_NAME_MONO}`, - serviceName: "monoService", + serviceName: `${DNP_NAME_MONO}`, isCore: false, }; @@ -58,7 +55,7 @@ services: const MONO_COMPOSE = ` version: '3.4' services: - monoService: + ${DNP_NAME_MONO}: image: "chentex/random-logger" container_name: ${monoContainer.containerName} `; @@ -120,7 +117,7 @@ describe("Add alias to running containers", function() { // Define the expected aliases. These should match the aliases added by the `addAliasToGivenContainers` function. const expectedMainAliases = ["mainService.logger.dappnode", "logger.dappnode"]; const expectedNotMainAliases = ["notmainService.logger.dappnode"]; - const expectedMonoContainerAliases = ["monoService.logger-mono.dappnode"]; + const expectedMonoContainerAliases = ["logger-mono.dappnode"]; // Assert that the actual aliases match our expectations expect(containerMainAliases).to.include.members(expectedMainAliases);