simplerubysteps 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32057d9ea30569964077a231e6e5ed88aff396dabc03151c1ef77bbac237a7e2
4
- data.tar.gz: 81d13b619c57a3b793ed7039a06e0ec2a730aa8844d70c8603f35277799090e0
3
+ metadata.gz: cbf93121e4c57f68c9353ad4eda79f37ad26b5208828ac25ba4d2ea59ff30cf3
4
+ data.tar.gz: bfc81ca54385937d13a69cd6a6e38aa303dd146fc28c98791357b62e80577def
5
5
  SHA512:
6
- metadata.gz: 68f92e9f3b4e7d0e5249e7df03f97a9e133ab51274117b2538b8672bf8e6221c6c8d2130679395ff0e1e57ee18f6e0bf4c321fe8ce034e0fe14c53ca37150e65
7
- data.tar.gz: 1185695c5f0bc3c3d093cb7984c2947ec1ef2f4be737586f73ae058d2266ba90121d9d48b37608993877784eee729010867c15754309584beafe88c01d6b5836
6
+ metadata.gz: f5bb0d06cefb48b94ea846f24838f8cc90a915abe1ca7f8806b2f0d8f4ec4a60edc5a25c33f65d90eb81603dd70a3390f38d27e8a036ccbd70bf0a94dcc47006
7
+ data.tar.gz: cc120cf28d4ba8c4f8e8bb85c0a26245c9f1027e113d2a42aa232dafa0ffc0f903ce2ae8f17f3d22f7fd0aa7aa79581bd30dac596dfb7c060cbbec0ecf6d2275
data/lib/function.rb CHANGED
@@ -1,5 +1,25 @@
1
1
  require "simplerubysteps"
2
2
 
3
+ if ENV["QUEUE"]
4
+ require "aws-sdk-sqs"
5
+ require "json"
6
+
7
+ class QueueClient
8
+ def initialize(sqs_client, queue)
9
+ @sqs_client = sqs_client
10
+ @queue = queue
11
+ end
12
+
13
+ def send(data)
14
+ @sqs_client.send_message(queue_url: @queue, message_body: data.to_json)
15
+ end
16
+ end
17
+
18
+ $queue_client = QueueClient.new(Aws::SQS::Client.new, ENV["QUEUE"])
19
+ else
20
+ $queue_client = nil
21
+ end
22
+
3
23
  require "./workflow.rb"
4
24
 
5
25
  include Simplerubysteps
@@ -10,7 +30,11 @@ def handler(event:, context:)
10
30
  puts context.inspect if ENV["DEBUG"]
11
31
 
12
32
  if event["Token"]
13
- $sm.states[ENV["task"].to_sym].perform_action event["Input"], event["Token"]
33
+ unless $queue_client
34
+ $sm.states[ENV["task"].to_sym].perform_action event["Input"], event["Token"]
35
+ else
36
+ $sm.states[ENV["task"].to_sym].perform_queue_action event["Input"], event["Token"], $queue_client
37
+ end
14
38
  else
15
39
  $sm.states[ENV["task"].to_sym].perform_action event["Input"]
16
40
  end
@@ -38,8 +38,11 @@ Resources:
38
38
  Runtime: ruby2.7
39
39
  Environment:
40
40
  Variables:
41
- <% resource.each do |k, v| %>
42
- <%= k %>: <%= v %>
41
+ <% if resource["queue"] %>
42
+ QUEUE: !Ref MyQueue<%= index %>
43
+ <% end %>
44
+ <% resource["env"].each do |k, v| %>
45
+ <%= k %>: <%= v.inspect %>
43
46
  <% end %>
44
47
  LogGroup<%= index %>:
45
48
  Type: AWS::Logs::LogGroup
@@ -68,6 +71,32 @@ Resources:
68
71
  - logs:CreateLogStream
69
72
  - logs:PutLogEvents
70
73
  Resource: arn:aws:logs:*:*:*
74
+ <% if resource["iam_permissions"] %>
75
+ MyCustomPolicy<%= index %>:
76
+ Type: AWS::IAM::Policy
77
+ Properties:
78
+ PolicyName: custom
79
+ Roles:
80
+ - !Ref MyLambdaRole<%= index %>
81
+ PolicyDocument: <%= resource["iam_permissions"].inspect %>
82
+ <% end %>
83
+ <% if resource["queue"] %>
84
+ MyQueue<%= index %>:
85
+ Type: AWS::SQS::Queue
86
+ MyQueuePolicy<%= index %>:
87
+ Type: AWS::IAM::Policy
88
+ Properties:
89
+ PolicyName: queue
90
+ Roles:
91
+ - !Ref MyLambdaRole<%= index %>
92
+ PolicyDocument:
93
+ Version: '2012-10-17'
94
+ Statement:
95
+ - Effect: Allow
96
+ Action:
97
+ - 'sqs:SendMessage'
98
+ Resource: !GetAtt MyQueue<%= index %>.Arn
99
+ <% end %>
71
100
  <% end %>
72
101
  <% end %>
73
102
 
@@ -135,6 +164,16 @@ Outputs:
135
164
  StepFunctionsRoleARN:
136
165
  Value: !GetAtt StepFunctionsRole.Arn
