Module: Simplerubysteps

Defined in:
lib/simplerubysteps/dsl.rb,
lib/simplerubysteps/tool.rb,
lib/simplerubysteps/model.rb,
lib/simplerubysteps/version.rb,
lib/simplerubysteps/cloudformation.rb

Defined Under Namespace

Classes: Branch, Callback, Choice, ChoiceItem, Parallel, State, StateMachine, Task, Tool, Wait

Constant Summary collapse

VERSION =
"0.0.13"
CLOUDFORMATION_ERB_TEMPLATE =
"---\nAWSTemplateFormatVersion: \"2010-09-09\"\n\n<% if resources[:functions] %>\nParameters:  \n  LambdaS3:\n    Description: LambdaS3.\n    Type: String\n<% end %>\n\n<% if resources[:state_machine] %>\n  StepFunctionsS3:\n    Description: StepFunctionsS3.\n    Type: String\n  StateMachineType:\n    Description: StateMachineType.\n    Type: String\n<% end %>\n\nResources:\n  DeployBucket:\n    Type: AWS::S3::Bucket\n\n<% if resources[:functions] %>\n<% resources[:functions].each_with_index do |resource, index| %>\n  LambdaFunction<%= index %>:\n    Type: \"AWS::Lambda::Function\"\n    Properties:\n      Code:\n        S3Bucket: !Ref DeployBucket\n        S3Key: !Ref LambdaS3\n      Handler: function.handler      \n      Role: !GetAtt MyLambdaRole<%= index %>.Arn\n      Runtime: ruby2.7\n      Environment:\n        Variables:\n<% if resource[\"queue\"] %>\n          QUEUE: !Ref MyQueue<%= index %>        \n<% end %>          \n<% resource[\"env\"].each do |k, v| %>        \n          <%= k %>: <%= v.inspect %>\n<% end %>\n  LogGroup<%= index %>:\n    Type: AWS::Logs::LogGroup\n    Properties:\n      LogGroupName: !Sub \"/aws/lambda/${LambdaFunction<%= index %>}\"\n      RetentionInDays: 7  \n  MyLambdaRole<%= index %>:\n    Type: AWS::IAM::Role\n    Properties:\n      AssumeRolePolicyDocument:\n        Version: \"2012-10-17\"\n        Statement:\n          - Effect: Allow\n            Principal:\n              Service: lambda.amazonaws.com\n            Action:\n              - sts:AssumeRole\n      Policies:\n        - PolicyName: lambda-policy\n          PolicyDocument:\n            Version: \"2012-10-17\"\n            Statement:\n              - Effect: Allow\n                Action:\n                  - logs:CreateLogGroup\n                  - logs:CreateLogStream\n                  - logs:PutLogEvents\n                Resource: arn:aws:logs:*:*:*\n<% if resource[\"iam_permissions\"] %>\n  MyCustomPolicy<%= index %>:\n    Type: AWS::IAM::Policy\n    Properties:\n      PolicyName: custom\n      Roles:\n        - !Ref MyLambdaRole<%= index %>\n      PolicyDocument: <%= resource[\"iam_permissions\"].inspect %>\n<% end %>\n<% if resource[\"queue\"] %>\n  MyQueue<%= index %>:\n    Type: AWS::SQS::Queue\n  MyQueuePolicy<%= index %>:\n    Type: AWS::IAM::Policy\n    Properties:\n      PolicyName: queue\n      Roles:\n        - !Ref MyLambdaRole<%= index %>\n      PolicyDocument: \n        Version: '2012-10-17'\n        Statement:\n          - Effect: Allow\n            Action:\n              - 'sqs:SendMessage'\n            Resource: !GetAtt MyQueue<%= index %>.Arn\n<% end %>\n<% end %>\n<% end %>\n  \n<% if resources[:state_machine] %>\n  StepFunctionsStateMachine:\n    Type: \"AWS::StepFunctions::StateMachine\"\n    Properties:\n      DefinitionS3Location:\n        Bucket: !Ref DeployBucket\n        Key: !Ref StepFunctionsS3\n      RoleArn: !GetAtt StepFunctionsRole.Arn\n      StateMachineType: !Ref StateMachineType\n  StepFunctionsRole:\n    Type: AWS::IAM::Role\n    Properties:\n      AssumeRolePolicyDocument:\n        Version: \"2012-10-17\"\n        Statement:\n          - Effect: Allow\n            Principal:\n              Service: states.amazonaws.com\n            Action: sts:AssumeRole\n      Policies:\n        - PolicyName: StepFunctionsPolicy\n          PolicyDocument:\n            Version: \"2012-10-17\"\n            Statement:\n              - Effect: Allow\n                Action:\n                  - logs:CreateLogGroup\n                  - logs:CreateLogStream\n                  - logs:PutLogEvents\n                Resource: arn:aws:logs:*:*:*\n              - Effect: Allow\n                Action:\n                  - lambda:InvokeFunction\n                Resource: \n<% resources[:functions].each_with_index do |resource, index| %>                \n                  - !GetAtt LambdaFunction<%= index %>.Arn\n<% end %>                  \n<% end %>\n\nOutputs:\n  DeployBucket:\n    Value: !Ref DeployBucket\n\n<% if resources[:functions] %>    \n  LambdaCount:\n    Value: <%= resources[:functions].length %>\n<% resources[:functions].each_with_index do |resource, index| %>    \n  LambdaRoleARN<%= index %>:\n    Value: !GetAtt MyLambdaRole<%= index %>.Arn\n  LambdaFunctionARN<%= index %>:\n    Value: !GetAtt LambdaFunction<%= index %>.Arn\n  LambdaFunctionName<%= index %>:\n    Value: !Ref LambdaFunction<%= index %>\n<% end %>    \n<% end %>  \n\n<% if resources[:state_machine] %>\n  StepFunctionsStateMachineARN:\n    Value: !GetAtt StepFunctionsStateMachine.Arn\n  StateMachineType:\n    Value: !Ref StateMachineType\n  StepFunctionsRoleARN:\n    Value: !GetAtt StepFunctionsRole.Arn\n<% end %>    \n\n<% if resources[:functions] %>\n<% resources[:functions].each_with_index do |resource, index| %>\n<% if resource[\"queue\"] %>\n  <%= resource[\"env\"][\"task\"] %>Queue:\n    Value: !Ref MyQueue<%= index %>\n<% end %>  \n<% end %>    \n<% end %>    \n\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloudformation_yaml(resources) ⇒ Object



