Repository navigation
Expand file tree
/
Copy pathartifacts.js
More file actions
233 lines (189 loc) · 7.64 KB
/
Copy pathartifacts.js
File metadata and controls
233 lines (189 loc) · 7.64 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
This module handles all of the "artifact" (as defined by the worker) uploads and
deals with the extract of both single and multiple artifacts from the docker
container.
*/
const _ = require('lodash');
const mime = require('mime');
const tarStream = require('tar-stream');
const Debug = require('debug');
const assert = require('assert');
const { hrtime } = require('process');
const { fmtLog, fmtErrorLog } = require('../log');
const uploadToS3 = require('../upload_to_s3');
const waitForEvent = require('../wait_for_event');
let debug = Debug('docker-worker:middleware:artifact_extractor');
class Artifacts {
constructor() {
this.featureName = 'artifactHandler';
}
async uploadArtifact(taskHandler, name, artifact) {
let errors = [];
let container = taskHandler.dockerProcess.container;
let queue = taskHandler.queue;
let path = artifact.path;
let expiry;
if (artifact.expires) {
expiry = new Date(artifact.expires);
} else {
expiry = new Date(taskHandler.status.expires);
}
// Task specific information needed to generated signed put requests.
let taskId = taskHandler.status.taskId;
let runId = taskHandler.claim.runId;
// Raw tar stream for the content.
let contentStream;
try {
contentStream = await (new Promise((accept, reject) => {
return container.getArchive({ path }, (err, data) => {
if (err) {reject(err);}
accept(data);
});
}));
} catch (e) {
let error = `Artifact "${name}" not found at "${path}": ${e.message}`;
// Log the error but not as an error so that other systems to not think it's
// a fatal issue
taskHandler.stream.write(fmtLog(error));
// Create the artifact but as the type of "error" to indicate it is
// missing.
await queue.createArtifact(taskId, runId, name, {
storageType: 'error',
expires: expiry,
reason: 'file-missing-on-worker',
message: error,
});
// Return without throwing an error that would cause the task to fail. Too
// many tasks currently rely on the fact that the worker does not fail a task
// if an artifact is not present. Need to update tasks and stakeholders
// before changing this behavior.
return;
}
let tarExtract = tarStream.extract();
let checkedArtifactType = false;
let entryHandler = async function (header, stream, cb) {
// Trim the first part of the path off the entry.
let entryName = name;
let entryPath = header.name.split('/');
entryPath.shift();
if (entryPath.length && entryPath[0]) {
// the trimEnd here avoids superfluous `/` when the artifact key in the
// task payload contains a trailing `/`.
entryName = entryName.trimEnd('/') + '/' + entryPath.join('/');
}
// The first item in the tar should always match the intended artifact
// type.
if (!checkedArtifactType) {
// Only check once! Tar is ordered and docker gives us consistent
// contents so we do not need to check more then once.
checkedArtifactType = true;
if (header.type !== artifact.type) {
let error =
`Error uploading "${entryName}". Expected artifact to ` +
`be a "${artifact.type}" but was "${header.type}"`;
taskHandler.stream.write(fmtErrorLog(error));
// Return without throwing an error that would cause the task to fail. Too
// many tasks currently rely on the fact that the worker does not fail a task
// if an artifact is not present. Need to update tasks and stakeholders
// before changing this behavior.
//errors.push(error);
// Remove the entry listener immediately so no more entries are consumed
// while uploading the error artifact.
tarExtract.removeListener('entry', entryHandler);
// Make it clear that you must expected either files or directories.
await queue.createArtifact(taskId, runId, name, {
storageType: 'error',
expires: expiry,
reason: 'invalid-resource-on-worker',
message: error,
});
// Destroy the stream.
tarExtract.destroy();
// Notify the 'finish' listener that we are done.
tarExtract.emit('finish');
return;
}
}
// Skip any entry type that is not an artifact for uploads...
if (header.type !== 'file') {
stream.resume();
cb();
return;
}
let headers = {
'content-type': mime.getType(header.name) || 'application/octet-stream',
};
// Check file extension
let skipCompressionExtensions = taskHandler.runtime.artifacts.skipCompressionExtensions;
let compress = false;
if (!skipCompressionExtensions.some((ext) => header.name.endsWith(ext))) {
headers['content-encoding'] = 'gzip';
compress = true;
}
try {
let region = taskHandler.runtime.region;
assert.ok(region);
let monitor = taskHandler.runtime.monitor;
const start = hrtime.bigint();
let { digest, size } = await uploadToS3(taskHandler.queue, taskId, runId, stream,
entryName, expiry, headers, null, null, compress);
// save the time taken to upload the artifact
const end = hrtime.bigint();
let uploadTime = Number(end - start) / 1e6; // in ms
// report metrics globally...
monitor.measure('artifacts.global.size', size);
monitor.measure('artifacts.global.uploadTime', uploadTime);
monitor.measure('artifacts.global.throughput', size * 1e3 / uploadTime); // Bytes/sec
// and by region
monitor.measure(`artifacts.region.${region}.size`, size);
monitor.measure(`artifacts.region.${region}.uploadTime`, uploadTime);
monitor.measure(`artifacts.region.${region}.throughput`, size * 1e3 / uploadTime); // Bytes/sec
taskHandler.artifactHashes[entryName] = { sha256: digest };
} catch(err) {
debug(err);
// Log each error but don't throw yet. Try to upload as many artifacts as
// possible before handling the errors.
errors.push(err);
taskHandler.stream.write(
fmtErrorLog(`Error uploading "${entryName}" artifact. ${err.message}`),
);
}
// Resume the stream if there is an upload failure otherwise
// stream will never emit 'finish'
stream.resume();
cb();
};
// Individual tar entry.
tarExtract.on('entry', entryHandler);
// Wait for the tar to be finished extracting.
const finishEvent = waitForEvent(tarExtract, 'finish');
// Begin unpacking the tar.
contentStream.pipe(tarExtract);
await finishEvent;
if (errors.length) {
throw new Error(errors.join(' | '));
}
}
async stopped(taskHandler) {
// Can't create artifacts for a task that's been canceled
if (taskHandler.isCanceled()) {return;}
let artifacts = taskHandler.task.payload.artifacts;
taskHandler.artifactHashes = {};
let errors = {};
// Artifacts are optional...
if (typeof artifacts !== 'object') {return;}
// Upload all the artifacts in parallel.
await Promise.all(_.map(artifacts, (value, key) => {
return this.uploadArtifact(taskHandler, key, value).catch((err) => {
errors[key] = err;
});
}));
if (Object.keys(errors).length) {
_.map(errors, (value, key) => {
debug('Artifact upload %s failed, %s, as JSON: %j', key, value, value, value.stack);
});
throw new Error('Encountered error when uploading artifact(s)');
}
}
}
module.exports = Artifacts;