Class: ElasticBeans::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/environment.rb,
lib/elastic_beans/environment/exec.rb,
lib/elastic_beans/environment/worker.rb,
lib/elastic_beans/environment/scheduler.rb,
lib/elastic_beans/environment/webserver.rb

Overview

An Elastic Beanstalk environment which may or may not exist.

This is an abstract class; use the provided factories in this class or use a subclass (contained within this namespace) directly.

Direct Known Subclasses

Exec, Scheduler, Webserver, Worker

Defined Under Namespace

Classes: Exec, Scheduler, Webserver, Worker

Constant Summary collapse

TEMPLATE_NAME =

:category: Internal

"base"
TIER_NAME =

:category: Internal

"Webserver"
TIER_TYPE =

:category: Internal

"Standard"
WORKER_TEMPLATE_NAME_PATTERN =

:category: Internal

/worker-(?<queue>\w+)/
WAIT_TIMEOUT =

:category: Internal

3600
DEGRADED_TIMEOUT =

:category: Internal

90

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, application:, elastic_beanstalk:, **_) ⇒ Environment

Returns a new instance of Environment.



95
96
97
98
99
# File 'lib/elastic_beans/environment.rb', line 95

def initialize(name, application:, elastic_beanstalk:, **_)
  @name = name
  @application = application
  @elastic_beanstalk = elastic_beanstalk
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/elastic_beans/environment.rb', line 30

def name
  @name
end

Class Method Details

.new_by_type(type, application:, **args) ⇒ Object

Create a new environment of a particular type. See the particular subclass being created for details on arguments. The name of the environment will be the application name, joined with the type (and the queue in the case of a worker).

Raises an error if the type is not recognized.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/elastic_beans/environment.rb', line 38

def self.new_by_type(type, application:, **args)
  name = "#{application.name}-#{type}"
  case type
  when "exec"
    ElasticBeans::Environment::Exec.new(name, application: application, **args)
  when "scheduler"
    ElasticBeans::Environment::Scheduler.new(name, application: application, **args)
  when "webserver"
    ElasticBeans::Environment::Webserver.new(name, application: application, **args)
  when "worker"
    queue_name = args[:queue] || "default"
    name = "#{name}-#{queue_name}"
    ElasticBeans::Environment::Worker.new(name, application: application, **args)
  else
    raise UnknownEnvironmentType.new(environment_type: type)
  end
end

.new_from_existing(environment, application:, **args) ⇒ Object

Create a representation of an existing environment. Uses the environment to discover the appropriate type (and queue, in the case of a worker). If the environment type is not recognized, the tier will be inspected. If the tier is worker, then the “default” queue will be assumed.

Raises an error if the environment type or tier is not recognized.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/elastic_beans/environment.rb', line 62

def self.new_from_existing(environment, application:, **args)
  app_name_pattern = /#{Regexp.escape(application.name)}/
  case environment.environment_name
  when /\A#{app_name_pattern}-exec\z/
    ElasticBeans::Environment::Exec.new(environment.environment_name, application: application, **args)
  when /\A#{app_name_pattern}-scheduler\z/
    ElasticBeans::Environment::Scheduler.new(environment.environment_name, application: application, **args)
  when /\A#{app_name_pattern}-webserver\z/
    ElasticBeans::Environment::Webserver.new(environment.environment_name, application: application, **args)
  when /\A#{app_name_pattern}-#{WORKER_TEMPLATE_NAME_PATTERN}\z/
    match = WORKER_TEMPLATE_NAME_PATTERN.match(environment.environment_name)
    ElasticBeans::Environment::Worker.new(
      environment.environment_name,
      application: application,
      queue: match[:queue],
      **args,
    )
  else
    case environment.tier.name
    when "WebServer"
      ElasticBeans::Environment::Webserver.new(environment.environment_name, application: application, **args)
    when "Worker"
      ElasticBeans::Environment::Worker.new(
        environment.environment_name,
        application: application,
        **args,
      )
    else
      raise UnknownEnvironmentType.new(environment_type: environment.tier.name)
    end
  end
end

Instance Method Details

#cnameObject

Returns the Elastic Beanstalk CNAME, set for a webserver environment.

Raises an error if the environment does not exist.



214
215
216
217
218
219
220
# File 'lib/elastic_beans/environment.rb', line 214

def cname
  environment = environment_description
  unless environment
    raise MissingEnvironmentError.new(environment: self, application: application)
  end
  environment.cname
end

#create(version:, tags: {}, blocking: true) ⇒ Object

Creates the environment in Elastic Beanstalk. Blocks until the environment is Ready, unless blocking is falsy.

Raises an error if the configuration template for this environment does not exist. Raises an error if the environment is not healthy.



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
# File 'lib/elastic_beans/environment.rb', line 106

