File Coverage

blib/lib/App/Goto/Amazon.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package App::Goto::Amazon;
2              
3 1     1   34352 use strict;
  1         3  
  1         36  
4 1     1   5 use warnings;
  1         2  
  1         29  
5 1     1   16 use v5.10;
  1         9  
  1         63  
6             our $VERSION = '0.01';
7              
8 1     1   2908 use Moo;
  1         39888  
  1         6  
9 1     1   3300 use App::Goto;
  1         40244  
  1         33  
10 1     1   1065 use Data::Dumper;
  1         7059  
  1         77  
11 1     1   885 use Config::Tiny;
  1         1005  
  1         27  
12 1     1   1499 use Net::Amazon::EC2;
  0            
  0            
13              
14             has ec2 => ( is => 'rw' );
15             has name => ( is => 'ro' );
16             has domain => ( is => 'rw', default => '' );
17             has instances => ( is => 'rw', default => sub { [] } );
18              
19             sub BUILD {
20             my $self = shift;
21              
22             # Connect to EC2
23             $self->ec2( Net::Amazon::EC2->new($self->ec2_params) );
24              
25             # Get all instances
26             my $reservations = $self->ec2->describe_instances;
27             foreach my $reservation (@$reservations) {
28             foreach my $instance ($reservation->instances_set) {
29             # Ensure only running instances
30             next unless $instance->instance_state->name eq 'running';
31             push @{ $self->instances }, $instance->name;
32             }
33             }
34              
35             # Exit with a list of hosts if no name specified
36             my $name = $self->name;
37             unless ($name) {
38             say join "\n", sort @{$self->instances};
39             exit;
40             }
41             # We have all we need. Let's goto!
42             my $goto = App::Goto->new({ args => [qr/$name/], config => $self->config });
43             my $cmd = $goto->cmd;
44             $cmd =~ s/\s+$//;
45             exec ( $cmd . $self->domain ) if $goto->cmd;
46             print "No valid host found for given arguments\n";
47             }
48              
49             sub config {
50             my $self = shift;
51             my %hosts;
52             map { $hosts{$_} = $_ } @{ $self->instances };
53             my $hosts = { hosts => \%hosts };
54             return $hosts;
55             }
56              
57             sub ec2_params {
58             my $self = shift;
59             my $cfg = Config::Tiny->read($ENV{HOME}.'/.ssa');
60              
61             $self->domain( $cfg->{_}->{domain} ) if $cfg->{_}->{domain};
62             map { $_ => $cfg->{_}->{$_} } qw/AWSAccessKeyId SecretAccessKey region/;
63             }
64              
65              
66             1;
67             __END__
68              
69             =encoding utf-8
70              
71             =head1 NAME
72              
73             App::Goto::Amazon - Shorthand way of ssh'ing to AWS EC2 servers
74              
75             =head1 SYNOPSIS
76              
77             use App::Goto::Amazon;
78              
79             =head1 DESCRIPTION
80              
81             App::Goto::Amazon is called by the included 'ssa' script. If no arguments are supplied,
82             ssa will simply print a list of running EC2 instances. If arguments are supplied, then
83             the script will look for an instance whose name matches all supplied arguments (in order)
84             and, if it finds one, will ssh to it. If the arguments are ambiguous, the first match will
85             be used
86              
87             Requires your Amazon keys in a ~/.ssa file - see ssa.example file for template
88              
89             =head1 AUTHOR
90              
91             Dominic Humphries E<lt>dominic@oneandoneis2.comE<gt>
92              
93             =head1 COPYRIGHT
94              
95             Copyright 2014- Dominic Humphries
96              
97             =head1 LICENSE
98              
99             This library is free software; you can redistribute it and/or modify
100             it under the same terms as Perl itself.
101              
102             =head1 SEE ALSO
103              
104             =cut