-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (60 loc) · 2.14 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
'use strict';
require('dotenv').load();
const exec = require('child_process').exec;
const fs = require('fs');
const Q = require('q');
const container = process.env.CONTAINER;
const imageName = process.env.IMAGE;
function generateDockerfile(volumes){
fs.writeFileSync('Dockerfile', 'FROM busybox\nMAINTAINER automatic\n');
volumes.forEach((volume) => {
fs.appendFileSync('Dockerfile', `RUN mkdir -p ${volume}\nADD ./data${volume} ${volume}\nVOLUME ${volume}\n`);
});
fs.appendFileSync('Dockerfile', 'CMD /bin/true\n');
}
function getVolumesData(volumes) {
let promises = [];
volumes.forEach((volume) => {
let deferred = Q.defer();
let target = `./data${volume}`.split('/');
target.pop();
target = target.join('/');
exec(`mkdir -p ./data${volume}; docker cp ${container}:${volume} ${target}`,
(err, stdout, stderr) => (err) ? deferred.reject(stderr): deferred.resolve(stdout));
promises.push(deferred.promise);
});
return Q.all(promises);
}
function verifyDockerVersion() {
let deferred = Q.defer();
exec(`docker version -f={{.Server.Version}}`,
(err, stdout, stderr) => (err) ? deferred.reject(stderr): deferred.resolve(stdout));
return deferred.promise;
}
function generateImage() {
exec(`docker inspect -f='{{range.Mounts}}{{.Destination}} {{end}}' ${container}`, (err, volumes) => {
if (err) {
throw err
}
volumes = volumes.split(' ');
volumes.pop();
console.log('Volumes:');
volumes.forEach((volume) => console.log(`- ${volume}`));
generateDockerfile(volumes);
return getVolumesData(volumes)
.then(() => {
exec(`docker build --tag=${imageName} .`, (error, stdout) => {
if (error) {
throw error;
}
console.log(stdout);
});
})
});
}
verifyDockerVersion()
.then(generateImage)
.catch((stderr) => {
console.log(stderr);
console.log('If your server API version is 1.23, use docker-autobuild-data-container:1.11');
});