| 1 | package MYDan::Bootstrap::ProcUpdate; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Bootstrap::ProcUpdate | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Bootstrap::ProcUpdate; | ||
| 10 | |||
| 11 | MYDan::Bootstrap::ProcUpdate->new( exec => /path/, interval => 3600, url => 'https://xxx' )->run(); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | use YAML::XS; | ||
| 20 | use File::Basename; | ||
| 21 | use LWP::UserAgent; | ||
| 22 | use File::Temp; | ||
| 23 | use Digest::MD5; | ||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my ( $class, %self ) = @_; | ||
| 28 | |||
| 29 | map{ die "$_ undef" unless $self{$_} }qw( url exec ); | ||
| 30 | |||
| 31 | die "noexec" unless -e $self{exec}; | ||
| 32 | |||
| 33 | bless \%self, ref $class || $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub run | ||
| 37 | { | ||
| 38 | my $this = shift; | ||
| 39 | |||
| 40 | $this->update(); | ||
| 41 | |||
| 42 | return unless my $interval = $this->{interval}; | ||
| 43 | |||
| 44 | sleep int 3 + rand $interval; | ||
| 45 | |||
| 46 | while(1) | ||
| 47 | { | ||
| 48 | $this->update(); | ||
| 49 | sleep $interval; | ||
| 50 | } | ||
| 51 | |||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub update | ||
| 56 | { | ||
| 57 | my $this = shift; | ||
| 58 | |||
| 59 | print "update ...\n"; | ||
| 60 | |||
| 61 | my ( $url, $exec ) = @$this{qw( url exec )}; | ||
| 62 | return unless my $key = uaget( $url ); | ||
| 63 | |||
| 64 | my ( %d, %dd, $ddd ); | ||
| 65 | for( split /\n/, $key ) | ||
| 66 | { | ||
| 67 | my ( $md5, $url, $name ); | ||
| 68 | |||
| 69 | unless( ( $md5, $url ) = $_ =~ /^([a-zA-Z0-9]{32}):(http.+)$/ ) | ||
| 70 | { | ||
| 71 | $ddd = 1; next; | ||
| 72 | } | ||
| 73 | |||
| 74 | $dd{ $name = basename $url } = 1; | ||
| 75 | |||
| 76 | next if -e "$exec/$name"; | ||
| 77 | next unless my $c = uaget( $url ); | ||
| 78 | |||
| 79 | my $TEMP = File::Temp->new(); | ||
| 80 | print $TEMP $c; | ||
| 81 | seek $TEMP, 0, 0; | ||
| 82 | my $tmd5 = Digest::MD5->new()->addfile( $TEMP )->hexdigest(); | ||
| 83 | |||
| 84 | next unless lc( $md5 ) eq lc( $tmd5 ); | ||
| 85 | chmod 0700, $TEMP->filename; | ||
| 86 | system "mv '$TEMP->filename' '$exec/$name'"; | ||
| 87 | } | ||
| 88 | |||
| 89 | return if $ddd; | ||
| 90 | |||
| 91 | map{ $d{ basename $_ } = 1 }glob "$exec/*"; | ||
| 92 | map{ delete $dd{$_} if delete $d{$_} }keys %dd; | ||
| 93 | return if keys %dd; | ||
| 94 | map{ unlink "$exec/$_" }keys %d; | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | sub uaget | ||
| 99 | { | ||
| 100 | my $url = shift; | ||
| 101 | |||
| 102 | my $ua = LWP::UserAgent->new(); | ||
| 103 | $ua->agent('Mozilla/9 [en] (Centos; Linux)'); | ||
| 104 | $ua->timeout( 10 ); | ||
| 105 | |||
| 106 | my $res = $ua->get( $url ); | ||
| 107 | my $code = $res->code(); | ||
| 108 | warn "get $url code: $code\n" unless $code == 200; | ||
| 109 | return $res->is_success ? $res->content : undef; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |