source: obs_notify_generic/obs_notify_generic.pm@ 1186

Last change on this file since 1186 was 1184, checked in by joergs, on Jun 12, 2015 at 3:41:38 PM

added timestamp in logfile, generate project_path parameter

File size: 6.2 KB
Line 
1#
2# obs_notify_generic
3#
4# Copyright (c) 2015 Jörg Steffens
5#
6# inspired of
7# https://build.opensuse.org/package/show/isv:B1-Systems:OBS/obs-notfy_email
8# Copyright (c) 2014 Christian Schneemann, B1 Systems GmbH
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License version 2 as
12# published by the Free Software Foundation.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program (see the file COPYING); if not, write to the
21# Free Software Foundation, Inc.,
22# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23#
24################################################################
25
26package obs_notify_generic;
27
28use strict;
29use warnings;
30
31#use BSConfig;
32use lib "/usr/lib/obs/server/plugins/";
33use obs_notify_generic_config;
34use Data::Dumper;
35use IPC::Run;
36use Time::Piece;
37
38#use XML::Simple;
39
40sub new
41{
42 my $self = {
43 'cfg' => ${obs_notify_generic_config::cfg},
44 'logfile' => ${obs_notify_generic_config::logfile},
45 };
46 if ( not $self->{'logfile'} ) {
47 $self->{'logfile'} = "/srv/obs/log/notify_generic.log";
48 #$self->{'logfile'} = "/tmp/notify_generic.log";
49 }
50 bless $self, shift;
51 return $self;
52}
53
54sub notify
55{
56 my ( $self, $type, $paramRef ) = @_;
57
58 $type = "UNKNOWN" unless $type;
59
60 my $cfg_type;
61 if ( $self->{'cfg'}->{$type} ) {
62 $cfg_type = $self->{'cfg'}->{$type};
63 } elsif ( $self->{'cfg'}->{'UNKNOWN'} ) {
64 $cfg_type = $self->{'cfg'}->{'UNKNOWN'};
65 }
66
67 for my $entry ( @{$cfg_type} ) {
68 my $match = 1;
69 foreach my $key ( keys %{ $entry->{'filter'} } ) {
70 my $value = $entry->{'filter'}->{$key};
71
72 #print "key: $key\n";
73 #print "value: $value\n";
74 if ( not( $paramRef->{$key} =~ /$value/ ) ) {
75 $match = 0;
76 }
77 }
78 if ($match) {
79 my $extra = undef;
80 if ( $entry->{'action'} ) {
81
82 #print $entry->{'action'}, "\n";
83 if ( $paramRef->{'project'} ) {
84 my $project = $paramRef->{'project'};
85 $project =~ s/:/:\//g;
86 $paramRef->{'project_path'}="/srv/obs/repos/" . $project;
87 }
88 my $check = join '|', keys %{$paramRef};
89 my $action = $entry->{'action'};
90 $action =~ s/\$($check)/$paramRef->{$1}/g;
91
92 #print $action, "\n";
93 my $in = undef;
94 my $out;
95 my $err;
96 my $rc;
97 eval {
98 my $process =
99 IPC::Run::start( [ "sh", "--login", "-c", "HOME=/usr/lib/obs $action", "2>&1" ],
100 \$in, \$out, \$err );
101 $process->finish();
102 $rc = $process->result(0);
103 };
104 if ($@) {
105
106 #print "eval: ", $@;
107 $out = join( "\n", $@ );
108 $rc = 127;
109 }
110
111 #print "rc: ", $rc, "\n";
112 #print "out: ", $out, "\n";
113 chomp($out);
114
115 #print "err: ", $err, "\n";
116 $extra = {
117 'action_template' => $entry->{'action'},
118 'action' => $action,
119 'returncode' => $rc,
120 'output' => $out,
121 };
122 }
123 if ( $entry->{'log'} ) {
124 $self->log( $type, $paramRef, $extra );
125 }
126 } else {
127
128 #print "no match\n";
129 }
130 }
131
132}
133
134sub error_msg
135{
136 print "FAILED: ", join( " ", @_ ) . "\n";
137}
138
139sub log
140{
141 my ( $self, $type, $paramRef, $extra ) = @_;
142
143 my $fh;
144 my $file_opened = 0;
145
146 if ( $self->{'logfile'} eq "STDOUT" ) {
147 $fh = *STDOUT;
148 } else {
149
150 if ( open( $fh, '>>', $self->{'logfile'} ) ) {
151 $file_opened = 1;
152 } else {
153 error_msg( "failed to open log file " . $self->{'logfile'} );
154 return 1;
155 }
156 }
157
158 print $fh localtime->strftime("%Y%m%d %H%M%S"), " TYPE=$type";
159
160 if ($extra) {
161 print $fh "\n";
162 print $fh Dumper( { 'param' => $paramRef, 'extra' => $extra } );
163 print $fh "\n";
164 } else {
165 print $fh " ";
166 print $fh Data::Dumper->new( [$paramRef] )->Indent(0)->Dump;
167 print $fh "\n";
168 }
169 if ($file_opened) {
170 close $fh;
171 }
172}
173
174# sub notify_setting {
175# my ($self, $project) = @_;
176# my ($xml, $attributes);
177#
178# $xml = $self->call_obs_api("/source/$project/_attribute/$notify_email_config::notify_attr");
179#
180# if ($xml) {
181# $attributes = XMLin($xml, KeyAttr => { }, ForceArray =>0);
182# }
183#
184# return $attributes->{attribute}->{'value'} || 0;
185# }
186
187# sub unique {
188# my ($self, @a) = @_;
189# return keys %{{ map { $_ => 1 } @a }};
190# }
191
192# sub get_obs_maintainer {
193# my ($self, $project, $package) = @_;
194# my ($request, @persons, $xml, $meta);
195# if ($package) {
196# $request = "/source/$project/$package/_meta";
197# $xml = $self->call_obs_api($request);
198# $meta = XMLin($xml, KeyAttr => {}, ForceArray => [ 'person'] );
199#
200# @persons = grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
201# }
202# $request = "/source/$project/_meta";
203#
204# $xml = $self->call_obs_api($request);
205# $meta = XMLin($xml, KeyAttr => {}, ForceArray => ['person'] );
206#
207# push @persons, grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
208#
209# @persons = $self->unique(map{ $_->{'userid'} } @persons);
210#
211# return @persons;
212# }
213#
214# sub get_obs_email {
215# my ($self, $user) = @_;
216#
217# my $xml = $self->call_obs_api("/person/$user");
218#
219# $xml = XMLin($xml, KeyAttr => {}, ForceArray => 0);
220#
221# return $xml->{'email'};
222# }
223
224# sub call_obs_api {
225# my ($self, $api_call) = @_;
226# return `$notify_email_config::curl_binary -k -s -u $notify_email_config::api_user:$notify_email_config::api_pass $notify_email_config::api_url/$api_call`;
227# }
228
2291;
Note: See TracBrowser for help on using the repository browser.