-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathartifact.js
More file actions
94 lines (77 loc) · 2.35 KB
/
artifact.js
File metadata and controls
94 lines (77 loc) · 2.35 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
import logger from "./log.js"
import * as db from './db.js'
import app_config from "./app_config.js"
import * as util from "./util.js"
import fs from 'fs'
import path from "path"
import ini from 'ini'
const artifact_dir = app_config.artifact_directory
export async function delete_artifacts(log, job_ctx) {
// job id should never repeat
const job_artifact_dir = path.join(artifact_dir, ""+job_ctx.job_id);
let files;
try {
files = await db.get_artifacts_by_id(job_ctx.job_id);
} catch (e) {
log.error("Failed to get artifacts for job from db: ", e);
return;
}
files.forEach((file) => {
if (fs.existsSync(file.path)) {
try {
fs.unlinkSync(file.path);
} catch (e) {
log.error("Failed to remove %s from file system", file.path, e);
}
} else
log.warn("Artifact %s missing from file system", file.path);
});
if (fs.existsSync(job_artifact_dir)) {
log.info("Removing job artifact directory %s", job_artifact_dir);
try {
fs.rmdirSync(job_artifact_dir);
} catch (e) {
log.error("Failed to remove job artifact directory %s", e)
}
}
try {
await db.clear_artifacts(job_ctx.job_id);
} catch (e) {
log.error("Failed to clear artifacts from the database", e);
}
}
export async function save_artifact(log, job_ctx, artifact) {
if (!fs.existsSync(artifact)) {
log.error("Artifact file '%s' does not exist", artifact);
return;
}
if (!fs.existsSync(artifact_dir)) {
log.info("Creating artifact directory %s", artifact_dir);
fs.mkdirSync(artifact_dir);
}
// job id should never repeat
const job_artifact_dir = path.join(artifact_dir, ""+job_ctx.job_id);
if (!fs.existsSync(job_artifact_dir)) {
log.info("Creating job artifact directory %s", job_artifact_dir);
fs.mkdirSync(job_artifact_dir);
}
const job_artifact_path = path.join(job_artifact_dir, path.basename(artifact));
log.info("Saving artifact %s -> %s", artifact, job_artifact_path);
try {
fs.copyFileSync(artifact, job_artifact_path);
} catch (e) {
log.error("Failed to copy file: ", e)
return;
}
try {
const artifact_id = await db.add_artifact(job_ctx.job_id, job_artifact_path);
log.info("Artifact %d saved in database", artifact_id);
} catch (e) {
log.error("Failed to store artifact information in database", e);
try {
fs.unlinkSync(job_artifact_path);
} catch (e) {
log.error("Failed to clean up copied artifact file!", e);
}
}
}