lib/VM/EC2/Instance/Located.pm

filter: Statementstmt 94.7% 18 / 19 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 89.2% 25 / 28 CC 3 SCAR 7.5
CC
3
Cov
87%
at_ec2
3.2
BEGIN
1.0
BEGIN
1.0
CRAP 2.1
↑ Summary
1 package VM::EC2::Instance::Located;
2 1 $VM::EC2::Instance::Located::VERSION = '0.13';
3 # ABSTRACT: A quick check to determine if the currently running code is running on a AWS EC2 instance.
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 VM::EC2::Instance::Located - determine if code is executing on an EC2 instance
12  
13 =head1 SYNOPSIS
14  
15 my $result = VM::EC2::Instance::Located::at_ec2();
16 if($result) {
17 print "Running at EC2\n";
18 } else {
19 print "Not running at EC2\n";
20 }
21  
22 =head1 DESCRIPTION
23  
24 Provides a function that determines if code is executing on an EC2
25 instance.
26  
27 Currently implemented by resolving instnace-data.ec2.internal. It
28 will succeed on an ec2 instance and fail otherwise.
29  
30 =cut
31  
32 =head1 METHODS
33  
34 =cut
35  
36 =head2 at_ec2
37  
38 Determines if the code is running at EC2.
39  
40 The answer is cached because typically the result does not change
41 unless you're able to move processes between an EC2 instance an a non
42 EC2 instance.
43  
44 Returns a boolean value answering the question.
45  
46 =cut
47  
48 1 my $target_hostname = 'instance-data.ec2.internal';
49  
50 1 my $known_answer;
51  
52 1 ▶sub at_ec2 {
Subroutine at_ec2: called CC=3 SCAR=11.8
Pod documented
53 1 ▶ return $known_answer if defined($known_answer);
Branch: 1 branch
BranchTrueFalseTotal
if defined $known_answer 0 1 1
54  
55 1 my $res = Net::DNS::Resolver->new;
56 1 my $reply = $res->search($target_hostname);
57
58 1 ▶ if ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply) 0 1 1
59 0 return $known_answer = 1;
60 }
61
62 1 return $known_answer = 0;
63 }
64  
65 1 1;
66  
67  
68  
↑ Summary