App-cryp-mn

 view release on metacpan or  search on metacpan

lib/App/cryp/Masternode/bulwark.pm  view on Meta::CPAN

            last;
        }

        my $fh;
        unless (open $fh, "<", $conf_path) {
            log_error "Can't open '$conf_path': $!, skipped reading ".
                "local wallet masternode configuration file";
            last;
        }

        my $linum = 0;
        while (my $line = <$fh>) {
            $linum++;
            $line =~ /\S/ or next;
            $line =~ /^\s*#/ and next;
            $line =~ /^(\S+)\s+([0-9]+(?:\.[0-9]+){3}):([0-9]+)\s+(\S+)\s+(\S+)\s+(\d+)\s*$/ or do {
                log_warn "$conf_path:$linum: Doesn't match pattern, ignored";
                next;
            };
            push @res, {
                name => $1,
                ip   => $2,
                port => $3,
                collateral_txid => $5,
                collateral_oidx => $6,
            };
        }
        close $fh;

      CHECK_STATUS:
        {
            last unless $args{detail} && $args{with_status} && @res;

            # pick one masternode to ssh into
            my $rec = $res[rand @res];

            my $ssh_user =
                $crypconf->{masternodes}{bulwark}{$rec->{name}}{ssh_user} //
                $crypconf->{masternodes}{bulwark}{default}{ssh_user} //
                "root";
            my $mn_user  =
                $crypconf->{masternodes}{bulwark}{$rec->{name}}{mn_user} //
                $crypconf->{masternodes}{bulwark}{default}{mn_user} //
                $ssh_user; # XXX can also detect

            log_trace "ssh_user=<$ssh_user>, mn_user=<$mn_user>";

            if ($ssh_user ne 'root' && $ssh_user ne $mn_user) {
                log_error "Won't be able to access bulwark-cli (user $mn_user) while we SSH as $ssh_user, skipped";
                last;
            }

            my $ssh_timeout =
                $crypconf->{masternodes}{bulwark}{$rec->{name}}{ssh_timeout} //
                $crypconf->{masternodes}{bulwark}{default}{ssh_timeout} //
                $conf->{GLOBAL}{ssh_timeout} // 300;

            log_trace "SSH-ing to $rec->{name} ($rec->{ip}) as $ssh_user to query masternode status (timeout=$ssh_timeout) ...";

            eval {
                local $SIG{ALRM} = sub { die "Timeout\n" };
                # XXX doesn't cleanup ssh process when timeout triggers. same
                # with IPC::Cmd, or System::Timeout (which is based on
                # IPC::Cmd). IPC::Run's timeout doesn't work?
                alarm $ssh_timeout;

                my $ssh_cmd = $ssh_user eq $mn_user ?
                    "bulwark-cli listmasternodes" :
                    "su $mn_user -c ".shell_quote("bulwark-cli listmasternodes");

                my $output;
                system({log=>1, shell=>0, capture_stdout=>\$output},
                       "ssh", "-l", $ssh_user, $rec->{ip}, $ssh_cmd);

                my $output_decoded;
                eval { $output_decoded = JSON::MaybeXS->new->decode($output) };
                if ($@) {
                    log_error "Can't decode JSON output '$output', skipped";
                    last CHECK_STATUS;
                }

                my %mns;
                for (@$output_decoded) {
                    my $key = "$_->{txhash} $_->{outidx}";
                    $mns{$key} = $_;
                }

                for my $rec (@res) {
                    my $key = "$rec->{collateral_txid} $rec->{collateral_oidx}";
                    if (exists $mns{$key}) {
                        $rec->{status} = $mns{$key}{status};
                        $rec->{last_seen} = $mns{$key}{lastseen};
                        $rec->{active_time} = $mns{$key}{activetime};
                    } else {
                        $rec->{status} = "(not found)";
                    }
                }
            };
            if ($@) {
                log_error "SSH timeout: $@, skipped";
                last;
            }
        } # CHECK_STATUS

        unless ($args{detail}) {
            @res = map {$_->{name}} @res;
        }

        [200, "OK", \@res];
    }
}

1;

# ABSTRACT: Bulwark (BWK) Masternode driver for App::cryp

__END__

=pod

=encoding UTF-8

=head1 NAME

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.309 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )