Class: OpenStax::Aws::SamStack

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

Instance Attribute Summary collapse

Attributes inherited from Stack

#absolute_template_path, #dry_run, #enable_termination_protection, #id, #name, #parameter_defaults, #region, #secrets_blocks, #tags, #volatile_parameters_block

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Stack

#apply_change_set, #aws_stack, #capabilities, #create, #create_change_set, #default_capabilities, #defines_secrets?, #delete, #deployed_parameters, #events, format_hash_as_stack_parameters, format_hash_as_tag_parameters, #output_value, #parameters_for_update, query, #resource, #resources, #secrets, #status, #tag_resources_not_handled_by_cloudformation, #template, #volatile_parameters, #wait_for_creation, #wait_for_deletion, #wait_for_update

Constructor Details

#initialize(id: nil, name:, tags: {}, region:, enable_termination_protection: false, absolute_template_path: nil, capabilities: nil, parameter_defaults: {}, volatile_parameters_block: nil, secrets_blocks: [], secrets_context: nil, secrets_namespace: nil, shared_secrets_substitutions_block: nil, cycle_if_different_parameter: nil, build_directory:, dry_run: true) ⇒ SamStack

Returns a new instance of SamStack.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openstax/aws/sam_stack.rb', line 6

def initialize(id: nil, name:, tags: {},
               region:, enable_termination_protection: false,
               absolute_template_path: nil,
               capabilities: nil, parameter_defaults: {},
               volatile_parameters_block: nil,
               secrets_blocks: [], secrets_context: nil, secrets_namespace: nil,
               shared_secrets_substitutions_block: nil,
               cycle_if_different_parameter: nil,
               build_directory:,
               dry_run: true)
  @build_directory = build_directory

  super(id: id,
        name: name,
        tags: tags,
        region: region,
        enable_termination_protection: enable_termination_protection,
        absolute_template_path: absolute_template_path,
        capabilities: capabilities,
        parameter_defaults: parameter_defaults,
        volatile_parameters_block: volatile_parameters_block,
        secrets_blocks: secrets_blocks,
        secrets_context: secrets_context,
        secrets_namespace: secrets_namespace,
        shared_secrets_substitutions_block: shared_secrets_substitutions_block,
        cycle_if_different_parameter: cycle_if_different_parameter,
        dry_run: dry_run)
end

Instance Attribute Details

#build_directoryObject (readonly)

Returns the value of attribute build_directory.



4
5
6
# File 'lib/openstax/aws/sam_stack.rb', line 4

def build_directory
  @build_directory
end

Class Method Details

.format_hash_as_cli_stack_parameters(params = {}) ⇒ Object



77
78
79
80
81
# File 'lib/openstax/aws/sam_stack.rb', line 77

def self.format_hash_as_cli_stack_parameters(params={})
  params.map{|key, value| "ParameterKey=#{key.to_s.camelcase},ParameterValue=#{value}"}
        .map{|item| "'" + item + "'"}
        .join(" ")
end

.format_tags_as_cli_tags(tags = []) ⇒ Object



83
84
85
86
# File 'lib/openstax/aws/sam_stack.rb', line 83

def self.format_tags_as_cli_tags(tags=[])
  tags.map{|tag| "'#{tag.key}=#{tag.value}'"}
      .join(" ")
end

Instance Method Details

#build(debug: false, env_vars: {}) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/openstax/aws/sam_stack.rb', line 35

def build(debug: false, env_vars: {})
  # SAM doesn't have an API or SDK - we have to make calls to its CLI
  env_vars_prefix = env_vars.map { |variable, value| "#{variable}=#{value}" }.join(' ')
  command = "sam build -t #{absolute_template_path} -b #{build_directory}"
  command += ' --debug' if debug
  command = "#{env_vars_prefix} #{command}" if env_vars_prefix.present?
  System.call(command, logger: logger, dry_run: dry_run)
end

#deploy(bucket_name:, params: {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openstax/aws/sam_stack.rb', line 44

def deploy(bucket_name:, params: {})
  # SAM doesn't have an API or SDK - we have to make calls to its CLI

  check_for_required_tags

  params = parameter_defaults.merge(params)

  command = "sam deploy" \
            " --template-file #{build_directory}/template.yaml" \
            " --capabilities CAPABILITY_IAM" \
            " --s3-bucket #{bucket_name}" \
            " --s3-prefix #{name}" \
            " --stack-name #{name}" \
            " --region #{region}"

  if params.any?
    command += " --parameter-overrides #{self.class.format_hash_as_cli_stack_parameters(params)}"
  end

  if tags.any?
    command += " --tags #{self.class.format_tags_as_cli_tags(tags)}"
  end

  System.call(command, logger: logger, dry_run: dry_run)

  if enable_termination_protection
    client.update_termination_protection({
      enable_termination_protection: true,
      stack_name: name
    })
  end
end