Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit e99b5ec

Browse files
committed
Pull image being deployed if no tar is given
1 parent 084b14a commit e99b5ec

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

src/docker/templates/image.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ exports.executeTemplate = async ({config, username, tempDockerDir, folder, resul
2020
// build docker image
2121
try {
2222
const {image, imageFile} = config;
23-
util.writeStatus(resultStream, {message: `Deploying project from image: ${image}..`, level: 'info'});
23+
const imageName = image.includes(':') ? image : `${image}:latest`;
24+
util.writeStatus(resultStream, {message: `Deploying project from image: ${imageName}..`, level: 'info'});
2425

2526
// import from tar if needed
2627
if (imageFile && imageFile.length) {
2728
util.writeStatus(resultStream, {message: `Importing image from file: ${imageFile}..`, level: 'info'});
2829
// get packed stream
2930
const tarStream = fs.createReadStream(path.join(tempDockerDir, folder, imageFile));
30-
const importRes = await docker.daemon.loadImage(tarStream, {tag: image});
31+
const importRes = await docker.daemon.loadImage(tarStream, {tag: imageName});
3132
util.logger.debug('Import result:', importRes);
33+
} else {
34+
// otherwise - pull given image to ensure it exists
35+
util.writeStatus(resultStream, {message: `Pulling image: ${imageName}..`, level: 'info'});
36+
const pullRes = await docker.pullImage(imageName);
37+
util.logger.debug('Pull result:', pullRes);
3238
}
3339

3440
// start image

src/routes/deploy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const logger = require('../logger');
99
const util = require('../util');
1010
const {getConfig, tempDockerDir} = require('../config');
1111
const docker = require('../docker/docker');
12+
const {pullImage} = require('../docker/init');
1213
const {build} = require('../docker/build');
1314
const {start} = require('../docker/start');
1415
const getTemplates = require('../docker/templates');
@@ -42,6 +43,7 @@ const deploy = async ({username, folder, existing, resultStream}) => {
4243
daemon: docker,
4344
build,
4445
start,
46+
pullImage,
4547
},
4648
util: Object.assign({}, util, {
4749
logger,

test/deploy.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {initNetwork} = require('../src/docker/network');
1818

1919
// create tar streams
2020
const streamDockerImage = tar.pack(path.join(__dirname, 'fixtures', 'docker-image-project'));
21+
const streamDockerImageExternal = tar.pack(path.join(__dirname, 'fixtures', 'docker-image-external'));
2122
const streamDocker = tar.pack(path.join(__dirname, 'fixtures', 'docker-project'));
2223
const streamNode = tar.pack(path.join(__dirname, 'fixtures', 'node-project'));
2324
const streamNodeLock = tar.pack(path.join(__dirname, 'fixtures', 'node-lock-project'));
@@ -158,6 +159,52 @@ test('Should deploy simple project from image and image tar', async done => {
158159
done();
159160
});
160161

162+
test('Should deploy simple project from external image', async done => {
163+
const options = Object.assign(optionsBase, {
164+
payload: streamDockerImageExternal,
165+
});
166+
167+
const response = await fastify.inject(options);
168+
// parse result into lines
169+
const result = response.payload
170+
.split('\n')
171+
.filter(l => l && l.length)
172+
.map(line => JSON.parse(line));
173+
174+
// find deployments
175+
const completeDeployments = result.find(it => it.deployments && it.deployments.length).deployments;
176+
177+
// check response
178+
expect(response.statusCode).toEqual(200);
179+
expect(completeDeployments.length).toEqual(1);
180+
expect(completeDeployments[0].Name.startsWith('/busybox')).toBeTruthy();
181+
182+
// check docker services
183+
const allContainers = await docker.listContainers();
184+
const containerInfo = allContainers.find(c => c.Names.includes(completeDeployments[0].Name));
185+
const name = completeDeployments[0].Name.slice(1);
186+
187+
expect(containerInfo).toBeDefined();
188+
expect(containerInfo.Labels['exoframe.deployment']).toEqual(name);
189+
expect(containerInfo.Labels['exoframe.user']).toEqual('admin');
190+
expect(containerInfo.Labels['exoframe.project']).toEqual('test-extimage-project');
191+
expect(containerInfo.Labels['traefik.backend']).toEqual(`${name}.test`);
192+
expect(containerInfo.Labels['traefik.docker.network']).toEqual('exoframe');
193+
expect(containerInfo.Labels['traefik.enable']).toEqual('true');
194+
expect(containerInfo.NetworkSettings.Networks.exoframe).toBeDefined();
195+
196+
const containerData = docker.getContainer(containerInfo.Id);
197+
const container = await containerData.inspect();
198+
expect(container.NetworkSettings.Networks.exoframe.Aliases.includes('testextimage')).toBeTruthy();
199+
expect(container.HostConfig.RestartPolicy).toMatchObject({Name: 'no', MaximumRetryCount: 0});
200+
201+
// cleanup
202+
const instance = docker.getContainer(containerInfo.Id);
203+
await instance.remove({force: true});
204+
205+
done();
206+
});
207+
161208
test('Should deploy simple node project', async done => {
162209
const options = Object.assign(optionsBase, {
163210
payload: streamNode,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test-docker-extimage-deploy",
3+
"hostname": "testextimage",
4+
"project": "test-extimage-project",
5+
"restart": "no",
6+
"image": "busybox"
7+
}

0 commit comments

Comments
 (0)