cfn_manage 0.7.0 → 0.8.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +20 -0
  5. data/Dockerfile +7 -0
  6. data/Gemfile +9 -0
  7. data/LICENSE +21 -0
  8. data/README.md +311 -0
  9. data/Rakefile +9 -0
  10. data/cfn_manage.gemspec +58 -0
  11. data/{bin → exe}/cfn_manage +78 -7
  12. data/{bin → exe}/usage.txt +31 -0
  13. data/lib/cfn_manage/cf_start_stop_environment.rb +84 -56
  14. data/lib/cfn_manage/globals.rb +90 -0
  15. data/lib/cfn_manage/handlers/alarm.rb +45 -0
  16. data/lib/cfn_manage/handlers/asg.rb +311 -0
  17. data/lib/cfn_manage/handlers/aurora_cluster.rb +97 -0
  18. data/lib/cfn_manage/handlers/documentdb.rb +89 -0
  19. data/lib/cfn_manage/handlers/ec2.rb +42 -0
  20. data/lib/cfn_manage/handlers/ecs_cluster.rb +219 -0
  21. data/lib/cfn_manage/handlers/rds.rb +142 -0
  22. data/lib/cfn_manage/handlers/spot_fleet.rb +56 -0
  23. data/lib/cfn_manage/handlers/transfer.rb +96 -0
  24. data/lib/cfn_manage/start_stop_handler_factory.rb +19 -19
  25. data/lib/cfn_manage/tag_finder.rb +77 -0
  26. data/lib/cfn_manage/version.rb +1 -1
  27. metadata +56 -21
  28. data/lib/cfn_manage/alarm_start_stop_handler.rb +0 -44
  29. data/lib/cfn_manage/asg_start_stop_handler.rb +0 -181
  30. data/lib/cfn_manage/aurora_cluster_start_stop_handler.rb +0 -97
  31. data/lib/cfn_manage/documentdb_cluster_start_stop_handler.rb +0 -89
  32. data/lib/cfn_manage/ec2_start_stop_handler.rb +0 -43
  33. data/lib/cfn_manage/ecs_cluster_start_stop_handler.rb +0 -80
  34. data/lib/cfn_manage/rds_start_stop_handler.rb +0 -134
  35. data/lib/cfn_manage/spot_fleet_start_stop_handler.rb +0 -57
  36. data/lib/cfn_manage/transfer_start_stop_handler.rb +0 -97
