simplerubysteps 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12126fa84a9d21228e4b705f57b100a007087d2d7cc2f2af84efcfc18c06ffd7
4
- data.tar.gz: 4f7d36681479f8f44370ea4615d0d13d395134c64fa31b822e6542cdcf924c92
3
+ metadata.gz: 8be19b6aa108f0bad7ca85714f134b3ae8876a43a2da627f685fc1f181a1e81b
4
+ data.tar.gz: feb25d925cc950177873b1e6a5a2bf19406d7b7a796ee0983d03a44461d49d6c
5
5
  SHA512:
6
- metadata.gz: bfc06b92ad3921c465ffde34a2440817c0161089e0577eb058adbd4840b88e478f07404fc09387a15a3c63edcd9961b1af6586f4400bab8b3b284b74a90c672d
7
- data.tar.gz: 26aebdba09383657ff151d6ed3abdd2cd1e16fb99a614c3f8686db973aa509777a8d65761b418c12809239d712a3e51be98aab8fcc56d3579df1750c54add53f
6
+ metadata.gz: '04878fef3099b7798994503ceb9c373d559a0bee170ac9747b26a4f27548cd978810627c08ffe579a05eaf0e2bec1ab6d82e7288852c07cec9438b9403181ec4'
7
+ data.tar.gz: 6c719c0cc7ae0e8a9e25387131f552ece86774eed9b45649589070d3e32f1f593e6bcac3915dca64ec836d0cdce647868b2b6a1f6d73c1e6f6efecc2379eadad
data/README.md CHANGED
@@ -42,7 +42,7 @@ simplerubysteps-deploy
42
42
  ```
43
43
  export AWS_PROFILE=...
44
44
  cd samples/sample1
45
- echo '{"foo": "John Wick"}' | simplerubysteps-workflow-run
45
+ echo '{"foo": "James Bond"}' | simplerubysteps-workflow-run
46
46
  ```
47
47
 
48
48
  ### Delete CloudFormation stack
data/bin/setup CHANGED
@@ -4,4 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  bin_dir = File.expand_path(File.dirname(__FILE__))
4
- shell_script_path = File.join(bin_dir, '../lib/deploy.sh')
4
+ shell_script_path = File.join(bin_dir, "../lib/deploy.sh")
5
5
 
6
6
  Kernel.exec(shell_script_path)
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  bin_dir = File.expand_path(File.dirname(__FILE__))
4
- shell_script_path = File.join(bin_dir, '../lib/destroy.sh')
4
+ shell_script_path = File.join(bin_dir, "../lib/destroy.sh")
5
5
 
6
6
  Kernel.exec(shell_script_path)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ bin_dir = File.expand_path(File.dirname(__FILE__))
4
+ shell_script_path = File.join(bin_dir, "../lib/logs.sh")
5
+
6
+ Kernel.exec(shell_script_path)
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  bin_dir = File.expand_path(File.dirname(__FILE__))
4
- shell_script_path = File.join(bin_dir, '../lib/workflow-run.sh')
4
+ shell_script_path = File.join(bin_dir, "../lib/workflow-run.sh")
5
5
 
6
6
  Kernel.exec(shell_script_path)
data/lib/function.rb CHANGED
@@ -1,9 +1,14 @@
1
- require 'json'
2
- require './workflow.rb'
1
+ require "json"
2
+ require "./workflow.rb"
3
3
 
4
4
  def handler(event:, context:)
5
- puts ENV.inspect
6
- puts event
7
- puts context.inspect
8
- $sm.states[event["Task"].to_sym].perform_action event["Input"]
5
+ puts ENV.inspect # FIXME remove DEBUG code
6
+ puts event # FIXME remove DEBUG code
7
+ puts context.inspect # FIXME remove DEBUG code
8
+
9
+ if event["Token"]
10
+ $sm.states[event["Task"].to_sym].perform_action event["Input"], event["Token"]
11
+ else
12
+ $sm.states[event["Task"].to_sym].perform_action event["Input"]
13
+ end
9
14
  end
