cumulus-aws 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +3 -0
  3. data/.travis.yml +12 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +29 -0
  6. data/LICENSE +202 -0
  7. data/README.md +41 -0
  8. data/autocomplete +137 -0
  9. data/bin/cumulus +658 -0
  10. data/cumulus +2 -0
  11. data/cumulus-aws.gemspec +20 -0
  12. data/lib/autoscaling/AutoScaling.rb +40 -0
  13. data/lib/autoscaling/loader/Loader.rb +56 -0
  14. data/lib/autoscaling/manager/Manager.rb +360 -0
  15. data/lib/autoscaling/models/AlarmConfig.rb +165 -0
  16. data/lib/autoscaling/models/AlarmDiff.rb +172 -0
  17. data/lib/autoscaling/models/AutoScalingDiff.rb +178 -0
  18. data/lib/autoscaling/models/GroupConfig.rb +330 -0
  19. data/lib/autoscaling/models/PolicyConfig.rb +135 -0
  20. data/lib/autoscaling/models/PolicyDiff.rb +73 -0
  21. data/lib/autoscaling/models/ScheduledActionDiff.rb +53 -0
  22. data/lib/autoscaling/models/ScheduledConfig.rb +96 -0
  23. data/lib/aws_extensions/ec2/DhcpOptions.rb +41 -0
  24. data/lib/aws_extensions/ec2/Instance.rb +29 -0
  25. data/lib/aws_extensions/ec2/NetworkAcl.rb +25 -0
  26. data/lib/aws_extensions/ec2/NetworkInterface.rb +14 -0
  27. data/lib/aws_extensions/ec2/RouteTable.rb +26 -0
  28. data/lib/aws_extensions/ec2/SecurityGroup.rb +16 -0
  29. data/lib/aws_extensions/ec2/Subnet.rb +28 -0
  30. data/lib/aws_extensions/ec2/Volume.rb +24 -0
  31. data/lib/aws_extensions/ec2/Vpc.rb +14 -0
  32. data/lib/aws_extensions/ec2/VpcEndpoint.rb +11 -0
  33. data/lib/aws_extensions/elb/BackendServerDescription.rb +12 -0
  34. data/lib/aws_extensions/elb/PolicyDescription.rb +14 -0
  35. data/lib/aws_extensions/kinesis/StreamDescription.rb +12 -0
  36. data/lib/aws_extensions/route53/AliasTarget.rb +21 -0
  37. data/lib/aws_extensions/s3/Bucket.rb +33 -0
  38. data/lib/aws_extensions/s3/BucketAcl.rb +28 -0
  39. data/lib/aws_extensions/s3/BucketCors.rb +17 -0
  40. data/lib/aws_extensions/s3/BucketLifecycle.rb +21 -0
  41. data/lib/aws_extensions/s3/BucketLogging.rb +18 -0
  42. data/lib/aws_extensions/s3/BucketNotification.rb +23 -0
  43. data/lib/aws_extensions/s3/BucketPolicy.rb +18 -0
  44. data/lib/aws_extensions/s3/BucketTagging.rb +15 -0
  45. data/lib/aws_extensions/s3/BucketVersioning.rb +14 -0
  46. data/lib/aws_extensions/s3/BucketWebsite.rb +49 -0
  47. data/lib/aws_extensions/s3/CORSRule.rb +27 -0
  48. data/lib/aws_extensions/s3/ReplicationConfiguration.rb +22 -0
  49. data/lib/cloudfront/CloudFront.rb +83 -0
  50. data/lib/cloudfront/loader/Loader.rb +31 -0
  51. data/lib/cloudfront/manager/Manager.rb +183 -0
  52. data/lib/cloudfront/models/CacheBehaviorConfig.rb +237 -0
  53. data/lib/cloudfront/models/CacheBehaviorDiff.rb +211 -0
  54. data/lib/cloudfront/models/CustomOriginConfig.rb +51 -0
  55. data/lib/cloudfront/models/CustomOriginDiff.rb +74 -0
  56. data/lib/cloudfront/models/DistributionConfig.rb +183 -0
  57. data/lib/cloudfront/models/DistributionDiff.rb +131 -0
  58. data/lib/cloudfront/models/InvalidationConfig.rb +37 -0
  59. data/lib/cloudfront/models/OriginConfig.rb +144 -0
  60. data/lib/cloudfront/models/OriginDiff.rb +86 -0
  61. data/lib/cloudfront/models/OriginSslProtocols.rb +28 -0
  62. data/lib/cloudfront/models/OriginSslProtocolsDiff.rb +39 -0
  63. data/lib/common/BaseLoader.rb +80 -0
  64. data/lib/common/manager/Manager.rb +148 -0
  65. data/lib/common/models/Diff.rb +114 -0
  66. data/lib/common/models/ListChange.rb +21 -0
  67. data/lib/common/models/TagsDiff.rb +55 -0
  68. data/lib/common/models/UTCTimeSource.rb +17 -0
  69. data/lib/conf/Configuration.rb +365 -0
  70. data/lib/ec2/EC2.rb +503 -0
  71. data/lib/ec2/IPProtocolMapping.rb +165 -0
  72. data/lib/ec2/loaders/EbsLoader.rb +19 -0
  73. data/lib/ec2/loaders/InstanceLoader.rb +32 -0
  74. data/lib/ec2/managers/EbsManager.rb +176 -0
  75. data/lib/ec2/managers/InstanceManager.rb +509 -0
  76. data/lib/ec2/models/EbsGroupConfig.rb +133 -0
  77. data/lib/ec2/models/EbsGroupDiff.rb +48 -0
  78. data/lib/ec2/models/InstanceConfig.rb +202 -0
  79. data/lib/ec2/models/InstanceDiff.rb +95 -0
  80. data/lib/elb/ELB.rb +148 -0
  81. data/lib/elb/loader/Loader.rb +65 -0
  82. data/lib/elb/manager/Manager.rb +581 -0
  83. data/lib/elb/models/AccessLogConfig.rb +82 -0
  84. data/lib/elb/models/AccessLogDiff.rb +47 -0
  85. data/lib/elb/models/HealthCheckConfig.rb +91 -0
  86. data/lib/elb/models/HealthCheckDiff.rb +50 -0
  87. data/lib/elb/models/ListenerConfig.rb +99 -0
  88. data/lib/elb/models/ListenerDiff.rb +91 -0
  89. data/lib/elb/models/LoadBalancerConfig.rb +239 -0
  90. data/lib/elb/models/LoadBalancerDiff.rb +265 -0
  91. data/lib/iam/IAM.rb +36 -0
  92. data/lib/iam/loader/Loader.rb +117 -0
  93. data/lib/iam/manager/IamGroups.rb +98 -0
  94. data/lib/iam/manager/IamResource.rb +288 -0
  95. data/lib/iam/manager/IamRoles.rb +112 -0
  96. data/lib/iam/manager/IamUsers.rb +54 -0
  97. data/lib/iam/manager/Manager.rb +29 -0
  98. data/lib/iam/migration/AssumeRoleUnifier.rb +34 -0
  99. data/lib/iam/migration/PolicyUnifier.rb +90 -0
  100. data/lib/iam/models/GroupConfig.rb +40 -0
  101. data/lib/iam/models/IamDiff.rb +132 -0
  102. data/lib/iam/models/PolicyConfig.rb +67 -0
  103. data/lib/iam/models/ResourceWithPolicy.rb +208 -0
  104. data/lib/iam/models/RoleConfig.rb +53 -0
  105. data/lib/iam/models/StatementConfig.rb +35 -0
  106. data/lib/iam/models/UserConfig.rb +21 -0
  107. data/lib/kinesis/Kinesis.rb +94 -0
  108. data/lib/kinesis/loader/Loader.rb +19 -0
  109. data/lib/kinesis/manager/Manager.rb +206 -0
  110. data/lib/kinesis/models/StreamConfig.rb +75 -0
  111. data/lib/kinesis/models/StreamDiff.rb +58 -0
  112. data/lib/lambda/Lambda.rb +41 -0
  113. data/lib/route53/loader/Loader.rb +32 -0
  114. data/lib/route53/manager/Manager.rb +241 -0
  115. data/lib/route53/models/AliasTarget.rb +86 -0
  116. data/lib/route53/models/RecordConfig.rb +178 -0
  117. data/lib/route53/models/RecordDiff.rb +140 -0
  118. data/lib/route53/models/Vpc.rb +24 -0
  119. data/lib/route53/models/ZoneConfig.rb +156 -0
  120. data/lib/route53/models/ZoneDiff.rb +118 -0
  121. data/lib/s3/S3.rb +89 -0
  122. data/lib/s3/loader/Loader.rb +66 -0
  123. data/lib/s3/manager/Manager.rb +296 -0
  124. data/lib/s3/models/BucketConfig.rb +321 -0
  125. data/lib/s3/models/BucketDiff.rb +167 -0
  126. data/lib/s3/models/GrantConfig.rb +189 -0
  127. data/lib/s3/models/GrantDiff.rb +50 -0
  128. data/lib/s3/models/LifecycleConfig.rb +142 -0
  129. data/lib/s3/models/LifecycleDiff.rb +46 -0
  130. data/lib/s3/models/LoggingConfig.rb +81 -0
  131. data/lib/s3/models/NotificationConfig.rb +157 -0
  132. data/lib/s3/models/NotificationDiff.rb +62 -0
  133. data/lib/s3/models/ReplicationConfig.rb +133 -0
  134. data/lib/s3/models/ReplicationDiff.rb +60 -0
  135. data/lib/s3/models/WebsiteConfig.rb +107 -0
  136. data/lib/security/SecurityGroups.rb +39 -0
  137. data/lib/security/loader/Loader.rb +94 -0
  138. data/lib/security/manager/Manager.rb +246 -0
  139. data/lib/security/models/RuleConfig.rb +161 -0
  140. data/lib/security/models/RuleDiff.rb +72 -0
  141. data/lib/security/models/RuleMigration.rb +127 -0
  142. data/lib/security/models/SecurityGroupConfig.rb +172 -0
  143. data/lib/security/models/SecurityGroupDiff.rb +112 -0
  144. data/lib/sns/SNS.rb +40 -0
  145. data/lib/sqs/SQS.rb +62 -0
  146. data/lib/sqs/loader/Loader.rb +34 -0
  147. data/lib/sqs/manager/Manager.rb +128 -0
  148. data/lib/sqs/models/DeadLetterConfig.rb +70 -0
  149. data/lib/sqs/models/DeadLetterDiff.rb +35 -0
  150. data/lib/sqs/models/QueueConfig.rb +115 -0
  151. data/lib/sqs/models/QueueDiff.rb +89 -0
  152. data/lib/util/Colors.rb +111 -0
  153. data/lib/util/StatusCodes.rb +51 -0
  154. data/lib/vpc/loader/Loader.rb +73 -0
  155. data/lib/vpc/manager/Manager.rb +954 -0
  156. data/lib/vpc/models/AclEntryConfig.rb +150 -0
  157. data/lib/vpc/models/AclEntryDiff.rb +54 -0
  158. data/lib/vpc/models/DhcpConfig.rb +100 -0
  159. data/lib/vpc/models/DhcpDiff.rb +90 -0
  160. data/lib/vpc/models/EndpointConfig.rb +76 -0
  161. data/lib/vpc/models/EndpointDiff.rb +69 -0
  162. data/lib/vpc/models/NetworkAclConfig.rb +87 -0
  163. data/lib/vpc/models/NetworkAclDiff.rb +116 -0
  164. data/lib/vpc/models/RouteConfig.rb +82 -0
  165. data/lib/vpc/models/RouteDiff.rb +50 -0
  166. data/lib/vpc/models/RouteTableConfig.rb +92 -0
  167. data/lib/vpc/models/RouteTableDiff.rb +101 -0
  168. data/lib/vpc/models/SubnetConfig.rb +113 -0
  169. data/lib/vpc/models/SubnetDiff.rb +78 -0
  170. data/lib/vpc/models/VpcConfig.rb +173 -0
  171. data/lib/vpc/models/VpcDiff.rb +315 -0
  172. data/rakefile.rb +8 -0
  173. metadata +245 -0
