-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
191 lines (170 loc) · 5.66 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
const path = require('path');
const defaults = require('lodash/defaults');
const map = require('lodash/map');
const get = require('lodash/get');
const compact = require('lodash/compact');
const flatten = require('lodash/flatten');
const fs = require('fs');
const kubeless = require('./kubeless');
class KubelessOfflinePlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.service = serverless.service;
this.log = serverless.cli.log.bind(serverless.cli);
this.options = options;
this.exitCode = 0;
this.commands = {
offline: {
usage: 'Simulates Kubeless NodeJS runtimes to run call your functions offline.',
lifecycleEvents: [
'start',
],
// add start nested options
commands: {
start: {
usage: 'Simulates Kubeless NodeJS to call your Kubeless functions offline using backward compatible initialization.',
lifecycleEvents: [
'init',
'end',
],
},
},
options: {
port: {
usage:
'Choose a port to run the offline server on, default 3000 '
+ '(e.g. "--port 3000" or "-p 3000")',
required: false,
shortcut: 'p',
type: 'string',
},
httpsProtocol: {
usage: 'To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both key.pem and server.crt files.',
shortcut: 'H',
type: 'string',
},
},
},
};
this.hooks = {
'offline:start:init': this.start.bind(this),
'offline:start': this.start.bind(this),
'offline:start:end': this.stop.bind(this),
};
this._convertRouteDefinitions.bind(this);
this._listenForSigInt.bind(this);
this._buildServer.bind(this);
}
/**
* Converts the serverless.yml function spec into one
* more easily consumed by Kubeless. The returned array
* contains objects in the form:
* ```
* {
* "file": "handlers",
* "function": "login",
* "route": "login",
* "timeout": 300
* }
* ```
* @return {Array<Object>} a set of function+route paths to be consumed by Kubeless
*/
_convertRouteDefinitions() {
const functions = get(this.serverless, 'service.functions');
const routes = compact(flatten(
map(functions, (spec, name) => {
const functionEnvironment = Object.assign({}, process.env, this.service.provider.environment, this.service.functions[name].environment);
// use serverless default http event if none are specified
// https://serverless.com/framework/docs/providers/kubeless/events/http/
const specEvents = spec.events && spec.events.length > 0 ? spec.events : [{ http: {path: name}}]
if (specEvents) {
return map(specEvents, event => {
const route = get(event, "http.path");
if (route) {
return {
file: spec.handler.split('.')[0],
function: spec.handler.split('.')[1],
route,
timeout: spec.timeout,
env: functionEnvironment,
};
}
})
}
})
));
return routes;
}
start() {
return Promise.resolve(this._buildServer())
.then(() => this._listenForSigInt())
.then(() => this.stop());
}
_listenForSigInt() {
// Listen for ctrl+c to stop the server
return new Promise(resolve => {
process.on('SIGINT', () => {
this.log('Kubeless offline halting...');
this.log('Please close all open connections to this server.');
resolve();
});
});
}
_buildServer() {
const routes = this._convertRouteDefinitions();
const customPath = get(this.serverless, 'service.custom.serverless-offline.location')
const servicePath = customPath ? path.join(this.serverless.config.servicePath, customPath) : this.serverless.config.servicePath;
const httpsDir = this.options.httpsProtocol || this.options.H;
let FUNC_SSL;
// HTTPS support
if (typeof httpsDir === 'string' && httpsDir.length > 0) {
FUNC_SSL = {
key: fs.readFileSync(path.resolve(httpsDir, 'key.pem'), 'utf8'),
cert: fs.readFileSync(path.resolve(httpsDir, 'server.crt'), 'utf8')
};
this.log(`Loaded SSL key & certificate from: ${httpsDir}`);
}
const serverConfig = defaults({
routes,
FUNC_PORT: this.options.p,
FUNC_SSL
}, {
BASE_PATH: servicePath,
FUNC_RUNTIME: "nodejs8",
FUNC_MEMORY_LIMIT: 1024,
FUNC_PORT: 3000,
logger: {
log: this.log,
error: this.log,
info: this.log,
warn: this.log,
},
})
this.log(`Starting kubeless offline server on port ${serverConfig.FUNC_PORT}`);
this.log("Loading handlers from:");
this.log(serverConfig.BASE_PATH);
this.log("Routes:");
const simpleRoutes = serverConfig.routes.map(route => {
let simpleRoute = Object.assign({}, route);
// don't log out the env
delete simpleRoute['env'];
return simpleRoute;
});
this.log(JSON.stringify(simpleRoutes, null, 2));
// Some users would like to know their environment outside of the handler
process.env.IS_OFFLINE = true;
// start the server
this.server = kubeless(serverConfig);
}
stop() {
this.log('Halting kubeless offline server');
this.log('Please close all open connections to this server.');
this.server.close(() => {
this.log('Kubeless offline server halted.');
this.server = null;
process.exit(this.exitCode);
});
}
}
module.exports = KubelessOfflinePlugin;