build-cloud 0.0.20 → 0.0.21

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
  SHA1:
3
- metadata.gz: a52e42a2f9f37f73e8cb750ff3d71454eed4a51f
4
- data.tar.gz: 4445d3159b1cfbcd1808ca5bc182f65a97a4c54a
3
+ metadata.gz: a9680bce5369f6136331f066c50a5df61e71a95f
4
+ data.tar.gz: a8f948cc45624a78d9a52aacb013c834733e4775
5
5
  SHA512:
6
- metadata.gz: f53a03aaecae0169ca975f7a79bf88b4ba0e206fcef9f41b2556a7863cdea98dfdc5f10af358899bbab3da5381d4df008e4060654e6cdc4f66af03843d7e4234
7
- data.tar.gz: dd3fb1f7f7fc8da8c70b78ab8afaedc25f18d462e0ee0da0a6bd7432728b944b3afa55de8e49b262bd641c0b622b5d8d572896d796d59c221fd4f64ddb5b1675
6
+ metadata.gz: 4f749a75d4ad5c186260f41efe4909ca475762de2a42487dbb433c55ab13d4766da153579e0dba272e744935bff7530b2beccdc7e81ded1e6db7f35c58c983f0
7
+ data.tar.gz: 242db7ff37fa2a8c3e282424fd3faea53c32c3c865efec02053bb7bfd6f316a64f8c598cebb3827b824adef41dd6d44f1fe32a030dba8841b30f95157b1a791c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ 2016-07-22 - version 0.0.21 - Add support for replacing changed ASG Launch Configurations
4
+
3
5
  2016-07-08 - version 0.0.20 - Add support for creating Users, Groups and lifecycle management of their policies. Add support for creating and deleting custom Managed Policies, no lifecycle support for policy versions. Improve IAM role support, lifecycle support for policies: now removes and updates role policies if they change/removed. Add policy lifecycle to S3 buckets.
4
6
 
5
7
  2016-06-17 - version 0.0.19 - add sqs support
data/build-cloud.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "build-cloud"
7
- spec.version = "0.0.20"
7
+ spec.version = "0.0.21"
8
8
  spec.authors = ["The Scale Factory"]
9
9
  spec.email = ["info@scalefactory.com"]
10
10
  spec.summary = %q{Tools for building resources in AWS}
@@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "rake"
21
21
 
22
22
  spec.add_dependency "fog", ">=1.22.0"
23
+ spec.add_dependency "fog-aws", ">=0.10.0"
23
24
  spec.add_dependency "pry", ">=0.9.12.6"
24
25
  end
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require "base64"
2
3
 
3
4
  class BuildCloud::LaunchConfiguration
4
5
 
@@ -21,10 +22,6 @@ class BuildCloud::LaunchConfiguration
21
22
  end
22
23
 
23
24
  def create
24
-
25
- return if exists?
26
-
27
- @log.info( "Creating launch configuration #{@options[:id]}" )
28
25
 
29
26
  options = @options.dup
30
27
 
@@ -80,15 +77,152 @@ class BuildCloud::LaunchConfiguration
80
77
 
81
78
  end
82
79
 
