This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (60 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// npm packages
const fs = require('fs');
const path = require('path');
const mavenDockerfile = () =>
`FROM maven:onbuild
EXPOSE 80
CMD mvn exec:java`;
// template name
exports.name = 'maven';
// function to check if the template fits this recipe
exports.checkTemplate = async ({tempDockerDir}) => {
// if project already has dockerfile - just exit
try {
const filesList = fs.readdirSync(tempDockerDir);
return filesList.includes('pom.xml');
} catch (e) {
return false;
}
};
// function to execute current template
exports.executeTemplate = async ({username, tempDockerDir, resultStream, util, docker}) => {
try {
// generate dockerfile
const dockerfile = mavenDockerfile();
const dfPath = path.join(tempDockerDir, 'Dockerfile');
fs.writeFileSync(dfPath, dockerfile, 'utf-8');
util.writeStatus(resultStream, {message: 'Deploying Maven project..', level: 'info'});
// build docker image
const buildRes = await docker.build({username, resultStream});
util.logger.debug('Build result:', buildRes);
// check for errors in build log
if (
buildRes.log
.map(it => it.toLowerCase())
.some(
it =>
(it.includes('error') && !it.includes('maven-error')) || (it.includes('failed') && !it.includes('optional'))
)
) {
util.logger.debug('Build log conains error!');
util.writeStatus(resultStream, {message: 'Build log contains errors!', level: 'error'});
resultStream.end('');
return;
}
// start image
const containerInfo = await docker.start(Object.assign({}, buildRes, {username, resultStream}));
util.logger.debug(containerInfo.Name);
// clean temp folder
await util.cleanTemp();
const containerData = docker.daemon.getContainer(containerInfo.Id);
const container = await containerData.inspect();
// return new deployments
util.writeStatus(resultStream, {message: 'Deployment success!', deployments: [container], level: 'info'});
resultStream.end('');
} catch (e) {
util.logger.debug('build failed!', e);
util.writeStatus(resultStream, {message: e.error, error: e.error, log: e.log, level: 'error'});
resultStream.end('');
}
};