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

#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from Jets::AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from Jets::AwsServices::GlobalMemoist

included

Instance Method Details

#accountObject

aws sts get-caller-identity



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jets/aws_info.rb', line 50

def 
  return '123456789' if Jets.env.test?
  return ENV['JETS_AWS_ACCOUNT'] if ENV['JETS_AWS_ACCOUNT']

  # ensure region set, required for sts.get_caller_identity.account to work
  ENV['AWS_REGION'] ||= region
  begin
    sts.get_caller_identity.
  rescue Aws::Errors::MissingCredentialsError, Aws::Errors::NoSuchEndpointError
    puts "INFO: You're missing AWS credentials. Only local services are currently available"
  rescue Seahorse::Client::NetworkingError
    puts "INFO: No internet connection available. Only local services are currently available"
  end
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
44
45
46
# File 'lib/jets/aws_info.rb', line 7

def region
  return 'us-east-1' if Jets.env.test?

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

  region = nil

  # First if aws binary is available
  # try to get it from the ~/.aws/config
  if which('aws')
    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}".color(:yellow)
      else
        # show full message as warning
        puts region.color(: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
  end

  # 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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jets/aws_info.rb', line 72

def s3_bucket
  return "fake-test-s3-bucket" if Jets.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 Exception => e
  # When user uses Jets::Application.default_iam_policy in their config/application.rb
  # it looks up the s3 bucket for the iam policy, but the project name has
  # not been loaded in the config yet.  We do some trickery with loading
  # the config twice in Application#load_app_config
  # The first load will result in a Aws::CloudFormation::Errors::ValidationError
  # since the Jets::Naming.parent_stack_name has not been properly set yet.
  #
  # Rescuing all exceptions in case there are other exceptions dont know about yet
  # Here are the known ones: Aws::CloudFormation::Errors::ValidationError, Aws::CloudFormation::Errors::InvalidClientTokenId
  BUCKET_DOES_NOT_YET_EXIST
end