137
166
  <% end %>
167
+
168
+ <% if resources[:functions] %>
169
+ <% resources[:functions].each_with_index do |resource, index| %>
170
+ <% if resource["queue"] %>
171
+ <%= resource["env"]["task"] %>Queue:
172
+ Value: !Ref MyQueue<%= index %>
173
+ <% end %>
174
+ <% end %>
175
+ <% end %>
176
+
138
177
  YAML
139
178
 
140
179
  def self.cloudformation_yaml(resources)
@@ -28,6 +28,23 @@ module Simplerubysteps
28
28
  $tasks.pop
29
29
  end
30
30
 
31
+ def sqs_callback(name)
32
+ t = $sm.add Callback.new(name)
33
+ t.queue = true
34
+
35
+ $tasks.last.next = t if $tasks.last
36
+
37
+ $tasks.push t
38
+ action do |input, token, queue_client|
39
+ queue_client.send({
40
+ input: input,
41
+ token: token,
42
+ })
43
+ end
44
+ yield if block_given?
45
+ $tasks.pop
46
+ end
47
+
31
48
  def action(&action_block)
32
49
  $tasks.last.action &action_block
33
50
  end
@@ -55,6 +72,10 @@ module Simplerubysteps
55
72
  choice.default = state
56
73
  end
57
74
 
75
+ def iam_permissions(permissions)
76
+ $tasks.last.iam_permissions = permissions
77
+ end
78
+
58
79
  def choice(name)
59
80
  t = $sm.add Choice.new(name)
60
81
 
@@ -37,7 +37,11 @@ module Simplerubysteps
37
37
  states.each do |name, state|
38
38
  if state.is_a? Task or state.is_a? Callback
39
39
  data.push({
40
- task: name,
40
+ env: {
41
+ task: name,
42
+ },
43
+ iam_permissions: state.iam_permissions,
44
+ queue: ((state.is_a? Callback) ? state.queue : nil),
41
45
  })
42
46
  end
43
47
  end
@@ -77,6 +81,8 @@ module Simplerubysteps
77
81
  end
78
82
 
79
83
  class Task < State
84
+ attr_accessor :iam_permissions
85
+
80
86
  def initialize(name)
81
87
  super
82
88
  @dict[:Type] = "Task"
@@ -115,6 +121,9 @@ module Simplerubysteps
115
121
  end
116
122
 
117
123
  class Callback < State
124
+ attr_accessor :iam_permissions
125
+ attr_accessor :queue
126
+
118
127
  def initialize(name)
119
128
  super
120
129
  @dict[:Type] = "Task"
@@ -136,6 +145,10 @@ module Simplerubysteps
136
145
  def perform_action(input, token)
137
146
  @action_block.call(input, token) if @action_block
138
147
  end
148
+
149
+ def perform_queue_action(input, token, queue_client)
150
+ @action_block.call(input, token, queue_client) if @action_block
151
+ end
139
152
  end
140
153
 
141
154
  class ChoiceItem
@@ -402,6 +402,25 @@ module Simplerubysteps
402
402
  response
403
403
  end
404
404
 
405
+ def stack_output(version, optional_output)
406
+ stack = nil
407
+ if version
408
+ stack = versioned_stack_name_from_current_dir(version)
409
+ else
410
+ stack = most_recent_stack_with_prefix unversioned_stack_name_from_current_dir
411
+ end
412
+ raise "State Machine is not deployed" unless stack
413
+
414
+ current_stack_outputs = stack_outputs(stack)
415
+ raise "State Machine is not deployed" unless current_stack_outputs
416
+
417
+ if optional_output
418
+ puts current_stack_outputs[optional_output]
419
+ else
420
+ puts current_stack_outputs.to_json
421
+ end
422
+ end
423
+
405
424
  def start(wait, input, version)
406
425
  stack = nil
407
426
  if version
@@ -539,6 +558,22 @@ module Simplerubysteps
539
558
  options[:token] = value
540
559
  end
541
560
 
561
+ opts.on("-h", "--help", "Display this help message") do
562
+ puts opts
563
+ exit
564
+ end
565
+ end,
566
+ "stack" => OptionParser.new do |opts|
567
+ opts.banner = "Usage: #{$0} stack [options]"
568
+
569
+ opts.on("--output VALUE", "Stack output") do |value|
570
+ options[:output] = value
571
+ end
572
+
573
+ opts.on("--version VALUE", "fix version (\"latest\" per default)") do |value|
574
+ options[:version] = value
575
+ end
576
+
542
577
  opts.on("-h", "--help", "Display this help message") do
543
578
  puts opts
544
579
  exit
@@ -587,6 +622,8 @@ module Simplerubysteps
587
622
  log options[:extract_pattern], options[:version]
588
623
  elsif options[:command] == "task-success"
589
624
  send_task_success options[:token], options[:input]
625
+ elsif options[:command] == "stack"
626
+ stack_output options[:version], options[:output]
590
627
  elsif options[:command] == "destroy"
591
628
  if options[:destroy_all]
592
629
  destroy(nil)
@@ -1,3 +1,3 @@
1
1
  module Simplerubysteps
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplerubysteps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Tschenett