-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (104 loc) · 3.81 KB
/
index.js
File metadata and controls
132 lines (104 loc) · 3.81 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
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
'use strict'
const { join } = require('path')
const { spawnSync } = require('child_process')
const { existsSync, renameSync } = require('fs')
const { log, progress } = require('@serverless/utils/log')
class ServerlessPluginRust {
constructor (serverless, options) {
this.serverless = serverless
this.options = options
this.logger = log
this.progress = progress.create({ message: 'Packaging rust functions' })
this.service = this.serverless.service
this.architecture = this.service.provider?.architecture || 'x86_64'
this.servicePath = this.serverless.config.servicePath || ''
this.hooks = {
'before:package:createDeploymentArtifacts': this.build.bind(this),
'before:deploy:function:packageFunction': this.build.bind(this)
}
}
log (message) {
if (typeof message === 'string') {
this.progress.update(message)
}
if (this.options.verbose) {
console.log(message)
}
}
setup () {
this.log('Checking provider compatibility')
if (this.service.provider.name !== 'aws') {
throw new this.serverless.classes.Error('Only aws provider is supported at the moment.')
}
this.log('Checking rust cargo lambda version')
const cargo = spawnSync('cargo', ['lambda', '-V'], { shell: true, encoding: 'utf-8' })
if (cargo.status !== 0) {
this.log(cargo)
throw new this.serverless.classes.Error('cargo lambda was not found in the project. Visit https://www.cargo-lambda.info for more information.')
}
}
get functions () {
return this.serverless.service.getAllFunctions()
}
beforeBuild () {
const targetPath = join(this.servicePath, 'target')
this.log('Cleanup target path')
const rm = spawnSync('rm', ['-rf', targetPath], { shell: true, encoding: 'utf-8' })
if (rm.status !== 0) {
this.log(rm)
throw new this.serverless.classes.Error('Error cleaning up target path')
}
}
build () {
this.setup()
this.beforeBuild()
this.log('Building artifacts')
const architecture = this.architecture === 'arm64' ? '--arm64' : null
const options = ['--release', architecture, '--output-format', 'zip']
const args = ['lambda', 'build', ...options].filter(Boolean)
const cargo = spawnSync('cargo', [...args], { shell: true, encoding: 'utf-8' })
if (cargo.status !== 0) {
this.log(cargo)
throw new this.serverless.classes.Error('cargo lambda build error. Visit https://www.cargo-lambda.info for more information.')
}
this.log('Building artifacts complete')
this.afterBuild()
}
success (message) {
return log.success(message)
}
afterBuild () {
this.functions.forEach((functionName) => {
const func = this.service.getFunction(functionName)
this.log(`Preparing ${func.handler} function for deploying`)
const basePath = join(
this.servicePath,
'target/lambda',
func.handler,
'bootstrap.zip'
)
if (!existsSync(basePath)) {
throw new this.serverless.classes.Error(`Base path ${basePath} not exists`)
}
this.log(`Renaming rust ${func.handler} function`)
const targetPath = join(
this.servicePath,
'target/lambda',
func.handler,
`${func.handler}-bootstrap.zip`
)
try {
renameSync(basePath, targetPath)
} catch (error) {
this.log(error)
throw new this.serverless.classes.Error(`Error renaming file from ${basePath} to ${targetPath}. Visit https://www.cargo-lambda.info for more information. Details: ${error.message}`)
}
this.log(`Renaming rust ${func.handler} function completed`)
func.package = func.package || {}
func.package.artifact = targetPath
this.progress.remove()
this.success('Thanks for using serverless-plugin-rust')
})
}
}
module.exports = ServerlessPluginRust