179
180
181
182
# File 'lib/simplerubysteps/cloudformation.rb', line 179

def self.cloudformation_yaml(resources)
  template = ERB.new(CLOUDFORMATION_ERB_TEMPLATE)
  template.result(binding)
end

Instance Method Details

#action(&action_block) ⇒ Object



85
86
87
# File 'lib/simplerubysteps/dsl.rb', line 85

def action(&action_block)
  $tasks.last.action &action_block
end

#branchObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simplerubysteps/dsl.rb', line 21

def branch
  sm_backup = $sm
  tasks_backup = $tasks

  $sm = $tasks.last.new_branch
  $tasks = []

  yield if block_given?

  $sm = sm_backup
  $tasks = tasks_backup
end

#callback(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/simplerubysteps/dsl.rb', line 58

def callback(name)
  t = $sm.add Callback.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#choice(name) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/simplerubysteps/dsl.rb', line 128

def choice(name)
  t = $sm.add Choice.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#defaultObject



151
152
153
154
155
# File 'lib/simplerubysteps/dsl.rb', line 151

def default
  $tasks.push $tasks.last
  yield if block_given?
  $tasks.pop
end

#default_transition_to(state) ⇒ Object



118
119
120
121
122
# File 'lib/simplerubysteps/dsl.rb', line 118

def default_transition_to(state)
  choice = $tasks.last.implicit_choice

  choice.default = state
end

#error_catch(state, error = "States.ALL") ⇒ Object



101
102
103
# File 'lib/simplerubysteps/dsl.rb', line 101

def error_catch(state, error = "States.ALL")
  $tasks.last.error_catch state
end

#error_retry(interval, max, backoff, error = "States.ALL") ⇒ Object



93
94
95
# File 'lib/simplerubysteps/dsl.rb', line 93

def error_retry(interval, max, backoff, error = "States.ALL")
  $tasks.last.error_retry(interval, max, backoff)
end

#iam_permissions(permissions) ⇒ Object



124
125
126
# File 'lib/simplerubysteps/dsl.rb', line 124

def iam_permissions(permissions)
  $tasks.last.iam_permissions = permissions
end

#kind(k) ⇒ Object



7
8
9
# File 'lib/simplerubysteps/dsl.rb', line 7

def kind(k)
  $sm.kind = k
end

#parallel(name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/simplerubysteps/dsl.rb', line 11

def parallel(name)
  t = $sm.add Parallel.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#pop_function_arnObject



4
5
6
7
8
9
# File 'lib/simplerubysteps/model.rb', line 4

def pop_function_arn
  return "unknown" unless $LAMBDA_FUNCTION_ARNS
  arn = $LAMBDA_FUNCTION_ARNS.first
  $LAMBDA_FUNCTION_ARNS.delete arn
  arn
end

#pop_function_nameObject



11
12
13
14
# File 'lib/simplerubysteps/model.rb', line 11

def pop_function_name
  return "unknown" unless pop_function_arn =~ /.+\:function\:(.+)/
  $1
end

#seconds(s) ⇒ Object



44
45
46
# File 'lib/simplerubysteps/dsl.rb', line 44

def seconds(s)
  $tasks.last.seconds = s
end

#sqs_callback(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simplerubysteps/dsl.rb', line 68

def sqs_callback(name)
  t = $sm.add Callback.new(name)
  t.queue = true

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  action do |input, token, queue_client|
    queue_client.send({
      input: input,
      token: token,
    })
  end
  yield if block_given?
  $tasks.pop
end

#string_matches(var, match) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/simplerubysteps/dsl.rb', line 138

def string_matches(var, match)
  c = ChoiceItem.new({
    :Variable => var,
    :StringMatches => match,
  })

  $tasks.last.add c

  $tasks.push c
  yield if block_given?
  $tasks.pop
end

#task(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/simplerubysteps/dsl.rb', line 48

def task(name)
  t = $sm.add Task.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end

#task_timeout(secs, state = nil) ⇒ Object



97
98
99
# File 'lib/simplerubysteps/dsl.rb', line 97

def task_timeout(secs, state = nil)
  $tasks.last.task_timeout secs, state
end

#transition(state) ⇒ Object



89
90
91
# File 'lib/simplerubysteps/dsl.rb', line 89

def transition(state)
  $tasks.last.next = state
end

#transition_to(state, &condition_block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/simplerubysteps/dsl.rb', line 105

def transition_to(state, &condition_block)
  choice = $tasks.last.implicit_choice

  c = ChoiceItem.new({
    :Variable => "$.#{choice.name}_#{state}",
    :StringMatches => "yes",
  })
  c.next = state
  c.implicit_condition_block = condition_block

  choice.add c
end

#wait(name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/simplerubysteps/dsl.rb', line 34

def wait(name)
  t = $sm.add Wait.new(name)

  $tasks.last.next = t if $tasks.last

  $tasks.push t
  yield if block_given?
  $tasks.pop
end