blib/lib/App/Goto2.pm

filter: Statementstmt 34.7% 25 / 72 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 6 totaltotal 25.6% 31 / 121 CC 19 SCAR 37.9
CC
19
Cov
25%
run
110.0
generate_ssh_cmd
30.0
get_aws
12.9
CRAP 44.5
↑ Summary
1 package App::Goto2;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.04';
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use MooseX::App::Simple qw(Color ConfigHome);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 option 'aws' => (
12 is => 'ro',
13 isa => 'Bool',
14 cmd_aliases => ['a'],
15 documentation => q/Retrieve AWS EC2 instances instead of using the config file/,
16 );
17  
18 1 option 'list' => (
19 is => 'ro',
20 isa => 'Bool',
21 cmd_aliases => ['l'],
22 documentation => q/Just print list of known servers & exit/,
23 );
24  
25 1 option 'verbose' => (
26 is => 'ro',
27 isa => 'Bool',
28 cmd_aliases => ['v'],
29 documentation => q/Verbose output/,
30 );
31  
32 1 option 'iterate' => (
33 is => 'ro',
34 isa => 'Bool',
35 cmd_aliases => ['i'],
36 documentation => q/Connect to all matching hosts one after the other/,
37 );
38  
39 1 option 'cmd' => (
40 is => 'ro',
41 isa => 'Str',
42 documentation => q/Run a command on the remote server(s)/,
43 );
44  
45 1 has 'config_hosts' => (
46 is => 'rw',
47 isa => 'Maybe[HashRef]',
48 );
49  
50 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
51 0 my ($self) = @_;
52  
53 # Use retrieved EC2 hosts instead of local config file entries if appropriate
54 0 ▶ $self->get_aws if $self->aws;
Branch: 1 branch
BranchTrueFalseTotal
if $self->aws 0 0 0
55  
56 0 my $hostre = join '.*', @{ $self->extra_argv };
57  
58 0 ▶ error("Must supply string to match host(s)") unless $hostre or $self->list;
Branch: 1 branch
BranchTrueFalseTotal
unless $hostre or $self->list 0 0 0
Condition0%0/3
$hostre or $self->list
l!l&&r!l&&!r
000
MC/DC0%0/2
$hostre or $self->list
$hostre $self->list
Truth table0%0/3
A or B
A= $hostreB= $self->list
ABresult
1X1
011
000
59  
60 0 my $hosts = $self->hosts;
61  
62 0 my @matching_hosts = grep { m/$hostre/ } sort keys %$hosts;
63  
64 0 ▶ error("No matching hosts found") unless @matching_hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @matching_hosts 0 0 0
65 0 ▶ say "Hosts found: @matching_hosts" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
66 # Just print a list of servers if appropriate
67 0 ▶ $self->print_list(\@matching_hosts) if $self->list;
Branch: 1 branch
BranchTrueFalseTotal
if $self->list 0 0 0
68  
69 # Either iterate over all matching hosts, or just use the first
70 0 for my $host ( @matching_hosts ) {
71 0 my $cmd = $self->generate_ssh_cmd($hosts->{$host});
72 0 ▶ say "Executing command: $cmd" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
73 0 system( $cmd );
74 0 ▶ exit unless $self->iterate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->iterate 0 0 0
75 }
76 }
77  
78 0 ▶sub get_aws {
Subroutine get_aws: not called CC=4 SCAR=25.6
Pod undocumented
79 0 my ($self) = @_;
80  
81 1 ▶ use Net::Amazon::EC2;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 0 my $aws = $self->_config_data->{aws};
84 my $ec2 = Net::Amazon::EC2->new(
85 AWSAccessKeyId => $aws->{AWSAccessKeyId},
86 SecretAccessKey => $aws->{SecretAccessKey},
87 region => $aws->{region}
88 0 );
89  
90 # Get all instances
91 0 my @instances;
92 0 my $reservations = $ec2->describe_instances;
93 0 foreach my $reservation (@$reservations) {
94 0 foreach my $instance ($reservation->instances_set) {
95 # Ensure only running instances
96 0 ▶ next unless $instance->instance_state->name eq 'running';
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->instance_state->name eq "running" 0 0 0
97 0 push @instances, $instance->name;
98 }
99 }
100  
101 # Create new hosts hash
102 0 my %hosts = map { $_ => { hostname => $_ . $aws->{domain} } } @instances;
103  
104 # Over-write the local config
105 0 $self->config_hosts(\%hosts);
106 }
107  
108 0 ▶sub print_list {
Subroutine print_list: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my ($self, $hostnames) = @_;
110  
111 0 my $hosts = $self->hosts;
112  
113 0 for my $host (@$hostnames) {
114 0 say "$host: " . $hosts->{$host}{hostname};
115 }
116 0 exit;
117 }
118  
119 0 ▶sub generate_ssh_cmd {
Subroutine generate_ssh_cmd: not called CC=5 SCAR=34.0
Pod undocumented
120 0 my ($self, $host) = @_;
121  
122 0 my $cmd = 'ssh ';
123  
124 0 ▶ $cmd .= ' -p ' . $host->{port} . ' ' if $host->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $host->{"port"} 0 0 0
125 0 ▶ $cmd .= ' -i ~/.ssh/' . $host->{ssh_key} . ' ' if $host->{ssh_key};
Branch: 1 branch
BranchTrueFalseTotal
if $host->{"ssh_key"} 0 0 0
126  
127 0 ▶ $cmd .= ' ' . $host->{user} . '@' if $host->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $host->{"user"} 0 0 0
128 0 $cmd .= $host->{hostname};
129  
130 0 ▶ $cmd .= " '" . $self->{cmd} . "'" if $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cmd"} 0 0 0
131  
132 0 return $cmd;
133 }
134  
135 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my ($msg) = @_;
137 0 say $msg;
138 0 exit 1;
139 }
140  
141 0 ▶sub hosts {
Subroutine hosts: not called CC=2 SCAR=17.9
Pod undocumented
142 0 my ($self) = @_;
143 0 ▶ $self->config_hosts( $self->_config_data->{hosts} ) unless $self->config_hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config_hosts 0 0 0
144 0 return $self->config_hosts
145 }
146  
147 1 1;
148 __END__
↑ Summary