blib/lib/MYDan/Util/Gateway.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 171 CC 19 SCAR 30.2
CC
19
Cov
0%
new
30.0
_status
30.0
off
30.0
CRAP 20.4
1 package MYDan::Util::Gateway;
2 use strict;
3 use warnings;
4  
5 use Expect;
6 use YAML::XS;
7 use Authen::OATH;
8 use Convert::Base32 qw( decode_base32 );
9 use MYDan::Node;
10 use MYDan::Util::Hosts;
11  
12 our $TIMEOUT = 20;
13  
14 =head1 SYNOPSIS
15  
16 use MYDan::Util::Gateway;
17 MYDan::Util::Gateway->new( '/path/conf');
18  
19 =cut
20  
21 sub new
22 {
23 my ( $class, $path ) = @_;
24  
25 my $conf = $path && -e $path ? eval{ YAML::XS::LoadFile $path } : +{};
26 die "load $path fail:$@" if $@;
27  
28 $conf = YAML::XS::Dump $conf;
29 map{ $conf =~ s/\$ENV\{$_\}/$ENV{$_}/g; }keys %ENV;
30 $conf = eval{ YAML::XS::Load $conf };
31 die "load conf fail:$@" if $@;
32  
33 my $range = MYDan::Node->new( MYDan::Util::OptConf->load()->dump( 'range') );
34 my $hosts = MYDan::Util::Hosts->new();
35 for my $k ( keys %$conf )
36 {
37 my $v = $conf->{$k};
38 next unless my $node = delete $v->{range};
39 delete $conf->{$k};
40 map{
41 my %v = %$v;
42 my %h = $hosts->match( $_ );
43 my $n = $h{$_} || $_;
44 $v{go} =~ s/{}/$n/g;
45 $conf->{"$k:$_"} = \%v;
46 }$range->load( $node )->list;
47 }
48  
49 bless $conf, ref $class || $class;
50 }
51  
52 sub _status
53 {
54 my ( $this, $name ) = @_;
55 return ( 'undef', 'undef' )unless my $v = $this->{$name};
56 my ( $go, $stat, $port ) = $v->{go};
57 $stat = 'config nomatch -D 127.0.0.1:\d+' unless $go =~ /-D\s+127.0.0.1:(\d+)\s+/;
58 $stat = `lsof -i:$1` ? 'on' : 'off' if $1;
59 return ( $stat, $1 );
60 }
61  
62 sub status
63 {
64 my $this = shift @_;
65 map{
66 printf "%s : stat => %s ; port => %s\n", $_, $this->_status($_);
67 }keys %$this;
68 }
69  
70 sub on
71 {
72 my ( $this, @name ) = @_;
73 @name = keys %$this unless @name;
74 for my $name ( @name )
75 {
76 my ( $stat, $port ) = $this->_status($name);
77 unless( $stat eq 'off' )
78 {
79 printf "%s : stat => %s ; port => %s\n", $name, $stat, $port;
80 next;
81 }
82 $this->go( $name );
83 printf "%s : stat => %s ; port => %s\n", $name, $this->_status($name);
84 }
85 }
86  
87  
88 sub off
89 {
90 my ( $this, @name ) = @_;
91 @name = keys %$this unless @name;
92 for my $name ( @name )
93 {
94 my ( $stat, $port ) = $this->_status($name);
95 unless( $stat eq 'on' )
96 {
97 printf "%s : stat => %s ; port => %s\n", $name, $stat, $port;
98 next;
99 }
100 map{ kill 'KILL', $1 if $_ =~ /^ssh\s+(\d+)\s+/ }`lsof -i:$port` if $port =~ /^\d+$/;
101 printf "%s : stat => %s ; port => %s\n", $name, $this->_status($name);
102 }
103 }
104  
105 sub go
106 {
107 my ( $this, $name ) = @_;
108  
109 my $conf = $this->{$name};
110  
111 my $exp = Expect->new();
112 $SIG{WINCH} = sub
113 {
114 $exp->slave->clone_winsize_from( \*STDIN );
115 kill WINCH => $exp->pid if $exp->pid;
116 local $SIG{WINCH} = $SIG{WINCH};
117 };
118  
119 $conf->{expect} = +{} unless $conf->{expect} && ref $conf->{expect} eq 'HASH';
120  
121 for ( keys %{$conf->{expect}} )
122 {
123 next unless $conf->{expect}{$_} =~ /googlecode\s*:\s*(\w+)/;
124 $conf->{expect}{$_} = Authen::OATH->new->totp( decode_base32( $1 ));
125 }
126  
127 $exp->slave->clone_winsize_from( \*STDIN );
128 print "debug:$conf->{go}\n" if $ENV{MYDan_DEBUG};
129 $exp->spawn( $conf->{go} );
130 $exp->expect
131 (
132 $TIMEOUT,
133 [ qr/[#\$%] $/ => sub { $exp->interact; } ],
134 [ qr/yes\/no/ => sub { $exp->send( "yes\n" ); exp_continue; } ],
135 map{ my $v = $conf->{expect}{$_};[ qr/$_/ => sub { $exp->send( "$v\n" ); exp_continue; } ] }keys %{ $conf->{expect} }
136 );
137 }
138  
139 1;