data/lib/logs.sh ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ export STACK_NAME=$(basename "$PWD")
4
+
5
+ export FUNCTION_NAME=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --output text --query "Stacks[].Outputs[]"|grep LambdaFunctionName|awk '{print $2}')
6
+
7
+ aws logs tail /aws/lambda/$FUNCTION_NAME
@@ -1,3 +1,3 @@
1
1
  module Simplerubysteps
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,177 +2,217 @@ require "simplerubysteps/version"
2
2
  require "json"
3
3
 
4
4
  module Simplerubysteps
5
- $FUNCTION_ARN=ENV["LAMBDA_FUNCTION_ARN"] ? ENV["LAMBDA_FUNCTION_ARN"] : "unknown"
5
+ # Step Functions JSON generator
6
+
7
+ $FUNCTION_ARN = ENV["LAMBDA_FUNCTION_ARN"] ? ENV["LAMBDA_FUNCTION_ARN"] : "unknown"
8
+
9
+ def function_name
10
+ return "unknown" unless $FUNCTION_ARN =~ /.+\:function\:(.+)/
11
+ $1
12
+ end
6
13
 
7
14
  class StateMachine
8
- attr_reader :states
9
- attr_reader :start_at
10
-
11
- def initialize()
12
- @states = {}
13
- end
14
-
15
- def add(state)
16
- @start_at = state.name unless @start_at
17
-
18
- @states[state.name] = state
19
- state.state_machine = self
20
-
21
- state
22
- end
23
-
24
- def render
25
- {
26
- :StartAt => @start_at,
27
- :States => @states.map { |name,state| [name, state.render] }.to_h
28
- }
29
- end
15
+ attr_reader :states
16
+ attr_reader :start_at
17
+
18
+ def initialize()
19
+ @states = {}
20
+ end
21
+
22
+ def add(state)
23
+ @start_at = state.name unless @start_at
24
+
25
+ @states[state.name] = state
26
+ state.state_machine = self
27
+
28
+ state
29
+ end
30
+
31
+ def render
32
+ {
33
+ :StartAt => @start_at,
34
+ :States => @states.map { |name, state| [name, state.render] }.to_h,
35
+ }
36
+ end
30
37
  end
31
38
 
32
39
  class State
33
- attr_reader :name
34
- attr_accessor :state_machine
35
-
36
- def initialize(name)
37
- @name = name
38
- @dict = {}
39
- end
40
-
41
- def []=(key,value)
42
- @dict[key] = value
43
- end
44
-
45
- def next=(state)
46
- @dict[:Next] = (state.is_a? Symbol) ? state : state.name
47
- end
48
-
49
- def render
50
- dict = @dict
51
- dict[:End] = true unless dict[:Next]
52
- dict
53
- end
40
+ attr_reader :name
41
+ attr_accessor :state_machine
42
+
43
+ def initialize(name)
44
+ @name = name
45
+ @dict = {}
46
+ end
47
+
48
+ def []=(key, value)
49
+ @dict[key] = value
50
+ end
51
+
52
+ def next=(state)
53
+ @dict[:Next] = (state.is_a? Symbol) ? state : state.name
54
+ end
55
+
56
+ def render
57
+ dict = @dict
58
+ dict[:End] = true unless dict[:Next]
59
+ dict
60
+ end
54
61
  end
55
62
 
56
63
  class Task < State