83
- launch_config = @as.configurations.new( options )
84
- launch_config.save
80
+ unless exists?
81
+ @log.info( "Creating launch configuration #{@options[:id]}" )
82
+ launch_config = @as.configurations.new( options )
83
+ launch_config.save
84
+ else
85
+ fog_options = {}
86
+ fog_object.attributes.each do |k, v|
87
+ if v.nil?
88
+ next
89
+ elsif k == :id
90
+ # launch config id has time stamp appended.
91
+ # fog_object returns the latest matching launch config
92
+ # but to do a match for changed values we need to modify
93
+ fog_options[k] = options[:id]
94
+ elsif k == :user_data
95
+ # need to decode the user_data
96
+ fog_options[k] = Base64.decode64(v)
97
+ elsif k == :block_device_mappings
98
+ # Some data jogging to get the block device mapping options into the
99
+ # same format that fog_object returns them.
100
+ fog_options[:block_device_mappings] = []
101
+ v.each do |mapping|
102
+ block_device = {}
103
+ for key, value in mapping
104
+ keyname = format("#{key}")
105
+ if keyname == 'Ebs'
106
+ for ebs_key, ebs_value in value
107
+ ebs_value = Integer(ebs_value) rescue ebs_value
108
+ block_device.merge!({ :"Ebs.#{ebs_key}" => ebs_value })
109
+ end
110
+ else
111
+ block_device.merge!({ :"#{keyname}" => value })
112
+ end
113
+ end
114
+ fog_options[k].push(block_device)
115
+ end
116
+ elsif k == :instance_monitoring
117
+ # instance monitoring value needs to be tweaked
118
+ fog_options[k] = {:enabled => v}
119
+ elsif k == :classic_link_security_groups and v.empty?
120
+ next
121
+ elsif k == :created_at or k == :arn
122
+ next
123
+ else
124
+ fog_options[k] = v
125
+ end
126
+ end
127
+
128
+ # Duplicate options and then ensure that some defaults are present if missing
129
+ munged_options = options.dup
130
+
131
+ # default ebs_optimized is false
132
+ if munged_options[:ebs_optimized].nil?
133
+ munged_options[:ebs_optimized] = false
134
+ end
135
+
136
+ if munged_options[:instance_monitoring].nil?
137
+ munged_options[:instance_monitoring] = {:enabled => true}
138
+ end
139
+
140
+ differences = Hash[*(
141
+ (fog_options.size > munged_options.size) \
142
+ ? fog_options.to_a - munged_options.to_a \
143
+ : munged_options.to_a - fog_options.to_a
144
+ ).flatten]
145
+
146
+ @log.debug("Fog options: #{fog_options.inspect}")
147
+ @log.debug("Munged options: #{munged_options.inspect}")
148
+
149
+ unless fog_options == munged_options
150
+ @log.debug("Differences between fog and options is: #{differences}")
151
+
152
+ @log.info("Updating Launch Configuration #{fog_object.id}")
153
+ # Now for some useful messaging
154
+ differences.each do |k,v|
155
+ @log.info(" ... updating #{k}")
156
+ end
157
+
158
+ # create new configuration
159
+ # update id to have current unix timestamp
160
+ munged_options = options.dup
161
+ munged_options[:id] = "#{munged_options[:id]}_#{Time.now.to_i}"
162
+ current_launch_id = "#{fog_object.id}"
163
+
164
+ dsl = @as.describe_launch_configurations( { 'LaunchConfigurationNames' => munged_options[:id]} )
165
+ dsl = dsl.body['DescribeLaunchConfigurationsResult']['LaunchConfigurations']
166
+ if dsl.empty?
167
+ @log.info("Creating launch configuration #{munged_options[:id]}")
168
+
169
+ # create new fog object
170
+ launch_config = @as.configurations.new( munged_options )
171
+ launch_config.save
172
+
173
+ # update any associated ASG's
174
+ asgs = @as.describe_auto_scaling_groups().body['DescribeAutoScalingGroupsResult']['AutoScalingGroups']
175
+ asgs.each do |asg|
176
+ @log.debug("Checking ASG #{asg['AutoScalingGroupName']}")
177
+ if asg['LaunchConfigurationName'] == current_launch_id
178
+ @log.info("Updating ASG #{asg['AutoScalingGroupName']} ")
179
+ @as.update_auto_scaling_group(asg['AutoScalingGroupName'], {'LaunchConfigurationName' => munged_options[:id]})
180
+ end
181
+ end
182
+
183
+ # delete old configuration
184
+ @log.info("Deleting launch configuration #{current_launch_id}")
185
+ @as.delete_launch_configuration( current_launch_id )
186
+ else
187
+ @log.error("Launch Configuration #{munged_options[:id]} already exists!")
188
+ @log.error("Not updating ASG Launch Configuration #{options[:id]}")
189
+ end
190
+ end
191
+
192
+ end
85
193
 
86
194
  @log.debug( launch_config.inspect )
87
195
 
88
196
  end
89
197
 
90
198
  def read
91
- @as.configurations.select { |lc| lc.id == @options[:id] }.first
199
+ # ASG Launch configs are now created with a _unix_timestamp at the end
200
+ # identify matching and return the one which was last created
201
+ confs = @as.configurations.select { |lc| lc.id.start_with?(@options[:id]) }
202
+
203
+ confs.each do |conf|
204
+ begin
205
+ id = conf.id.dup
206
+ # if ID's match exactly, then keep in list
207
+ next if id == @options[:id]
208
+ sliced = id.slice!("#{@options[:id]}_")
209
+ # if we can't slice off the id and _ then assume it's not one of our LC
210
+ raise "No initial time match" if sliced.nil?
211
+ time_slice = id.to_i
212
+ # if converted int and string don't match, then it's not a match
213
+ raise "No true int derived" unless time_slice.to_s == id
214
+ # if we have an int that is 0, then it's not a match
215
+ raise "Time int derived as 0" if time_slice == 0
216
+ dt = Time.at(time_slice)
217
+ @log.debug("Derived time stamp for #{conf.id} is #{dt}")
218
+ rescue
219
+ @log.debug("#{conf.id} is not a match")
220
+ @log.debug("Details: #{$!}")
221
+ confs.delete(conf)
222
+ end
223
+ end
224
+
225
+ confs.max_by {|o| o.created_at}
92
226
  end
93
227
 
94
228
  alias_method :fog_object, :read
@@ -104,4 +238,3 @@ class BuildCloud::LaunchConfiguration
104
238
  end
105
239
 
106
240
  end
107
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: build-cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Scale Factory
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-12 00:00:00.000000000 Z
11
+ date: 2016-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.22.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: fog-aws
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pry
57
71
  requirement: !ruby/object:Gem::Requirement