-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (126 loc) · 3.98 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
'use strict'
const fs = require('fs')
const { spawnSync } = require('child_process')
const path = require('path')
const PackagePlugin = require('serverless/lib/plugins/package/package')
const IGNORE_OUTPUT = { stdio: ['ignore', process.stdout, process.stderr] }
const NIM_RUNTIME = 'nim'
const PROVIDED_RUNTIME = 'provided'
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.hooks = {
'before:package:createDeploymentArtifacts': this.build.bind(this),
'after:package:createDeploymentArtifacts': this.cleanup.bind(this),
'before:deploy:function:packageFunction': this.build.bind(this),
'after:deploy:function:packageFunction': this.cleanup.bind(this)
}
this.custom = {
flags: ['-d:release'],
dockerFlags: [],
dockerTag: 'nimlang/nim',
useDocker: false,
...((this.serverless.service.custom || {}).nim || {})
}
}
async build() {
const { config, service } = this.serverless
if (service.provider.name !== 'aws') {
this.serverless.cli.log('Nim runtime only supports AWS provider')
return
}
const functionNames = this.nimFunctionNames()
if (functionNames.length === 0) {
this.serverless.cli.log('No Nim functions found')
return
}
const packagePlugin = this.serverless.pluginManager.plugins.find(
plugin => plugin.constructor.name === PackagePlugin.name // TODO: more robust way to access Package plugin?
)
if (!packagePlugin) {
throw new Error('Package plugin not found')
}
for (const funcName of functionNames) {
this.serverless.cli.log(
`Building and packaging Nim function ${funcName}...`
)
const func = service.getFunction(funcName)
this.compileFunction(func)
func.package = func.package || {}
func.package.exclude = ['**/*']
func.package.include = (func.package.include || []).concat('bootstrap')
const artifactPath = await packagePlugin.packageFunction(funcName)
func.package.artifact = path.join(
'.serverless',
path.basename(artifactPath)
)
// Ensure runtime is set to a sane value for other plugins:
if (func.runtime === NIM_RUNTIME) {
func.runtime = PROVIDED_RUNTIME
}
}
if (service.provider.runtime === NIM_RUNTIME) {
service.provider.runtime = PROVIDED_RUNTIME
}
}
compileFunction(func) {
const { config } = this.serverless
const srcPath = path.join(
config.servicePath,
path.extname(func.handler) ? func.handler : func.handler + '.nim'
)
const binaryPath = path.join(config.servicePath, 'bootstrap')
const { dockerFlags, dockerTag, flags, useDocker } = {
...this.custom,
...func.nim
}
const { status } = useDocker
? spawnSync(
'docker',
[
'run',
'--rm',
'-v',
`${config.servicePath}:/app`,
'-w',
'/app',
...dockerFlags,
dockerTag,
'sh',
'-c',
`nimble c ${flags.join(' ')} -y -o:bootstrap ${func.handler}`
],
IGNORE_OUTPUT
)
: spawnSync(
'nimble',
['c', ...flags, '-y', `-o:${binaryPath}`, srcPath],
IGNORE_OUTPUT
)
if (status !== 0) {
throw new Error('Compiling Nim function failed - check output above')
}
}
cleanup() {
const binaryPath = path.join(
this.serverless.config.servicePath,
'bootstrap'
)
if (fs.existsSync(binaryPath)) {
fs.unlinkSync(binaryPath)
}
}
nimFunctionNames() {
const { service } = this.serverless
const functionNames = this.options.function
? [this.options.function]
: this.serverless.service.getAllFunctions()
return functionNames.filter(
name =>
(service.getFunction(name).runtime || service.provider.runtime) ===
NIM_RUNTIME
)
}
}
module.exports = ServerlessPlugin