Repository navigation
Expand file tree
/
Copy pathdocker_save.js
More file actions
112 lines (94 loc) · 3.57 KB
/
Copy pathdocker_save.js
File metadata and controls
112 lines (94 loc) · 3.57 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const Debug = require('debug');
const fs = require('mz/fs');
const path = require('path');
const tar = require('tar-fs');
const taskcluster = require('taskcluster-client');
const slugid = require('slugid');
const uploadToS3 = require('../upload_to_s3');
const zlib = require('zlib');
const pipe = require('promisepipe');
let debug = Debug('docker-worker:features:docker-save');
function createImageName(taskId, runId) {
return `${taskId.toLowerCase().replace(/[_-]/g, '0')}-${runId}`;
}
class DockerSave {
constructor() {
this.featureName = 'dockerSave';
}
//commits and uploads the docker image as an artifact
async uploadContainer(task) {
let imageName = createImageName(task.status.taskId, task.runId);
//temporary path for saved file
let pathname = path.join(task.runtime.dockerVolume, slugid.v4() + '.tar');
await task.dockerProcess.container.commit({
// repo name must be lower case and not contain underscores
repo: imageName,
});
let image = await task.runtime.docker.getImage(`${imageName}:latest`);
let imgStream = await image.get();
let zipStream = zlib.createGzip();
const output = fs.createWriteStream(pathname);
await pipe(imgStream, zipStream, output);
let uploadStream = fs.createReadStream(pathname);
let expiration = taskcluster.fromNow(task.runtime.dockerSave.expiration);
expiration = new Date(Math.min(expiration, new Date(task.task.expires)));
await uploadToS3(task.queue, task.status.taskId, task.runId,
uploadStream, 'public/dockerImage.tar', expiration, {
'content-type': 'application/x-tar',
'content-encoding': 'gzip',
});
debug('docker image uploaded');
await image.remove();
//cleanup
fs.unlink(pathname, err => {
if (err) {
task.runtime.log('[alert-operator] could not delete cache save tarball, worker may run out of hdd space\r\n'
+ err + err.stack);
}
});
}
//uploading a cache folder as an artifact
async uploadCache(task, cacheStr) {
let cache = task.runtime.volumeCache.getCacheDetails(cacheStr);
//temporary path for saved file
let pathname = path.join(task.runtime.dockerVolume, slugid.v4() + '.tar');
const output = fs.createWriteStream(pathname);
await pipe(
tar.pack(cache.cacheLocation, { dereference: true }),
zlib.createGzip(),
output,
);
let expiration = taskcluster.fromNow(task.runtime.dockerSave.expiration);
expiration = new Date(Math.min(expiration, new Date(task.task.expires)));
await uploadToS3(task.queue, task.status.taskId, task.runId,
fs.createReadStream(pathname),
'public/cache/' + cache.cacheName + '.tar',
expiration, {
'content-type': 'application/x-tar',
'content-encoding': 'gzip',
});
debug('%s uploaded', cache.cacheName);
//cleanup
fs.unlink(pathname, err => {
if (err) {
task.runtime.log('[alert-operator] could not delete cache save tarball, worker may run out of hdd space\r\n'
+ err + err.stack);
}
});
}
async killed(task) {
let errors = [];
let volumeCaches = task.volumeCaches || [];
await Promise.all(volumeCaches.map((cacheStr) => this.uploadCache(task, cacheStr).catch(err => err.push(err)))
.concat(this.uploadContainer(task).catch((err) => errors.push(err))));
if (errors.length > 0) {
let errorStr = 'cache could not be uploaded: ';
errors.forEach((err) => {
errorStr = errorStr + err + err.stack;
});
debug(errorStr);
throw new Error(errorStr);
}
}
}
module.exports = DockerSave;