@@ -0,0 +1,56 @@
1
+ require 'cfn_manage/aws_credentials'
2
+
3
+ module CfnManage
4
+ module StartStopHandler
5
+ class SpotFleet
6
+
7
+ def initialize(fleet_id, options = {})
8
+ @fleet_id = fleet_id
9
+ credentials = CfnManage::AWSCredentials.get_session_credentials("startstopfleet_#{fleet_id}")
10
+ @ec2_client = Aws::EC2::Client.new(retry_limit: 20)
11
+ if credentials != nil
12
+ @ec2_client = Aws::EC2::Client.new(credentials: credentials, retry_limit: 20)
13
+ end
14
+
15
+ @fleet = @ec2_client.describe_spot_fleet_requests({spot_fleet_request_ids:[fleet_id]})
16
+ @fleet = @fleet.spot_fleet_request_configs[0].spot_fleet_request_config
17
+ end
18
+
19
+ def start(configuration)
20
+
21
+ $log.info("Setting fleet #{@fleet_id} capacity to #{configuration['target_capacity']}")
22
+ @ec2_client.modify_spot_fleet_request({
23
+ spot_fleet_request_id: @fleet_id,
24
+ target_capacity: configuration['target_capacity'],
25
+ })
26
+
27
+ return configuration
28
+ end
29
+
30
+ def stop
31
+
32
+ if @fleet.target_capacity == 0
33
+ $log.info("Spot fleet #{@fleet_id} already stopped")
34
+ return nil
35
+ end
36
+
37
+ configuration = {
38
+ target_capacity: @fleet.target_capacity
39
+ }
40
+
41
+ $log.info("Setting fleet #{@fleet_id} capacity to 0")
42
+ @ec2_client.modify_spot_fleet_request({
43
+ spot_fleet_request_id: @fleet_id,
44
+ target_capacity: 0,
45
+ })
46
+
47
+ return configuration
48
+ end
49
+
50
+ def wait(wait_states=[])
51
+ $log.debug("Not waiting for spot fleet #{@fleet_id}")
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,96 @@
1
+ require 'cfn_manage/aws_credentials'
2
+ require 'aws-sdk-transfer'
3
+
4
+ module CfnManage
5
+ module StartStopHandler
6
+ class Transfer
7
+
8
+ def initialize(server_id, options = {})
9
+ sftpId = server_id.split("/")
10
+ @server_id = sftpId.last
11
+ credentials = CfnManage::AWSCredentials.get_session_credentials("startstoptransfer_#{@server_id}")
12
+ @client = Aws::Transfer::Client.new(retry_limit: 20)
13
+ if credentials != nil
14
+ @client = Aws::Transfer::Client.new(credentials: credentials, retry_limit: 20)
15
+ end
16
+ end
17
+
18
+ def start(configuration)
19
+
20
+ state = get_state()
21
+
22
+ if state != "OFFLINE"
23
+ $log.warn("SFTP Server #{@server_id} is in a state of #{state} and can not be started.")
24
+ return
25
+ end
26
+
27
+ $log.info("Starting SFTP Server #{@server_id}")
28
+ @client.start_server({
29
+ server_id: @server_id,
30
+ })
31
+ unless CfnManage.skip_wait?
32
+ $log.info("Waiting for SFTP to start #{@cluster_id}")
33
+ wait('ONLINE')
34
+ end
35
+
36
+ return configuration
37
+ end
38
+
39
+ def stop()
40
+
41
+ state = get_state()
42
+
43
+ if state != "ONLINE"
44
+ $log.warn("SFTP Server #{@server_id} is in a state of #{state} and can not be stopped.")
45
+ return {}
46
+ end
47
+
48
+ @client.stop_server({
49
+ server_id: @server_id,
50
+ })
51
+ unless CfnManage.skip_wait?
52
+ $log.info("Waiting for SFTP to stop #{@cluster_id}")
53
+ wait('OFFLINE')
54
+ end
55
+ return {}
56
+ end
57
+
58
+ def get_state()
59
+ resp = @client.describe_server({
60
+ server_id: @server_id,
61
+ })
62
+ return resp.server.state
63
+ end
64
+
65
+ def wait(completed_state)
66
+
67
+ state_count = 0
68
+ steady_count = 2
69
+ attempts = 0
70
+ until attempts == (max_attempts = 60*6) do
71
+ state = get_state()
72
+ $log.info("SFTP Server #{@server_id} state: #{state}, waiting for #{completed_state}")
73
+
74
+ if state == "#{completed_state}"
75
+ state_count = state_count + 1
76
+ $log.info("#{state_count}/#{steady_count}")
77
+ elsif ["START_FAILED", "STOP_FAILED"].include?(state)
78
+ $log.error("SFTP Server #{@server_id} failed to reach state #{completed_state} with current state #{state}!")
79
+ break
80
+ else
81
+ state_count = 0
82
+ end
83
+ break if state_count == steady_count
84
+ attempts = attempts + 1
85
+ sleep(15)
86
+ end
87
+
88
+ if attempts == max_attempts
89
+ $log.error("SFTP Server #{@server_id} did not enter #{completed_state} state, however continuing operations...")
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -1,12 +1,12 @@
1
- require 'cfn_manage/asg_start_stop_handler'
2
- require 'cfn_manage/ec2_start_stop_handler'
3
- require 'cfn_manage/rds_start_stop_handler'
4
- require 'cfn_manage/aurora_cluster_start_stop_handler'
5
- require 'cfn_manage/alarm_start_stop_handler'
6
- require 'cfn_manage/spot_fleet_start_stop_handler'
7
- require 'cfn_manage/ecs_cluster_start_stop_handler'
8
- require 'cfn_manage/documentdb_cluster_start_stop_handler'
9
- require 'cfn_manage/transfer_start_stop_handler'
1
+ require 'cfn_manage/handlers/asg'
2
+ require 'cfn_manage/handlers/ec2'
3
+ require 'cfn_manage/handlers/rds'
4
+ require 'cfn_manage/handlers/aurora_cluster'
5
+ require 'cfn_manage/handlers/alarm'
6
+ require 'cfn_manage/handlers/spot_fleet'
7
+ require 'cfn_manage/handlers/ecs_cluster'
8
+ require 'cfn_manage/handlers/documentdb'
9
+ require 'cfn_manage/handlers/transfer'
10
10
 
