Skip to content

Commit

Permalink
add pulling watchower image
Browse files Browse the repository at this point in the history
  • Loading branch information
William-De71 committed Oct 31, 2024
1 parent b67e043 commit 09bda7f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
7 changes: 5 additions & 2 deletions server/api/controllers/system.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ module.exports = function SystemController(gladys) {
* @apiGroup System
*/
async function updateContainers(req, res) {
const update = await gladys.system.updateContainers();
res.json(update);
gladys.event.emit(EVENTS.SYSTEM.UPDATE_CONTAINERS);
res.json({
success: true,
message: 'Update started, the system checks and installs a new image if available'
});
}

/**
Expand Down
13 changes: 9 additions & 4 deletions server/lib/system/system.updateContainers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ const logger = require('../../utils/logger');
/**
* @description Update containers.
* @example
* const state = await updateContainers();
* await updateContainers();
*/
async function updateContainers() {
if (!this.dockerode) {
throw new PlatformNotCompatible('SYSTEM_NOT_RUNNING_DOCKER');
}

const watchtowerImage = 'containrrr/watchtower';

logger.info(`Pulling ${watchtowerImage} image...`);
await this.pull(watchtowerImage);

try {
const container = await this.dockerode.createContainer({
Image: 'containrrr/watchtower',
// dockerode.
const container = await this.createContainer({
Image: watchtowerImage,
Cmd: ['--run-once', '--cleanup', '--include-restarting'],
HostConfig: {
AutoRemove: true,
Expand All @@ -35,7 +41,6 @@ async function updateContainers() {
logger.debug('Error running Watchtower:', e);
}


}

module.exports = {
Expand Down
62 changes: 62 additions & 0 deletions server/test/lib/system/system.updateContainers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 Job = require('../../../lib/job');

const sequelize = {
close: fake.resolves(null),
};

const event = {
on: fake.resolves(null),
emit: fake.returns(null),
};

const job = new Job(event);

const config = {
tempFolder: '/tmp/gladys',
};

describe('system.updateContainers', () => {
let system;

beforeEach(async () => {
system = new System(sequelize, event, config, job);
await system.init();
// Reset all fakes invoked within init call
sinon.reset();
});

afterEach(() => {
sinon.reset();
});

it('should fail: not run inside docker', async () => {
system.dockerode = undefined;

try {
await system.installUpgrade();
assert.fail('should have fail');
} catch (e) {
expect(e).be.instanceOf(PlatformNotCompatible);
expect(e).to.have.property('message', 'SYSTEM_NOT_RUNNING_DOCKER');

assert.notCalled(sequelize.close);
assert.notCalled(event.on);
}
});



});

0 comments on commit 09bda7f

Please sign in to comment.