Class: Jets::AwsInfo

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/jets/aws_info.rb

Constant Summary collapse

BUCKET_DOES_NOT_YET_EXIST =

If bucket does not exist, do not use the cache value and check for the bucket again. This is because we can build the app before deploying it for the first time. The deploy sequence ensure an minimal stack exists and will return a s3 bucket value for real deployments though, just not for the ‘jets build` only command.

"bucket-does-not-yet-exist"
@@s3_bucket =

use const to save from misspellings

BUCKET_DOES_NOT_YET_EXIST

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #lambda, #logs, #s3, #s3_resource, #sns, #sts

Methods included from Jets::AwsServices::StackStatus

#stack_exists?, #stack_in_progress?

Instance Method Details

#accountObject

aws sts get-caller-identity



47
48
49
50
51
52
# File 'lib/jets/aws_info.rb', line 47

def 
  return '123456789' if test?
  # ensure region set, required for sts.get_caller_identity.account to work
  ENV['AWS_REGION'] ||= region
  sts.get_caller_identity.
end

#regionObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jets/aws_info.rb', line 7

def region
  return 'us-east-1' if test?

  return ENV['JETS_AWS_REGION'] if ENV['JETS_AWS_REGION'] # highest precedence
  return ENV['AWS_REGION'] if ENV['AWS_REGION']

  region = nil

  # First try to get it from the ~/.aws/config
  region = `aws configure get region 2>&1`.strip rescue nil
  exit_code = $?.exitstatus
  if exit_code != 0
    exception_message = region.split("\n").grep(/botocore\.exceptions/).first
    if exception_message
      puts "WARN: #{exception_message}".colorize(:yellow)
    else
      # show full message as warning
      puts region.colorize(:yellow)
    end
    puts "You can also get rid of this message by setting AWS_REGION or configuring ~/.aws/config with the region"
    region = nil
  end
  region = nil if region == ''
  return region if region

  # Second try the metadata endpoint, should be available on AWS Lambda environment
  # https://stackoverflow.com/questions/4249488/find-region-from-within-an-ec2-instance
  begin
    az = `curl -s --max-time 3 --connect-timeout 5 http://169.254.169.254/latest/meta-data/placement/availability-zone`
    region = az.strip.chop # remove last char
    region = nil if region == ''
  rescue
  end
  return region if region

  'us-east-1' # default if all else fails
end

#s3_bucketObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jets/aws_info.rb', line 61

def s3_bucket
  return "fake-test-s3-bucket" if ENV['TEST']
  return @@s3_bucket unless @@s3_bucket == BUCKET_DOES_NOT_YET_EXIST

  resp = cfn.describe_stacks(stack_name: Jets::Naming.parent_stack_name)
  stack = resp.stacks.first
  output = stack["outputs"].find { |o| o["output_key"] == "S3Bucket" }
  @@s3_bucket = output["output_value"] # s3_bucket
rescue
  # rescuing all exceptions in case here
  # Here are the known ones: Aws::CloudFormation::Errors::ValidationError, Aws::CloudFormation::Errors::InvalidClientTokenId
  BUCKET_DOES_NOT_YET_EXIST
end

#test?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/jets/aws_info.rb', line 75

def test?
  ENV['TEST'] || ENV['CIRCLECI']
end