11
11
  module CfnManage
12
12
 
@@ -15,34 +15,34 @@ module CfnManage
15
15
  # Factory method to get start/stop handler based on CloudFormation
16
16
  # resource type. If resource_id passed in does not exist, it is
17
17
  # very likely that exception will be raised
18
- def self.get_start_stop_handler(resource_type, resource_id, skip_wait)
18
+ def self.get_start_stop_handler(resource_type, resource_id, options)
19
19
  case resource_type
20
20
  when 'AWS::AutoScaling::AutoScalingGroup'
21
- return CfnManage::AsgStartStopHandler.new(resource_id, skip_wait)
21
+ return CfnManage::StartStopHandler::Asg.new(resource_id,options)
22
22
 
23
23
  when 'AWS::EC2::Instance'
24
- return CfnManage::Ec2StartStopHandler.new(resource_id, skip_wait)
24
+ return CfnManage::StartStopHandler::Ec2.new(resource_id,options)
25
25
 
26
26
  when 'AWS::RDS::DBInstance'
27
- return CfnManage::RdsStartStopHandler.new(resource_id, skip_wait)
27
+ return CfnManage::StartStopHandler::Rds.new(resource_id,options)
28
28
 
29
29
  when 'AWS::RDS::DBCluster'
30
- return CfnManage::AuroraClusterStartStopHandler.new(resource_id, skip_wait)
30
+ return CfnManage::StartStopHandler::AuroraCluster.new(resource_id,options)
31
31
 
32
32
  when 'AWS::DocDB::DBCluster'
33
- return CfnManage::DocumentDbClusterStartStopHandler.new(resource_id, skip_wait)
33
+ return CfnManage::StartStopHandler::DocumentDb.new(resource_id,options)
34
34
 
35
35
  when 'AWS::CloudWatch::Alarm'
36
- return CfnManage::AlarmStartStopHandler.new(resource_id)
36
+ return CfnManage::StartStopHandler::Alarm.new(resource_id,options)
37
37
 
38
38
  when 'AWS::EC2::SpotFleet'
39
- return CfnManage::SpotFleetStartStopHandler.new(resource_id, skip_wait)
39
+ return CfnManage::StartStopHandler::SpotFleet.new(resource_id,options)
40
40
 
41
41
  when 'AWS::ECS::Cluster'
42
- return CfnManage::EcsClusterStartStopHandler.new(resource_id, skip_wait)
42
+ return CfnManage::StartStopHandler::EcsCluster.new(resource_id,options)
43
43
 
44
44
  when 'AWS::Transfer::Server'
45
- return CfnManage::TransferStartStopHandler.new(resource_id, skip_wait)
45
+ return CfnManage::StartStopHandler::Transfer.new(resource_id,options)
46
46
 
47
47
  else
48
48
  return nil
