Skip to content

Commit

Permalink
update compose & compose test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen committed Oct 2, 2023
1 parent 7556581 commit f7cb715
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/dappmanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "npm run test:file \"./{,!(node_modules)/**}/*.test.ts\" ",
"test:file": "mocha",
"test:int": "npm run test:int:file \"./{,!(node_modules)/**}/*.test.int.ts\" ",
"test:int:file": "mocha --config ./test/integration/.mocharc.yaml --timeout 120000",
"test:int:file": "mocha --config ./test/integration/.mocharc.yaml --timeout 180000",
"test:all": "npm run test && npm run test:int",
"lint": "eslint . --ext .ts --fix",
"prettier": "prettier --write 'src/**/*.*' 'test/**/*.*'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,53 @@ export function migrateCoreNetworkAndAliasInCompose(

const rawServiceNetworks = compose.services()[container.serviceName].get().networks;

if (rawServiceNetworks) {
// Gets all the networks defined in the service
const serviceNetworks = parseServiceNetworks(rawServiceNetworks);
if (!rawServiceNetworks) {
throw Error(`No networks found in ${container.serviceName} service`);
}

const dncoreServiceNetwork = serviceNetworks[params.DNP_PRIVATE_NETWORK_NAME];
const serviceNetworks = parseServiceNetworks(rawServiceNetworks);

// Gets current aliases of "params.DNP_PRIVATE_NETWORK_NAME", usually dncore_network
const currentDncoreNetworkAliases = dncoreServiceNetwork.aliases || [];
const dncoreServiceNetwork = serviceNetworks[params.DNP_PRIVATE_NETWORK_NAME];

//add new aliases to current aliases set
const newAliases = uniq([...currentDncoreNetworkAliases, ...aliases]);
if (!dncoreServiceNetwork) {
throw Error(`No "dncore_network" found in ${container.serviceName} service`);
}

// Gets the network "dncore_network" from the general compose file
const dncoreComposeNetwork = compose.getComposeNetwork(params.DNP_PRIVATE_NETWORK_NAME);
const currentDncoreNetworkAliases = dncoreServiceNetwork.aliases || [];

// Return if migration was done, compose is already updated
if (isComposeNetworkAndAliasMigrated(dncoreComposeNetwork, dncoreServiceNetwork, compose.compose.version, newAliases)) return
//add new aliases to current aliases set
const newAliases = uniq([...currentDncoreNetworkAliases, ...aliases]);

// Ensure/update compose file version 3.5
compose.compose = {
...compose.compose,
version: params.MINIMUM_COMPOSE_VERSION
};
// Gets the network "dncore_network" from the general compose file
const dncoreComposeNetwork = compose.getComposeNetwork(params.DNP_PRIVATE_NETWORK_NAME);

// Gets the old network "network" from the service compose file. This is an old network that is to be removed".
// If it is already removed, returns {}
const oldServiceNetwork = serviceNetworks[params.DNP_PRIVATE_NETWORK_NAME_FROM_CORE];

// This tries to remove the old network called "network" from the container in the compose file
// It is only done if network "network" exists or the new network "dncore_network" exists
if (dncoreComposeNetwork || !isEmpty(oldServiceNetwork)) {
compose.services()[container.serviceName].removeNetwork(params.DNP_PRIVATE_NETWORK_NAME_FROM_CORE);
}
// Return if migration was done, compose is already updated
if (isComposeNetworkAndAliasMigrated(dncoreComposeNetwork, dncoreServiceNetwork, compose.compose.version, newAliases)) return;

// Ensure/update compose file version 3.5
compose.compose = {
...compose.compose,
version: params.MINIMUM_COMPOSE_VERSION
};

// This adds the new network with the new aliases into the compose file
compose.services()[container.serviceName].addNetwork(
params.DNP_PRIVATE_NETWORK_NAME,
{ ...oldServiceNetwork, aliases: newAliases },
{ external: true, name: params.DNP_PRIVATE_NETWORK_NAME }
);
// Gets the old network "network" from the service compose file. This is an old network that is to be removed".
// If it is already removed, returns {}
const oldServiceNetwork = serviceNetworks[params.DNP_PRIVATE_NETWORK_NAME_FROM_CORE];

compose.write();
} else {
// Throw error if no networks defined in the service
throw Error(`No networks defined in ${container.serviceName} service`);
// This tries to remove the old network called "network" from the container in the compose file
// It is only done if network "network" exists or the new network "dncore_network" exists
if (dncoreComposeNetwork || !isEmpty(oldServiceNetwork)) {
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(
params.DNP_PRIVATE_NETWORK_NAME,
{ ...oldServiceNetwork, aliases: newAliases },
{ external: true, name: params.DNP_PRIVATE_NETWORK_NAME }
);

compose.write();
}

// function isMainServiceOfMultiServicePackage(container: PackageContainer): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe("Migration", () => {
version: '3.4'
networks:
dncore_network:
name: dncore_network
external: true
name: dncore_network
services:
dappmanager.dnp.dappnode.eth:
image: "chentex/random-logger"
Expand All @@ -36,19 +36,17 @@ services:
const composeToBeMigratedBefore = `
version: '3.4'
networks:
network:
driver: bridge
ipam:
config:
- subnet: 172.33.0.0/16
dncore_network:
name: dncore_network
external: true
services:
dappmanager.dnp.dappnode.eth:
image: "chentex/random-logger"
container_name: DAppNodeCore-dappmanager.dnp.dappnode.eth
restart: always
dns: 172.33.1.2
networks:
network:
dncore_network:
ipv4_address: 172.33.1.7`;

const dncoreNetwork = params.DNP_PRIVATE_NETWORK_NAME;
Expand Down Expand Up @@ -94,8 +92,8 @@ services:
version: '3.5'
networks:
dncore_network:
external: true
name: dncore_network
external: true
services:
dappmanager.dnp.dappnode.eth:
image: chentex/random-logger
Expand All @@ -107,11 +105,10 @@ services:
ipv4_address: 172.33.1.7
aliases:
- dappmanager.dnp.dappnode.eth.test-migration.dappnode`;

migrateCoreNetworkAndAliasInCompose(
container,
["dappmanager.dnp.dappnode.eth.test-migration.dappnode"]
);

const aliases = ["dappmanager.dnp.dappnode.eth.test-migration.dappnode"];
migrateCoreNetworkAndAliasInCompose(container, aliases);

const composeAfter = fs.readFileSync(
`${testMigrationPath}/test-migration/docker-compose.yml`,
{ encoding: "utf8" }
Expand All @@ -120,10 +117,9 @@ services:
});

it("Should do not do migration", async () => {
migrateCoreNetworkAndAliasInCompose(
container,
["dappmanager.dnp.dappnode.eth.test-migration.dappnode"]
);
const aliases = ["dappmanager.dnp.dappnode.eth.test-migration.dappnode"];
migrateCoreNetworkAndAliasInCompose(container, aliases);

const composeAfter = fs.readFileSync(
`${testMigrationPath}/test-migration/docker-compose-migrated.yml`,
{ encoding: "utf8" }
Expand Down

0 comments on commit f7cb715

Please sign in to comment.