aws-rails-provisioner 0.0.0.rc1

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.
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+
3
+ module Aws::RailsProvisioner
4
+ module Parser
5
+
6
+ def parse(file_path)
7
+ config = YAML.load(File.read(file_path))
8
+ symbolize_keys(config)
9
+ end
10
+
11
+ private
12
+
13
+ def symbolize_keys(hash)
14
+ hash.inject({}) do |h, (k,v)|
15
+ v = symbolize_keys(v) if v.respond_to?(:keys)
16
+ h[k.to_sym] = v
17
+ h
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,448 @@
1
+ module Aws::RailsProvisioner
2
+ class Scaling
3
+
4
+ # Configuration for Fargate service scaling
5
+ # @param [Hash] options
6
+ #
7
+ # @option options [required, Integer] :max_capacity maximum capacity to scale to
8
+ #
9
+ # @option options [Integer] :min_capacity minimum capacity to scale to
10
+ # default to 1
11
+ #
12
+ # @option options [Hash] :on_cpu scale out or in to achieve a
13
+ # target CPU utilization
14
+ # @example: at `aws-rails-provisioner.yml`
15
+ # scaling:
16
+ # on_cpu:
17
+ # target_util_percent: 80
18
+ # scale_in_cool_down: 300
19
+ # @see {Aws::RailsProvisioner::Scaling::BaseScaling}
20
+ #
21
+ # @option options [Hash] :on_memory scale out or in to achieve a
22
+ # target memory utilization
23
+ # @example: at `aws-rails-provisioner.yml`
24
+ # scaling:
25
+ # on_memory:
26
+ # target_util_percent: 80
27
+ # scale_out_cool_down: 200
28
+ # @see {Aws::RailsProvisioner::Scaling::BaseScaling}
29
+ #
30
+ # @option options [Hash] :on_metric scale out or in based on a
31
+ # metric value
32
+ # @example: at `aws-rails-provisioner.yml`
33
+ # on_metric:
34
+ # adjustment_type: percentchangeincapacity
35
+ # min_adjustment_magnitude: 10
36
+ # cooldown: 300
37
+ # metric:
38
+ # name: foo
39
+ # @see {Aws::RailsProvisioner::Scaling::MetricScaling}
40
+ #
41
+ # @option options [Hash] :on_custom_metric scale out or in to track
42
+ # a custom metric
43
+ # @example: at `aws-rails-provisioner.yml`
44
+ # scaling:
45
+ # on_custom_metric:
46
+ # target_value: 100
47
+ # scale_in_cooldown: 300
48
+ # scale_out_cooldown: 500
49
+ # metric:
50
+ # name: foo
51
+ # @see {Aws::RailsProvisioner::Scaling::MetricScaling}
52
+ #
53
+ # @option options [Hash] :on_request scale out or in to achieve a
54
+ # target ALB request count per target
55
+ # @example: at `aws-rails-provisioner.yml`
56
+ # scaling:
57
+ # on_request:
58
+ # requests_per_target: 100000
59
+ # disable_scale_in: true
60
+ # @see {Aws::RailsProvisioner::Scaling::BaseScaling}
61
+ #
62
+ # @option options [Hash] :on_schedule scale out or in based on time
63
+ # @example: at `aws-rails-provisioner.yml`
64
+ # scaling:
65
+ # on_schedule:
66
+ # schedule: 'at(yyyy-mm-ddThh:mm:ss)'
67
+ # max_capacity: 10
68
+ # min_capacity: 5
69
+ # @see {Aws::RailsProvisioner::Scaling::ScheduleScaling}
70
+ #
71
+ def initialize(options = {})
72
+ @max_capacity = options.fetch(:max_capacity)
73
+ @min_capacity = options[:min_capacity]
74
+
75
+ @on_cpu = _scaling_props(:cpu, options[:cpu])
76
+ @on_memory = _scaling_props(:memory, options[:memory])
77
+ @on_metric = _scaling_props(:metric, options[:metric])
78
+ @on_request = _scaling_props(:request, options[:request])
79
+ @on_schedule = _scaling_props(:schedule, options[:schedule])
80
+ @to_track_custome_metric = _scaling_props(:custom, options[:custom_metric])
81
+ end
82
+
83
+ # @return [Integer]
84
+ attr_reader :max_capacity
85
+
86
+ # @return [Integer]
87
+ attr_reader :min_capacity
88
+
89
+ # @return [BaseScaling]
90
+ attr_reader :on_cpu
91
+
92
+ # @return [BaseScaling]
93
+ attr_reader :on_memory
94
+
95
+ # @return [BaseScaling]
96
+ attr_reader :on_request
97
+
98
+ # @return [MetricScaling]
99
+ attr_reader :on_metric
100
+
101
+ # @return [MetricScaling]
102
+ attr_reader :to_track_custom_metric
103
+
104
+ # @return [ScheduleScaling]
105
+ attr_reader :on_schedule
106
+
107
+ private
108
+
109
+ def _scaling_props(type, opts)
110
+ return if opts.nil?
111
+ case type
112
+ when :cpu, :memory, :request
113
+ BaseScaling.new(type, opts)
114
+ when :metric, :custom
115
+ MetricScaling.new(type, opts)
116
+ when :schedule
117
+ ScheduleScaling.new(type, opts)
118
+ else
119
+ raise Aws::RailsProvisioner::Errors::ValidationError.new(
120
+ 'Unsupported Scaling type.')
121
+ end
122
+ end
123
+
124
+ class MetricScaling
125
+
126
+ # Configuration for metric scaling policy
127
+ # @param [Hash] options
128
+ #
129
+ # @option options [String] :adjustment_type how the adjustment numbers
130
+ # inside 'intervals' are interpreted, available for `:on_metric`, supporting
131
+ # types: `change_in_capacity`, `percent_change_in_capacity` or `exact_capacity`
132
+ #
133
+ # @option options [Integer] :min_adjustment_magnitude available for
134
+ # `:on_metric`, when :adjustment_type set to `percentchangeincapacity`
135
+ # minimum absolute number to adjust capacity with as result of percentage scaling.
136
+ #
137
+ # @option options [Integer] :cooldown grace period after scaling activity
138
+ # in seconds, available for `:on_metric`
139
+ #
140
+ # @option options [Array<ScalingInterval>] :scaling_steps intervals for scaling, array of
141
+ # Aws::RailsProvisioner::Scaling::MetricScaling::ScalingInterval, available for
142
+ # `:on_metric`
143
+ # @example: at `aws-rails-provisioner.yml`
144
+ # on_metric:
145
+ # scaling_steps:
146
+ # -
147
+ # change: 10
148
+ # lower: 30
149
+ # upper: 60
150
+ # -
151
+ # change: 20
152
+ # lower: 0
153
+ # upper: 20
154
+ # @see {Aws::RailsProvisioner::Scaling::MetricScaling::ScalingInterval}
155
+ #
156
+ # @option options [Hash] :metric
157
+ # @example: at `aws-rails-provisioner.yml`
158
+ # on_metric:
159
+ # metric:
160
+ # name: foo
161
+ # namespace: bar
162
+ # dimensions:
163
+ # key:value
164
+ # on_custom_metric:
165
+ # metric:
166
+ # name: baz
167
+ # @see {Aws::RailsProvisioner::Scaling::MetricScaling::Metric}
168
+ #
169
+ # @option options [Integer] :target_value the target value to achieve for the metric
170
+ # available for :custom_metric
171
+ #
172
+ # @option options [Boolean] :disable_scale_in whether scale in by the
173
+ # target tracking policy is disabled, available for :custom_metric
174
+ #
175
+ # @option options [Integer] :scale_in_cooldown period (in seconds) after a scale in activity
176
+ # completes before another scale in activity can start, available for :custom_metric
177
+ #
178
+ # @option options [Integer] :scale_out_cooldown period (in seconds) after a scale out activity
179
+ # completes before another scale out activity can start, available for :custom_metric
180
+ #
181
+ def initialize(type, options)
182
+ @metric = Metric.new(type, options)
183
+ if type == :custom
184
+ @target_value = options[:target_value]
185
+ @disable_scale_in = !!options[:disable_scale_in]
186
+ @scale_in_cooldown = options[:scale_in_cooldown]
187
+ @scale_out_cooldown = options[:scale_out_cooldown]
188
+ else # :metric
189
+ @scaling_steps = _scaling_steps(options[:scaling_steps] || [])
190
+ @cooldown_sec = options[:cooldown]
191
+ @adjustment_type = Aws::RailsProvisioner::Utils.adjustment_type(
192
+ options[:adjustment_type]) if options[:adjustment_type]
193
+ if @adjustment_type == 'PercentChangeInCapacity'
194
+ @min_adjustment_magnitude = options[:min_adjustment_magnitude]
195
+ end
196
+ end
197
+ end
198
+
199
+ # @return [Metric]
200
+ attr_reader :metric
201
+
202
+ # @return [Integer]
203
+ attr_reader :target_value
204
+
205
+ # @return [Boolean]
206
+ attr_reader :disable_scale_in
207
+
208
+ # @return [Integer]
209
+ attr_reader :scale_in_cooldown
210
+
211
+ # @return [Integer]
212
+ attr_reader :scale_out_cooldown
213
+
214
+ # @return [Array<ScalingInterval>]
215
+ attr_reader :scaling_steps
216
+
217
+ # @return [String]
218
+ attr_reader :adjustment_type
219
+
220
+ # @return [Integer]
221
+ attr_reader :cooldown_sec
222
+
223
+ # @return [Integer]
224
+ attr_reader :min_adjustment_magnitude
225
+
226
+ class ScalingInterval
227
+
228
+ # Configuration for each scaling interval in
229
+ # scaling steps
230
+ # @param [Hash] options
231
+ #
232
+ # @option options [Integer] :change the capacity adjustment
233
+ # to apply in this interval, interpreted differently based on :adjustment_type
234
+ # * `changeincapacity` - add the adjustment to the current capacity. The number
235
+ # can be positive or negative
236
+ # * `percentchangeincapacity` - add or remove the given percentage of the
237
+ # current capacity to itself. The number can be in the range [-100..100]
238
+ # * `exactcapacity` - set the capacity to this number. The number must be positive
239
+ #
240
+ # @option options [Integer] :lower lower bound of the interval, scaling
241
+ # adjustment will be applied if the metric is higher than this value
242
+ #
243
+ # @option options [Integer] :upper upper bound of the interval, scaling
244
+ # adjustment will be applied if the metric is lower than this value
245
+ #
246
+ def initialize(options = {})
247
+ @change = options[:change]
248
+ @lower = options[:lower]
249
+ @upper = options[:upper]
250
+ end
251
+
252
+ # @return [Integer]
253
+ attr_reader :change
254
+
255
+ # @return [Integer]
256
+ attr_reader :lower
257
+
258
+ # @return [Integer]
259
+ attr_reader :upper
260
+
261
+ end
262
+
263
+ class Metric
264
+
265
+ # Metric to scale on
266
+ # https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html
267
+ # @param [Hash] options
268
+ #
269
+ # @option options [String] :name
270
+ #
271
+ # @option options [String] :namespace
272
+ #
273
+ # @option options [Integer] :period
274
+ #
275
+ # @option options [String] :statistic
276
+ #
277
+ # @option options [String] :color
278
+ #
279
+ # @option options [String] :label
280
+ #
281
+ # @option options [Hash] :dimensions
282
+ #
283
+ # @option options [String] :unit available unit:
284
+ # `Seconds`, `Microseconds`, `Milliseconds`
285
+ # `Bytes`, `Kilobytes`, `Megabytes`, `Gigabytes`, `Terabytes`
286
+ # `Bits`, `Kilobits`, `Megabits`, `Gigabits`, `Terabits`,
287
+ # `Percent`, `Count`, `None`,
288
+ # `BytesPerSecond`, `KilobytesPerSecond`, `MegabytesPerSecond`,
289
+ # `GigabytesPerSecond`, `TerabytesPerSecond`, `BitsPerSecond`,
290
+ # `KilobitsPerSecond`, `MegabitsPerSecond`, `GigabitsPerSecond`,
291
+ # `TerabitsPerSecond`, `CountPerSecond`
292
+ #
293
+ def initialize(type, options = {})
294
+ @name = options.fetch(:name)
295
+ @namespace = options.fetch(:namespace)
296
+ @color = options[:color]
297
+ @dimensions = Aws::RailsProvisioner::Utils.to_pairs(
298
+ options[:dimensions]) if options[:dimensions]
299
+ @label = options[:label]
300
+ @period_sec = options[:period]
301
+ @statistic = options[:statistics]
302
+ @unit = options[:unit]
303
+ end
304
+
305
+ # @return [String]
306
+ attr_reader :name
307
+
308
+ # @return [String]
309
+ attr_reader :namespace
310
+
311
+ # @return [String]
312
+ attr_reader :color
313
+
314
+ # @return [String]
315
+ attr_reader :dimensions
316
+
317
+ # @return [String]
318
+ attr_reader :label
319
+
320
+ # @return [Integer]
321
+ attr_reader :period_sec
322
+
323
+ # @return [String]
324
+ attr_reader :statistic
325
+
326
+ # @return [String]
327
+ attr_reader :unit
328
+
329
+ end
330
+
331
+ def scaling_steps?
332
+ @scaling_steps && !@scaling_steps.empty?
333
+ end
334
+
335
+ private
336
+
337
+ def _scaling_steps(steps)
338
+ steps.map {|step| ScalingInterval.new(step)}
339
+ end
340
+
341
+ end
342
+
343
+ class BaseScaling
344
+
345
+ # Configuration for scaling policy
346
+ # @param [Hash] options
347
+ #
348
+ # @option options [Boolean] :disable_scale_in whether scale in
349
+ # by the target tracking policy is disabled, default as `false`
350
+ #
351
+ # @option options [Integer] :scale_in_cooldown period in seconds
352
+ # after a scale in activity completes before another scale in activity
353
+ # can start
354
+ #
355
+ # @option options [Integer] :scale_out_cooldown period in seconds
356
+ # after a scale in activity completes before another scale in activity
357
+ # can start
358
+ #
359
+ # @option options [Integer] :target_util_percent available for
360
+ # * :on_cpu , target average CPU utilization across the task
361
+ # * :on_memory , target average memory utilization across the task
362
+ #
363
+ # @option options [Integer] :requests_per_target available for
364
+ # :on_request, ALB requests per target
365
+ #
366
+ def initialize(type, options = {})
367
+ @disable_scale_in = !!options[:disable_scale_in]
368
+ @scale_in_cooldown = options[:scale_in_cooldown]
369
+ @scale_out_cooldown = options[:scale_out_cooldown]
370
+ var_name = _type_2_var(type)
371
+ instance_variable_set("@#{var_name}", options[var_name.to_sym])
372
+ end
373
+
374
+ # @return [Integer]
375
+ attr_reader :target_util_percent
376
+
377
+ # @return [Integer]
378
+ attr_reader :requests_per_target
379
+
380
+ # @return [Boolean]
381
+ attr_reader :disable_scale_in
382
+
383
+ # @return [Integer]
384
+ attr_reader :scale_in_cooldown
385
+
386
+ # @return [Integer]
387
+ attr_reader :scale_out_cooldown
388
+
389
+ private
390
+
391
+ def _type_2_var(type)
392
+ case type
393
+ when :cpu, :memory then 'target_util_percent'
394
+ when :request then 'requests_per_target'
395
+ end
396
+ end
397
+
398
+ end
399
+
400
+ class ScheduleScaling
401
+
402
+ # Configurations for scaling policy based on time
403
+ # @param [Hash] options
404
+ #
405
+ # @option options [required, String] :schedule when to perform this action
406
+ # support formats:
407
+ # * 'at(yyyy-mm-ddThh:mm:ss)'
408
+ # * 'rate(value unit)'
409
+ # * 'cron(fields)'
410
+ #
411
+ # @option options [Integer] :max_capacity the new maximum capacity
412
+ #
413
+ # @option options [Integer] :min_capacity the new minimum capacity
414
+ #
415
+ # @option options [Integer] :start_time when this scheduled action becomes active
416
+ # milliseconds since Epoch time
417
+ #
418
+ # @option options [Integer] :end_time when this scheduled action expires
419
+ # milliseconds since Epoch time
420
+ #
421
+ def initialize(options = {})
422
+ @schedule = options.fetch(:schedule)
423
+ @max_capacity = options[:max_capacity]
424
+ @min_capacity = options[:min_capacity]
425
+ @start_time = options[:start_time]
426
+ @end_time = options[:end_time]
427
+ end
428
+
429
+ # @return [String]
430
+ attr_reader :schedule
431
+
432
+ # @return [Integer]
433
+ attr_reader :max_capacity
434
+
435
+ # @return [Integer]
436
+ attr_reader :min_capacity
437
+
438
+ # @return [Integer]
439
+ attr_reader :start_time
440
+
441
+ # @return [Integer]
442
+ attr_reader :end_time
443
+
444
+ end
445
+
446
+ end
447
+
448
+ end