-
Notifications
You must be signed in to change notification settings - Fork 130
/
parser.js
175 lines (138 loc) · 4.52 KB
/
parser.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
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
'use strict';
const _ = require('lodash');
const { FunModule } = require('../install/module');
const { DockerfileParser } = require('dockerfile-ast');
const dockerOpts = require('../docker-opts');
const fs = require('fs-extra');
const RESERVED_DOCKER_CMD = [
'FROM', 'Add', 'ONBUILD',
'ARG', 'CMD', 'ENTRYPOINT',
'VOLUME', 'STOPSIGNAL'];
function resolveEnv(envs = {}) {
if (_.isString(envs)) {
throw new Error('fun.yml env property must be a map');
}
return _.map(envs || {}, (v, k) => {
if (k === 'PATH') {
return `${k}=${v}:$PATH`;
} else if (k === 'LD_LIBRARY_PATH') {
return `${k}=${v}:$LD_LIBRARY_PATH`;
}
return `${k}=${v}`;
});
}
function funymlToFunfile(funymlPath) {
const funModule = FunModule.load(funymlPath);
const runtime = funModule.runtime;
let firstAptCmd = true;
let firstShellCmd = true;
const content = [];
content.push(`RUNTIME ${runtime}`);
content.push('WORKDIR /code');
for (const t of funModule.tasks) {
let env = resolveEnv(t.attrs.env).join(' ');
if (!_.isEmpty(env)) {
env = ' ' + env;
}
const target = t.attrs.target;
let targetParameter = '';
if (target) {
targetParameter = ` -t ${target}`;
}
const cwd = t.attrs.cwd;
let cwdCmd = '';
if (cwd) {
cwdCmd = ` cd ${cwd} &&`;
}
switch (t.type) {
case 'pip': {
if (t.attrs.local) {
content.push(`RUN${cwdCmd}${env} fun-install pip install ${t.attrs.pip}${targetParameter}`);
} else {
content.push(`RUN${cwdCmd}${env} pip install ${t.attrs.pip}`);
}
break;
}
case 'apt': {
if (t.attrs.local) {
content.push(`RUN${cwdCmd}${env} fun-install apt-get install ${t.attrs.apt}${targetParameter}`);
} else {
if (firstAptCmd) {
content.push(`RUN apt-get update`);
}
content.push(`RUN${cwdCmd}${env} apt-get install ${t.attrs.apt}`);
}
firstAptCmd = false;
break;
}
case 'shell': {
// If add `COPY . /code` all the time, there is no way to reuse the docker build cache.
// However, without `COPY . /code`, it may encounter exceptions when depending resources on code.
// So now add `COPY . /code` only if funyml contains shell cmd
// see https://github.com/alibaba/funcraft/issues/483
// see https://github.com/alibaba/funcraft/issues/448
if (firstShellCmd) {
content.push('COPY . /code');
firstShellCmd = false;
}
const commands = _.split(t.attrs.shell, '\n');
let first = true;
for (const command of commands) {
if (command) {
if (first) {
content.push(`RUN${cwdCmd}${env} ${command}`);
first = false;
} else {
const lastCommand = content.pop();
content.push(`${lastCommand} \\`);
content.push(` ${env} ${command}`);
}
}
}
break;
}
default:
console.error('unkown task %s', t);
}
}
return content.join('\n');
}
async function funfileToDockerfile(funfilePath) {
const content = await fs.readFile(funfilePath, 'utf8');
const funfile = DockerfileParser.parse(content);
const dockerfile = [];
for (let instruction of funfile.getInstructions()) {
const ins = instruction.getInstruction();
if (_.includes(RESERVED_DOCKER_CMD, ins)) {
throw new Error(`Currently, Funfile does not support the semantics of '${ins}'.
If you have a requirement, you can submit the issue at https://github.com/alibaba/funcraft/issues.`);
}
if (ins.toUpperCase() === 'RUNTIME') {
const runtimeArgs = instruction.getArguments();
if (runtimeArgs.length !== 1) {
throw new Error('invalid RUNTIME for Funfile');
}
const runtime = runtimeArgs[0].getValue();
const imageName = await dockerOpts.resolveRuntimeToDockerImage(runtime, true);
dockerfile.push('FROM ' + await dockerOpts.resolveImageNameForPull(imageName));
} else {
const range = instruction.getRange();
dockerfile.push(instruction.getRangeContent(range));
}
}
return dockerfile.join('\n');
}
function parsePairs(val, vars) {
/*
* Key-value pairs, separated by equal signs
* keys can only contain letters, numbers, and underscores
* values can be any character
*/
const group = val.match(/(^[a-zA-Z_][a-zA-Z\d_]*)=(.*)/);
vars = vars || {};
if (group) {
vars[group[1]] = group[2];
}
return vars;
}
module.exports = { funymlToFunfile, funfileToDockerfile, resolveEnv, parsePairs };