Class: Moonshot::Plugins::CodeDeploySetup

Inherits:
Object
  • Object
show all
Includes:
CredsHelper
Defined in:
lib/plugins/code_deploy_setup.rb

Overview

Plugin to ensure CodeDeploy has all necessary S3 buckets created. Defaults to using ENV as default region if not configured.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CredsHelper

#as_client, #cd_client, #cf_client, #ec2_client, #iam_client, #s3_client

Constructor Details

#initialize(name, prefix: '', regions: [ENV['AWS_REGION']]) ⇒ CodeDeploySetup

Returns a new instance of CodeDeploySetup.



12
13
14
15
16
17
18
19
20
21
# File 'lib/plugins/code_deploy_setup.rb', line 12

def initialize(name, prefix: '', regions: [ENV['AWS_REGION']])
  @regions = regions.reject(&:nil?)
  if @regions.empty?
    raise ArgumentError, 'CodeDeploySetup requires at least one region.' \
                         ' Set regions argument or ENV[\'AWS_REGION\'].'
  end

  @name = name
  @prefix = prefix
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/plugins/code_deploy_setup.rb', line 10

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



10
11
12
# File 'lib/plugins/code_deploy_setup.rb', line 10

def prefix
  @prefix
end

#regionsObject (readonly)

Returns the value of attribute regions.



10
11
12
# File 'lib/plugins/code_deploy_setup.rb', line 10

def regions
  @regions
end

Instance Method Details

#bucket_name(region = ) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/plugins/code_deploy_setup.rb', line 23

def bucket_name(region = ENV['AWS_REGION'])
  if ENV.key?('S3_BUCKET')
    ENV['S3_BUCKET']
  else
    "#{@name}-#{region}"
  end
end

#bucket_prefixObject



31
32
33
# File 'lib/plugins/code_deploy_setup.rb', line 31

def bucket_prefix
  @prefix.empty? ? '' : "#{@prefix}/"
end

#run_hook(_resources) ⇒ Object Also known as: pre_create, pre_deploy

Hook entry points to ensure S3 Buckets are available for CodeDeploy



49
50
51
# File 'lib/plugins/code_deploy_setup.rb', line 49

def run_hook(_resources)
  setup_code_deploy_s3_buckets
end

#setup_code_deploy_s3_bucketsObject

Create an S3 bucket in each supported region for CodeDeploy



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plugins/code_deploy_setup.rb', line 36

def setup_code_deploy_s3_buckets
  @regions.uniq.each do |region|
    client = s3_client(region:)
    name = bucket_name(region)
    bucket = Aws::S3::Bucket.new(
      name,
      client:
    )
    bucket.create unless bucket.exists?
  end
end