-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.js
121 lines (97 loc) · 2.59 KB
/
pipeline.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
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
const { Stage } = require('./stage');
const path = require('path');
const CONFIG_VERSION = '1';
const RESOLVERS = [];
class Pipeline {
static get RESOLVERS () {
return RESOLVERS;
}
static reset (empty = false) {
RESOLVERS.splice(0);
if (!empty) {
Pipeline.addResolver(require('./resolvers/cicd')());
Pipeline.addResolver(require('./resolvers/compose')());
Pipeline.addResolver(require('./resolvers/stack')());
Pipeline.addResolver(require('./resolvers/docker')());
}
}
static addResolver (resolver) {
RESOLVERS.push(resolver);
}
static async resolve (workDir) {
for (const resolve of RESOLVERS) {
const config = await resolve(workDir);
const name = path.basename(workDir);
if (config) {
return new Pipeline(workDir, {
version: CONFIG_VERSION,
name,
...config,
});
}
}
throw new Error('Failed to resolve working directory');
}
static validate ({ version, name, stages = {} }) {
if (!version) {
throw new Error('Version must be specified');
}
if (!name) {
throw new Error('Name must be specified');
}
if (!stages || !Object.keys(stages).length) {
throw new Error('Stages cannot be empty');
}
return {
version,
name,
stages,
};
}
constructor (workDir, config) {
this.workDir = workDir;
const { version, name, stages } = Pipeline.validate(config);
this.version = version;
this.name = name;
this.stages = {};
for (const name in stages) {
this.stages[name] = new Stage(this, {
...stages[name],
name,
});
}
}
dump () {
const { version, name } = this;
const stages = {};
for (const name in this.stages) {
stages[name] = this.stages[name].dump();
}
return {
version,
name,
stages,
};
}
async run ({ env, labels, attach = false, logger = () => undefined } = {}) {
logger({ pipeline: this.name, level: 'head', message: `Running ${this.name} ...` });
for (const name in this.stages) {
await this.stages[name].run({ env, labels, attach, logger });
}
}
async abort ({ env, logger = () => undefined } = {}) {
logger({ pipeline: this.name, level: 'head', message: `Aborting ${this.name} ...` });
for (const name in this.stages) {
await this.stages[name].abort({ env, logger });
}
}
getStage (name) {
const stage = this.stages[name];
if (!stage) {
throw new Error('Stage not found');
}
return stage;
}
}
Pipeline.reset();
module.exports = { Pipeline };