-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
80 lines (66 loc) · 2.28 KB
/
main.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
const aws = require('aws-sdk')
const clui = require('clui')
const yaml = require ('js-yaml')
const fs = require ('fs')
aws.config.update({region:'ap-northeast-1'})
const cloudformation = new aws.CloudFormation()
const creatingSpinner = new clui.Spinner('Creating stack ...', ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'])
const deletingSpinner = new clui.Spinner('Deleting stack ...', ['⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽', '⣾'])
let filepath = process.argv[2]
if (filepath === undefined) {
console.error('Template filepath is required.\n')
process.exit(1)
}
let stacks = loadStacksConfig(filepath)
let createdStackCount = 0
creationProcess()
.catch((error) => {
console.error("Create failed.")
console.error(' ERROR CODE : ', error.code)
console.error(' ERROR MESSAGE: ', error.message, "\n")
deletionProcess()
})
// functions
async function creationProcess () {
for (let i = 0; i < stacks.length; i++) {
let stackData = await cloudformation.createStack(stacks[i]).promise()
.catch(error => { return Promise.reject(error) })
createdStackCount++
stacks[i]['StackId'] = stackData.StackId
creatingSpinner.start()
await cloudformation.waitFor('stackCreateComplete', {StackName: stackData.StackId}).promise()
.catch(error => {
creatingSpinner.stop()
return Promise.reject(error)
})
creatingSpinner.stop()
console.log("Create complete: ", stackData.StackId, "\n")
}
}
async function deletionProcess () {
for (; createdStackCount > 0; createdStackCount--) {
let process = await cloudformation.deleteStack({StackName: stacks[createdStackCount - 1]['StackId']}).promise()
.catch(error => {
console.error(error)
process.exit(1)
})
deletingSpinner.start()
await cloudformation.waitFor('stackDeleteComplete', {StackName: stacks[createdStackCount - 1]['StackId']}).promise()
.catch(error => {
deletingSpinner.stop()
console.error(error)
process.exit(1)
})
deletingSpinner.stop()
console.log("Delete complete: ", stacks[createdStackCount - 1]['StackId'], "\n")
}
}
function loadStacksConfig (filepath) {
try {
let doc = yaml.safeLoad(fs.readFileSync(filepath, 'utf8'))
return doc['Stacks']
} catch (err) {
console.error(err)
process.exit(1)
}
}