Skip to content

Commit e529427

Browse files
committed
add invoke command
1 parent 2a74eed commit e529427

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

lib/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const httpIamRole = require('./deploy/events/apiGateway/iamRole');
1111
const httpDeployment = require('./deploy/events/apiGateway/deployment');
1212
const httpRestApi = require('./deploy/events/apiGateway/restApi');
1313
const httpInfo = require('./deploy/events/apiGateway/endpointInfo');
14+
const invoke = require('./invoke/invoke');
1415
const yamlParser = require('./yamlParser');
1516
const naming = require('./naming');
1617
const _ = require('lodash');
@@ -37,6 +38,7 @@ class ServerlessStepFunctions {
3738
httpMethods,
3839
httpIamRole,
3940
httpDeployment,
41+
invoke,
4042
yamlParser,
4143
naming
4244
);
@@ -51,7 +53,7 @@ class ServerlessStepFunctions {
5153
],
5254
options: {
5355
name: {
54-
usage: 'State Machine name',
56+
usage: 'The StateMachine name',
5557
shortcut: 'n',
5658
required: true,
5759
},
@@ -82,7 +84,7 @@ class ServerlessStepFunctions {
8284
'invoke:stepf:invoke': () => BbPromise.bind(this)
8385
.then(this.invoke),
8486
'deploy:initialize': () => BbPromise.bind(this)
85-
.then(this.yamlParse),
87+
.then(this.yamlParse),
8688
'deploy:compileFunctions': () => BbPromise.bind(this)
8789
.then(this.compileIamRole)
8890
.then(this.compileStateMachines)
@@ -108,7 +110,23 @@ class ServerlessStepFunctions {
108110
}
109111

110112
invoke() {
111-
113+
return BbPromise.bind(this)
114+
.then(this.yamlParse)
115+
.then(this.getStateMachineArn)
116+
.then(this.startExecution)
117+
.then(this.describeExecution)
118+
.then((result) => {
119+
if (result.status === 'FAILED') {
120+
return this.getExecutionHistory()
121+
.then((error) => {
122+
this.serverless.cli.consoleLog(_.merge(result, error.events[error.events.length - 1]
123+
.executionFailedEventDetails));
124+
process.exitCode = 1;
125+
});
126+
}
127+
this.serverless.cli.consoleLog(result);
128+
return BbPromise.resolve();
129+
});
112130
}
113131

114132
display() {

lib/invoke/invoke.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
const BbPromise = require('bluebird');
4+
const path = require('path');
5+
6+
module.exports = {
7+
getStateMachineArn() {
8+
const stateMachineOutputKey =
9+
this.getStateMachineOutputLogicalId(this.options.name);
10+
const stackName = this.provider.naming.getStackName(this.options.stage);
11+
12+
return this.provider.request('CloudFormation',
13+
'describeStacks',
14+
{ StackName: stackName },
15+
this.options.stage,
16+
this.options.region)
17+
.then((result) => {
18+
if (result) {
19+
result.Stacks[0].Outputs.forEach((output) => {
20+
if (output.OutputKey === stateMachineOutputKey) {
21+
this.stateMachineArn = output.OutputValue;
22+
}
23+
});
24+
25+
if (!this.stateMachineArn) {
26+
const errorMessage = [
27+
`"${this.options.name}" stateMachine does not exists.`,
28+
].join('');
29+
throw new this.serverless.classes.Error(errorMessage);
30+
}
31+
}
32+
33+
return BbPromise.resolve();
34+
});
35+
},
36+
37+
startExecution() {
38+
let inputData;
39+
40+
if (this.options.data) {
41+
inputData = this.options.data;
42+
} else if (this.options.path) {
43+
const absolutePath = path.isAbsolute(this.options.path) ?
44+
this.options.path :
45+
path.join(this.serverless.config.servicePath, this.options.path);
46+
if (!this.serverless.utils.fileExistsSync(absolutePath)) {
47+
throw new this.serverless.classes.Error('The file you provided does not exist.');
48+
}
49+
inputData = JSON.stringify(this.serverless.utils.readFileSync(absolutePath));
50+
}
51+
52+
return this.provider.request('StepFunctions',
53+
'startExecution',
54+
{
55+
stateMachineArn: this.stateMachineArn,
56+
input: inputData,
57+
},
58+
this.options.stage,
59+
this.options.region)
60+
.then((result) => {
61+
this.executionArn = result.executionArn;
62+
return BbPromise.resolve();
63+
}).catch((error) => {
64+
throw new this.serverless.classes.Error(error.message);
65+
});
66+
},
67+
68+
describeExecution() {
69+
return this.provider.request('StepFunctions',
70+
'describeExecution',
71+
{
72+
executionArn: this.executionArn,
73+
},
74+
this.options.stage,
75+
this.options.region)
76+
.then((result) => {
77+
if (result.status === 'RUNNING') {
78+
this.serverless.cli.printDot();
79+
return this.setTimeout()
80+
.then(() => this.describeExecution());
81+
}
82+
return BbPromise.resolve(result);
83+
});
84+
},
85+
86+
getExecutionHistory() {
87+
return this.provider.request('StepFunctions',
88+
'getExecutionHistory',
89+
{
90+
executionArn: this.executionArn,
91+
},
92+
this.options.stage,
93+
this.options.region)
94+
.then((result) => BbPromise.resolve(result));
95+
},
96+
97+
setTimeout() {
98+
return new BbPromise((resolve) => {
99+
setTimeout(resolve, 5000);
100+
});
101+
},
102+
};

0 commit comments

Comments
 (0)