@@ -0,0 +1,77 @@
1
+ require 'cfn_manage/aws_credentials'
2
+
3
+ require 'aws-sdk-autoscaling'
4
+
5
+ module CfnManage
6
+ class TagFinder
7
+
8
+ attr_reader :priority, :opts
9
+
10
+ def initialize(resource_id)
11
+ @resource_id = resource_id
12
+ @tags = []
13
+ end
14
+
15
+ def get_tags(resource_type)
16
+ case resource_type
17
+ when 'AWS::AutoScaling::AutoScalingGroup'
18
+ asg()
19
+ when 'AWS::EC2::Instance'
20
+ ec2()
21
+ when 'AWS::ECS::Cluster'
22
+ ecs_cluster()
23
+ end
24
+ end
25
+
26
+ def priority()
27
+ @tags.select {|tag| tag.key == 'cfn_manage:prority'}.collect {|tag| tag.value}.first
28
+ end
29
+
30
+ def options()
31
+ # collect all the cfn_manage tags and pass the back as a hash
32
+ # so they can be passed into the resource handers
33
+ options = @tags.select {|tag| tag.key.start_with?('cfn_manage:') }
34
+ options.collect { |tag| { tag.key.split(':').last.to_sym => tag.value } }.reduce(Hash.new,:merge)
35
+ end
36
+
37
+ def asg()
38
+ credentials = CfnManage::AWSCredentials.get_session_credentials("cfn_manage_get_tags")
39
+ client = Aws::AutoScaling::Client.new(credentials: credentials, retry_limit: 20)
40
+ resp = client.describe_tags({
41
+ filters: [
42
+ {
43
+ name: "auto-scaling-group",
44
+ values: [@resource_id]
45
+ }
46
+ ]
47
+ })
48
+ @tags = resp.tags
49
+ end
50
+
51
+ def ec2()
52
+ credentials = CfnManage::AWSCredentials.get_session_credentials("cfn_manage_get_tags")
53
+ client = Aws::EC2::Client.new(credentials: credentials, retry_limit: 20)
54
+ resp = client.describe_tags({
55
+ filters: [
56
+ {
57
+ name: "resource-id",
58
+ values: [@resource_id]
59
+ }
60
+ ]
61
+ })
62
+ @tags = resp.tags
63
+ end
64
+
65
+ def ecs_cluster()
66
+ credentials = CfnManage::AWSCredentials.get_session_credentials("cfn_manage_get_tags")
67
+ client = Aws::ECS::Client.new(credentials: credentials, retry_limit: 20)
68
+ resp = client.describe_clusters({
69
+ clusters: [@resource_id],
70
+ include: ["TAGS"]
71
+ })
72
+ cluster = resp.clusters.first
73
+ @tags = cluster.tags
74
+ end
75
+
76
+ end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module CfnManage
2
- VERSION="0.7.0".freeze
2
+ VERSION="0.8.3".freeze
3
3
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfn_manage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Base2Services
8
8
  - Nikola Tosic
9
9
  - Angus Vine
10
10
  autorequire:
11
- bindir: bin
11
+ bindir: exe
12
12
  cert_chain: []
13
- date: 2019-08-09 00:00:00.000000000 Z
13
+ date: 2020-05-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk-core
@@ -232,6 +232,26 @@ dependencies:
232
232
  - - "<"
233
233
  - !ruby/object:Gem::Version
234
234
  version: '2'
235
+ - !ruby/object:Gem::Dependency
236
+ name: aws-sdk-elasticloadbalancingv2
237
+ requirement: !ruby/object:Gem::Requirement
238
+ requirements:
239
+ - - "~>"
240
+ - !ruby/object:Gem::Version
241
+ version: '1'
242
+ - - "<"
243
+ - !ruby/object:Gem::Version
244
+ version: '2'
245
+ type: :runtime
246
+ prerelease: false
247
+ version_requirements: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - "~>"
250
+ - !ruby/object:Gem::Version
251
+ version: '1'
252
+ - - "<"
253
+ - !ruby/object:Gem::Version
254
+ version: '2'
235
255
  - !ruby/object:Gem::Dependency
236
256
  name: bundler
237
257
  requirement: !ruby/object:Gem::Requirement
@@ -252,14 +272,14 @@ dependencies:
252
272
  requirements:
253
273
  - - "~>"
254
274
  - !ruby/object:Gem::Version
255
- version: '10.0'
275
+ version: '13.0'
256
276
  type: :development
257
277
  prerelease: false
258
278
  version_requirements: !ruby/object:Gem::Requirement
259
279
  requirements:
260
280
  - - "~>"
261
281
  - !ruby/object:Gem::Version
262
- version: '10.0'
282
+ version: '13.0'
263
283
  - !ruby/object:Gem::Dependency
