@@ -83,10 +83,9 @@ def do_cmd(cmd)
8383 print "#{ result } \n "
8484end
8585
86- def update_eb_environment ( version_label = nil )
86+ def update_or_create_eb_environment_options_to_remove
8787 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?
88+
9089 rails_options_keys = rails_options . map { |opt | opt [ :option_name ] }
9190 env_config = AWS . elastic_beanstalk . client . describe_configuration_settings ( :application_name => ENV [ 'APP_NAME' ] , :environment_name => ENV [ 'ENVIRONMENT' ] )
9291 options_to_remove = env_config [ :configuration_settings ] . first [ :option_settings ] . select do |opt |
@@ -96,23 +95,48 @@ def update_eb_environment(version_label = nil)
9695 options_to_remove . each { |opt | puts "(Info) options removed:#{ opt [ :option_name ] } =#{ opt [ :value ] } " }
9796
9897 options_to_remove . each { |opt | opt . delete ( :value ) }
98+ options_to_remove
99+ end
100+
101+ def update_eb_environment ( options = { } )
102+ version_label = options [ :version_label ]
103+ auto_create = options [ :auto_create ]
99104
105+ envs = AWS . elastic_beanstalk . client . describe_environments ( :application_name => ENV [ 'APP_NAME' ] , :environment_names => [ ENV [ 'ENVIRONMENT' ] ] )
106+ puts "envs[:environments] = #{ envs [ :environments ] } "
107+ env = envs [ 0 ]
108+ unless env . nil? || env [ :status ] =="Terminated"
100109 if version_label . nil?
101110 AWS . elastic_beanstalk . client . update_environment ( :environment_name => ENV [ 'ENVIRONMENT' ] ,
102111 :option_settings => all_options ,
103- :options_to_remove => options_to_remove )
112+ :options_to_remove => update_or_create_eb_environment_options_to_remove )
104113 else
105114 AWS . elastic_beanstalk . client . update_environment ( :environment_name => ENV [ 'ENVIRONMENT' ] ,
106115 :version_label => @version_label ,
107116 :option_settings => all_options ,
108- :options_to_remove => options_to_remove )
117+ :options_to_remove => update_or_create_eb_environment_options_to_remove )
109118 end
110119 new_env_config = AWS . elastic_beanstalk . client . describe_configuration_settings ( :application_name => ENV [ 'APP_NAME' ] , :environment_name => ENV [ 'ENVIRONMENT' ] )
111120 puts "New env config"
112121 new_env_config [ :configuration_settings ] . first [ :option_settings ] . each { |opt | puts "(Info) #{ opt [ :option_name ] } =#{ opt [ :value ] } " if opt [ :namespace ] == "aws:elasticbeanstalk:application:environment" }
113122 puts " ----- END ----- "
114- else
115- puts "(Warning) Environment \" #{ ENV [ 'ENVIRONMENT' ] } \" doesn't exist"
123+ else
124+ if auto_create
125+ if version_label . nil?
126+ AWS . elastic_beanstalk . client . create_environment ( :application_name => ENV [ 'APP_NAME' ] ,
127+ :environment_name => ENV [ 'ENVIRONMENT' ] ,
128+ :solution_stack_name => ENV [ 'STACK' ] ,
129+ :option_settings => all_options )
130+ else
131+ AWS . elastic_beanstalk . client . create_environment ( :application_name => ENV [ 'APP_NAME' ] ,
132+ :environment_name => ENV [ 'ENVIRONMENT' ] ,
133+ :solution_stack_name => ENV [ 'STACK' ] ,
134+ :version_label => @version_label ,
135+ :option_settings => all_options )
136+ end
137+ else
138+ puts "(Warning) Environment \" #{ ENV [ 'ENVIRONMENT' ] } \" doesn't exist"
139+ end
116140 end
117141end
118142
@@ -288,7 +312,7 @@ def print_env
288312
289313 eb = AWS . elastic_beanstalk
290314 eb . client . create_application_version aws_app_opt
291- update_eb_environment ( @version_label )
315+ update_eb_environment ( { :version_label => @version_label , :auto_create => true } )
292316 end
293317
294318 desc "deploy to elastic beanstalk"
0 commit comments