forked from Authress-Engineering/aws-architect.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
265 lines (227 loc) · 11.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Automatically configure microservice in AWS
Copyright (C) 2018 Warren Parad
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
let archiver = require('archiver');
let aws = require('aws-sdk');
let exec = require('child_process').exec;
let fs = require('fs-extra');
let path = require('path');
let os = require('os');
let uuid = require('uuid');
let http = require('http');
let Server = require('./lib/server');
let ApiGatewayManager = require('./lib/ApiGatewayManager');
let LambdaManager = require('./lib/LambdaManager');
let ApiConfiguration = require('./lib/ApiConfiguration');
let BucketManager = require('./lib/BucketManager');
let CloudFormationDeployer = require('./lib/CloudFormationDeployer');
let LockFinder = require('./lib/lockFinder');
function AwsArchitect(packageMetadata, apiOptions, contentOptions) {
this.PackageMetadata = packageMetadata;
this.ContentOptions = contentOptions || {};
this.deploymentBucket = (apiOptions || {}).deploymentBucket;
this.SourceDirectory = (apiOptions || {}).sourceDirectory;
if (!aws.config.region && apiOptions.regions && apiOptions.regions[0]) {
aws.config.update({ region: apiOptions.regions[0] });
}
this.Configuration = new ApiConfiguration(apiOptions, 'index.js', aws.config.region || 'us-east-1');
if (this.Configuration.Regions.length === 0) { throw new Error('A single region must be defined in the apiOptions.'); }
if (this.Configuration.Regions.length > 1) { throw new Error('Only deployments to a single region are allowed at this time.'); }
this.Region = this.Configuration.Regions[0];
let apiGatewayFactory = new aws.APIGateway({ region: this.Region });
this.ApiGatewayManager = new ApiGatewayManager(this.PackageMetadata.name, this.PackageMetadata.version, apiGatewayFactory);
let lambdaFactory = new aws.Lambda({ region: this.Region });
this.LambdaManager = new LambdaManager(this.PackageMetadata.name, lambdaFactory);
let s3Factory = new aws.S3({ region: this.Region });
this.BucketManager = new BucketManager(s3Factory, this.ContentOptions.bucket);
let cloudFormationClient = new aws.CloudFormation({ region: this.Region });
this.CloudFormationDeployer = new CloudFormationDeployer(cloudFormationClient, this.BucketManager, this.deploymentBucket);
}
function GetAccountIdPromise() {
return new aws.IAM().getUser({}).promise()
.then(data => data.User.Arn.split(':')[4])
.catch(() => {
//assume EC2 instance profile
return new Promise((resolve, reject) => {
http.get('http://169.254.169.254/latest/dynamic/instance-identity/document', res => {
if (res.statusCode >= 400) { return Promise.reject('Failed to lookup AWS AccountID, please specify by running as IAM user or with credentials.'); }
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
try {
let json = JSON.parse(data);
resolve(json.accountId);
} catch (exception) {
reject(JSON.stringify({ Title: 'Failure trying to parse AWS AccountID from metadata', Error: exception.stack || exception.toString(), Details: exception, Response: data }));
}
});
res.on('error', error => reject(error));
return null;
});
});
});
}
AwsArchitect.prototype.publishZipArchive = async function(options = {}) {
if (!options.zipFileName || !this.deploymentBucket || !options.sourceDirectory) {
throw Error('The zipFileName, sourceDirectory, api options deploymentBucket must be specified.');
}
let tmpDir = path.join(os.tmpdir(), `zipDirectory-${uuid.v4()}`);
await new Promise((resolve, reject) => { fs.stat(options.sourceDirectory, (error, stats) => error || !stats.isDirectory ? reject(error || 'NotDirectoryError') : resolve()); });
await fs.copy(options.sourceDirectory, tmpDir);
let zipArchivePath = path.join(tmpDir, options.zipFileName);
await new Promise((resolve, reject) => {
let zipStream = fs.createWriteStream(zipArchivePath);
zipStream.on('close', () => resolve());
let archive = archiver.create('zip', {});
archive.on('error', e => reject({ Error: e }));
archive.pipe(zipStream);
archive.glob('**', { dot: true, cwd: tmpDir, ignore: options.zipFileName });
archive.finalize();
});
await this.BucketManager.DeployLambdaPromise(this.deploymentBucket, zipArchivePath, `${this.PackageMetadata.name}/${this.PackageMetadata.version}/${options.zipFileName}`);
};
AwsArchitect.prototype.publishLambdaArtifactPromise = AwsArchitect.prototype.PublishLambdaArtifactPromise = async function(options = {}) {
let lambdaZip = options && options.zipFileName || 'lambda.zip';
let tmpDir = path.join(os.tmpdir(), `lambda-${uuid.v4()}`);
await new Promise((resolve, reject) => {
fs.stat(this.SourceDirectory, (error, stats) => {
if (error) { return reject({ Error: `Path does not exist: ${this.SourceDirectory} - ${error}` }); }
if (!stats.isDirectory) { return reject({ Error: `Path is not a directory: ${this.SourceDirectory}` }); }
return resolve(null);
});
});
await fs.copy(this.SourceDirectory, tmpDir);
// (default: true) If set to true, will attempt to copy and install packages related to deployment (i.e. package.json for node)
if (options.autoHandleCompileOfSourceDirectory !== false) {
await new LockFinder().findLockFile(this.SourceDirectory).then(lockFile => {
return lockFile ? fs.copy(lockFile.file, path.join(tmpDir, path.basename(lockFile.file))) : Promise.resolve();
});
try {
await fs.writeJson(path.join(tmpDir, 'package.json'), this.PackageMetadata);
} catch (error) {
return { Error: 'Failed writing production package.json file.', Details: error };
}
const exists = await fs.pathExists(path.join(tmpDir, 'yarn.lock')).catch(() => false);
let cmd = exists ? 'yarn --prod --frozen-lockfile' : 'npm install --production';
await new Promise((resolve, reject) => {
/* eslint-disable-next-line no-unused-vars */
exec(cmd, { cwd: tmpDir }, (error, stdout, stderr) => {
if (error) { return reject({ Error: 'Failed installing production npm modules.', Details: error }); }
return resolve(tmpDir);
});
});
}
let zipArchivePath = path.join(tmpDir, lambdaZip);
await new Promise((resolve, reject) => {
let zipStream = fs.createWriteStream(zipArchivePath);
zipStream.on('close', () => resolve({ Archive: zipArchivePath }));
let archive = archiver.create('zip', {});
archive.on('error', e => reject({ Error: e }));
archive.pipe(zipStream);
archive.glob('**', { dot: true, cwd: tmpDir, ignore: lambdaZip });
archive.finalize();
});
let bucket = options && options.bucket || this.deploymentBucket;
if (bucket) {
await this.BucketManager.DeployLambdaPromise(bucket, zipArchivePath, `${this.PackageMetadata.name}/${this.PackageMetadata.version}/${lambdaZip}`);
}
};
AwsArchitect.prototype.validateTemplate = AwsArchitect.prototype.ValidateTemplate = function(stackTemplate, stackConfiguration) {
return this.CloudFormationDeployer.validateTemplate(stackTemplate, stackConfiguration && stackConfiguration.stackName, `${this.PackageMetadata.name}/${this.PackageMetadata.version}`);
};
AwsArchitect.prototype.deployTemplate = AwsArchitect.prototype.DeployTemplate = function(stackTemplate, stackConfiguration, parameters) {
return this.CloudFormationDeployer.deployTemplate(stackTemplate, stackConfiguration, parameters, `${this.PackageMetadata.name}/${this.PackageMetadata.version}`);
};
AwsArchitect.prototype.deployStagePromise = AwsArchitect.prototype.DeployStagePromise = function(stage, lambdaVersion) {
if (!stage) { throw new Error('Deployment stage is not defined.'); }
if (!lambdaVersion) { throw new Error('Deployment lambdaVersion is not defined.'); }
return this.ApiGatewayManager.GetApiGatewayPromise()
.then(result => result.Id)
.then(restApiId => this.ApiGatewayManager.DeployStagePromise(restApiId, stage, lambdaVersion));
};
function getStageName(stage) {
return stage.replace(/[^a-zA-Z0-9-]/g, '-');
}
AwsArchitect.prototype.removeStagePromise = AwsArchitect.prototype.RemoveStagePromise = function(stage) {
if (!stage) { throw new Error('Deployment stage is not defined.'); }
let stageName = getStageName(stage);
let apiGatewayPromise = this.ApiGatewayManager.GetApiGatewayPromise();
return apiGatewayPromise
.then(result => this.ApiGatewayManager.RemoveStagePromise(result.Id, stageName))
.then(result => ({
title: 'Successfully delete stage',
stage: stageName,
details: result
}));
};
AwsArchitect.prototype.publishAndDeployStagePromise = AwsArchitect.prototype.PublishAndDeployStagePromise = async function(options = {}) {
let stage = options.stage;
let stageName = getStageName(stage);
let functionName = options.functionName;
let bucket = options.deploymentBucketName || this.deploymentBucket;
let deploymentKey = options.deploymentKeyName;
if (!stage) { throw new Error('Deployment stage is not defined.'); }
let apiGateway = await this.ApiGatewayManager.GetApiGatewayPromise();
let apiGatewayId = apiGateway.Id;
let accountId = await GetAccountIdPromise();
return this.LambdaManager.PublishNewVersion(functionName, bucket, deploymentKey)
.then(async lambda => {
let lambdaArn = lambda.FunctionArn;
let lambdaVersion = lambda.Version;
await this.LambdaManager.SetAlias(functionName, stageName, lambdaVersion);
await this.LambdaManager.SetPermissionsPromise(accountId, lambdaArn, apiGatewayId, this.Region, stageName);
return {
LambdaFunctionArn: lambdaArn,
LambdaVersion: lambdaVersion,
RestApiId: apiGatewayId
};
})
.then(result => {
return this.ApiGatewayManager.DeployStagePromise(result.RestApiId, stageName, stage, result.LambdaVersion)
.then(data => {
return {
LambdaResult: result,
ApiGatewayResult: data,
ServiceApi: `https://${result.RestApiId}.execute-api.${this.Region}.amazonaws.com/${stageName}`
};
});
})
.catch(failure => {
return Promise.reject({ Error: 'Failed to create and deploy updates.', Details: failure });
});
};
AwsArchitect.prototype.publishWebsite = AwsArchitect.prototype.PublishWebsite = function(version, options = {}) {
if (!this.BucketManager.Bucket) { throw new Error('Bucket in cotent options has not been defined.'); }
if (!this.ContentOptions.contentDirectory) { throw new Error('Content directory is not defined.'); }
if (!version) { throw new Error('Deployment version is not defined.'); }
return this.BucketManager.Deploy(this.ContentOptions.contentDirectory, version, options.cacheControlRegexMap || [], options.contentTypeMappingOverride);
};
AwsArchitect.prototype.run = AwsArchitect.prototype.Run = async function(port, logger) {
try {
let indexPath = path.join(this.SourceDirectory, 'index.js');
let api = require(indexPath);
let server = new Server(this.ContentOptions.contentDirectory, api, logger);
let attemptPort = port || 8080;
let resolvedPort = await server.Run(attemptPort);
if (resolvedPort !== attemptPort) {
console.log('Requested Port is in use. Using the next available port.');
}
return Promise.resolve({ title: `Server started successfully at 'http://localhost:${resolvedPort}', lambda routes available at /api, /triggers/event, /triggers/schedule.`, server });
} catch (exception) {
return Promise.reject({ title: 'Failed to start server', error: exception.stack || exception });
}
};
module.exports = AwsArchitect;