-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeploy.js
159 lines (146 loc) · 4.02 KB
/
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
159
let series = require('run-series')
let parallel = require('run-parallel')
let pkg = require('../')
let { toLogicalID } = require('@architect/utils')
let _inventory = require('@architect/inventory')
let path = require('path')
let fs = require('fs')
let sam = require('./package')
let spawn = require('./spawn')
let awsLite = require('@aws-lite/client')
/**
* deploy
*
* Deploys an .arc file as a nested cloudformation stack.
*
* (Note: all params are optional)
*
* @param {Object} params
* @param {Boolean} params.log - enable/disable logging (default false)
* @param {Boolean} params.verbose - enable/disable verbose logging (default false)
* @param {Boolean} params.production - deploy to production (default false)
* @param {Boolean} params.clean - delete generated files (default true)
* @param {Object} params.arc - parsed .arc file (if not passed in will read the .arc in cwd)
* @param {Function} callback - node style errback
* @returns {Promise}
*/
module.exports = function deploy (params = {}, callback) {
/**
* the basic flow is this:
*
* 1.) reads the current .arc generates cfn templates:
*
* - appname-cfn.json
* - appname-cfn-http.json
* - appname-cfn-events.json
* - appname-cfn-queues.json
* - appname-cfn-tables.json
*
* 2.) package all templates into yaml (and uploads assets to the cfn bucket):
*
* - appname-cfn.yaml
* - appname-cfn-http.yaml
* - appname-cfn-events.yaml
* - appname-cfn-queues.yaml
* - appname-cfn-tables.yaml
*
* 3.) runs `sam deploy` on:
*
* - appname-cfn.yaml
*/
// return a promise if no callback is supplied
let promise
if (!callback) {
promise = new Promise(function ugh (res, rej) {
callback = function errback (err, result) {
if (err) rej(err)
else res(result)
}
})
}
// params
let log = params.log || false
let verbose = params.verbose || false
let production = params.production || false
let clean = params.clean || true
// derived
let cfn
let bucket
let appname
let name
let aws
series([
function getInv (callback) {
_inventory({}, function (err, inventory) {
if (err) callback(err)
else {
let { inv, get } = inventory
cfn = pkg(inventory)
bucket = get.aws('bucket')
appname = inv.app
name = `${toLogicalID(appname)}${production ? 'Production' : 'Staging'}`
}
})
},
function toCFN (callback) {
parallel(Object.keys(cfn).map(k => {
return function writes (callback) {
fs.writeFile(k, JSON.stringify(cfn[k], null, 2), callback)
}
}), callback)
},
function samPackage (callback) {
series(Object.keys(cfn).map(k => {
return function packages (callback) {
sam({
filename: k,
bucket,
log,
verbose,
}, callback)
}
}), callback)
},
function getAws (callback) {
awsLite({ plugins: [ import('@aws-lite/s3') ] })
.then(_aws => {
aws = _aws
callback()
})
.catch(err => callback(err))
},
function uploadToS3 (callback) {
parallel(Object.keys(cfn).map(k => {
return function uploads (callback) {
let Key = k.replace('json', 'yaml')
aws.s3.PutObject({
Bucket: bucket,
Key,
File: path.join(process.cwd(), Key),
}, callback)
}
}), callback)
},
function samDeploy (callback) {
spawn('sam', [ 'deploy',
'--template-file', `${appname}-cfn.yaml`,
'--stack-name', name,
'--s3-bucket', bucket,
'--capabilities', 'CAPABILITY_AUTO_EXPAND', 'CAPABILITY_IAM',
], { log, verbose }, callback)
},
function cleanup (callback) {
if (clean) {
parallel(Object.keys(cfn).map(k => {
return function writes (callback) {
fs.unlink(path.join(process.cwd(), k), callback)
}
}), callback)
}
else {
callback()
}
},
], callback)
return promise
}