Class: Jets::Cfn::Resource::Codebuild::Project::Ec2

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/cfn/resource/codebuild/project/ec2.rb

Direct Known Subclasses

Lambda

Instance Method Summary collapse

Methods inherited from Base

#attributes, #config, #logical_id, #normalize_tags, #parameters, #properties, #replacements, #replacer, #standarize, #template, truncate_id, #type

Methods included from Util::Camelize

#camelize

Methods included from Util::Logging

#log

Instance Method Details

#build_specObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 77

def build_spec
  <<~EOL
    version: 0.2

    phases:
      build:
        commands:
           - uname -a
  EOL
end

#codebuild_logical_idObject

interface method



110
111
112
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 110

def codebuild_logical_id
  "Codebuild"
end

#compute_typeObject

interface method



120
121
122
123
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 120

def compute_type
  # Note: PrivilegedMode is set at the end in finalize_properties based on the final Type
  config.codebuild.project.compute_type
end

#definitionObject



3
4
5
6
7
8
9
10
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 3

def definition
  {
    codebuild_logical_id => {
      Type: "AWS::CodeBuild::Project",
      Properties: finalize_properties(props)
    }
  }
end

#environmentObject



89
90
91
92
93
94
95
96
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 89

def environment
  env = compute_type
  env.deep_merge!(config.codebuild.project.environment)
  env.deep_merge!(EnvironmentVariables: environment_variables) if environment_variables
  # sort for correct diffing. Deploy#changed? checks the template body diff
  env[:EnvironmentVariables].sort_by! { |h| h[:Name] }
  env
end

#environment_variablesObject



99
100
101
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 99

def environment_variables
  Env.new.vars
end

#finalize_properties(props) ⇒ Object

Certain properties are not supported for certain Compute Types We set this at the end based on the final Environment Type

PrivilegedMode: Only supported for non-LAMBDA environment types Cache: non-LAMBDA environment types

CREATE_FAILED AWS::CodeBuild::Project Codebuild Cannot specify timeoutInMinutes for lambda compute
TimeoutInMinutes: 20, # TODO: make this configurable
CREATE_FAILED AWS::CodeBuild::Project
Codebuild Cannot specify cache for lambda compute. Error Code: InvalidInputException

PrivilegedMode: true, # Note: does not seem to be actually needed for docker support When using PrivilegedMode for ARM_LAMBDA_CONTAINER it’ll error though. Not officially supporting ARM_LAMBDA_CONTAINER when packaging code anyway. Just noting for posterity.



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/jets/cfn/resource/codebuild/project/ec2.rb', line 53

def finalize_properties(props)
  if environment[:Type].include?("LAMBDA")
    props[:Cache] = {
      Type: "NO_CACHE"
    }
    props[:Environment][:PrivilegedMode] = false
  else
    props[:Cache] = {
      Type: "LOCAL",
      Modes: ["LOCAL_DOCKER_LAYER_CACHE", "LOCAL_CUSTOM_CACHE"]
    }
    props[:TimeoutInMinutes] = timeout_in_minutes # not supported for lambda compute
    props[:Environment][:PrivilegedMode] = true
  end

  if config.codebuild.fleet.enable
    props[:Environment][:Fleet] = {
      FleetArn: "!Ref CodebuildFleet"
    }
  end

  props
end

#outputsObject



103
104
105
106
107
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 103

def outputs
  {
    codebuild_logical_id => "!Ref #{codebuild_logical_id}"
  }
end

#project_nameObject

interface method



115
116
117
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 115

def project_name
  "#{Jets.project.namespace}-remote"
end

#propsObject

Do not name this method properties as that’s a computed method



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 13

def props
  {
    Name: project_name,
    Artifacts: {
      Type: "NO_ARTIFACTS"
    },
    Environment: environment,
    Source: {
      Type: "NO_SOURCE",
      BuildSpec: build_spec
    },
    ServiceRole: "!Ref CodebuildRole",
    LogsConfig: {
      CloudWatchLogs: {
        Status: "ENABLED"
      }
    }
  }
end

#timeout_in_minutesObject

Only supported for EC2 compute type. Lambda compute type does not support.



34
35
36
# File 'lib/jets/cfn/resource/codebuild/project/ec2.rb', line 34

def timeout_in_minutes
  config.codebuild.project.timeout || config.codebuild.project.timeout_in_minutes
end