Skip to content

Commit 32d3c83

Browse files
committed
Fix double update on deploy, refactor update env methods
1 parent 21ebd26 commit 32d3c83

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

lib/tasks/eb_fast_deploy.rb

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ def do_cmd(cmd)
8383
print "#{result}\n"
8484
end
8585

86+
def update_eb_environment(version_label = nil)
87+
rails_default_options_names = [:AWS_ACCESS_KEY_ID, :AWS_SECRET_KEY, :BUNDLE_WITHOUT, :PARAM1, :PARAM2, :RACK_ENV, :RAILS_SKIP_ASSET_COMPILATION, :RAILS_SKIP_MIGRATIONS]
88+
envs = AWS.elastic_beanstalk.client.describe_environments(:application_name=>ENV['APP_NAME'], :environment_names => [ENV['ENVIRONMENT']])
89+
unless envs[:environments].empty?
90+
rails_options_keys = rails_options.map{|opt| opt[:option_name]}
91+
env_config = AWS.elastic_beanstalk.client.describe_configuration_settings(:application_name=>ENV['APP_NAME'], :environment_name => ENV['ENVIRONMENT'])
92+
options_to_remove = env_config[:configuration_settings].first[:option_settings].select do |opt|
93+
opt[:namespace] == "aws:elasticbeanstalk:application:environment" and !rails_options_keys.include?(opt[:option_name]) and !rails_default_options_names.include?(opt[:option_name].to_sym )
94+
end
95+
96+
options_to_remove.each{|opt| puts "(Info) options removed:#{opt[:option_name]}=#{opt[:value]}" }
97+
98+
options_to_remove.each{|opt| opt.delete(:value) }
99+
100+
if version_label.nil?
101+
AWS.elastic_beanstalk.client.update_environment(:environment_name => ENV['ENVIRONMENT'],
102+
:option_settings => all_options,
103+
:options_to_remove => options_to_remove )
104+
else
105+
AWS.elastic_beanstalk.client.update_environment(:environment_name => ENV['ENVIRONMENT'],
106+
:option_settings => all_options,
107+
:options_to_remove => options_to_remove )
108+
end
109+
new_env_config = AWS.elastic_beanstalk.client.describe_configuration_settings(:application_name=>ENV['APP_NAME'], :environment_name => ENV['ENVIRONMENT'])
110+
puts "New env config"
111+
new_env_config[:configuration_settings].first[:option_settings].each {|opt| puts "(Info) #{opt[:option_name]}=#{opt[:value]}" if opt[:namespace] == "aws:elasticbeanstalk:application:environment" }
112+
puts " ----- END ----- "
113+
else
114+
puts "(Warning) Environment \"#{ ENV['ENVIRONMENT'] }\" doesn't exist"
115+
end
116+
end
117+
86118
def check_required_variables!(variables_array)
87119
variables_array.each do |opt|
88120
raise "(Error) #{opt} not defined" if ENV[opt].nil?
@@ -237,30 +269,23 @@ def print_env
237269

238270
aws_app_opt = {
239271
application_name: ENV['APP_NAME'],
240-
# description: "deploy",
241272
source_bundle: {
242273
s3_bucket: ENV['AWS_DEPLOY_BUCKET'],
243274
s3_key: @deploy_zip_filename
244275
},
245276
version_label: @version_label
246277
}
247278

248-
aws_env_opt = {
249-
environment_name: ENV['ENVIRONMENT'],
250-
version_label: @version_label
251-
}
252-
253279
eb = AWS.elastic_beanstalk
254280
eb.client.create_application_version aws_app_opt
255-
eb.client.update_environment aws_env_opt
281+
update_eb_environment( @version_label )
256282
end
257283

258284
desc "deploy to elastic beanstalk"
259285
task :deploy => [
260286
:assets,
261287
:bundle_pack,
262288
:upload,
263-
:update_eb_environment,
264289
:create_and_deploy_version
265290
] do
266291
set_vars
@@ -315,30 +340,7 @@ def app_exists?
315340
desc "update environment on Elastic Beanstalk"
316341
task :update_eb_environment => :create_eb_environment do
317342
set_vars
318-
RAILS_DEFAULT_OPTIONS_KEYS = [:AWS_ACCESS_KEY_ID, :AWS_SECRET_KEY, :BUNDLE_WITHOUT, :PARAM1, :PARAM2, :RACK_ENV, :RAILS_SKIP_ASSET_COMPILATION, :RAILS_SKIP_MIGRATIONS]
319-
envs = AWS.elastic_beanstalk.client.describe_environments(:application_name=>ENV['APP_NAME'], :environment_names => [ENV['ENVIRONMENT']])
320-
unless envs[:environments].empty?
321-
rails_options_keys = rails_options.map{|opt| opt[:option_name]}
322-
env_config = AWS.elastic_beanstalk.client.describe_configuration_settings(:application_name=>ENV['APP_NAME'], :environment_name => ENV['ENVIRONMENT'])
323-
options_to_remove = env_config[:configuration_settings].first[:option_settings].select do |opt|
324-
opt[:namespace] == "aws:elasticbeanstalk:application:environment" and !rails_options_keys.include?(opt[:option_name]) and !RAILS_DEFAULT_OPTIONS_KEYS.include?(opt[:option_name].to_sym )
325-
end
326-
327-
options_to_remove.each{|opt| puts "(Info) options removed:#{opt[:option_name]}=#{opt[:value]}" }
328-
329-
options_to_remove.each{|opt| opt.delete(:value) }
330-
331-
AWS.elastic_beanstalk.client.update_environment(:environment_name => ENV['ENVIRONMENT'],
332-
:option_settings => all_options,
333-
:options_to_remove => options_to_remove )
334-
335-
new_env_config = AWS.elastic_beanstalk.client.describe_configuration_settings(:application_name=>ENV['APP_NAME'], :environment_name => ENV['ENVIRONMENT'])
336-
puts "New env config"
337-
new_env_config[:configuration_settings].first[:option_settings].each {|opt| puts "(Info) #{opt[:option_name]}=#{opt[:value]}" if opt[:namespace] == "aws:elasticbeanstalk:application:environment" }
338-
puts " ----- END ----- "
339-
else
340-
puts "(Warning) Environment \"#{ ENV['ENVIRONMENT'] }\" doesn't exist"
341-
end
343+
update_eb_environment
342344
end
343345
end
344346

0 commit comments

Comments
 (0)