Class: Uc3SamSceptre::Proxy
- Inherits:
-
Object
- Object
- Uc3SamSceptre::Proxy
- 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
-
#account_env ⇒ Object
Required.
-
#admin_email_key ⇒ Object
Required.
-
#cf_args ⇒ Object
Required.
-
#cf_client ⇒ Object
Required.
-
#ecr_uri_key ⇒ Object
Required.
-
#env ⇒ Object
Required.
-
#git_repo ⇒ Object
Required.
-
#prefix ⇒ Object
Required.
-
#program ⇒ Object
Required.
-
#region ⇒ Object
Required.
-
#s3_arn_key ⇒ Object
Required.
-
#service ⇒ Object
Required.
-
#ssm_args ⇒ Object
Required.
-
#ssm_client ⇒ Object
Required.
-
#ssm_key_prefix ⇒ Object
Required.
-
#stack_exports ⇒ Object
Required.
-
#stack_suffix ⇒ Object
Required.
-
#static_args ⇒ Object
Required.
-
#subservice ⇒ Object
Required.
Instance Method Summary collapse
-
#build ⇒ Object
Run a SAM build.
-
#delete ⇒ Object
Run a SAM delete.
-
#deploy(guided: false) ⇒ Object
Run a SAM deploy.
-
#initialize(**params) ⇒ Proxy
constructor
Initialize the SAM proxy.
-
#package ⇒ Object
Run a SAM package (build and upload to the S3 bucket or ECR).
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 61 |
# 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] @s3_url_key = params[:s3_url_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.}" end |
Instance Attribute Details
#account_env ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def account_env @account_env end |
#admin_email_key ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def admin_email_key @admin_email_key end |
#cf_args ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def cf_args @cf_args end |
#cf_client ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def cf_client @cf_client end |
#ecr_uri_key ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def ecr_uri_key @ecr_uri_key end |
#env ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def env @env end |
#git_repo ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def git_repo @git_repo end |
#prefix ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def prefix @prefix end |
#program ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def program @program end |
#region ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def region @region end |
#s3_arn_key ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def s3_arn_key @s3_arn_key end |
#service ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def service @service end |
#ssm_args ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def ssm_args @ssm_args end |
#ssm_client ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def ssm_client @ssm_client end |
#ssm_key_prefix ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def ssm_key_prefix @ssm_key_prefix end |
#stack_exports ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def stack_exports @stack_exports end |
#stack_suffix ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def stack_suffix @stack_suffix end |
#static_args ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def static_args @static_args end |
#subservice ⇒ Object
Required
21 22 23 |
# File 'lib/uc3-sam-sceptre.rb', line 21 def subservice @subservice end |
Instance Method Details
#build ⇒ Object
Run a SAM build
64 65 66 |
# File 'lib/uc3-sam-sceptre.rb', line 64 def build system 'sam build' end |
#delete ⇒ Object
Run a SAM delete
106 107 108 |
# File 'lib/uc3-sam-sceptre.rb', line 106 def delete system "sam delete --stack-name #{@stack_name}" end |
#deploy(guided: false) ⇒ Object
Run a SAM deploy
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/uc3-sam-sceptre.rb', line 86 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 #{(admin_email: _locate_value(key: @admin_email_key)).join(' ')}" args << "--parameter-overrides #{_build_deploy_overrides.join(' ')}" system "sam deploy #{args.join(' ')}" end |
#package ⇒ Object
Run a SAM package (build and upload to the S3 bucket or ECR)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/uc3-sam-sceptre.rb', line 69 def package args = [ "--force-upload", "--metadata #{(admin_email: true).join(',')}" ] 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? puts "S3 BUCKET: #{@s3_arn_key}" pp args puts "ARGS: #{args.join(' ')}" system "sam package #{args.join(' ')}" end |