Class: OpenStax::Aws::DeploymentBase

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/deployment_base.rb

Defined Under Namespace

Classes: Status

Constant Summary collapse

RESERVED_ENV_NAMES =
[
  "external", # used to namespace external secrets in the parameter store
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_name: nil, region:, name:, dry_run: true) ⇒ DeploymentBase

Returns a new instance of DeploymentBase.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openstax/aws/deployment_base.rb', line 41

def initialize(env_name: nil, region:, name:, dry_run: true)
  if RESERVED_ENV_NAMES.include?(env_name)
    raise "#{env_name} is a reserved word and cannot be used as an environment name"
  end

  # Allow a blank env_name but normalize it to `nil`
  @env_name = env_name.blank? ? nil : env_name

  if @env_name && !@env_name.match(/^[a-zA-Z][a-zA-Z0-9-]*$/)
    raise "The environment name must consist only of letters, numbers, and hyphens, " \
          "and must start with a letter."
  end

  @region = region
  @name = name
  @dry_run = dry_run
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



35
36
37
# File 'lib/openstax/aws/deployment_base.rb', line 35

def dry_run
  @dry_run
end

#env_nameObject (readonly)

Returns the value of attribute env_name.



35
36
37
# File 'lib/openstax/aws/deployment_base.rb', line 35

def env_name
  @env_name
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/openstax/aws/deployment_base.rb', line 35

def name
  @name
end

#regionObject (readonly)

Returns the value of attribute region.



35
36
37
# File 'lib/openstax/aws/deployment_base.rb', line 35

def region
  @region
end

Class Method Details

.inherited(child_class) ⇒ Object



208
209
210
211
212
213
# File 'lib/openstax/aws/deployment_base.rb', line 208

def inherited(child_class)
  # Copy any tags defined in the parent to the child so that it can access them
  # while not using class variables that are shared across all classes in the
  # that inherit here
  child_class.instance_variable_set("@tags", tags.dup)
end

.loggerObject



215
216
217
# File 'lib/openstax/aws/deployment_base.rb', line 215

def logger
  OpenStax::Aws.configuration.logger
end

.sam_build_directory(*directory_parts) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/openstax/aws/deployment_base.rb', line 87

def sam_build_directory(*directory_parts)
  if method_defined?("sam_build_directory")
    raise "Can only set buisam_build_directoryld_directory once per class definition"
  end

  define_method "sam_build_directory" do
    File.expand_path(File.join(*directory_parts))
  end
end

.secrets(id, &block) ⇒ Object



97
98
99
100
101
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
# File 'lib/openstax/aws/deployment_base.rb', line 97

def secrets(id, &block)
  if id.blank?
    raise "The first argument to `secrets` must be a non-blank ID"
  end

  if !id.to_s.match(/^[a-zA-Z][a-zA-Z0-9_]*$/)
    raise "The first argument to `secrets` must consist only of letters, numbers, and underscores, " \
          "and must start with a letter."
  end

  if method_defined?("#{id}_secrets")
    raise "Can only define the `#{id}` secrets once per class definition"
  end

  if method_defined?("#{id}_stack")
    raise "Cannot define `#{id}` secrets because there is a stack with that ID"
  end

  define_method("#{id}_secrets") do |for_create_or_update: false|
    secrets_factory = SecretsFactory.new(
      region: region,
      namespace: [env_name, name],
      context: self,
      dry_run: dry_run,
      for_create_or_update: for_create_or_update,
      shared_substitutions_block: @shared_secrets_substitutions_block
    )

    secrets_factory.namespace(id)
    secrets_factory.instance_exec({}, &block)
    secrets_factory.instance
  end
end

.secrets_substitutions(&block) ⇒ Object



192
193
194
195
196
# File 'lib/openstax/aws/deployment_base.rb', line 192

def secrets_substitutions(&block)
  define_method("shared_secrets_substitutions_block") do
    block
  end
end

.stack(id, &block) ⇒ Object



135
136
137
138
139
140
141
142
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/openstax/aws/deployment_base.rb', line 135

def stack(id, &block)
  if id.blank?
    raise "The first argument to `stack` must be a non-blank ID"
  end

  if !id.to_s.match(/^[a-zA-Z][a-zA-Z0-9_]*$/)
    raise "The first argument to `stack` must consist only of letters, numbers, and underscores, " \
          "and must start with a letter."
  end

  if method_defined?("#{id}_secrets")
    raise "Cannot define `#{id}` stack because there are secrets with that ID"
  end

  stack_ids.push(id)

  define_method("#{id}_stack") do
    instance_variable_get("@#{id}_stack") || begin
      stack_factory = StackFactory.new(id: id, deployment: self)
      stack_factory.instance_eval(&block) if block_given?

      # Fill in missing attributes using deployment variables and conventions

      if stack_factory.name.blank?
        stack_factory.name([env_name,name,id].compact.join("-").gsub("_","-"))
      end

      if stack_factory.region.blank?
        stack_factory.region(region)
      end

      if stack_factory.dry_run.nil?
        stack_factory.dry_run(dry_run)
      end

      if stack_factory.enable_termination_protection.nil?
        stack_factory.enable_termination_protection(is_production?)
      end

      if stack_factory.absolute_template_path.blank?
        stack_factory.autoset_absolute_template_path(respond_to?(:template_directory) ? template_directory : "")
      end

      # Populate parameter defaults that match convention names

      if OpenStax::Aws.configuration.infer_parameter_defaults
        defaults = parameter_defaults_from_template(stack_factory.absolute_template_path)
        defaults.each{|key,value| stack_factory.parameter_defaults[key] ||= value}
      end

      stack_factory.build.tap do |stack|
        instance_variable_set("@#{id}_stack", stack)
      end
    end
  end
