Class: ElasticBeans::ConfigurationTemplate

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

Overview

An Elastic Beanstalk saved configuration (also known as a configuration template) 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

Base

Defined Under Namespace

Classes: Base, Exec, Scheduler, Webserver, Worker

Constant Summary collapse

SOLUTION_STACK_PATTERN =

Matches the latest available Ruby Puma solution stack when one was not specified.

/\A64bit Amazon Linux (?<date>\d+(\.\d+)*) v(?<version>\d+(\.\d+)*) running Ruby (?<ruby_version>\d+(\.\d+)*) \(Puma\)\z/
SOURCE_CONFIGURATION =

The source configuration for new configuration templates. Set to ElasticBeans::ConfigurationTemplate::Base to include all custom configuration that has already been performed.

{template_name: "base"}
WORKER_TEMPLATE_NAME_PATTERN =

:category: Internal

/\Aworker-(?<queue>\w+)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

:category: Internal



68
69
70
71
72
73
74
75
76
77
# File 'lib/elastic_beans/configuration_template.rb', line 68

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.



22
23
24
# File 'lib/elastic_beans/configuration_template.rb', line 22

def name
  @name
end

Class Method Details

.new_by_type(type, **args) ⇒ Object

Create a new configuration template of a particular type. See the particular subclass being created for details on arguments.

Raises an error if the type is not recognized.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elastic_beans/configuration_template.rb', line 28

def self.new_by_type(type, **args)
  case type
  when "base"
    ElasticBeans::ConfigurationTemplate::Base.new(**args)
  when "exec"
    ElasticBeans::ConfigurationTemplate::Exec.new(**args)
  when "scheduler"
    ElasticBeans::ConfigurationTemplate::Scheduler.new(**args)
  when "webserver"
    ElasticBeans::ConfigurationTemplate::Webserver.new(**args)
  when "worker"
    ElasticBeans::ConfigurationTemplate::Worker.new(**args)
  else
    raise UnknownConfigurationType.new(type: type)
  end
end

.new_from_existing(template_name, **args) ⇒ Object

Create a representation of an existing configuration template. Uses the template_name to discover the appropriate type (and queue, in the case of a worker).

Raises an error if the template_name is not recognized.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elastic_beans/configuration_template.rb', line 49

def self.new_from_existing(template_name, **args)
  case template_name
  when "base"
    ElasticBeans::ConfigurationTemplate::Base.new(**args)
  when "exec"
    ElasticBeans::ConfigurationTemplate::Exec.new(**args)
  when "scheduler"
    ElasticBeans::ConfigurationTemplate::Scheduler.new(**args)
  when "webserver"
    ElasticBeans::ConfigurationTemplate::Webserver.new(**args)
  when WORKER_TEMPLATE_NAME_PATTERN
    match = WORKER_TEMPLATE_NAME_PATTERN.match(template_name)
    ElasticBeans::ConfigurationTemplate::Worker.new(queue: match[:queue], **args)
  else
    raise UnknownConfigurationType.new(type: template_name)
  end
end

Instance Method Details

#environmentObject

Fetches the running environment from the application.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elastic_beans/configuration_template.rb', line 80

def environment
  if defined? @environment
    return @environment
  end

  environment_type = self.class.name.split("::").last
  if Environment.constants.none? { |name| name.to_s == environment_type }
    return @environment = nil
  end

  environment_class = Environment.const_get(environment_type)
  @environment = application.environments.find { |environment| environment.is_a?(environment_class) }
end

#option_settingsObject

Returns option settings built in #upsert, or fetches their current values from Elastic Beanstalk if #upsert has not been called.



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

def option_settings
  # do not fetch option settings from Elastic Beanstalk, because those cannot be naively used.
  # the set built in #build_option_settings is what we care about.
  @option_settings ||= build_option_settings
end

#options_to_removeObject

Returns options to remove built in #upsert, or returns an empty array. Will only be present if some options have been explicitly removed.



103
104
105
106
107
# File 'lib/elastic_beans/configuration_template.rb', line 103

def options_to_remove
  # If #upsert has not been called, return an empty array.
  # Nothing to remove unless options were explicitly disabled via #upsert.
  @options_to_remove ||= []
end

#solution_stack_nameObject

Finds the latest available Ruby Puma solution stack (matching SOLUTION_STACK_PATTERN).



110
111
112
# File 'lib/elastic_beans/configuration_template.rb', line 110

def solution_stack_name
  @solution_stack_name ||= latest_solution_stack
end

#upsert(**args) ⇒ Object

Create or update the configuration template in Elastic Beanstalk. Arguments are passed to #build_option_settings and #build_options_to_remove. See the appropriate subclass for what the arguments should be.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/elastic_beans/configuration_template.rb', line 117

def upsert(**args)
  @option_settings = build_option_settings(**args)
  @options_to_remove = build_options_to_remove(**args)
  if configuration_settings_description
    elastic_beanstalk.update_configuration_template(
      application_name: application.name,
      template_name: name,
      option_settings: option_settings,
      options_to_remove: options_to_remove,
    )
  else
    elastic_beanstalk.create_configuration_template(
      application_name: application.name,
      template_name: name,
      solution_stack_name: solution_stack_name,
      source_configuration: source_configuration,
      option_settings: option_settings,
    )
  end
rescue ::Aws::ElasticBeanstalk::Errors::Throttling
  sleep 5
  retry
end