Class: Uc3SamSceptre::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-sam-sceptre.rb

Overview

Ruby proxy for running SAM dynamically

Constant Summary collapse

DEFAULT_REGION =
'us-west-2'
DEFAULT_PROGRAM =
'uc3'
DEFAULT_ENV =
'dev'
MSG_INVALID_PARAMS =
'Invalid initialization params. Expected at least the [:program, :service, :subservice, /
:env, :git_repo, :stack_suffix, :admin_email_key] and one of the [:ecr_uri_key, :s3_arn_key]
and at least one of the following arrays: [:fetchable_cf_params, :static_cf_params]. /
See the README.md for examples.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Proxy

Initialize the SAM proxy



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
# File 'lib/uc3-sam-sceptre.rb', line 27

def initialize(**params)
  @region = params.fetch(:region, DEFAULT_REGION)
  @program = params.fetch(:program, DEFAULT_PROGRAM)
  @service = params[:service]
  @subservice = params[:subservice]
  @env = params.fetch(:env, DEFAULT_ENV)
  @git_repo = params[:git_repo]
  @stack_suffix = params[:stack_suffix]
  @ecr_uri_key = params[:ecr_uri_key]
  @s3_arn_key = params[:s3_arn_key]
  @admin_email_key = params[:admin_email_key]

  @auto = params.fetch(:auto, false)

  # Determine which AWS account we are working with, dev or prd (stg and prd are shared)
  @account_env = %w[stg prd].include?(@env) ? 'prd' : 'dev'
  @prefix = "#{@program}-#{@service}-#{@subservice}-#{@env}"
  @ssm_key_prefix = params.fetch(:ssm_key_prefix, "/#{@prefix.gsub('-', '/')}/")

  @fetchable_cf_params = params.fetch(:fetchable_cf_params, [])
  @static_cf_params = params.fetch(:static_cf_params, [])

  raise ProxyError, MSG_INVALID_PARAMS if @service.nil? || @subservice.nil? || @git_repo.nil? || @stack_suffix.nil? ||
                                          (@ecr_uri_key.nil? && @s3_arn_key.nil?) ||
                                          (@fetchable_cf_params.empty? && @static_cf_params.empty?)

  @ssm_client = Aws::SSM::Client.new(region: DEFAULT_REGION)
  @cf_client = Aws::CloudFormation::Client.new(region: DEFAULT_REGION)

  @stack_exports = cf_client.list_exports.exports
  @stack_name = "#{@prefix}-#{@stack_suffix}"
rescue Aws::Errors::ServiceError => e
  raise ClientError, "Unable to connect to AWS resource. - #{e.message}"
end

Instance Attribute Details

#account_envObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def 
  @account_env
end

#admin_email_keyObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def admin_email_key
  @admin_email_key
end

#cf_argsObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def cf_args
  @cf_args
end

#cf_clientObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def cf_client
  @cf_client
end

#ecr_uri_keyObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def ecr_uri_key
  @ecr_uri_key
end

#envObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def env
  @env
end

#git_repoObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def git_repo
  @git_repo
end

#prefixObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def prefix
  @prefix
end

#programObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def program
  @program
end

#regionObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def region
  @region
end

#s3_arn_keyObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def s3_arn_key
  @s3_arn_key
end

#serviceObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def service
  @service
end

#ssm_argsObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def ssm_args
  @ssm_args
end

#ssm_clientObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def ssm_client
  @ssm_client
end

#ssm_key_prefixObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def ssm_key_prefix
  @ssm_key_prefix
end

#stack_exportsObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def stack_exports
  @stack_exports
end

#stack_suffixObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def stack_suffix
  @stack_suffix
end

#static_argsObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def static_args
  @static_args
end

#subserviceObject

Required



21
22
23
# File 'lib/uc3-sam-sceptre.rb', line 21

def subservice
  @subservice
end

Instance Method Details

#buildObject

Run a SAM build



63
64
65
# File 'lib/uc3-sam-sceptre.rb', line 63

def build
  system 'sam build'
end

#deleteObject

Run a SAM delete



88
89
90
# File 'lib/uc3-sam-sceptre.rb', line 88

def delete
  system "sam delete --stack-name #{@stack_name}"
end

#deploy(guided: false) ⇒ Object

Run a SAM deploy



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/uc3-sam-sceptre.rb', line 68

def deploy(guided: false)
  args = [
    "--stack-name #{@stack_name}",
    "--confirm-changeset #{!@auto}",
    '--capabilities CAPABILITY_NAMED_IAM',
    '--disable-rollback false'
  ]

  # Add the S3 or ECR details depending on what we're working with
  args << "--s3-bucket #{_locate_value(key: @s3_arn_key)&.gsub('arn:aws:s3:::', '')}" unless @s3_arn_key.nil?
  args << "--s3-prefix #{@stack_name}" unless @s3_arn_key.nil?
  args << "--image-repository #{_locate_value(key: @ecr_uri_key)}" unless @ecr_uri_key.nil?

  args << "--guided" if guided
  args << "--tags #{_sam_tags(admin_email: _locate_value(key: @admin_email_key)).join(' ')}"
  args << "--parameter-overrides #{_build_deploy_overrides.join(' ')}"
  system "sam deploy #{args.join(' ')}"
end