-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy paths3_deploy.js
158 lines (143 loc) · 3.67 KB
/
s3_deploy.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
'use strict'
const process = require('process')
const crypto = require('crypto')
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createBucket-property
const S3_LOCATION_POSSIBLE_VALUES = [
'EU',
'af-south-1',
'ap-east-1',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-south-1',
'ap-southeast-1',
'ap-southeast-2',
'ap-southeast-3',
'ca-central-1',
'cn-north-1',
'cn-northwest-1',
'eu-central-1',
'eu-north-1',
'eu-south-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'me-south-1',
'sa-east-1',
'us-east-2',
'us-gov-east-1',
'us-gov-west-1',
'us-west-1',
'us-west-2'
]
class S3Deploy {
constructor (aws, region) {
// Authenticated `aws` object in `lib/main.js`
this.s3 = new aws.S3({
region,
apiVersion: '2006-03-01'
})
}
_md5 (str) {
return crypto
.createHash('md5')
.update(str, 'utf8')
.digest('hex')
}
_convertRegionStringToEnvVarName (region) {
if (region == null) return 'undefined'
return region.replace(/-/g, '_').toUpperCase()
}
_getBucketNameFromEnvVar (region) {
const key = [
'S3',
this._convertRegionStringToEnvVarName(region),
'BUCKET'
].join('_')
return process.env[key]
}
_getS3KeyPrefixFromEnvVar (region) {
const key = [
'S3',
this._convertRegionStringToEnvVarName(region),
'PREFIX'
].join('_')
return process.env[key]
}
_bucketName (params) {
const bucketNameFromEnvVar = this._getBucketNameFromEnvVar(params.region)
if (bucketNameFromEnvVar != null) return bucketNameFromEnvVar
return [
params.FunctionName,
params.region,
this._md5(params.FunctionName + params.region)
]
.join('-')
.substr(0, 63).toLowerCase()
}
_s3Key (params) {
const s3Prefix = this._getS3KeyPrefixFromEnvVar(params.region)
const keys = [`deploy-package-${params.FunctionName}.zip`]
if (s3Prefix != null) {
keys.unshift(s3Prefix.replace(/\/$/, ''))
}
return keys.join('/')
}
_getS3Location (region) {
return S3_LOCATION_POSSIBLE_VALUES.includes(region) ? region : null
}
_createBucket (params) {
const _params = {
Bucket: params.bucketName
}
const s3Location = this._getS3Location(params.region)
if (s3Location != null) {
_params.CreateBucketConfiguration = {
LocationConstraint: s3Location
}
}
return new Promise((resolve, reject) => {
this.s3.createBucket(_params, (err, data) => {
if (err) {
// Ignored created
if (err.code === 'BucketAlreadyOwnedByYou') return resolve({})
return reject(err)
}
resolve(data)
})
})
}
_putObject (params, buffer) {
const _params = {
Body: buffer,
Bucket: params.bucketName,
Key: params.s3Key
}
return new Promise((resolve, reject) => {
this.s3.putObject(_params, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
putPackage (params, region, buffer) {
const _params = Object.assign({ region }, params)
_params.bucketName = this._bucketName(_params)
_params.s3Key = this._s3Key(_params)
return this._createBucket(_params).then((result) => {
if (result.Location != null) {
console.log('=> S3 Bucket created:')
console.log(`===> ${_params.bucketName}`)
}
return this._putObject(_params, buffer)
}).then((result) => {
console.log('=> Deploy the zip file to S3:')
console.log(`===> ${_params.bucketName}/${_params.s3Key}`)
return {
S3Bucket: _params.bucketName,
S3Key: _params.s3Key
}
})
}
}
module.exports = S3Deploy