-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…1074) Co-authored-by: Pierre-Gilles Leymarie <pierregilles.leymarie@gmail.com>
- Loading branch information
1 parent
6d67688
commit 65d7fa3
Showing
19 changed files
with
390 additions
and
28 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { PlatformNotCompatible } = require('../../utils/coreErrors'); | ||
|
||
/** | ||
* @description Remove an Docker container. | ||
* @param {string} containerId - Container id. | ||
* @param {Object} options - Options for removal (see https://docs.docker.com/engine/api/v1.37/#operation/ContainerDelete). | ||
* @returns {Promise} The removed container. | ||
* @example | ||
* await removeContainer(options); | ||
*/ | ||
async function removeContainer(containerId, options = {}) { | ||
if (!this.dockerode) { | ||
throw new PlatformNotCompatible('SYSTEM_NOT_RUNNING_DOCKER'); | ||
} | ||
|
||
const container = await this.dockerode.getContainer(containerId); | ||
return container.remove(options); | ||
} | ||
|
||
module.exports = { | ||
removeContainer, | ||
}; |
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
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,47 @@ | ||
const logger = require('../../../utils/logger'); | ||
const { CONFIGURATION, DEFAULT } = require('./constants'); | ||
const containerParams = require('../docker/eclipse-mosquitto-container.json'); | ||
|
||
/** | ||
* @description Updates MQTT container configuration according to required changes. | ||
* @param {Object} configuration - MQTT service configuration. | ||
* @returns {Promise} Current MQTT network configuration. | ||
* @example | ||
* updateContainer({ mqttUrl, mqttPort }); | ||
*/ | ||
async function updateContainer(configuration) { | ||
logger.info('MQTT: checking for required changes...'); | ||
|
||
// Check for port listener option | ||
const { brokerContainerAvailable, mosquittoVersion } = configuration; | ||
if (brokerContainerAvailable && !mosquittoVersion) { | ||
logger.info('MQTT: update to mosquitto v2 required...'); | ||
const dockerContainers = await this.gladys.system.getContainers({ | ||
all: true, | ||
filters: { name: [containerParams.name] }, | ||
}); | ||
|
||
// Remove non versionned container | ||
if (dockerContainers.length !== 0) { | ||
const [container] = dockerContainers; | ||
await this.gladys.system.removeContainer(container.id, { force: true }); | ||
} | ||
|
||
// Reinstall container with explicit version | ||
const newContainer = await this.installContainer(false); | ||
await this.gladys.system.restartContainer(newContainer.id); | ||
|
||
await this.gladys.variable.setValue( | ||
CONFIGURATION.MQTT_MOSQUITTO_VERSION, | ||
DEFAULT.MOSQUITTO_VERSION, | ||
this.serviceId, | ||
); | ||
logger.info('MQTT: update to mosquitto v2 done'); | ||
} | ||
|
||
return configuration; | ||
} | ||
|
||
module.exports = { | ||
updateContainer, | ||
}; |
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,68 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const { fake, assert } = sinon; | ||
|
||
const proxyquire = require('proxyquire').noCallThru(); | ||
|
||
const { PlatformNotCompatible } = require('../../../utils/coreErrors'); | ||
const DockerodeMock = require('./DockerodeMock.test'); | ||
|
||
const System = proxyquire('../../../lib/system', { | ||
dockerode: DockerodeMock, | ||
}); | ||
|
||
const sequelize = { | ||
close: fake.resolves(null), | ||
}; | ||
|
||
const event = { | ||
on: fake.resolves(null), | ||
emit: fake.resolves(null), | ||
}; | ||
|
||
const config = { | ||
tempFolder: '/tmp/gladys', | ||
}; | ||
|
||
describe('system.removeContainer', () => { | ||
let system; | ||
|
||
beforeEach(async () => { | ||
system = new System(sequelize, event, config); | ||
await system.init(); | ||
// Reset all fakes invoked within init call | ||
sinon.reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.reset(); | ||
}); | ||
|
||
it('should failed as not on docker env', async () => { | ||
system.dockerode = undefined; | ||
|
||
try { | ||
await system.removeContainer('my-container'); | ||
assert.fail('should have fail'); | ||
} catch (e) { | ||
expect(e).be.instanceOf(PlatformNotCompatible); | ||
|
||
assert.notCalled(sequelize.close); | ||
assert.notCalled(event.on); | ||
assert.notCalled(event.emit); | ||
} | ||
}); | ||
|
||
it('should removeContainer command with success', async () => { | ||
const result = await system.removeContainer('my-container'); | ||
|
||
expect(result).to.be.eq(true); | ||
|
||
assert.notCalled(sequelize.close); | ||
assert.notCalled(event.on); | ||
assert.notCalled(event.emit); | ||
|
||
assert.calledOnce(system.dockerode.getContainer); | ||
}); | ||
}); |
Oops, something went wrong.