Skip to content

Commit

Permalink
Merge pull request #131 from dappnode/v0.1.16
Browse files Browse the repository at this point in the history
v0.1.16

Former-commit-id: 3e3f6d8
  • Loading branch information
eduadiez authored Nov 15, 2018
2 parents eea48b6 + 5893ecb commit 1c88266
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 35 deletions.
24 changes: 14 additions & 10 deletions build/src/src/calls/restartPackageVolumes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const getPath = require('utils/getPath');
const parse = require('utils/parse');
const params = require('params');
const docker = require('modules/docker');
const dockerList = require('modules/dockerList');
const {eventBus, eventBusTag} = require('eventBus');


Expand All @@ -18,28 +18,32 @@ const {eventBus, eventBusTag} = require('eventBus');
async function restartPackageVolumes({
id,
}) {
const dnpList = await dockerList.listContainers();
const dnp = dnpList.find((_dnp) => _dnp.name && _dnp.name.includes(id));
if (!dnp) {
throw Error(`Could not found an container with the name: ${id}`);
}
const dockerComposePath = getPath.dockerComposeSmart(id, params);
if (!fs.existsSync(dockerComposePath)) {
throw Error('No docker-compose found: ' + dockerComposePath);
}

if (id.includes('dappmanager.dnp.dappnode.eth')) {
throw Error('The installer cannot be restarted');
}

const packageVolumes = parse.serviceVolumes(dockerComposePath, id);

// If there are no volumes don't do anything
if (!packageVolumes.length) {
if (!dnp.volumes || !dnp.volumes.length) {
return {
message: id+' has no volumes ',
};
}

// Remove volumes
await docker.compose.rm(dockerComposePath, {v: true});
for (const volumeName of packageVolumes) {
await docker.volume.rm(volumeName);
if (dnp.isCORE) {
// docker-compose down can't be called because of the shared network
await docker.compose.rm(dockerComposePath);
await docker.volume.rm(dnp.volumes.map((v) => v.name).join(' '));
} else {
await docker.compose.down(dockerComposePath, {volumes: true});
}
// Restart docker to apply changes
await docker.compose.up(dockerComposePath);
Expand All @@ -48,7 +52,7 @@ async function restartPackageVolumes({
eventBus.emit(eventBusTag.emitPackages);

return {
message: 'Restarted '+id+' volumes: ' + packageVolumes.join(', '),
message: 'Restarted '+id+' volumes',
logMessage: true,
userAction: true,
};
Expand Down
1 change: 0 additions & 1 deletion build/src/src/calls/updatePackageEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const updatePackageEnv = async ({
throw Error('The installer cannot be restarted');
}

await docker.compose.down(dockerComposePath);
await docker.compose.up(dockerComposePath);

// Emit packages update
Expand Down
1 change: 1 addition & 0 deletions build/src/src/modules/dockerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function format(c) {
name: name,
shortName: shortName,
ports: mapPorts(c.Ports),
volumes: c.Mounts.map(({Name, Source}) => ({name: Name, path: Source})),
state: c.State,
running: !/^Exited /i.test(c.Status),
};
Expand Down
3 changes: 2 additions & 1 deletion build/src/src/utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ function envFile(envFileData) {
.split('\n')
.filter((row) => row.length > 0 )
.map((row) => {
res[row.split('=')[0]] = row.split('=')[1];
const [key, value] = row.split(/=(.+)/);
res[key] = value;
});

return res;
Expand Down
77 changes: 57 additions & 20 deletions build/src/test/calls/restartPackageVolumes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const sinon = require('sinon');
const fs = require('fs');
const getPath = require('utils/getPath');
const validate = require('utils/validate');
const docker = require('modules/docker');
const parse = require('utils/parse');

chai.should();

Expand All @@ -17,36 +15,75 @@ describe('Call function: restartPackageVolumes', function() {
};

const PACKAGE_NAME = 'test.dnp.dappnode.eth';
const CORE_PACKAGE_NAME = 'testCore.dnp.dappnode.eth';
const DOCKERCOMPOSE_PATH = getPath.dockerCompose(PACKAGE_NAME, params);
const CORE_DOCKERCOMPOSE_PATH = getPath.dockerCompose(CORE_PACKAGE_NAME, params);

const docker = {
compose: {
rm: sinon.stub(),
down: sinon.stub(),
up: sinon.stub(),
},
volume: {
rm: sinon.stub(),
},
};
// Declare stub behaviour. If done chaining methods, sinon returns an erorr:

const dockerList = {
listContainers: async () => ([
{
name: CORE_PACKAGE_NAME,
isCORE: true,
volumes: [
{name: 'vol1'},
{name: 'vol2'},
],
},
{
name: PACKAGE_NAME,
volumes: [
{name: 'vol3'},
],
},
]),
};

const restartPackageVolumes = proxyquire('calls/restartPackageVolumes', {
'modules/docker': docker,
'modules/dockerList': dockerList,
'params': params,
});

before(() => {
validate.path(DOCKERCOMPOSE_PATH);
fs.writeFileSync(DOCKERCOMPOSE_PATH, 'docker-compose');
for (const path of [DOCKERCOMPOSE_PATH, CORE_DOCKERCOMPOSE_PATH]) {
validate.path(path);
fs.writeFileSync(path, 'docker-compose');
}
});

it('should remove the package volumes of a CORE', async () => {
const res = await restartPackageVolumes({id: CORE_PACKAGE_NAME});
// sinon.assert.called(docker.compose.rm);
sinon.assert.called(docker.compose.rm);
sinon.assert.calledWith(docker.volume.rm, 'vol1 vol2');
sinon.assert.called(docker.compose.up);
expect(res).to.be.ok;
expect(res).to.have.property('message');
});

it('should remove the package volumes', async () => {
// Mock docker
sinon.replace(docker.volume, 'rm', sinon.fake());
sinon.replace(docker.compose, 'rm', sinon.fake());
sinon.replace(docker.compose, 'up', sinon.fake());
// Mock parse
const packageVolumes = ['vol1', 'vol2'];
sinon.replace(parse, 'serviceVolumes', sinon.fake.returns(packageVolumes));
const restartPackageVolumes = proxyquire('calls/restartPackageVolumes', {
'utils/parse': parse,
'modules/docker': docker,
'params': params,
});
let res = await restartPackageVolumes({id: PACKAGE_NAME});
it('should remove the package volumes of a NOT CORE', async () => {
const res = await restartPackageVolumes({id: PACKAGE_NAME});
// sinon.assert.called(docker.compose.rm);
sinon.assert.calledWith(docker.volume.rm.firstCall, 'vol1');
sinon.assert.calledWith(docker.volume.rm.secondCall, 'vol2');
sinon.assert.called(docker.compose.down);
sinon.assert.called(docker.compose.up);
expect(res).to.be.ok;
expect(res).to.have.property('message');
});

after(() => {
fs.unlinkSync(DOCKERCOMPOSE_PATH);
fs.unlinkSync(CORE_DOCKERCOMPOSE_PATH);
});
});
1 change: 0 additions & 1 deletion build/src/test/calls/updatePackageEnv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('Call function: updatePackageEnv', function() {
});
// Verify
// docker.compose should be used to reset the package
sinon.assert.calledWith(docker.compose.down, dockerComposePath);
sinon.assert.calledWith(docker.compose.up, dockerComposePath);
// The envs should have been written
let envString = fs.readFileSync(envFilePath, 'utf8');
Expand Down
5 changes: 3 additions & 2 deletions dappnode_package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dappmanager.dnp.dappnode.eth",
"version": "0.1.15",
"version": "0.1.16",
"description": "Dappnode package responsible for providing the DappNode Package Manager",
"avatar": "/ipfs/QmdT2GX9ybaoE25HBk7is8CDnfn2KaFTpZVkLdfkAQs1PN",
"type": "dncore",
Expand All @@ -17,8 +17,9 @@
"subnet": "172.33.0.0/16",
"ipv4_address": "172.33.1.7"
},
"author": "Eduardo Antuña <[email protected]> (https://github.com/eduadiez)",
"author": "DAppNode Association <[email protected]> (https://github.com/dappnode)",
"contributors": [
"Eduardo Antuña <[email protected]> (https://github.com/eduadiez)",
"DAppLion <[email protected]> (https://github.com/dapplion)"
],
"keywords": [
Expand Down

0 comments on commit 1c88266

Please sign in to comment.