Commit c82d367ae7eb560732b905eb33265f1021f2b1d0

Authored by Athos
1 parent 6f1ee21c

Create CoprStatus package

Signed-off-by: Athos Ribeiro <athoscribeiro@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Showing 3 changed files with 180 additions and 174 deletions   Show diff stats
app.psgi 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +#!/usr/bin/perl
  2 +use strict;
  3 +use warnings;
  4 +use CoprStatus;
  5 +use LWP::UserAgent;
  6 +use Plack::Builder;
  7 +use Plack::Request;
  8 +
  9 +my $app = sub {
  10 + my $env = shift;
  11 +
  12 + my $request = Plack::Request->new($env);
  13 + my $route = $CoprStatus::ROUTING{$request->path_info};
  14 + if ($route) {
  15 + return $route->($env);
  16 + }
  17 + return [
  18 + '404',
  19 + [ 'Content-Type' => 'text/html' ],
  20 + [ '404 Not Found' ],
  21 + ];
  22 +};
  23 +
  24 +builder {
  25 + enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
  26 + $app;
  27 +}
... ...
lib/CoprStatus.pm 0 → 100644
... ... @@ -0,0 +1,153 @@
  1 +package CoprStatus;
  2 +use strict;
  3 +use warnings;
  4 +use JSON;
  5 +use Text::Template;
  6 +
  7 +$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
  8 +
  9 +sub copr_info {
  10 + my $ua = LWP::UserAgent->new;
  11 + $ua->timeout(20);
  12 + $ua->env_proxy;
  13 + $ua->ssl_opts(SSL_verify_mode => 0x00);
  14 +
  15 + my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
  16 + my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/");
  17 +
  18 + my $json = JSON->new->allow_nonref;
  19 +
  20 + my $dec_result_v4 = $json->decode($result_v4->decoded_content);
  21 + my $dec_result_v5 = $json->decode($result_v5->decoded_content);
  22 + my $info = {};
  23 +
  24 + foreach(@{$dec_result_v4->{'packages'}}) {
  25 + my $package = $_->{'pkg_name'};
  26 + my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
  27 + my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
  28 + $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";
  29 + }
  30 +
  31 + foreach(@{$dec_result_v5->{'packages'}}) {
  32 + my $package = $_->{'pkg_name'};
  33 + my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
  34 + my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
  35 + $info->{$package}->{'v5_version'} = $version if $status eq "succeeded";
  36 + }
  37 +
  38 + foreach my $key (%{$info}) {
  39 + next if(ref($key) eq 'HASH');
  40 +
  41 + my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec");
  42 + my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
  43 + if($version =~ /%\{version\}/) {
  44 + $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
  45 + }
  46 +
  47 + my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
  48 + $version = "$version-$release";
  49 + $info->{$key}->{'git_version'} = $version;
  50 + }
  51 +
  52 + return $info;
  53 +}
  54 +
  55 +sub compare_versions {
  56 + my $info = copr_info();
  57 + my $match = {};
  58 + foreach my $key (%{$info}) {
  59 + next if(ref($key) eq 'HASH');
  60 + if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) {
  61 + $match->{$key} = 1;
  62 + }
  63 + else {
  64 + $match->{$key} = 0;
  65 + }
  66 + }
  67 +
  68 + return $match;
  69 +}
  70 +
  71 +sub info2html {
  72 + my $info = copr_info();
  73 + my $table_entries="";
  74 + foreach my $key (%{$info}) {
  75 + next if(ref($key) eq 'HASH');
  76 + my $fill_v4_row;
  77 + my $fill_v5_row;
  78 + if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) {
  79 + $fill_v4_row = "success";
  80 + }
  81 + else {
  82 + $fill_v4_row = "danger";
  83 + }
  84 +
  85 + if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) {
  86 + $fill_v5_row = "success";
  87 + }
  88 + else {
  89 + $fill_v5_row = "danger";
  90 + }
  91 +
  92 + $table_entries .= "<tr>
  93 + <td><b>$key</b></td>
  94 + <td>$info->{$key}->{'git_version'}</td>
  95 + <td class=\"$fill_v4_row\">$info->{$key}->{'v4_version'}</td>
  96 + <td class=\"$fill_v5_row\">$info->{$key}->{'v5_version'}</td>
  97 + </tr>";
  98 + }
  99 +
  100 + return $table_entries;
  101 +}
  102 +
  103 +sub build_html {
  104 + my $data = {
  105 + title => "SPB Copr Stats",
  106 + table_entries => info2html()
  107 + };
  108 +
  109 + my $template = Text::Template->new(
  110 + TYPE => 'FILE',
  111 + SOURCE => 'template.html.tt'
  112 + );
  113 +
  114 + return $template->fill_in(HASH => $data);
  115 +}
  116 +
  117 +our %ROUTING = (
  118 + '/' => \&serve_html,
  119 + '/api' => \&serve_json,
  120 + '/api/status' => \&serve_json_status
  121 + );
  122 +
  123 +sub serve_html {
  124 + return [
  125 + '200',
  126 + [ 'Content-Type' => 'text/html'],
  127 + [ build_html() ],
  128 + ];
  129 +};
  130 +
  131 +sub serve_json {
  132 + my $info = copr_info();
  133 + my $json = JSON->new->allow_nonref;
  134 + my $json_info = $json->encode($info);
  135 + return [
  136 + '200',
  137 + [ 'Content-Type' => 'application/json'],
  138 + [ $json_info ],
  139 + ];
  140 +};
  141 +
  142 +sub serve_json_status {
  143 + my $info = compare_versions();
  144 + my $json = JSON->new->allow_nonref;
  145 + my $json_info = $json->encode($info);
  146 + return [
  147 + '200',
  148 + [ 'Content-Type' => 'application/json'],
  149 + [ $json_info ],
  150 + ];
  151 +};
  152 +
  153 +1;
... ...
spb_copr_status.psgi
... ... @@ -1,174 +0,0 @@
1   -#!/usr/bin/perl
2   -use strict;
3   -use warnings;
4   -use JSON;
5   -use Text::Template;
6   -use LWP::UserAgent;
7   -use Plack::Builder;
8   -use Plack::Request;
9   -
10   -$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
11   -
12   -sub copr_info {
13   - my $ua = LWP::UserAgent->new;
14   - $ua->timeout(20);
15   - $ua->env_proxy;
16   - $ua->ssl_opts(SSL_verify_mode => 0x00);
17   -
18   - my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
19   - my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/");
20   -
21   - my $json = JSON->new->allow_nonref;
22   -
23   - my $dec_result_v4 = $json->decode($result_v4->decoded_content);
24   - my $dec_result_v5 = $json->decode($result_v5->decoded_content);
25   - my $info = {};
26   -
27   - foreach(@{$dec_result_v4->{'packages'}}) {
28   - my $package = $_->{'pkg_name'};
29   - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
30   - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
31   - $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";
32   - }
33   -
34   - foreach(@{$dec_result_v5->{'packages'}}) {
35   - my $package = $_->{'pkg_name'};
36   - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
37   - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
38   - $info->{$package}->{'v5_version'} = $version if $status eq "succeeded";
39   - }
40   -
41   - foreach my $key (%{$info}) {
42   - next if(ref($key) eq 'HASH');
43   -
44   - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec");
45   - my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
46   - if($version =~ /%\{version\}/) {
47   - $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
48   - }
49   -
50   - my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
51   - $version = "$version-$release";
52   - $info->{$key}->{'git_version'} = $version;
53   - }
54   -
55   - return $info;
56   -}
57   -
58   -sub compare_versions {
59   - my $info = copr_info();
60   - my $match = {};
61   - foreach my $key (%{$info}) {
62   - next if(ref($key) eq 'HASH');
63   - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) {
64   - $match->{$key} = 1;
65   - }
66   - else {
67   - $match->{$key} = 0;
68   - }
69   - }
70   -
71   - return $match;
72   -}
73   -
74   -sub info2html {
75   - my $info = copr_info();
76   - my $table_entries="";
77   - foreach my $key (%{$info}) {
78   - next if(ref($key) eq 'HASH');
79   - my $fill_v4_row;
80   - my $fill_v5_row;
81   - if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) {
82   - $fill_v4_row = "success";
83   - }
84   - else {
85   - $fill_v4_row = "danger";
86   - }
87   -
88   - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version}) {
89   - $fill_v5_row = "success";
90   - }
91   - else {
92   - $fill_v5_row = "danger";
93   - }
94   -
95   - $table_entries .= "<tr>
96   - <td><b>$key</b></td>
97   - <td>$info->{$key}->{'git_version'}</td>
98   - <td class=\"$fill_v4_row\">$info->{$key}->{'v4_version'}</td>
99   - <td class=\"$fill_v5_row\">$info->{$key}->{'v5_version'}</td>
100   - </tr>";
101   - }
102   -
103   - return $table_entries;
104   -}
105   -
106   -sub build_html {
107   - my $data = {
108   - title => "SPB Copr Stats",
109   - table_entries => info2html()
110   - };
111   -
112   - my $template = Text::Template->new(
113   - TYPE => 'FILE',
114   - SOURCE => 'template.html.tt'
115   - );
116   -
117   - return $template->fill_in(HASH => $data);
118   -}
119   -
120   -my %ROUTING = (
121   - '/' => \&serve_html,
122   - '/api' => \&serve_json,
123   - '/api/status' => \&serve_json_status
124   - );
125   -
126   -my $app = sub {
127   - my $env = shift;
128   -
129   - my $request = Plack::Request->new($env);
130   - my $route = $ROUTING{$request->path_info};
131   - if ($route) {
132   - return $route->($env);
133   - }
134   - return [
135   - '404',
136   - [ 'Content-Type' => 'text/html' ],
137   - [ '404 Not Found' ],
138   - ];
139   -};
140   -
141   -sub serve_html {
142   - return [
143   - '200',
144   - [ 'Content-Type' => 'text/html'],
145   - [ build_html() ],
146   - ];
147   -};
148   -
149   -sub serve_json {
150   - my $info = copr_info();
151   - my $json = JSON->new->allow_nonref;
152   - my $json_info = $json->encode($info);
153   - return [
154   - '200',
155   - [ 'Content-Type' => 'application/json'],
156   - [ $json_info ],
157   - ];
158   -};
159   -
160   -sub serve_json_status {
161   - my $info = compare_versions();
162   - my $json = JSON->new->allow_nonref;
163   - my $json_info = $json->encode($info);
164   - return [
165   - '200',
166   - [ 'Content-Type' => 'application/json'],
167   - [ $json_info ],
168   - ];
169   -};
170   -
171   -builder {
172   - enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
173   - $app;
174   -}