spb_copr_status.psgi
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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(10);
$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 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 .= "<tr>
<td><b>$key</b></td>
<td>$info->{$key}->{'git_version'}</td>
<td class=\"$fill_v4_row\">$info->{$key}->{'v4_version'}</td>
<td class=\"$fill_v5_row\">$info->{$key}->{'v5_version'}</td>
</tr>";
}
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
);
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 ],
];
};
builder {
enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
$app;
}