57
- def initialize(name)
58
- super
59
- @dict[:Type] = "Task"
60
- @dict[:Resource] = $FUNCTION_ARN
61
- @dict[:"Parameters"] = {
62
- :Task => name,
63
- "Input.$" => "$"
64
- }
65
- end
66
-
67
- def action(&action_block)
68
- @action_block = action_block
69
- end
70
-
71
- def perform_action(input)
72
- @action_block.call(input) if @action_block
73
- end
64
+ def initialize(name)
65
+ super
66
+ @dict[:Type] = "Task"
67
+ @dict[:Resource] = $FUNCTION_ARN
68
+ @dict[:Parameters] = {
69
+ :Task => name,
70
+ "Input.$" => "$",
71
+ }
72
+ end
73
+
74
+ def action(&action_block)
75
+ @action_block = action_block
76
+ end
77
+
78
+ def perform_action(input)
79
+ @action_block.call(input) if @action_block
80
+ end
81
+ end
82
+
83
+ class Callback < State
84
+ def initialize(name)
85
+ super
86
+ @dict[:Type] = "Task"
87
+ @dict[:Resource] = "arn:aws:states:::lambda:invoke.waitForTaskToken"
88
+ @dict[:Parameters] = {
89
+ :FunctionName => function_name,
90
+ :Payload => {
91
+ :Task => name,
92
+ "Input.$" => "$",
93
+ "Token.$" => "$$.Task.Token",
94
+ },
95
+ }
96
+ end
97
+
98
+ def action(&action_block)
99
+ @action_block = action_block
100
+ end
101
+
102
+ def perform_action(input, token)
103
+ @action_block.call(input, token) if @action_block
104
+ end
74
105
  end
75
106
 
76
- class ChoiceItem
77
- def initialize(dict = {}, state = nil)
78
- @dict = dict
79
- self.next = state if state
80
- end
107
+ class ChoiceItem
108
+ def initialize(dict = {}, state = nil)
109
+ @dict = dict
110
+ self.next = state if state
111
+ end
81
112
 
82
- def next=(state)
83
- @dict[:Next] = (state.is_a? Symbol) ? state : state.name
84
- end
113
+ def next=(state)
114
+ @dict[:Next] = (state.is_a? Symbol) ? state : state.name
115
+ end
85
116
 
86
- def render
87
- @dict
88
- end
117
+ def render
118
+ @dict
119
+ end
89
120
  end
90
121
 
91
122
  class Choice < State
92
- attr_reader :choices
93
-
94
- def initialize(name)
95
- super
96
- @choices = []
97
- @dict[:Type] = "Choice"
98
- end
99
-
100
- def add(item)
101
- @choices.push item
102
- end
103
-
104
- def add_string_matches(var, match, state)
105
- add ChoiceItem.new({
106
- :Variable => var,
107
- :StringMatches => match
108
- }, state)
109
- end
110
-
111
- def default=(state)
112
- @dict[:Default] = (state.is_a? Symbol) ? state : state.name
113
- end
114
-
115
- def next=(state)
116
- self.default = state
117
- end
118
-
119
- def render
120
- dict = @dict.clone
121
- dict[:Choices] = @choices.map { |item| item.render }
122
- dict
123
- end
123
+ attr_reader :choices
124
+
125
+ def initialize(name)
126
+ super
127
+ @choices = []
128
+ @dict[:Type] = "Choice"
129
+ end
130
+
131
+ def add(item)
132
+ @choices.push item
133
+ end
134
+
135
+ def add_string_matches(var, match, state)
136
+ add ChoiceItem.new({
137
+ :Variable => var,
138
+ :StringMatches => match,
139
+ }, state)
140
+ end
141
+
142
+ def default=(state)
143
+ @dict[:Default] = (state.is_a? Symbol) ? state : state.name
144
+ end
145
+
146
+ def next=(state)
147
+ self.default = state
148
+ end
149
+
150
+ def render
151
+ dict = @dict.clone
152
+ dict[:Choices] = @choices.map { |item| item.render }
153
+ dict
154
+ end
124
155
  end
125
156
 
126
- ################################################################################
157
+ # Workflow DSL
127
158
 
128
159
  $sm = StateMachine.new
129
160
  $tasks = []
130
161
 
131
162
  def task(name)
132
- t = $sm.add Task.new(name)
163
+ t = $sm.add Task.new(name)
133
164
 
134
- $tasks.last.next = t if $tasks.last
165
+ $tasks.last.next = t if $tasks.last
135
166
 
136
- $tasks.push t
137
- yield if block_given?
138
- $tasks.pop
167
+ $tasks.push t
168
+ yield if block_given?
169
+ $tasks.pop
139
170
  end
140
171
 
172
+ def callback(name)
173
+ t = $sm.add Callback.new(name)
174
+
175
+ $tasks.last.next = t if $tasks.last
176
+
177
+ $tasks.push t
178
+ yield if block_given?
179
+ $tasks.pop
180
+ end
141
181
 
142
182
  def action(&action_block)
143
- $tasks.last.action &action_block
183
+ $tasks.last.action &action_block
144
184
  end
145
185
 
146
186
  def transition(state)
147
- $tasks.last.next = state
187
+ $tasks.last.next = state
148
188
  end
149
189
 
150
190
  def choice(name)
151
- t = $sm.add Choice.new(name)
152
-
153
- $tasks.last.next = t if $tasks.last
191
+ t = $sm.add Choice.new(name)
192
+
193
+ $tasks.last.next = t if $tasks.last
154
194
 
155
- $tasks.push t
156
- yield if block_given?
157
- $tasks.pop
195
+ $tasks.push t
196
+ yield if block_given?
197
+ $tasks.pop
158
198
  end
159
199
 
160
200
  def string_matches(var, match)
161
- c = ChoiceItem.new({
162
- :Variable => var,
163
- :StringMatches => match
164
- })
201
+ c = ChoiceItem.new({
202
+ :Variable => var,
203
+ :StringMatches => match,
204
+ })
165
205
 
166
- $tasks.last.add c
206
+ $tasks.last.add c
167
207
 
168
- $tasks.push c
169
- yield if block_given?
170
- $tasks.pop
208
+ $tasks.push c
209
+ yield if block_given?
210
+ $tasks.pop
171
211
  end
172
212
 
173
213
  def default
174
- $tasks.push $tasks.last
175
- yield if block_given?
176
- $tasks.pop
214
+ $tasks.push $tasks.last
215
+ yield if block_given?
216
+ $tasks.pop
177
217
  end
178
218
  end
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ TOKEN=$(simplerubysteps-logs|grep Token|sort|tail -n 1|ruby -ne 'print $1 if /Token\"=>\"(.+)\"/')
4
+
5
+ echo "{\"continued\":\"$(date)\"}"|./send-task-success.sh $TOKEN
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ tee /tmp/taskoutput.json > /dev/null
4
+ TASK_OUTPUT="$(cat /tmp/taskoutput.json)"
5
+
6
+ aws stepfunctions send-task-success --task-token="$1" --task-output "$TASK_OUTPUT"
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ echo '{"foo": "John Wick"}'|simplerubysteps-workflow-run
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ echo '{"foo": "James Bond"}'|simplerubysteps-workflow-run
@@ -4,37 +4,37 @@ require "simplerubysteps"
4
4
  include Simplerubysteps
5
5
 
6
6
  task :t1 do
7
- action do |input|
8
- puts "Task t1: #{input}"
9
- input.merge({ "Foo1" => (input["foo"] == "John Wick" ? "ja" : "nein") })
10
- end
11
-
12
- choice :t2 do
13
- string_matches "$.Foo1", "ja" do
14
- task :t3 do
15
- action do |input|
16
- puts "Task t3: #{input}"
17
- input.merge({ "Foo3": "Bar3x" })
18
- end
7
+ action do |input|
8
+ puts "Task t1: #{input}"
9
+ input.merge({ "Foo1" => (input["foo"] == "John Wick" ? "ja" : "nein") })
10
+ end
19
11
 
20
- transition :t5
21
- end
12
+ choice :t2 do
13
+ string_matches "$.Foo1", "ja" do
14
+ callback :t3 do
15
+ action do |input, token|
16
+ puts "Callback t3: #{input}, #{token}"
17
+ # input.merge({ "Foo3": "Bar3x" })
22
18
  end
23
19
 
