| 1 | package MYDan::Util::Go; | ||
| 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 | |||
| 10 | our $TIMEOUT = 20; | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use MYDan::Util::Go; | ||
| 15 | MYDan::Util::Go->new( '/path/go')->go( 'grep' ); | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ( $class, $path ) = @_; | ||
| 22 | |||
| 23 | my $conf = $path && -e $path ? eval{ YAML::XS::LoadFile $path } : +{}; | ||
| 24 | die "load $path fail:$@" if $@; | ||
| 25 | |||
| 26 | $conf = YAML::XS::Dump $conf; | ||
| 27 | map{ $conf =~ s/\$ENV\{$_\}/$ENV{$_}/g; }keys %ENV; | ||
| 28 | $conf = eval{ YAML::XS::Load $conf }; | ||
| 29 | die "load conf fail:$@" if $@; | ||
| 30 | |||
| 31 | my $range = MYDan::Node->new( MYDan::Util::OptConf->load()->dump( 'range') ); | ||
| 32 | my $hosts = MYDan::Util::Hosts->new(); | ||
| 33 | for my $k ( keys %$conf ) | ||
| 34 | { | ||
| 35 | my $v = $conf->{$k}; | ||
| 36 | next unless my $node = delete $v->{range}; | ||
| 37 | delete $conf->{$k}; | ||
| 38 | map{ | ||
| 39 | my %v = %$v; | ||
| 40 | my %h = $hosts->match( $_ ); | ||
| 41 | my $n = $h{$_} || $_; | ||
| 42 | $v{go} =~ s/{}/$n/g; | ||
| 43 | $conf->{"$k:$_"} = \%v; | ||
| 44 | }$range->load( $node )->list; | ||
| 45 | } | ||
| 46 | |||
| 47 | bless $conf, ref $class || $class; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub go | ||
| 51 | { | ||
| 52 | |||
| 53 | my ( $this, $grep ) = @_; | ||
| 54 | |||
| 55 | my @host = sort keys %$this; | ||
| 56 | |||
| 57 | GOTO: | ||
| 58 | |||
| 59 | @host = grep{ $_ =~ /$grep/ }@host if defined $grep; | ||
| 60 | |||
| 61 | return unless @host; | ||
| 62 | |||
| 63 | my $i = 0; | ||
| 64 | if( @host > 1 ) | ||
| 65 | { | ||
| 66 | my @host = map { sprintf "[ %d ] %s", $_ + 1, $host[$_] } 0 .. $#host; | ||
| 67 | print STDERR join "\n", @host, "please select: [ 1 ] "; | ||
| 68 | |||
| 69 | my $x = <STDIN>; | ||
| 70 | if( $x && $x =~ s/^\/// ) { $grep = $x; chomp $grep; goto GOTO; } | ||
| 71 | $i = $1 - 1 if $x =~ /(\d+)/ && $1 && $1 <= @host; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $conf = $this->{$host[$i]}; | ||
| 75 | |||
| 76 | my $exp = Expect->new(); | ||
| 77 | $SIG{WINCH} = sub | ||
| 78 | { | ||
| 79 | $exp->slave->clone_winsize_from( \*STDIN ); | ||
| 80 | kill WINCH => $exp->pid if $exp->pid; | ||
| 81 | local $SIG{WINCH} = $SIG{WINCH}; | ||
| 82 | }; | ||
| 83 | |||
| 84 | $conf->{expect} = +{} unless $conf->{expect} && ref $conf->{expect} eq 'HASH'; | ||
| 85 | |||
| 86 | for ( keys %{$conf->{expect}} ) | ||
| 87 | { | ||
| 88 | next unless $conf->{expect}{$_} =~ /googlecode\s*:\s*(\w+)/; | ||
| 89 | $conf->{expect}{$_} = Authen::OATH->new->totp( decode_base32( $1 )); | ||
| 90 | } | ||
| 91 | |||
| 92 | $exp->slave->clone_winsize_from( \*STDIN ); | ||
| 93 | $exp->spawn( $conf->{go} ); | ||
| 94 | $exp->expect | ||
| 95 | ( | ||
| 96 | $TIMEOUT, | ||
| 97 | [ qr/[#\$%] $/ => sub { $exp->interact; } ], | ||
| 98 | [ qr/yes\/no/ => sub { $exp->send( "yes\n" ); exp_continue; } ], | ||
| 99 | map{ my $v = $conf->{expect}{$_};[ qr/$_/ => sub { $exp->send( "$v\n" ); exp_continue; } ] }keys %{ $conf->{expect} } | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; |