From c82d367ae7eb560732b905eb33265f1021f2b1d0 Mon Sep 17 00:00:00 2001 From: Athos Ribeiro Date: Tue, 3 Nov 2015 14:07:04 -0200 Subject: [PATCH] Create CoprStatus package --- app.psgi | 27 +++++++++++++++++++++++++++ lib/CoprStatus.pm | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spb_copr_status.psgi | 174 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 3 files changed, 180 insertions(+), 174 deletions(-) create mode 100644 app.psgi create mode 100644 lib/CoprStatus.pm delete mode 100644 spb_copr_status.psgi diff --git a/app.psgi b/app.psgi new file mode 100644 index 0000000..22670cb --- /dev/null +++ b/app.psgi @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use strict; +use warnings; +use CoprStatus; +use LWP::UserAgent; +use Plack::Builder; +use Plack::Request; + +my $app = sub { + my $env = shift; + + my $request = Plack::Request->new($env); + my $route = $CoprStatus::ROUTING{$request->path_info}; + if ($route) { + return $route->($env); + } + return [ + '404', + [ 'Content-Type' => 'text/html' ], + [ '404 Not Found' ], + ]; +}; + +builder { + enable "Static", path => qr!^(/css|/js)!, pass_through => 1; + $app; +} diff --git a/lib/CoprStatus.pm b/lib/CoprStatus.pm new file mode 100644 index 0000000..f0546e9 --- /dev/null +++ b/lib/CoprStatus.pm @@ -0,0 +1,153 @@ +package CoprStatus; +use strict; +use warnings; +use JSON; +use Text::Template; + +$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; + +sub copr_info { + my $ua = LWP::UserAgent->new; + $ua->timeout(20); + $ua->env_proxy; + $ua->ssl_opts(SSL_verify_mode => 0x00); + + my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/"); + my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/"); + + my $json = JSON->new->allow_nonref; + + my $dec_result_v4 = $json->decode($result_v4->decoded_content); + my $dec_result_v5 = $json->decode($result_v5->decoded_content); + my $info = {}; + + foreach(@{$dec_result_v4->{'packages'}}) { + my $package = $_->{'pkg_name'}; + my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; + my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; + $info->{$package}->{'v4_version'} = $version if $status eq "succeeded"; + } + + foreach(@{$dec_result_v5->{'packages'}}) { + my $package = $_->{'pkg_name'}; + my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; + my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; + $info->{$package}->{'v5_version'} = $version if $status eq "succeeded"; + } + + foreach my $key (%{$info}) { + next if(ref($key) eq 'HASH'); + + my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec"); + my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; + if($version =~ /%\{version\}/) { + $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; + } + + my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; + $version = "$version-$release"; + $info->{$key}->{'git_version'} = $version; + } + + return $info; +} + +sub compare_versions { + my $info = copr_info(); + my $match = {}; + foreach my $key (%{$info}) { + next if(ref($key) eq 'HASH'); + if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) { + $match->{$key} = 1; + } + else { + $match->{$key} = 0; + } + } + + return $match; +} + +sub info2html { + my $info = copr_info(); + my $table_entries=""; + foreach my $key (%{$info}) { + next if(ref($key) eq 'HASH'); + my $fill_v4_row; + my $fill_v5_row; + if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) { + $fill_v4_row = "success"; + } + else { + $fill_v4_row = "danger"; + } + + if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) { + $fill_v5_row = "success"; + } + else { + $fill_v5_row = "danger"; + } + + $table_entries .= " + $key + $info->{$key}->{'git_version'} + $info->{$key}->{'v4_version'} + $info->{$key}->{'v5_version'} + "; + } + + return $table_entries; +} + +sub build_html { + my $data = { + title => "SPB Copr Stats", + table_entries => info2html() + }; + + my $template = Text::Template->new( + TYPE => 'FILE', + SOURCE => 'template.html.tt' + ); + + return $template->fill_in(HASH => $data); +} + +our %ROUTING = ( + '/' => \&serve_html, + '/api' => \&serve_json, + '/api/status' => \&serve_json_status + ); + +sub serve_html { + return [ + '200', + [ 'Content-Type' => 'text/html'], + [ build_html() ], + ]; +}; + +sub serve_json { + my $info = copr_info(); + my $json = JSON->new->allow_nonref; + my $json_info = $json->encode($info); + return [ + '200', + [ 'Content-Type' => 'application/json'], + [ $json_info ], + ]; +}; + +sub serve_json_status { + my $info = compare_versions(); + my $json = JSON->new->allow_nonref; + my $json_info = $json->encode($info); + return [ + '200', + [ 'Content-Type' => 'application/json'], + [ $json_info ], + ]; +}; + +1; diff --git a/spb_copr_status.psgi b/spb_copr_status.psgi deleted file mode 100644 index ea680d5..0000000 --- a/spb_copr_status.psgi +++ /dev/null @@ -1,174 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -use JSON; -use Text::Template; -use LWP::UserAgent; -use Plack::Builder; -use Plack::Request; - -$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; - -sub copr_info { - my $ua = LWP::UserAgent->new; - $ua->timeout(20); - $ua->env_proxy; - $ua->ssl_opts(SSL_verify_mode => 0x00); - - my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/"); - my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/"); - - my $json = JSON->new->allow_nonref; - - my $dec_result_v4 = $json->decode($result_v4->decoded_content); - my $dec_result_v5 = $json->decode($result_v5->decoded_content); - my $info = {}; - - foreach(@{$dec_result_v4->{'packages'}}) { - my $package = $_->{'pkg_name'}; - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; - $info->{$package}->{'v4_version'} = $version if $status eq "succeeded"; - } - - foreach(@{$dec_result_v5->{'packages'}}) { - my $package = $_->{'pkg_name'}; - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; - $info->{$package}->{'v5_version'} = $version if $status eq "succeeded"; - } - - foreach my $key (%{$info}) { - next if(ref($key) eq 'HASH'); - - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec"); - my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; - if($version =~ /%\{version\}/) { - $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; - } - - my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; - $version = "$version-$release"; - $info->{$key}->{'git_version'} = $version; - } - - return $info; -} - -sub compare_versions { - my $info = copr_info(); - my $match = {}; - foreach my $key (%{$info}) { - next if(ref($key) eq 'HASH'); - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) { - $match->{$key} = 1; - } - else { - $match->{$key} = 0; - } - } - - return $match; -} - -sub info2html { - my $info = copr_info(); - my $table_entries=""; - foreach my $key (%{$info}) { - next if(ref($key) eq 'HASH'); - my $fill_v4_row; - my $fill_v5_row; - if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) { - $fill_v4_row = "success"; - } - else { - $fill_v4_row = "danger"; - } - - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) { - $fill_v5_row = "success"; - } - else { - $fill_v5_row = "danger"; - } - - $table_entries .= " - $key - $info->{$key}->{'git_version'} - $info->{$key}->{'v4_version'} - $info->{$key}->{'v5_version'} - "; - } - - return $table_entries; -} - -sub build_html { - my $data = { - title => "SPB Copr Stats", - table_entries => info2html() - }; - - my $template = Text::Template->new( - TYPE => 'FILE', - SOURCE => 'template.html.tt' - ); - - return $template->fill_in(HASH => $data); -} - -my %ROUTING = ( - '/' => \&serve_html, - '/api' => \&serve_json, - '/api/status' => \&serve_json_status - ); - -my $app = sub { - my $env = shift; - - my $request = Plack::Request->new($env); - my $route = $ROUTING{$request->path_info}; - if ($route) { - return $route->($env); - } - return [ - '404', - [ 'Content-Type' => 'text/html' ], - [ '404 Not Found' ], - ]; -}; - -sub serve_html { - return [ - '200', - [ 'Content-Type' => 'text/html'], - [ build_html() ], - ]; -}; - -sub serve_json { - my $info = copr_info(); - my $json = JSON->new->allow_nonref; - my $json_info = $json->encode($info); - return [ - '200', - [ 'Content-Type' => 'application/json'], - [ $json_info ], - ]; -}; - -sub serve_json_status { - my $info = compare_versions(); - my $json = JSON->new->allow_nonref; - my $json_info = $json->encode($info); - return [ - '200', - [ 'Content-Type' => 'application/json'], - [ $json_info ], - ]; -}; - -builder { - enable "Static", path => qr!^(/css|/js)!, pass_through => 1; - $app; -} -- libgit2 0.21.2