264
284
  name: rspec
265
285
  requirement: !ruby/object:Gem::Requirement
@@ -330,51 +350,66 @@ dependencies:
330
350
  - - "~>"
331
351
  - !ruby/object:Gem::Version
332
352
  version: '0.16'
333
- description: ''
353
+ description: Start and stop aws resources in a cloudformation stack
334
354
  email: itsupport@base2services.com
335
355
  executables:
336
356
  - cfn_manage
357
+ - usage.txt
337
358
  extensions: []
338
359
  extra_rdoc_files: []
339
360
  files:
340
- - bin/cfn_manage
341
- - bin/usage.txt
361
+ - ".gitignore"
362
+ - ".rspec"
363
+ - ".travis.yml"
364
+ - Dockerfile
365
+ - Gemfile
366
+ - LICENSE
367
+ - README.md
368
+ - Rakefile
369
+ - cfn_manage.gemspec
370
+ - exe/cfn_manage
371
+ - exe/usage.txt
342
372
  - lib/cfn_manage.rb
343
- - lib/cfn_manage/alarm_start_stop_handler.rb
344
- - lib/cfn_manage/asg_start_stop_handler.rb
345
- - lib/cfn_manage/aurora_cluster_start_stop_handler.rb
346
373
  - lib/cfn_manage/aws_credentials.rb
347
374
  - lib/cfn_manage/cf_common.rb
348
375
  - lib/cfn_manage/cf_progress_tracker.rb
349
376
  - lib/cfn_manage/cf_start_stop_environment.rb
350
- - lib/cfn_manage/documentdb_cluster_start_stop_handler.rb
351
- - lib/cfn_manage/ec2_start_stop_handler.rb
352
- - lib/cfn_manage/ecs_cluster_start_stop_handler.rb
353
- - lib/cfn_manage/rds_start_stop_handler.rb
354
- - lib/cfn_manage/spot_fleet_start_stop_handler.rb
377
+ - lib/cfn_manage/globals.rb
378
+ - lib/cfn_manage/handlers/alarm.rb
379
+ - lib/cfn_manage/handlers/asg.rb
380
+ - lib/cfn_manage/handlers/aurora_cluster.rb
381
+ - lib/cfn_manage/handlers/documentdb.rb
382
+ - lib/cfn_manage/handlers/ec2.rb
383
+ - lib/cfn_manage/handlers/ecs_cluster.rb
384
+ - lib/cfn_manage/handlers/rds.rb
385
+ - lib/cfn_manage/handlers/spot_fleet.rb
386
+ - lib/cfn_manage/handlers/transfer.rb
355
387
  - lib/cfn_manage/start_stop_handler_factory.rb
356
- - lib/cfn_manage/transfer_start_stop_handler.rb
388
+ - lib/cfn_manage/tag_finder.rb
357
389
  - lib/cfn_manage/version.rb
358
390
  homepage: https://github.com/base2Services/cfn-start-stop-stack/blob/master/README.md
359
391
  licenses:
360
392
  - MIT
361
- metadata: {}
393
+ metadata:
394
+ allowed_push_host: https://rubygems.org
395
+ homepage_uri: https://github.com/base2Services/cfn-start-stop-stack/blob/master/README.md
396
+ source_code_uri: https://github.com/base2services/aws-client-vpn
362
397
  post_install_message:
363
398
  rdoc_options: []
364
399
  require_paths:
365
400
  - lib
366
401
  required_ruby_version: !ruby/object:Gem::Requirement
367
402
  requirements:
368
- - - ">="
403
+ - - "~>"
369
404
  - !ruby/object:Gem::Version
370
- version: '0'
405
+ version: '2.5'
371
406
  required_rubygems_version: !ruby/object:Gem::Requirement
372
407
  requirements:
373
408
  - - ">="
374
409
  - !ruby/object:Gem::Version
375
410
  version: '0'
376
411
  requirements: []
377
- rubygems_version: 3.0.4
412
+ rubygems_version: 3.1.3
378
413
  signing_key:
379
414
  specification_version: 4
380
415
  summary: Manage AWS Cloud Formation stacks