Class: Jets::AwsServices::AwsInfo

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Jets::AwsServices
Defined in:
lib/jets/aws_services/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 Jets::AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from StackStatus

#output_value, #stack_exists?

Methods included from GlobalMemoist

included, #reset_cache!

Instance Method Details

#accountObject

aws sts get-caller-identity



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/aws_services/aws_info.rb', line 57

def 
  return "123456789" if Jets.env.test?
  return jets_info. if jets_info.respond_to?(: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, Aws::STS::Errors::InvalidClientTokenId
    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

#jets_infoObject



52
53
54
# File 'lib/jets/aws_services/aws_info.rb', line 52

def jets_info
  Jets::Core::Config::Info.instance
end

#regionObject



6
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
47
48
49
# File 'lib/jets/aws_services/aws_info.rb', line 6

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 = begin
      `aws configure get region 2>&1`.strip
    rescue
      nil
    end
    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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jets/aws_services/aws_info.rb', line 79

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::Names.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::Names.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