Class: Jets::Cfn::Builders::ParentBuilder

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Interface, Stagger
Defined in:
lib/jets/cfn/builders/parent_builder.rb,
lib/jets/cfn/builders/parent_builder/stagger.rb

Defined Under Namespace

Modules: Stagger

Instance Method Summary collapse

Methods included from Stagger

#add_stagger, #stagger_batch_size, #stagger_enabled

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Methods included from Interface

#add_description, #add_output, #add_outputs, #add_parameter, #add_parameters, #add_resource, #add_resources, #add_template_resource, #build, #post_process_template, #template, #text, #write

Constructor Details

#initialize(options = {}) ⇒ ParentBuilder

Returns a new instance of ParentBuilder.



9
10
11
12
# File 'lib/jets/cfn/builders/parent_builder.rb', line 9

def initialize(options={})
  @options = options
  @template = ActiveSupport::HashWithIndifferentAccess.new(Resources: {})
end

Instance Method Details

#add_api_deploymentObject



139
140
141
142
# File 'lib/jets/cfn/builders/parent_builder.rb', line 139

def add_api_deployment
  resource = Jets::Resource::ChildStack::ApiDeployment.new(@options[:s3_bucket])
  add_child_resources(resource)
end

#add_api_gatewayObject



120
121
122
123
# File 'lib/jets/cfn/builders/parent_builder.rb', line 120

def add_api_gateway
  resource = Jets::Resource::ChildStack::ApiGateway.new(@options[:s3_bucket])
  add_child_resources(resource)
end

#add_api_resourcesObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jets/cfn/builders/parent_builder.rb', line 125

def add_api_resources
  expression = "#{Jets::Names.template_path_prefix}-api-resources-*"
  # IE: path: #{Jets.build_root}/templates/demo-dev-2-api-resources-1.yml"
  Dir.glob(expression).sort.each do |path|
    next unless File.file?(path)

    regexp = Regexp.new("#{Jets.config.project_namespace}-api-resources-(\\d+).yml") # tricky to escape \d pattern
    md = path.match(regexp)
    page = md[1]
    resource = Jets::Resource::ChildStack::ApiResource.new(@options[:s3_bucket], page: page)
    add_child_resources(resource)
  end
end

#add_app_class_stack(path) ⇒ Object



104
105
106
107
108
# File 'lib/jets/cfn/builders/parent_builder.rb', line 104

def add_app_class_stack(path)
  resource = Jets::Resource::ChildStack::AppClass.new(@options[:s3_bucket], path: path)
  add_stagger(resource)
  add_child_resources(resource)
end

#add_authorizer_resources(path) ⇒ Object



110
111
112
113
# File 'lib/jets/cfn/builders/parent_builder.rb', line 110

def add_authorizer_resources(path)
  resource = Jets::Resource::ChildStack::Authorizer.new(@options[:s3_bucket], path: path)
  add_child_resources(resource)
end

#add_child_resources(resource) ⇒ Object



144
145
146
147
# File 'lib/jets/cfn/builders/parent_builder.rb', line 144

def add_child_resources(resource)
  add_resource(resource)
  add_outputs(resource.outputs)
end

#add_shared_resources(path) ⇒ Object



115
116
117
118
# File 'lib/jets/cfn/builders/parent_builder.rb', line 115

def add_shared_resources(path)
  resource = Jets::Resource::ChildStack::Shared.new(@options[:s3_bucket], path: path)
  add_child_resources(resource) if resource.resources?
end

#build_child_resourcesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jets/cfn/builders/parent_builder.rb', line 70

def build_child_resources
  for_each_path(:app) do |path|
    add_app_class_stack(path)
  end
  for_each_path(:shared) do |path|
    add_shared_resources(path)
  end

  if full? and !Jets::Router.routes.empty?
    for_each_path(:authorizers) do |path|
      add_authorizer_resources(path)
    end
    add_api_gateway
    add_api_resources
    add_api_deployment
  end
end

#build_minimal_resourcesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/jets/cfn/builders/parent_builder.rb', line 25

def build_minimal_resources
  add_description("Jets: #{Jets.version} Code: #{Util::Source.version}")

  # Initial s3 bucket, used to store code zipfile and templates Jets generates
  #
  # AWS changed the default behavior of s3 buckets to block public access
  #   https://aws.amazon.com/blogs/aws/amazon-s3-block-public-access-another-layer-of-protection-for-your-accounts-and-buckets/
  #   https://github.com/aws-amplify/amplify-cli/issues/12503
  #
  # Jets uploads assets to s3 bucket with acl: "public-read" here
  #   https://github.com/boltops-tools/jets/blob/c5858ec2706a606665a92c3ada3f16ae4c753372/lib/jets/cfn/upload.rb#L97
  #
  # Use minimal s3 bucket policy to allow public read access to assets.
  # Leave the other options as comments to help document the default behavior.
  resource = Jets::Resource::S3::Bucket.new(logical_id: "s3_bucket",
    PublicAccessBlockConfiguration: {
      BlockPublicAcls: false,
      # BlockPublicPolicy: false,
      # IgnorePublicAcls: false,
      # RestrictPublicBuckets: false
    },
    OwnershipControls: {
      Rules: [{ObjectOwnership: "ObjectWriter"}]
    },
    bucket_encryption: {
      server_side_encryption_configuration: [
        server_side_encryption_by_default: {
          sse_algorithm: "AES256"
      }]}
  )
  add_resource(resource)
  add_outputs(resource.outputs)

  return unless full?
  # Add application-wide IAM policy from Jets.config.iam_role
  resource = Jets::Resource::Iam::ApplicationRole.new
  add_resource(resource)
  add_outputs(resource.outputs)

  return if Jets.poly_only?
  resource = Jets::Resource::Lambda::GemLayer.new
  add_resource(resource)
  add_outputs(resource.outputs)
end

#composeObject

compose is an interface method



15
16
17
18
# File 'lib/jets/cfn/builders/parent_builder.rb', line 15

def compose
  build_minimal_resources
  build_child_resources if full?
end

#for_each_path(type) ⇒ Object

Example paths:

#{Jets.build_root}/templates/demo-dev-2-shared-resources.yml
#{Jets.build_root}/templates/demo-dev-2-app-comments_controller.yml
#{Jets.build_root}/templates/demo-dev-2-authorizers-main_authorizer.yml


92
93
94
95
96
97
98
# File 'lib/jets/cfn/builders/parent_builder.rb', line 92

def for_each_path(type)
  expression = "#{Jets::Names.template_path_prefix}-#{type}-*"
  Dir.glob(expression).each do |path|
    next unless File.file?(path)
    yield(path)
  end
end

#full?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/jets/cfn/builders/parent_builder.rb', line 100

def full?
  @options[:templates] || @options[:stack_type] == :full
end

#template_pathObject

template_path is an interface method



21
22
23
# File 'lib/jets/cfn/builders/parent_builder.rb', line 21

def template_path
  Jets::Names.parent_template_path
end