data/cumulus ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env bash
2
+ ~/.cumulus/bin/cumulus "$@"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require "bundler"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cumulus-aws"
7
+ s.version = "0.11.1"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Keilan Jackson", "Mark Siebert"]
10
+ s.email = "cumulus@lucidchart.com"
11
+ s.homepage = "http://lucidsoftware.github.io/cumulus/"
12
+ s.summary = "AWS Configuration Manager"
13
+ s.description = "Cumulus allows you to manage your AWS infrastructure by creating JSON configuration files that describe your AWS resources."
14
+ s.files = `git ls-files | grep -v ^conf/`.split($/)
15
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ s.license = "Apache-2.0"
17
+
18
+ s.add_runtime_dependency "aws-sdk", "2.2.8"
19
+ s.add_runtime_dependency "parse-cron", "~> 0.1.4"
20
+ end
@@ -0,0 +1,40 @@
1
+ require "conf/Configuration"
2
+
3
+ require "aws-sdk"
4
+
5
+ module Cumulus
6
+ module AutoScaling
7
+ class << self
8
+ @@client = Aws::AutoScaling::Client.new(Configuration.instance.client)
9
+
10
+ # Public
11
+ #
12
+ # Returns an array of instance ids that are in any autoscaling groups
13
+ def instance_ids
14
+ @instance_ids ||= groups.map { |gr| gr.instances.map { |i| i.instance_id } }.flatten
15
+ end
16
+
17
+ # Public
18
+ #
19
+ # Returns a Hash of autoscaling group name to Aws::AutoScaling::Types::AutoScalingGroup
20
+ def named_groups
21
+ @named_groups ||= Hash[groups.map { |group| [group.auto_scaling_group_name, group] }]
22
+ end
23
+
24
+ # Public: Lazily load auto scaling groups
25
+ def groups
26
+ @groups = init_groups
27
+ end
28
+
29
+ private
30
+
31
+ # Internal: Load all auto scaling groups
32
+ #
33
+ # Returns an array of Aws::AutoScaling::Types::AutoScalingGroup
34
+ def init_groups
35
+ @@client.describe_auto_scaling_groups.auto_scaling_groups
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,56 @@
1
+ require "autoscaling/models/GroupConfig"
2
+ require "autoscaling/models/PolicyConfig"
3
+ require "common/BaseLoader"
4
+ require "conf/Configuration"
5
+
6
+ module Cumulus
7
+ module AutoScaling
8
+ # Public: Load AutoScaling assets
9
+ module Loader
10
+ include Common::BaseLoader
11
+
12
+ @@groups_dir = Configuration.instance.autoscaling.groups_directory
13
+ @@group_loader = Proc.new { |name, json| GroupConfig.new(name, json) }
14
+ @@static_dir = Configuration.instance.autoscaling.static_policy_directory
15
+ @@template_dir = Configuration.instance.autoscaling.template_policy_directory
16
+ @@policy_loader = Proc.new { |name, json| PolicyConfig.new(json) }
17
+
18
+ # Public: Load all autoscaling group configurations as GroupConfig objects
19
+ #
20
+ # Returns an array of GroupConfig objects
21
+ def Loader.groups
22
+ Common::BaseLoader.resources(@@groups_dir, &@@group_loader)
23
+ end
24
+
25
+ # Public: Load a single autoscaling group configuration as a GroupConfig
26
+ # object
27
+ #
28
+ # file - the name of the file the configuration is located in
29
+ #
30
+ # Returns the corresponding GroupConfig object
31
+ def Loader.group(file)
32
+ Common::BaseLoader.resource(file, @@groups_dir, &@@group_loader)
33
+ end
34
+
35
+ # Public: Load a static scaling policy
36
+ #
37
+ # file - the file the policy definition is found in
38
+ #
39
+ # Returns a PolicyConfig object that contains the configuration
40
+ def Loader.static_policy(file)
41
+ Common::BaseLoader.resource(file, @@static_dir, &@@policy_loader)
42
+ end
43
+
44
+ # Public: Load a template scaling policy
45
+ #
46
+ # file - the file the template definition is found in
47
+ # variables - a Hash of variables to apply to the template
48
+ #
49
+ # Returns a PolicyConfig object corresponding to the applied template
50
+ def Loader.template_policy(file, variables)
51
+ Common::BaseLoader.template(file, @@template_dir, variables, &@@policy_loader)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,360 @@
1
+ require "autoscaling/AutoScaling"
2
+ require "autoscaling/loader/Loader"
3
+ require "autoscaling/models/AlarmDiff"
4
+ require "autoscaling/models/AutoScalingDiff"
5
+ require "autoscaling/models/ScheduledActionDiff"
6
+ require "common/manager/Manager"
7
+ require "util/Colors"
8
+
9
+ require "aws-sdk"
10
+
11
+ module Cumulus
12
+ module AutoScaling
13
+ # Public: The main class for the AutoScaling management module
14
+ class Manager < Common::Manager
15
+ # Public: Constructor. Initializes the AWS client.
16
+ def initialize
17
+ super()
18
+ @aws = Aws::AutoScaling::Client.new(Configuration.instance.client)
19
+ @cloudwatch = Aws::CloudWatch::Client.new(Configuration.instance.client)
20
+ end
21
+
22
+ # Public: Migrate AWS Autoscaling to Cumulus configuration.
23
+ def migrate
24
+ groups_dir = "#{@migration_root}/groups"
25
+
26
+ if !Dir.exists?(@migration_root)
27
+ Dir.mkdir(@migration_root)
28
+ end
29
+ if !Dir.exists?(groups_dir)
30
+ Dir.mkdir(groups_dir)
31
+ end
32
+
33
+ aws_resources.each do |name, resource|
34
+ puts "Processing #{name}..."
35
+ config = GroupConfig.new(resource.auto_scaling_group_name)
36
+ config.populate(resource)
37
+ config.populate_scheduled(@aws.describe_scheduled_actions({
38
+ auto_scaling_group_name: resource.auto_scaling_group_name
39
+ }).scheduled_update_group_actions)
40
+ config.populate_policies(@aws.describe_policies({
41
+ auto_scaling_group_name: resource.auto_scaling_group_name
42
+ }).scaling_policies)
43
+
44
+ puts "Writing #{resource.auto_scaling_group_name} configuration to file..."
45
+ File.open("#{groups_dir}/#{config.name}.json", 'w') { |f| f.write(config.pretty_json) }
46
+ end
47
+
48
+ end
49
+
50
+ def resource_name
51
+ "AutoScaling Group"
52
+ end
53
+
54
+ def local_resources
55
+ @local_resources ||= Hash[Loader.groups.map { |local| [local.name, local] }]
56
+ end
57
+
58
+ def aws_resources
59
+ @aws_resources ||= AutoScaling::named_groups
60
+ end
61
+
62
+ def unmanaged_diff(aws)
63
+ AutoScalingDiff.unmanaged(aws)
64
+ end
65
+
66
+ def added_diff(local)
67
+ AutoScalingDiff.added(local)
68
+ end
69
+
70
+ def diff_resource(local, aws)
71
+ local.diff(aws, @aws)
72
+ end
73
+
74
+ private
75
+
76
+ # Internal: Create an AutoScaling Group
77
+ #
78
+ # group - the group to create
79
+ def create(group)
80
+
81
+ hash = group.to_aws(true)
82
+
83
+ # If there are local scheduled actions, override the min, max
84
+ # and desired capacity if it exists on the last scheduled action
85
+ last_scheduled = group.last_scheduled
86
+ if last_scheduled
87
+ hash[:min_size] = last_scheduled.min || hash[:min_size]
88
+ hash[:max_size] = last_scheduled.max || hash[:max_size]
89
+ hash[:desired_capacity] = last_scheduled.desired || hash[:desired_capacity]
90
+ end
91
+
92
+ @aws.create_auto_scaling_group(hash)
93
+ update_tags(group, {}, group.tags)
94
+ update_load_balancers(group, [], group.load_balancers)
95
+ update_scheduled_actions(group, [], group.scheduled.map { |k, v| k })
96
+ update_scaling_policies(group, [], group.policies.map { |k, v| k })
97
+ if group.enabled_metrics.size > 0
98
+ update_metrics(group, [], group.enabled_metrics)
99
+ end
100
+
101
+ # update alarms for each created scaling policy
102
+ group.policies.each do |policy_name, policy|
103
+ policy_arn = @aws.describe_policies({
104
+ auto_scaling_group_name: group.name,
105
+ policy_names: [policy_name]
106
+ }).scaling_policies[0].policy_arn
107
+ update_alarms(policy, policy_arn, [], policy.alarms.map { |k , v| k })
108
+ end
109
+ end
110
+
111
+ # Internal: Update an AutoScaling Group
112
+ #
113
+ # group - the group to update
114
+ # diffs - the diffs between local and AWS configuration
115
+ def update(group, diffs)
116
+ hash = group.to_aws(Configuration.instance.autoscaling.force_size)
117
+ if !Configuration.instance.autoscaling.override_launch_config_on_sync
118
+ hash.delete(:launch_configuration_name)
119
+ end
120
+
121
+ update_group = false
122
+
123
+ diffs.each do |diff|
124
+ case diff.type
125
+ when AutoScalingChange::MIN
126
+ update_group = true
127
+
128
+ # Override the min size value because it could be different from the actual
129
+ # configured min size if there were scheduled actions
130
+ hash[:min_size] = diff.local
131
+ when AutoScalingChange::MAX
132
+ update_group = true
133
+
134
+ # Override the max size value because it could be different from the actual
135
+ # configured max size if there were scheduled actions
136
+ hash[:max_size] = diff.local
137
+ when AutoScalingChange::DESIRED
138
+ update_group = true
139
+
140
+ # Override the desired size value because it could be different from the actual
141
+ # configured desired size if there were scheduled actions
142
+ hash[:desired_capacity] = diff.local
143
+ when AutoScalingChange::COOLDOWN,
144
+ AutoScalingChange::CHECK_TYPE,
145
+ AutoScalingChange::CHECK_GRACE,
146
+ AutoScalingChange::SUBNETS,
147
+ AutoScalingChange::TERMINATION,
148
+ AutoScalingChange::LAUNCH
149
+ update_group = true
150
+ when AutoScalingChange::TAGS
151
+ update_tags(group, diff.tags_to_remove, diff.tags_to_add)
152
+ when AutoScalingChange::LOAD_BALANCER
153
+ update_load_balancers(group, diff.load_balancers_to_remove, diff.load_balancers_to_add)
154
+ when AutoScalingChange::METRICS
155
+ update_metrics(group, diff.metrics_to_disable, diff.metrics_to_enable)
156
+ when AutoScalingChange::SCHEDULED
157
+ remove = diff.changes.removed.map { |d| d.aws.scheduled_action_name }
158
+ update = (diff.changes.added + diff.changes.modified).map { |d| d.local.name }
159
+
160
+ update_scheduled_actions(group, remove, update)
161
+ when AutoScalingChange::POLICY
162
+ remove = diff.policy_diffs.reject do |d|
163
+ d.type != PolicyChange::UNMANAGED
164
+ end.map { |d| d.aws.policy_name }
165
+ update = diff.policy_diffs.select do |d|
166
+ d.type != PolicyChange::UNMANAGED and d.type != PolicyChange::ALARM
167
+ end.map { |d| d.local.name }
168
+ update_scaling_policies(group, remove, update)
169
+
170
+ # update alarms for existing policies
171
+ alarms = diff.policy_diffs.select { |d| d.type == PolicyChange::ALARM }
172
+ alarms.each do |policy_diff|
173
+ remove = policy_diff.alarm_diffs.reject do |d|
174
+ d.type != AlarmChange::UNMANAGED
175
+ end.map { |d| d.aws.alarm_name }
176
+ update = policy_diff.alarm_diffs.select do |d|
177
+ d.type != AlarmChange::UNMANAGED
178
+ end.map { |u| u.local.name }
179
+
180
+ update_alarms(policy_diff.local, policy_diff.policy_arn, remove, update)
181
+ end
182
+
183
+ # create alarms for new policies
184
+ new_policies = diff.policy_diffs.select { |d| d.type == PolicyChange::ADD }
185
+ new_policies.each do |policy_diff|
186
+ config = policy_diff.local
187
+ policy_arn = @aws.describe_policies({
188
+ auto_scaling_group_name: group.name,
189
+ policy_names: [config.name]
190
+ }).scaling_policies[0].policy_arn
191
+ update_alarms(config, policy_arn, [], config.alarms.map {|k , v| k })
192
+ end
193
+ end
194
+ end
195
+
196
+ @aws.update_auto_scaling_group(hash) if update_group
197
+ end
198
+
199
+ # Internal: Update the tags for an autoscaling group.
200
+ #
201
+ # group - the autoscaling group to update
202
+ # remove - the tags to remove
203
+ # add - the tags to add
204
+ def update_tags(group, remove, add)
205
+ @aws.delete_tags({
206
+ tags: remove.map { |k, v|
207
+ {
208
+ key: k,
209
+ resource_id: group.name,
210
+ resource_type: "auto-scaling-group"
211
+ }
212
+ }
213
+ })
214
+ @aws.create_or_update_tags({
215
+ tags: add.map { |k, v|
216
+ {
217
+ key: k,
218
+ value: v,
219
+ resource_id: group.name,
220
+ resource_type: "auto-scaling-group",
221
+ propagate_at_launch: true
222
+ }
223
+ }
224
+ })
225
+ end
226
+
227
+ # Internal: Update the load balancers for an autoscaling group.
228
+ #
229
+ # group - the autoscaling group to update
230
+ # remove - the load balancers to remove
231
+ # add - the load balancers to add
232
+ def update_load_balancers(group, remove, add)
233
+ @aws.detach_load_balancers({
234
+ auto_scaling_group_name: group.name,
235
+ load_balancer_names: remove
236
+ })
237
+ @aws.attach_load_balancers({
238
+ auto_scaling_group_name: group.name,
239
+ load_balancer_names: add
240
+ })
241
+ end
242
+
243
+ # Internal: Update the metrics enabled for an autoscaling group.
244
+ #
245
+ # group - the autoscaling group to update
246
+ # disable - the metrics to disable
247
+ # enable - the metrics to enable
248
+ def update_metrics(group, disable, enable)
249
+ @aws.disable_metrics_collection({
250
+ auto_scaling_group_name: group.name,
251
+ metrics: disable
252
+ })
253
+ @aws.enable_metrics_collection({
254
+ auto_scaling_group_name: group.name,
255
+ metrics: enable,
256
+ granularity: "1Minute"
257
+ })
258
+ end
259
+
260
+ # Internal: Update the scheduled actions for an autoscaling group.
261
+ #
262
+ # group - the group the scheduled actions belong to
263
+ # remove - the names of the actions to remove
264
+ # update - the names of the actions to update
265
+ def update_scheduled_actions(group, remove, update)
266
+ # remove any unmanaged scheduled actions
267
+ remove.each do |name|
268
+ puts Colors.blue("\tdeleting scheduled action #{name}...")
269
+ @aws.delete_scheduled_action({
270
+ auto_scaling_group_name: group.name,
271
+ scheduled_action_name: name
272
+ })
273
+ end
274
+
275
+ # update or create all scheduled actions that have changed in local config
276
+ group.scheduled.each do |name, config|
277
+ if update.include?(name)
278
+ puts Colors.blue("\tupdating scheduled action #{name}...")
279
+ @aws.put_scheduled_update_group_action({
280
+ auto_scaling_group_name: group.name,
281
+ scheduled_action_name: name,
282
+ start_time: config.start,
283
+ end_time: config.end,
284
+ recurrence: config.recurrence,
285
+ min_size: config.min,
286
+ max_size: config.max,
287
+ desired_capacity: config.desired
288
+ })
289
+ end
290
+ end
291
+ end
292
+
293
+ # Internal: Update the scaling policies for an autoscaling group
294
+ #
295
+ # group - the group the scaling policies belong to
296
+ # remove - the names of the scaling policies to remove
297
+ # update - the names of the scaling policies to update
298
+ def update_scaling_policies(group, remove, update)
299
+ # remove any unmanaged scaling policies
300
+ remove.each do |name|
301
+ @aws.delete_policy({
302
+ auto_scaling_group_name: group.name,
303
+ policy_name: name
304
+ })
305
+ end
306
+
307
+ # update or create all policies that have changed in local config
308
+ group.policies.each do |name, config|
309
+ if update.include?(name)
310
+ puts Colors.blue("\tupdating scaling policy #{name}...")
311
+ @aws.put_scaling_policy({
312
+ auto_scaling_group_name: group.name,
313
+ policy_name: name,
314
+ adjustment_type: config.adjustment_type,
315
+ min_adjustment_step: config.min_adjustment,
316
+ scaling_adjustment: config.adjustment,
317
+ cooldown: config.cooldown
318
+ })
319
+ end
320
+ end
321
+ end
322
+
323
+ # Internal: Update the cloudwatch alarms for a scaling policy
324
+ #
325
+ # policy - the policy config the alarms belong to
326
+ # policy_arn - the arn of the policy for which to update arns
327
+ # remove - the names of the alarms to remove
328
+ # update - the names of the alarms to create or update
329
+ def update_alarms(policy, policy_arn, remove, update)
330
+ @cloudwatch.delete_alarms({
331
+ alarm_names: remove
332
+ })
333
+
334
+ policy.alarms.each do |name, config|
335
+ if update.include?(name)
336
+ puts Colors.blue("\tupdating cloudwatch alarm #{name}...")
337
+ @cloudwatch.put_metric_alarm({
338
+ alarm_name: config.name,
339
+ alarm_description: config.description,
340
+ actions_enabled: config.actions_enabled,
341
+ metric_name: config.metric,
342
+ namespace: config.namespace,
343
+ statistic: config.statistic,
344
+ dimensions: config.dimensions.map { |k, v| { name: k, value: v } },
345
+ period: config.period,
346
+ unit: config.unit,
347
+ evaluation_periods: config.evaluation_periods,
348
+ threshold: config.threshold,
349
+ comparison_operator: config.comparison,
350
+ ok_actions: config.action_states.include?("ok") ? [policy_arn] : nil,
351
+ alarm_actions: config.action_states.include?("alarm") ? [policy_arn] : nil,
352
+ insufficient_data_actions: config.action_states.include?("insufficient-data") ? [policy_arn] : nil
353
+ }.reject { |k, v| v == nil })
354
+ end
355
+ end
356
+ end
357
+
358
+ end
359
+ end
360
+ end