24
- default do
25
- task :t4 do
26
- action do |input|
27
- puts "Task t4: #{input}"
28
- input.merge({ "Foo4": "Bar4xy" })
29
- end
30
- end
20
+ transition :t5
21
+ end
22
+ end
23
+
24
+ default do
25
+ task :t4 do
26
+ action do |input|
27
+ puts "Task t4: #{input}"
28
+ input.merge({ "Foo4": "Bar4xy" })
31
29
  end
30
+ end
32
31
  end
32
+ end
33
33
  end
34
34
 
35
35
  task :t5 do
36
- action do |input|
37
- puts "Task t5: #{input}"
38
- input.merge({ "Foo5" => "Bar5" })
39
- end
36
+ action do |input|
37
+ puts "Task t5: #{input}"
38
+ input.merge({ "Foo5" => "Bar5" })
39
+ end
40
40
  end
@@ -3,31 +3,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "simplerubysteps/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "simplerubysteps"
7
- spec.version = Simplerubysteps::VERSION
8
- spec.authors = ["Christian Tschenett"]
9
- spec.email = ["simplerubysteps@furthermore.ch"]
6
+ spec.name = "simplerubysteps"
7
+ spec.version = Simplerubysteps::VERSION
8
+ spec.authors = ["Christian Tschenett"]
9
+ spec.email = ["simplerubysteps@furthermore.ch"]
10
10
 
11
- spec.summary = %q{simplerubysteps makes it easy to manage AWS Step Functions with ruby (eventually - this is an early alpha version and should not really be used by everyone).}
12
-
13
- spec.homepage = "https://github.com/chtz/simplerubysteps"
14
- spec.license = "MIT"
11
+ spec.summary = %q{simplerubysteps makes it easy to manage AWS Step Functions with ruby (eventually - this is an early alpha version and should not really be used by everyone).}
15
12
 
16
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
13
+ spec.homepage = "https://github.com/chtz/simplerubysteps"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
17
17
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
18
  end
19
- spec.files << "lib/deploy.sh"
20
- spec.files << "lib/destroy.sh"
21
- spec.files << "lib/workflow-run.sh"
22
- spec.files << "lib/statemachine.yaml"
23
- spec.files << "lib/function.rb"
24
19
 
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
- spec.executables << "simplerubysteps-deploy"
28
- spec.executables << "simplerubysteps-destroy"
29
- spec.executables << "simplerubysteps-workflow-run"
30
-
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+
31
23
  spec.require_paths = ["lib"]
32
24
 
33
25
  spec.add_development_dependency "bundler", "~> 1.17"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplerubysteps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Tschenett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-09 00:00:00.000000000 Z
11
+ date: 2023-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,6 +44,7 @@ email:
44
44
  executables:
45
45
  - simplerubysteps-deploy
46
46
  - simplerubysteps-destroy
47
+ - simplerubysteps-logs
47
48
  - simplerubysteps-workflow-run
48
49
  extensions: []
49
50
  extra_rdoc_files: []
@@ -57,14 +58,20 @@ files:
57
58
  - bin/setup
58
59
  - exe/simplerubysteps-deploy
59
60
  - exe/simplerubysteps-destroy
61
+ - exe/simplerubysteps-logs
60
62
  - exe/simplerubysteps-workflow-run
61
63
  - lib/deploy.sh
62
64
  - lib/destroy.sh
63
65
  - lib/function.rb
66
+ - lib/logs.sh
64
67
  - lib/simplerubysteps.rb
65
68
  - lib/simplerubysteps/version.rb
66
69
  - lib/statemachine.yaml
67
70
  - lib/workflow-run.sh
71
+ - samples/sample1/continue-callbackbranch.sh
72
+ - samples/sample1/send-task-success.sh
73
+ - samples/sample1/start-callbackbranch.sh
74
+ - samples/sample1/start-directbranch.sh
68
75
  - samples/sample1/workflow.rb
69
76
  - simplerubysteps.gemspec
70
77
  homepage: https://github.com/chtz/simplerubysteps