-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathutils.js
147 lines (128 loc) · 3.37 KB
/
utils.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
'use strict'
const _ = require('lodash')
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const execSync = require('child_process').execSync
const aws = require('aws-sdk')
const s3 = new aws.S3()
const region = 'us-east-1'
const dynamodb = new aws.DynamoDB({ region })
const cloudformation = new aws.CloudFormation({ region })
function getApiGatewayEndpoint(outputs) {
return outputs.ServiceEndpoint.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
}
async function getStackOutputs(stackName) {
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()
const stack = result.Stacks[0]
const keys = stack.Outputs.map((x) => x.OutputKey)
const values = stack.Outputs.map((x) => x.OutputValue)
return _.zipObject(keys, values)
}
async function getDynamodbItemWithHashKey(tableName, hashKeyAttribute, hashKey) {
return await dynamodb
.getItem({
Key: {
[hashKeyAttribute]: hashKey
},
TableName: tableName
})
.promise()
}
async function getDynamodbItemWithHashKeyAndRangeKey(
tableName,
hashKeyAttribute,
hashKey,
rangeKeyAttribute,
rangeKey
) {
return await dynamodb
.getItem({
Key: {
[hashKeyAttribute]: hashKey,
[rangeKeyAttribute]: rangeKey
},
TableName: tableName
})
.promise()
}
async function putDynamodbItem(tableName, item) {
await dynamodb
.putItem({
Item: item,
TableName: tableName
})
.promise()
}
async function cleanUpDynamodbItems(tableName, hashKeyAttribute, rangeKeyAttribute) {
const items = await dynamodb.scan({ TableName: tableName }).promise()
if (items.Count > 0) {
await Promise.all(
items.Items.map(async (item) => {
const key = {
[hashKeyAttribute]: item[hashKeyAttribute]
}
if (rangeKeyAttribute) {
key[rangeKeyAttribute] = item[rangeKeyAttribute]
}
await dynamodb
.deleteItem({
Key: key,
TableName: tableName
})
.promise()
})
)
}
}
async function getS3Object(bucket, key) {
const resp = await s3
.getObject({
Bucket: bucket,
Key: key
})
.promise()
return resp.Body
}
async function deleteS3Object(bucket, key) {
await s3
.deleteObject({
Bucket: bucket,
Key: key
})
.promise()
}
function deployService(stage, config) {
execSync(`npx serverless deploy --stage ${stage} --config ${path.basename(config)}`, {
stdio: 'inherit',
cwd: path.dirname(config)
})
}
function removeService(stage, config) {
execSync(`npx serverless remove --stage ${stage} --config ${path.basename(config)}`, {
stdio: 'inherit',
cwd: path.dirname(config)
})
}
async function deployWithRandomStage(config) {
const serviceName = yaml.safeLoad(fs.readFileSync(config)).service
const stage = Math.random()
.toString(32)
.substring(2)
const stackName = `${serviceName}-${stage}`
deployService(stage, config)
const outputs = await getStackOutputs(stackName)
const endpoint = getApiGatewayEndpoint(outputs)
return { stackName, stage, outputs, endpoint, region }
}
module.exports = {
deployService,
removeService,
deployWithRandomStage,
getS3Object,
deleteS3Object,
getDynamodbItemWithHashKey,
getDynamodbItemWithHashKeyAndRangeKey,
putDynamodbItem,
cleanUpDynamodbItems
}