-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (79 loc) · 2.97 KB
/
index.js
File metadata and controls
103 lines (79 loc) · 2.97 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
const File = Java.type('java.io.File')
const JString = Java.type('java.lang.String')
const FileInputStream = Java.type('java.io.FileInputStream')
const Mac = Java.type('javax.crypto.Mac')
const SecretKeySpec = Java.type('javax.crypto.spec.SecretKeySpec')
const Base64 = Java.type('java.util.Base64')
const fs = require('fs')
const httpClient = require('http-client')
const consoleColors = require('console-colors')
const config = fs.readJson('./config.json')
const serverlessCfg = config['serverless-cli']
const redColor = consoleColors.make(consoleColors.COLORS.RED)
const greenColor = consoleColors.make(consoleColors.COLORS.GREEN)
const blueColor = consoleColors.make(consoleColors.COLORS.BLUE)
if (!serverlessCfg) {
throw new Error('Deve existir uma chave "serverless" no arquivo config.json')
}
function cliDeploy (runInfo) {
if (!serverlessCfg.environments) {
throw new Error('A propriedade "serverless.environments" é obrigatória.')
}
const envName = runInfo.args.env || 'dev'
const envToDeploy = serverlessCfg.environments[envName]
if (!envToDeploy) {
throw new Error('Environment ' + envName + ' não encontrado em "serverless.environments"')
}
console.log('Starting API deployment to environment:', blueColor(envName))
const apiFolder = new File(serverlessCfg.api || './api')
if (!apiFolder.exists()) {
throw new Error('A pasta de api não foi encontrada, crie uma pasta chamada "api" ou altere a propriedade "serverless.api" com o caminho desejado.')
}
let apiZip
try {
console.log('- Creating API zip')
apiZip = File.createTempFile('api', '.zip')
fs.zip(apiFolder, apiZip)
const apiEndpoint = '/admin/deploy/api'
const deployURL = envToDeploy.host + apiEndpoint
console.log('- Deploying API zip on ', blueColor(envToDeploy.host))
try {
const response = httpClient
.post(deployURL, new FileInputStream(apiZip))
.contentType('application/zip')
.property('token', buildToken(apiEndpoint, envToDeploy))
.fetch()
if (response.code === 200) {
console.log(greenColor('API successfully deployed'))
} else {
console.log(redColor(response.body.message))
}
} catch (e) {
console.log(redColor('Failed to deploy API, reason: ' + e.message))
}
} finally {
fs.deleteQuietly(apiZip)
}
}
function buildToken (api, envToDeploy) {
const sha256HMAC = Mac.getInstance('HmacSHA256')
const secretKey = new SecretKeySpec(getSecret(envToDeploy), 'HmacSHA256')
sha256HMAC.init(secretKey)
return Base64.getEncoder().encodeToString(sha256HMAC.doFinal(getBytes(api)))
}
function getSecret (envToDeploy) {
return getBytes(env('serverless.secret') || envToDeploy.secret || serverlessCfg.secret)
}
function getBytes (str) {
return new JString(str).getBytes('UTF-8')
}
exports = {
name: ['deploy'],
description: 'Deploy the app',
args: [{
name: 'env',
description: 'The environment to deploy the application'
}],
options: [],
runner: cliDeploy
}