Class: JackAndTheElasticBeanstalk::EB::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/jack_and_the_elastic_beanstalk/eb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_name:, logger:, client:, environment_name:) ⇒ Environment

Returns a new instance of Environment.



67
68
69
70
71
72
73
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 67

def initialize(application_name:, logger:, client:, environment_name:)
  @application_name = application_name
  @logger = logger
  @client = client
  @environment_name = environment_name
  @timeout = 600
end

Instance Attribute Details

#application_nameObject (readonly)

Returns the value of attribute application_name.



61
62
63
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 61

def application_name
  @application_name
end

#clientObject (readonly)

Returns the value of attribute client.



63
64
65
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 63

def client
  @client
end

#environment_nameObject (readonly)

Returns the value of attribute environment_name.



64
65
66
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 64

def environment_name
  @environment_name
end

#loggerObject (readonly)

Returns the value of attribute logger.



62
63
64
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 62

def logger
  @logger
end

#timeoutObject

Returns the value of attribute timeout.



65
66
67
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 65

def timeout
  @timeout
end

Instance Method Details

#configuration_settingObject



90
91
92
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 90

def configuration_setting
  @configuration_setting ||= client.describe_configuration_settings(application_name: application_name, environment_name: environment_name).configuration_settings.first
end

#dataObject



84
85
86
87
88
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 84

def data
  @data ||= client.describe_environments(application_name: application_name).environments.find {|env|
    env.environment_name == environment_name
  }
end

#data=(v) ⇒ Object



80
81
82
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 80

def data=(v)
  @data = v
end

#deploy(label:) ⇒ Object



245
246
247
248
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 245

def deploy(label:)
  client.update_environment(environment_id: environment_id,
                            version_label: label)
end

#destroyObject



223
224
225
226
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 223

def destroy
  logger.info("jeb::eb") { "Terminating #{environment_name}..." }
  client.terminate_environment(environment_id: environment_id)
end

#ensure_version!(expected_label:) ⇒ Object



250
251
252
253
254
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 250

def ensure_version!(expected_label:)
  unless data.version_label == expected_label
    raise "Unexpected version label: expected=#{expected_label}, actual=#{data.version_label}"
  end
end

#env_varsObject



94
95
96
97
98
99
100
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 94

def env_vars
  configuration_setting.option_settings.each.with_object({}) do |option, hash|
    if option.namespace == "aws:elasticbeanstalk:application:environment"
      hash[option.option_name] = option.value
    end
  end
end

#environment_idObject



219
220
221
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 219

def environment_id
  data.environment_id
end

#healthObject



228
229
230
231
232
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 228

def health
  logger.info("jeb::eb") { "Downloading health data on #{environment_name}..." }

  client.describe_environment_health(environment_id: environment_id, attribute_names: ["All"])
end

#refreshObject



75
76
77
78
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 75

def refresh
  @data = nil
  @configuration_setting = nil
end

#resourcesObject



234
235
236
237
238
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 234

def resources
  logger.info("jeb::eb") { "Downloading resources on #{environment_name}..."}

  client.describe_environment_resources(environment_id: environment_id)
end

#restartObject



240
241
242
243
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 240

def restart
  logger.info("jeb::eb") { "Restarting #{environment_name}..." }
  client.restart_app_server(environment_id: environment_id)
end

#scaleObject



177
178
179
180
181
182
183
184
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 177

def scale
  option_settings = configuration_setting.option_settings

  min = option_settings.find {|option| option.namespace == "aws:autoscaling:asg" && option.option_name == "MinSize" }.value.to_i
  max = option_settings.find {|option| option.namespace == "aws:autoscaling:asg" && option.option_name == "MaxSize" }.value.to_i

  min...max
end

#set_env_vars(env) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 102

def set_env_vars(env)
  need_update = env.all? {|key, value|
    if value
      env_vars[key] == value
    else
      !env_vars.key?(key)
    end
  }

  if need_update
    logger.info("jeb::eb") { "Env vars looks like identical; skip" }
  else
    logger.info("jeb::eb") { "Updating environment variables" }

    options_to_update = []
    options_to_remove = []

    env.each do |key, value|
      if value
        options_to_update << {
          namespace: "aws:elasticbeanstalk:application:environment",
          option_name: key.to_s,
          value: value.to_s
        }
      else
        options_to_remove << {
          namespace: "aws:elasticbeanstalk:application:environment",
          option_name: key.to_s
        }
      end
    end

    client.update_environment(application_name: application_name,
                              environment_name: environment_name,
                              option_settings: options_to_update,
                              options_to_remove: options_to_remove)

    refresh
  end
end

#set_scale(scale) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 190

def set_scale(scale)
  if scale.is_a?(Integer)
    scale = scale...scale
  end

  if self.scale == scale
    logger.info("jeb::eb") { "New scale is identical to current scale; skip" }
  else
    logger.info("jeb::eb") { "Scaling to #{scale}" }

    client.update_environment(application_name: application_name,
                              environment_name: environment_name,
                              option_settings: [
                                {
                                  namespace: "aws:autoscaling:asg",
                                  option_name: "MinSize",
                                  value: scale.begin.to_s
                                },
                                {
                                  namespace: "aws:autoscaling:asg",
                                  option_name: "MaxSize",
                                  value: scale.end.to_s
                                }
                              ])

    refresh
  end
end

#statusObject



186
187
188
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 186

def status
  data.status
end

#synchronize_update(timeout: self.timeout) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/jack_and_the_elastic_beanstalk/eb.rb', line 143

def synchronize_update(timeout: self.timeout)
  logger.info("jeb::eb") { "Synchronizing update started... (timeout = #{timeout})" }

  yield if block_given?

  start = Time.now
  wait = 1

  while true
    refresh
    st = status

    logger.info("jeb::eb") { "#{environment_name}:: status=#{st}" }

    case st
    when "Ready"
      break
    when "Updating", "Launching", "Aborting"
      # ok
    else
      raise "Unexpected status: #{st}"
    end

    if Time.now - start > timeout
      raise "Timeout exceeded"
    end

    sleep wait
    wait = [wait*2, 30].min
  end

  logger.info("jeb::eb") { "Synchronized in #{(Time.now - start).to_i} seconds" }
end