Skip to content

Commit

Permalink
rebase to develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen committed Sep 22, 2023
1 parent 0abe6eb commit 768c78b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/dappmanager/src/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -44,22 +44,15 @@ export async function addAliasToRunningContainers(): Promise<void> {
export async function addAliasToGivenContainers(containers: PackageContainer[]): Promise<void> {
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);
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ 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;

const monoContainer: PackageContainer = {
...mockContainer,
containerName: "DAppNodeTest-logger.dnp.dappnode.eth",
dnpName: `${DNP_NAME_MONO}`,
serviceName: "monoService",
serviceName: `${DNP_NAME_MONO}`,
isCore: false,
};

Expand Down Expand Up @@ -58,7 +55,7 @@ services:
const MONO_COMPOSE = `
version: '3.4'
services:
monoService:
${DNP_NAME_MONO}:
image: "chentex/random-logger"
container_name: ${monoContainer.containerName}
`;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 768c78b

Please sign in to comment.