11'use strict' ;
22const BbPromise = require ( 'bluebird' ) ;
33const path = require ( 'path' ) ;
4- const _ = require ( 'lodash' ) ;
54
65class AwsStepFunctionsDeploy {
76 constructor ( serverless , options ) {
@@ -15,31 +14,47 @@ class AwsStepFunctionsDeploy {
1514 stepf : {
1615 usage : 'Deploy Step functions' ,
1716 lifecycleEvents : [
18- 'deploy'
17+ 'deploy' ,
1918 ] ,
2019 options : {
2120 statemachine : {
2221 usage : 'Name of the State Machine' ,
2322 shortcut : 'sm' ,
2423 required : true ,
25- }
26- }
27- }
28- }
29- }
24+ } ,
25+ } ,
26+ } ,
27+ } ,
28+ } ,
3029 } ;
3130
3231 this . hooks = {
3332 'deploy:stepf:deploy' : this . action . bind ( this ) ,
3433 } ;
3534 }
3635
36+ setStateMachineArn ( ) {
37+ return this . provider . request ( 'STS' ,
38+ 'getCallerIdentity' ,
39+ { } ,
40+ this . options . stage ,
41+ this . options . region )
42+ . then ( ( result ) => {
43+ const region = this . options . region || 'us-east-1' ;
44+ this . stateMachineArn =
45+ `arn:aws:states:${ region } :${ result . Account } :stateMachine:${ this . options . statemachine } ` ;
46+ return BbPromise . resolve ( ) ;
47+ } ) ;
48+ }
49+
3750 action ( ) {
3851 this . serverless . cli . consoleLog ( 'Start Deploy Step Functions' ) ;
3952 BbPromise . bind ( this )
4053 . then ( this . yamlParse )
54+ . then ( this . setStateMachineArn )
4155 . then ( this . compile )
42- . then ( this . deploy ) ;
56+ . then ( this . deleteStateMachine )
57+ . then ( this . createStateMachine ) ;
4358 }
4459
4560 yamlParse ( ) {
@@ -70,60 +85,51 @@ class AwsStepFunctionsDeploy {
7085
7186 if ( typeof this . stepFunctions [ this . options . statemachine ] === 'undefined' ) {
7287 const errorMessage = [
73- `Step function "${ this . options . statemachine } " is not exists` ,
74- ] . join ( '' ) ;
75- throw new this . serverless . classes
76- . Error ( errorMessage ) ;
88+ `Step function "${ this . options . statemachine } " is not exists` ,
89+ ] . join ( '' ) ;
90+ throw new this . serverless . classes . Error ( errorMessage ) ;
7791 }
7892
79- //@todo get lambda arn from functionname
80- this . awsStateLanguage [ this . options . statemachine ] = JSON . stringify ( this . stepFunctions [ this . options . statemachine ] ) ;
93+ // @todo get lambda arn from functionname
94+ this . awsStateLanguage [ this . options . statemachine ] =
95+ JSON . stringify ( this . stepFunctions [ this . options . statemachine ] ) ;
8196 return BbPromise . resolve ( ) ;
8297 }
8398
84- deploy ( ) {
85- let stateMachineArn ;
86-
87- return this . provider . request ( 'STS' ,
88- 'getCallerIdentity' ,
89- { } ,
99+ deleteStateMachine ( ) {
100+ return this . provider . request ( 'StepFunctions' ,
101+ 'deleteStateMachine' ,
102+ {
103+ stateMachineArn : this . stateMachineArn ,
104+ } ,
90105 this . options . stage ,
91106 this . options . region )
92- . then ( ( result ) => {
93- const region = this . options . region || 'us-east-1' ;
94- stateMachineArn = `arn:aws:states:${ region } :${ result . Account } :stateMachine:${ this . options . statemachine } ` ;
95- } ) . then ( ( result ) => {
96- return this . provider . request ( 'StepFunctions' ,
97- 'describeStateMachine' ,
98- {
99- stateMachineArn
100- } ,
101- this . options . stage ,
102- this . options . region ) ;
103- } ) . then ( ( result ) => {
104- console . log ( result )
105- return this . provider . request ( 'StepFunctions' ,
106- 'deleteStateMachine' ,
107- {
108- stateMachineArn
109- } ,
110- this . options . stage ,
111- this . options . region ) ;
107+ . then ( ( ) => BbPromise . resolve ( ) ) ;
108+ }
112109
113- } ) . then ( ( result ) => {
114- return this . provider . request ( 'StepFunctions' ,
115- 'createStateMachine' ,
116- {
117- definition : this . awsStateLanguage [ this . options . statemachine ] ,
118- name : this . options . statemachine ,
119- roleArn : ''
120- } ,
121- this . options . stage ,
122- this . options . region )
123- } ) . catch ( ( error ) => {
124- console . log ( error ) ;
110+ createStateMachine ( ) {
111+ return this . provider . request ( 'StepFunctions' ,
112+ 'createStateMachine' ,
113+ {
114+ definition : this . awsStateLanguage [ this . options . statemachine ] ,
115+ name : this . options . statemachine ,
116+ roleArn : '' ,
117+ } ,
118+ this . options . stage ,
119+ this . options . region )
120+ . then ( ( ) => BbPromise . resolve ( ) )
121+ . catch ( ( error ) => {
122+ if ( error . message . match ( / S t a t e M a c h i n e i s b e i n g d e l e t e d / ) ) {
123+ setTimeout ( this . createStateMachine . bind ( this ) , 5000 ) ;
124+ }
125125 } ) ;
126126 }
127127
128+ deploy ( ) {
129+ return BbPromise . bind ( this )
130+ . then ( this . deleteStateMachine )
131+ . then ( this . createStateMachine ) ;
132+ }
133+
128134}
129135module . exports = AwsStepFunctionsDeploy ;
0 commit comments