Class: Putpaws::Provision::Resources::CodebuildProject

Inherits:
Base
  • Object
show all
Defined in:
lib/putpaws/provision/resources/codebuild_project.rb

Constant Summary collapse

ENVIRONMENT_TYPES =

Derived from cpu_architecture; devops.environment_type / environment_image override the derivation (useful for staged arch migration).

{
  'ARM64' => 'ARM_CONTAINER',
  'X86_64' => 'LINUX_CONTAINER',
}
DEFAULT_IMAGES =
{
  'ARM64' => 'aws/codebuild/amazonlinux-aarch64-standard:3.0',
  'X86_64' => 'aws/codebuild/standard:7.0',
}

Instance Attribute Summary

Attributes inherited from Base

#clients, #config, #state

Instance Method Summary collapse

Methods inherited from Base

#ensure!, #initialize, #kind, #plan

Constructor Details

This class inherits a constructor from Putpaws::Provision::Resources::Base

Instance Method Details

#changed?(current) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 79

def changed?(current)
  current.source&.type != desired_params[:source][:type] ||
    current.source&.location != desired_params[:source][:location] ||
    current.source&.buildspec != desired_params[:source][:buildspec] ||
    current.service_role != desired_params[:service_role] ||
    current.environment&.type != desired_params[:environment][:type] ||
    current.environment&.image != desired_params[:environment][:image] ||
    current.environment&.compute_type != desired_params[:environment][:compute_type] ||
    current.vpc_config&.subnets.to_a.sort != desired_params[:vpc_config][:subnets].sort ||
    current.vpc_config&.security_group_ids.to_a.sort != desired_params[:vpc_config][:security_group_ids].sort
end

#create! ⇒ Object



91
92
93
94
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 91

def create!
  res = clients.codebuild.create_project(**desired_params)
  {codebuild_project: res.project.name}
end

#current ⇒ Object



74
75
76
77
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 74

def current
  res = clients.codebuild.batch_get_projects(names: [name])
  res.projects.first
end

#desired_params ⇒ Object



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
69
70
71
72
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 43

def desired_params
  {
    name: name,
    description: "Managed by putpaws for #{config.service_name}",
    source: {
      type: devops[:source_type] || 'GITHUB',
      location: devops[:source_repository],
      buildspec: devops[:buildspec] || 'buildspec.yml',
    },
    artifacts: {type: 'NO_ARTIFACTS'},
    environment: {
      type: environment_type,
      image: environment_image,
      compute_type: devops[:compute_type] || 'BUILD_GENERAL1_SMALL',
      privileged_mode: devops.fetch(:privileged_mode, true),
    },
    service_role: config.roles[:codebuild_role_arn],
    logs_config: {
      cloud_watch_logs: {status: 'ENABLED', group_name: config.build_log_group}
    },
    # CI needs VPC access (Ex: db:migrate against RDS). Reuse the
    # service security group so the build reaches whatever the app can.
    vpc_config: {
      vpc_id: config.base[:vpc_id],
      subnets: config.base[:subnets],
      security_group_ids: security_group_ids,
    },
    tags: [MANAGED_TAG],
  }
end

#devops ⇒ Object



23
24
25
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 23

def devops
  config.devops_settings
end

#environment_image ⇒ Object



31
32
33
34
35
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 31

def environment_image
  devops[:environment_image] ||
    (devops[:environment_images] || {})[config.cpu_architecture.to_sym] ||
    DEFAULT_IMAGES.fetch(config.cpu_architecture)
end

#environment_type ⇒ Object



27
28
29
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 27

def environment_type
  devops[:environment_type] || ENVIRONMENT_TYPES.fetch(config.cpu_architecture)
end

#name ⇒ Object



19
20
21
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 19

def name
  config.build_project_name
end

#outputs(current) ⇒ Object



101
102
103
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 101

def outputs(current)
  {codebuild_project: current.name}
end

#security_group_ids ⇒ Object



37
38
39
40
41
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 37

def security_group_ids
  ids = state.resources[:security_group_ids]
  raise "security_group_ids not found in state. Run the service step first." if ids.nil? || ids.empty?
  ids
end

#update!(_current) ⇒ Object



96
97
98
99
# File 'lib/putpaws/provision/resources/codebuild_project.rb', line 96

def update!(_current)
  res = clients.codebuild.update_project(**desired_params)
  {codebuild_project: res.project.name}
end