-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: removing only networks started by hedera instead of global prune #686
Merged
georgi-l95
merged 8 commits into
hiero-ledger:main
from
arianejasuwienas:remove-selected-networks-instead-of-prune
Jul 17, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dce9a2
fix: removing only networks started by hedera instead of global prune
arianejasuwienas 728fc89
fix: typo
arianejasuwienas 1c19719
fix: removing all the networks with hedera-prefix instead of the ones…
arianejasuwienas a77e477
fix: removing the bridge suffix from one of the bridge typed networks…
arianejasuwienas 851de02
fix: removing extra networks
arianejasuwienas 358a4b6
fix: ignoring network in evm yaml files
arianejasuwienas 4e8569b
fix: ignoring network in multinode yaml files since they excend servi…
arianejasuwienas 6cfd740
fix: ignoring network in multinode yaml files since they excend servi…
arianejasuwienas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*- | ||
* | ||
* Hedera Local Node | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import shell from 'shelljs'; | ||
import { IS_WINDOWS, NETWORK_PREFIX } from '../constants'; | ||
|
||
/** | ||
* Checks if the given string is a valid Docker network ID. | ||
* A valid Docker network ID is a 12-character hexadecimal string. | ||
* | ||
* @param {string} id - The string to be validated as a Docker network ID. | ||
* @returns {boolean} - Returns true if the string is a valid Docker network ID, false otherwise. | ||
* | ||
* @example | ||
* const id = "89ded1eca1d5"; | ||
* console.log(isCorrectDockerId(id)); // Output: true | ||
* | ||
* @example | ||
* const invalidId = "invalidID123"; | ||
* console.log(isCorrectDockerId(invalidId)); // Output: false | ||
*/ | ||
const isCorrectDockerId = (id: string) => id.trim() !== '' && /^[a-f0-9]{12}$/.test(id); | ||
|
||
/** | ||
* Provides utility methods for safe networks removal. | ||
*/ | ||
export class SafeDockerNetworkRemover { | ||
/** | ||
* Removes all the networks started by docker compose. Only networks with the "hedera-" prefix will be affected. | ||
*/ | ||
public static removeAll() { | ||
const result = shell.exec(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`); | ||
if (!result || result.stderr !== '') { | ||
return; | ||
} | ||
result.stdout.split('\n').filter(isCorrectDockerId).forEach((id) => { | ||
shell.exec(`docker network rm ${id} -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/*- | ||
* | ||
* Hedera Local Node | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { SafeDockerNetworkRemover } from '../../../src/utils/SafeDockerNetworkRemover'; | ||
import { IS_WINDOWS, NETWORK_PREFIX } from '../../../src/constants'; | ||
import { getTestBed } from '../testBed'; | ||
import { load } from 'js-yaml'; | ||
import { readdirSync, readFileSync } from 'fs'; | ||
import { join } from "path"; | ||
|
||
describe('SafeDockerNetworkRemover', () => { | ||
let shellExecStub: sinon.SinonStub; | ||
|
||
before(() => { | ||
let { | ||
shellStubs | ||
} = getTestBed({ | ||
workDir: 'testDir', | ||
}); | ||
|
||
shellExecStub = shellStubs.shellExecStub; | ||
}); | ||
|
||
after(() => { | ||
shellExecStub.restore(); | ||
}); | ||
|
||
describe('removeByName', () => { | ||
it('should not proceed if docker network ls command fails', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stderr: 'error' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledOnce).to.be.true; | ||
expect(shellExecStub.calledWith(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`)).to.be.true; | ||
}); | ||
|
||
it('should not proceed if docker network ls command returns no result', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: '', stderr: '' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`)).to.be.true; | ||
}); | ||
|
||
it('should remove valid Docker network IDs', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: '89ded1eca1d5\ninvalidID123\n', stderr: '' }); | ||
shellExecStub.withArgs(`docker network rm 89ded1eca1d5 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`).returns({}); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network rm 89ded1eca1d5 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.true; | ||
}); | ||
|
||
it('should not remove invalid Docker network IDs', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: 'invalidID123\nanotherInvalid\n', stderr: '' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network rm invalidID123 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.false; | ||
expect(shellExecStub.calledWith(`docker network rm anotherInvalid -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('config check', () => { | ||
it('check if all of the networks from composer yaml files are listed in the NETWORK_NAMES const', () => { | ||
const relativePath = '../../..'; | ||
const files = readdirSync(join(__dirname, relativePath)).filter(name => /^docker-compose.*\.yml$/.test(name)); | ||
for (const file of files) { | ||
const data = readFileSync(join(__dirname, `${relativePath}/${file}`)); | ||
const config = load(data.toString()); | ||
for (const network of Object.values(config.networks || {}).map((network: any) => network.name.trim())) { | ||
expect(network.startsWith(NETWORK_PREFIX), `Network '${network}' does not start with the prefix '${NETWORK_PREFIX}'. It won't be removed by 'npm run stop'`).to.be.true; | ||
} | ||
} | ||
}); | ||
it('check if all services have a network set and all network names start with "hedera-"', () => { | ||
const relativePath = '../../..'; | ||
const files = readdirSync(join(__dirname, relativePath)).filter(name => /^docker-compose(?!.*(evm|multinode)\.yml$).*\.yml$/.test(name)); | ||
for (const file of files) { | ||
const data = readFileSync(join(__dirname, `${relativePath}/${file}`)); | ||
const config = load(data.toString()); | ||
const services = config.services || {}; | ||
for (const [serviceName, serviceConfig] of Object.entries(services)) { | ||
if (serviceConfig.extends || (serviceConfig.network_mode || '' === 'none')) { | ||
continue; // The child service might have inherited the network. There is no network in non-network mode. | ||
} | ||
const networks = serviceConfig.networks; | ||
expect(networks, `Service '${serviceName}' does not have a network set.`).to.exist; | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe for consistency's sake all networks should have a -bridge suffix, or none of them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@isavov
I chose the latter option since there is only one network with the “bridge” suffix and a few without it.
I have only updated the name used by Docker, not the Docker Compose level alias, as changing the alias seemed to be outside the current scope.