From 457d289f1c7e0cf8016a81d506fd95399a4c59bd Mon Sep 17 00:00:00 2001 From: eleksir Date: Thu, 14 Nov 2019 01:43:38 +0300 Subject: [PATCH] Introduce buildinfo.pm, update sample config update TODO + Add buildinfo.pm - api for slackbuild env metadata * Update .psgi files * update sample config * update TODO - remove checked-in milestones --- TODO | 4 +-- data/myapi.json.sample | 4 +++ lib/buildinfo.pm | 66 ++++++++++++++++++++++++++++++++++++++++++ myapi-bots.psgi | 4 +-- myapi-confcheck.pl | 2 +- myapi-repo.psgi | 6 ++++ 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 lib/buildinfo.pm diff --git a/TODO b/TODO index 312fcb4..e6a595e 100644 --- a/TODO +++ b/TODO @@ -5,12 +5,10 @@ * paste/link shortener * shortener api for goo.gl or same services * paste api for some pastebins -* image downloader -* repo uploader * metagen.pm: make metadata files in atomic way * metagen.pm: validate metadata * simple monitoring api (common metrics: cpu, mem, swap, la, etc) * jabber bot: send memos to offline participants when they return * announce youtube video title * common bot functions move to separate module -* bot bridge telegram-irc-jabber \ No newline at end of file +* anounce html page title \ No newline at end of file diff --git a/data/myapi.json.sample b/data/myapi.json.sample index 088a2e5..6c6ccf3 100644 --- a/data/myapi.json.sample +++ b/data/myapi.json.sample @@ -46,4 +46,8 @@ "myslack" : "/var/www/repo/myslack", "stuff" : "/var/www/repo/stuff" }, + "buildinfo" : { + "backports" : "/path/to/myslack.json", + "thrirdparty" : "/path/to/stuff.json" + } } diff --git a/lib/buildinfo.pm b/lib/buildinfo.pm new file mode 100644 index 0000000..b2cf95c --- /dev/null +++ b/lib/buildinfo.pm @@ -0,0 +1,66 @@ +package buildinfo; + +use strict; +use warnings "all"; +use vars qw/$VERSION/; +use conf; +use JSON::PP; +use Data::Dumper; + +use Exporter qw(import); +our @EXPORT = qw(buildinfo); + +$VERSION = "1.0"; + +my $c = loadConf(); + +sub buildinfo($) { + my $repo = shift; + my ($status, $content, $msg) = ('400', 'text/plain', "Bad Request?\n"); + + return ($status, $content, $msg) unless ($repo =~ /\//); + + my ($config, $package) = split(/\//, $repo); + + unless (defined($c->{buildinfo}->{$config})) { + $msg = "Config repo $config is not defined in config.\n"; + return ($status, $content, $msg); + } + + open (JSON, "<", $c->{buildinfo}->{$config}) or do { + $status = '500'; + $msg = "Unable to open $c->{buildinfo}->{$config}; $!\n"; + return ($status, $content, $msg); + }; + + my $sep = $/; + my $json = ; + close JSON; + $/ = $sep; + + my $j = eval { decode_json($json) } or do { + $status = '500'; + $msg = "Error during decoding $c->{buildinfo}->{$config}; $@\n"; + return ($status, $content, $msg); + }; + + unless (defined($j->{$package})) { + $msg = "$package is not defined in config for $config.\n"; + return ($status, $content, $msg); + } + + $json = JSON::PP->new->pretty->canonical->indent_length(4)->encode($j->{$package}) or do { + $status = '500'; + $msg = "Unable to encode json.\n"; + return ($status, $content, $msg); + }; + + $status = '200'; + $content = 'application/json'; + $msg = $json; + return ($status, $content, $msg); +} + +1; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/myapi-bots.psgi b/myapi-bots.psgi index 19ae4e6..a90b063 100644 --- a/myapi-bots.psgi +++ b/myapi-bots.psgi @@ -5,7 +5,7 @@ use warnings "all"; use lib qw(./lib ./vendor_perl ./vendor_perl/lib/perl5); use conf; use jabberbot; -use telegrambot; +use ircbot; use threads; my $CONF = loadConf(); @@ -14,7 +14,7 @@ if ($CONF->{api}->{prefix} eq '/') { $CONF->{api}->{prefix} = ''; } my $prefix = $CONF->{api}->{prefix}; threads->create('run_jabberbot')->detach; -threads->create('run_telegrambot')->detach; +threads->create('run_ircbot')->detach; my $app = sub { my $env = shift; diff --git a/myapi-confcheck.pl b/myapi-confcheck.pl index 7e8394d..108f1fe 100755 --- a/myapi-confcheck.pl +++ b/myapi-confcheck.pl @@ -13,7 +13,7 @@ open (C, $conf) || die "No such file $conf\n"; $jstr = ; close C; -my $c = decode_json($jstr); +my $c = JSON::PP->new->decode($jstr); my $j = JSON::PP->new->pretty->canonical->indent_length(4); print $j->encode($c); diff --git a/myapi-repo.psgi b/myapi-repo.psgi index e0bd90b..12e80ea 100644 --- a/myapi-repo.psgi +++ b/myapi-repo.psgi @@ -6,6 +6,7 @@ use lib qw(./lib ./vendor_perl ./vendor_perl/lib/perl5); use conf; use upload; use metagen; +use buildinfo; my $CONF = loadConf(); @@ -38,9 +39,14 @@ my $app = sub { if (defined($env->{HTTP_REPO})) { ($status, $content, $msg) = metagen($env->{HTTP_REPO}); } + } elsif ($env->{PATH_INFO} eq "$prefix/buildinfo") { + if (defined($env->{HTTP_REPO})) { + ($status, $content, $msg) = buildinfo($env->{HTTP_REPO}); + } } + return [ $status, [ 'Content-Type' => $content, 'Content-Length' => length($msg) ],