source: obs_notify_generic/obs_notify_generic.pm@ 1204

Last change on this file since 1204 was 1190, checked in by joergs, on Jun 17, 2015 at 6:07:05 PM

perltidy

File size: 7.0 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 Proc::Daemon;
37use Time::Piece;
38
39#use XML::Simple;
40
41# stdout goes to /srv/obs/log/src_server.log
42
43sub new
44{
45 my $self = {
46 'cfg' => ${obs_notify_generic_config::cfg},
47 'logfile' => ${obs_notify_generic_config::logfile},
48 };
49 if ( not $self->{'logfile'} ) {
50 ##$self->{'logfile'} = "/tmp/notify_generic.log";
51 $self->{'logfile'} = "/srv/obs/log/notify_generic.log";
52 }
53 bless $self, shift;
54 return $self;
55}
56
57sub notify
58{
59 my ( $self, $type, $paramRef ) = @_;
60
61 $type = "UNKNOWN" unless $type;
62
63 my $cfg_type;
64 if ( $self->{'cfg'}->{$type} ) {
65 $cfg_type = $self->{'cfg'}->{$type};
66 } elsif ( $self->{'cfg'}->{'UNKNOWN'} ) {
67 $cfg_type = $self->{'cfg'}->{'UNKNOWN'};
68 }
69
70 for my $entry ( @{$cfg_type} ) {
71 my $match = 1;
72 foreach my $key ( keys %{ $entry->{'filter'} } ) {
73 my $value = $entry->{'filter'}->{$key};
74
75 #print "key: $key\n";
76 #print "value: $value\n";
77 if ( not( exists( $paramRef->{$key} ) ) ) {
78 error_msg( "$type ("
79 . Data::Dumper->new( [$paramRef] )->Indent(0)->Dump
80 . "): filter for '"
81 . $key
82 . "' is invalid" );
83 $match = 0;
84
85 # exit loop
86 last;
87 } elsif ( not( $paramRef->{$key} =~ /$value/ ) ) {
88 $match = 0;
89 }
90 }
91 if ($match) {
92 my $extra = undef;
93 if ( $entry->{'action'} ) {
94
95 if ( $paramRef->{'project'} ) {
96 my $project = $paramRef->{'project'};
97 $project =~ s/:/:\//g;
98 $paramRef->{'project_path'} = "/srv/obs/repos/" . $project;
99 }
100
101 # create regex,
102 # test against longest string first to avoid partial replacement ($repo, $repository)
103 my $check = join '|',
104 sort { length($b) <=> length($a) } keys %{$paramRef};
105 my $action = $entry->{'action'};
106 $action =~ s/\$($check)/$paramRef->{$1}/g;
107
108 $extra = {
109 'action_template' => $entry->{'action'},
110 'action' => $action,
111 };
112
113 #$self->run_wait( $action, $extra );
114 $self->run_daemon( $action, $extra );
115 }
116 if ( $entry->{'log'} ) {
117 $self->log( $type, $paramRef, $extra );
118 }
119 } else {
120
121 #print "no match\n";
122 }
123 }
124}
125
126sub run_wait
127{
128 my ( $self, $action, $extra ) = @_;
129
130 my $in = undef;
131 my $out;
132 my $err;
133 my $rc;
134 eval {
135 my $process =
136 IPC::Run::start(
137 [ "sh", "--login", "-c", "HOME=/usr/lib/obs $action", "2>&1" ],
138 \$in, \$out, \$err );
139 $process->finish();
140 $rc = $process->result(0);
141 };
142 if ($@) {
143
144 #print "eval: ", $@;
145 $out = join( "\n", $@ );
146 $rc = 127;
147 }
148
149 chomp($out);
150
151 $extra->{'returncode'} = $rc;
152 $extra->{'output'} = $out;
153}
154
155sub run_daemon
156{
157 my ( $self, $action, $extra ) = @_;
158
159 my $daemon = Proc::Daemon->new();
160 my $pid = $daemon->Init(
161 {
162 exec_command => "date --rfc-3339=seconds; echo '$action'; $action",
163 child_STDOUT => "+>>" . $self->{'logfile'} . ".out.log",
164 child_STDERR => "+>>" . $self->{'logfile'} . ".out.log",
165 }
166 );
167
168 #print $daemon->Status($pid);
169
170 $extra->{'pid'} = $pid;
171}
172
173sub error_msg
174{
175 print "FAILED: ", join( " ", @_ ) . "\n";
176}
177
178sub log
179{
180 my ( $self, $type, $paramRef, $extra ) = @_;
181
182 my $fh;
183 my $file_opened = 0;
184
185 if ( $self->{'logfile'} eq "STDOUT" ) {
186 $fh = *STDOUT;
187 } else {
188
189 if ( open( $fh, '>>', $self->{'logfile'} ) ) {
190 $file_opened = 1;
191 } else {
192 error_msg( "failed to open log file " . $self->{'logfile'} );
193 return 1;
194 }
195 }
196
197 print $fh localtime->strftime("%Y%m%d %H%M%S"), " TYPE=$type";
198
199 if ($extra) {
200 print $fh "\n";
201 print $fh Dumper( { 'param' => $paramRef, 'extra' => $extra } );
202 print $fh "\n";
203 } else {
204 print $fh " ";
205 print $fh Data::Dumper->new( [$paramRef] )->Indent(0)->Dump;
206 print $fh "\n";
207 }
208 if ($file_opened) {
209 close $fh;
210 }
211}
212
213# sub notify_setting {
214# my ($self, $project) = @_;
215# my ($xml, $attributes);
216#
217# $xml = $self->call_obs_api("/source/$project/_attribute/$notify_email_config::notify_attr");
218#
219# if ($xml) {
220# $attributes = XMLin($xml, KeyAttr => { }, ForceArray =>0);
221# }
222#
223# return $attributes->{attribute}->{'value'} || 0;
224# }
225
226# sub unique {
227# my ($self, @a) = @_;
228# return keys %{{ map { $_ => 1 } @a }};
229# }
230
231# sub get_obs_maintainer {
232# my ($self, $project, $package) = @_;
233# my ($request, @persons, $xml, $meta);
234# if ($package) {
235# $request = "/source/$project/$package/_meta";
236# $xml = $self->call_obs_api($request);
237# $meta = XMLin($xml, KeyAttr => {}, ForceArray => [ 'person'] );
238#
239# @persons = grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
240# }
241# $request = "/source/$project/_meta";
242#
243# $xml = $self->call_obs_api($request);
244# $meta = XMLin($xml, KeyAttr => {}, ForceArray => ['person'] );
245#
246# push @persons, grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
247#
248# @persons = $self->unique(map{ $_->{'userid'} } @persons);
249#
250# return @persons;
251# }
252#
253# sub get_obs_email {
254# my ($self, $user) = @_;
255#
256# my $xml = $self->call_obs_api("/person/$user");
257#
258# $xml = XMLin($xml, KeyAttr => {}, ForceArray => 0);
259#
260# return $xml->{'email'};
261# }
262
263# sub call_obs_api {
264# my ($self, $api_call) = @_;
265# 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`;
266# }
267
2681;
Note: See TracBrowser for help on using the repository browser.