def create(version:, tags: {}, blocking: true)
  begin
    elastic_beanstalk.describe_configuration_settings(
      application_name: application.name,
      template_name: template_name,
    ).configuration_settings.empty?
  rescue ::Aws::ElasticBeanstalk::Errors::InvalidParameterValue
    raise MissingConfigurationError
  end

  tags_list = tags.map { |k, v| {key: k, value: v} }
  elastic_beanstalk.create_environment(
    application_name: application.name,
    environment_name: name,
    solution_stack_name: configuration_template.solution_stack_name,
    tier: {name: tier_name, type: tier_type},
    template_name: template_name,
    version_label: version,
    tags: tags_list,
  )

  wait_environment(wait_status: "Launching", wait_health_status: ["Unknown", "Pending"]) if blocking
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
rescue ::Aws::Errors::ServiceError => e
  raise UnhealthyEnvironmentError.new(
    environment_name: name,
    cause: e,
  )
end

#deploy_version(version_label, blocking: true) ⇒ Object

Deploys the specified version label to the environment. Blocks until the environment is Ready, unless blocking is falsy.

Raises an error if the environment is not healthy.



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/elastic_beans/environment.rb', line 142

def deploy_version(version_label, blocking: true)
  elastic_beanstalk.update_environment(
    environment_name: name,
    version_label: version_label,
  )

  wait_environment(wait_status: "Updating", wait_health_status: "Info") if blocking
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
rescue ::Aws::Errors::ServiceError => e
  raise UnhealthyEnvironmentError.new(environment_name: name, cause: e)
end

#instance_idsObject

Returns the instance IDs that are part of this environment.

Raises an error if the environment does not exist.



225
226
227
228
229
# File 'lib/elastic_beans/environment.rb', line 225

def instance_ids
  environment_resources.instances.map(&:id)
rescue ::Aws::ElasticBeanstalk::Errors::InvalidParameterValue
  raise MissingEnvironmentError.new(environment: self, application: application)
end

#restart(blocking: true) ⇒ Object

Restarts the environment application servers. Blocks until the environment is Ready, unless blocking is falsy.

Raises an error if the environment is not healthy.



160
161
162
163
164
165
166
167
168
# File 'lib/elastic_beans/environment.rb', line 160

def restart(blocking: true)
  elastic_beanstalk.restart_app_server(environment_name: name)
  wait_environment(wait_status: "Updating", wait_health_status: "Info") if blocking
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
rescue ::Aws::ElasticBeanstalk::Errors::InvalidParameterValue
  raise MissingEnvironmentError.new(environment: self, application: application)
end

#scale(min_size:, max_size:, blocking: true) ⇒ Object

Updates the environment configuration to change the minimum and maximum size of the autoscaling group. Blocks until the environment is Ready, unless blocking is falsy.

Raises an error if the environment is not healthy.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/elastic_beans/environment.rb', line 174

def scale(min_size:, max_size:, blocking: true)
  option_settings = [
    {namespace: "aws:autoscaling:asg", option_name: "MinSize", value: min_size},
    {namespace: "aws:autoscaling:asg", option_name: "MaxSize", value: max_size},
  ]
  elastic_beanstalk.update_environment(
    environment_name: name,
    option_settings: option_settings,
  )
  wait_environment(wait_status: "Updating", wait_health_status: "Info") if blocking
rescue ::Aws::ElasticBeanstalk::Errors::InvalidParameterValue
  raise MissingEnvironmentError.new(environment: self, application: application)
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
end

#statusObject

Returns the status of the environment in Elastic Beanstalk.

Raises an error if the environment does not exist.



234
235
236
237
238
239
240
# File 'lib/elastic_beans/environment.rb', line 234

def status
  environment = environment_description
  unless environment
    raise MissingEnvironmentError.new(environment: self, application: application)
  end
  environment.status
end

#update_configuration(configuration_template, blocking: true) ⇒ Object

Updates the environment configuration with the option settings from the given configuration template. Handy when the configuration template has been updated and you want those changes to take effect immediately. Blocks until the environment is Ready, unless blocking is falsy.

Raises an error if the environment is not healthy.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/elastic_beans/environment.rb', line 196

def update_configuration(configuration_template, blocking: true)
  elastic_beanstalk.update_environment(
    environment_name: name,
    option_settings: configuration_template.option_settings,
    options_to_remove: configuration_template.options_to_remove,
    solution_stack_name: configuration_template.solution_stack_name,
  )
  wait_environment(wait_status: "Updating", wait_health_status: "Info") if blocking
rescue ::Aws::ElasticBeanstalk::Errors::InvalidParameterValue
  raise MissingEnvironmentError.new(environment: self, application: application)
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
end