end

.stack_idsObject



131
132
133
# File 'lib/openstax/aws/deployment_base.rb', line 131

def stack_ids
  @stack_ids ||= []
end

.tag(key, value = nil, &block) ⇒ Object



198
199
200
201
202
# File 'lib/openstax/aws/deployment_base.rb', line 198

def tag(key, value=nil, &block)
  raise 'The first argument to `tag` must not be blank' if key.blank?
  raise '`tag` must be given a value or a block' if value.nil? && !block_given?
  tags[key] = block_given? ? block : value
end

.tagsObject



204
205
206
# File 'lib/openstax/aws/deployment_base.rb', line 204

def tags
  @tags ||= HashWithIndifferentAccess.new
end

.template_directory(*directory_parts) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/openstax/aws/deployment_base.rb', line 77

def template_directory(*directory_parts)
  if method_defined?("template_directory")
    raise "Can only set template_directory once per class definition"
  end

  define_method "template_directory" do
    File.join(*directory_parts)
  end
end

Instance Method Details

#built_in_parameter_default(parameter_name) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/openstax/aws/deployment_base.rb', line 250

def built_in_parameter_default(parameter_name)
  case parameter_name
  when "EnvName"
    env_name
  when /(.+)StackName$/
    send("#{$1}Stack".underscore).name rescue nil
  end
end

#deployed_parametersObject



244
245
246
247
248
# File 'lib/openstax/aws/deployment_base.rb', line 244

def deployed_parameters
  stacks.each_with_object({}) do |stack, hash|
    hash[stack.name] = stack.deployed_parameters
  end
end

#env_name!Object



64
65
66
67
# File 'lib/openstax/aws/deployment_base.rb', line 64

def env_name!
  raise "`env_name` is blank" if env_name.blank?
  env_name
end

#failed_statuses_tableObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/openstax/aws/deployment_base.rb', line 263

def failed_statuses_table
  rows = []

  stacks.each do |stack|
    stack.status(reload: false).failed_events_since_last_user_event.each do |event|
      rows.push([stack.name, event.status_text, event.status_reason])
    end if stack.status.failed?
  end

  column_widths = [
    2 + rows.reduce(0) { |result, rowdata| [result, rowdata[0].length].max },
    2 + rows.reduce(0) { |result, rowdata| [result, rowdata[1].length].max },
    0
  ]

  output = []

  output.push(["Stack", "Status", "Reason"].each_with_index.map { |header, index| header.ljust(column_widths[index]) }.join(''))

  rows.each { |rowdata|
    output.push(rowdata.each_with_index.map { |value, index| value.ljust(column_widths[index]) }.join(''))
  }

  output.join("\n")
end

#name!Object



59
60
61
62
# File 'lib/openstax/aws/deployment_base.rb', line 59

def name!
  raise "`name` is blank" if name.blank?
  name
end

#parameter_defaults_from_template(template_or_absolute_template_path) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/openstax/aws/deployment_base.rb', line 220

def parameter_defaults_from_template(template_or_absolute_template_path)
  template = template_or_absolute_template_path.is_a?(String) ?
               Template.from_absolute_file_path(template_or_absolute_template_path) :
               template_or_absolute_template_path

  template.parameter_names.each_with_object({}) do |parameter_name, defaults|
    value = parameter_default(parameter_name) ||
            built_in_parameter_default(parameter_name)

    if !value.nil?
      defaults[parameter_name.underscore.to_sym] = value
    end
  end
end

#shared_secrets_substitutions_blockObject



259
260
261
# File 'lib/openstax/aws/deployment_base.rb', line 259

def shared_secrets_substitutions_block
  nil # can be overridden by the DSL
end

#stacksObject



235
236
237
# File 'lib/openstax/aws/deployment_base.rb', line 235

def stacks
  self.class.stack_ids.map{|id| self.send("#{id}_stack")}
end

#status(reload: false) ⇒ Object



239
240
241
242
# File 'lib/openstax/aws/deployment_base.rb', line 239

def status(reload: false)
  @status = nil if reload
  @status ||= Status.new(self)
end

#tagsObject



69
70
71
72
73
# File 'lib/openstax/aws/deployment_base.rb', line 69

def tags
  self.class.tags.each_with_object(HashWithIndifferentAccess.new) do |(key, value), hsh|
    hsh[key] = value.is_a?(Proc) ? instance_eval(&value) : value
  end
end