-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (130 loc) · 4.82 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
const core = require('@actions/core');
const github = require('@actions/github');
const H = require('highland');
const R = require('ramda');
const aws = require('aws-sdk');
const fs = require('fs');
const cfn = H.streamifyAll(new aws.CloudFormation());
const inputKeys = ['template', 'stack-name', 'capabilities', 'parameters'];
const DEBUG = R.isEmpty(core.getInput('debug')) ? false : true;
const log = ctx => obj => {
if (DEBUG) {
console.log(ctx);
console.log(JSON.stringify(obj, null, 2));
}
};
const processCapabilities = capabilities => capabilities === '' ? [] : capabilities
.replace(/^\ +/, '')
.replace(/\ +$/, '')
.replace(/\ +/g, ' ')
.split(' ');
const processParameters = parameters => parameters === '' ? [] : parameters
.replace(/^\ +/, '')
.replace(/\ +$/, '')
.replace(/\ +/g, ' ')
.split(' ')
.map(parameter => parameter.split('='))
.map(([ParameterKey, ParameterValue]) => ({
ParameterKey,
ParameterValue
}));
const getStackInputs = inputKeys => (({
template: Template,
'stack-name': StackName,
capabilities,
parameters
}) => ({
Template,
StackName,
Capabilities: processCapabilities(capabilities),
Parameters: processParameters(parameters)
}))(R.fromPairs(R.zip(
inputKeys,
inputKeys.map(core.getInput)
)));
const waitForStackReady = StackName => cfn.describeStacksStream({ StackName })
.doto(log('waitForStackReady: describeStacksStream'))
.map(({ Stacks: [{ StackStatus }] }) => StackStatus)
.doto(log('waitForStackReady: StackStatus'))
.errors((error, push) => {
log('waitForStackReady: describeStacksStream: error')(error);
return error.message.indexOf('does not exist') !== -1
? push(null, 'INIT')
: push(error);
})
.flatMap(H.wrapCallback((StackStatus, callback) => /^.*_FAILED/.test(StackStatus)
? callback({ StackStatus, message: `Stack ${StackName} is in the ${StackStatus} state. Fix that before trying to deploy again` })
: callback(null, StackStatus)
))
.flatMap(StackStatus => /^.*_COMPLETE$/.test(StackStatus) || R.contains(StackStatus, R.keys(StackStatusHandlers))
? H([StackStatus])
: H((push, next) => setTimeout(() => next(waitForStackReady(StackName)), 30000))
);
const StackStatusHandlers = {
INIT: ({ StackName, Capabilities, Parameters, TemplateBody }) => H([{
StackName,
Capabilities,
Parameters,
TemplateBody
}])
.doto(({ StackName }) => console.log(`Stack ${StackName} does not exist yet: creating ...`))
.flatMap(params => cfn.createStackStream(params)),
ROLLBACK_COMPLETE: ({ StackName }) => H([{ StackName }])
.doto(({ StackName }) => console.log(`Stack ${StackName} is in ROLLBACK_COMPLETE state: deleting ...`))
.flatMap(params => cfn.deleteStackStream(params)),
DEFAULT: ({ StackName, Capabilities, Parameters, TemplateBody, StackStatus }) => H([{
StackName,
Capabilities,
Parameters,
TemplateBody
}])
.doto(({ StackName }) => console.log(`Stack ${StackName} is in ${StackStatus} state: updating ...`))
.flatMap(params => cfn.updateStackStream(params))
.errors((error, push) => error.message === 'No updates are to be performed.' ? push(null, {}) : push({
StackStatus,
...error
}))
};
return H([getStackInputs(inputKeys)])
.doto(log('processed input: synchronous'))
.flatMap(({ Template, ...inputs }) => H.wrapCallback(fs.readFile)(Template)
.map(body => body.toString('utf8'))
.map(TemplateBody => ({
...inputs,
TemplateBody
}))
)
.doto(log('processed input: asynchronous'))
.flatMap(({ StackName, ...inputs }) => waitForStackReady(StackName)
.doto(log('result of waiting for stack: phase 1'))
.flatMap(StackStatus => (StackStatusHandlers[StackStatus] || StackStatusHandlers['DEFAULT'])({
StackName,
...inputs,
StackStatus
}))
.doto(log('result of operating on stack'))
.flatMap(() => waitForStackReady(StackName))
.doto(log('result of waiting for stack: phase 2'))
.flatMap(StackStatus => {
if (StackStatus === 'UPDATE_COMPLETE' || StackStatus === 'CREATE_COMPLETE') return H([StackStatus]);
if (StackStatus === 'INIT') return StackStatusHandlers[StackStatus]({
StackName,
...inputs,
StackStatus
})
.doto(log('result of operating on stack'))
.flatMap(() => waitForStackReady(StackName))
.doto(log('result of waiting for stack: phase 3'));
return H(Promise.reject({ StackStatus, message: `Stack ${StackName} deployment failed` }));
})
)
.errors(error => {
console.error(JSON.stringify(error));
core.setOutput('stack-status', error.StackStatus);
core.setOutput('message', error.message);
if (core.getInput('never-fail') === '') core.setFailed(error.message);
})
.each(StackStatus => {
console.log(`final status is ${StackStatus}`);
core.setOutput('stack-status', StackStatus);
});