source: obs_notify_generic/obs_notify_generic.pm@ 1179

Last change on this file since 1179 was 1179, checked in by joergs, on Jun 3, 2015 at 7:20:54 PM

make more external commands work

File size: 6.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;
36
37#use XML::Simple;
38
39sub new
40{
41 my $self = {
42 'cfg' => ${obs_notify_generic_config::cfg},
43 'logfile' => ${obs_notify_generic_config::logfile},
44 };
45 if ( not $self->{'logfile'} ) {
46 $self->{'logfile'} = "/srv/obs/log/notify_generic.log";
47 #$self->{'logfile'} = "/tmp/notify_generic.log";
48 }
49 bless $self, shift;
50 return $self;
51}
52
53sub notify
54{
55 my ( $self, $type, $paramRef ) = @_;
56
57 $type = "UNKNOWN" unless $type;
58
59 my $cfg_type;
60 if ( $self->{'cfg'}->{$type} ) {
61 $cfg_type = $self->{'cfg'}->{$type};
62 } elsif ( $self->{'cfg'}->{'UNKNOWN'} ) {
63 $cfg_type = $self->{'cfg'}->{'UNKNOWN'};
64 }
65
66 for my $entry ( @{$cfg_type} ) {
67 my $match = 1;
68 foreach my $key ( keys %{ $entry->{'filter'} } ) {
69 my $value = $entry->{'filter'}->{$key};
70
71 #print "key: $key\n";
72 #print "value: $value\n";
73 if ( not( $paramRef->{$key} =~ /$value/ ) ) {
74 $match = 0;
75 }
76 }
77 if ($match) {
78 my $extra = undef;
79 if ( $entry->{'action'} ) {
80
81 #print $entry->{'action'}, "\n";
82 my $check = join '|', keys %{$paramRef};
83 my $action = $entry->{'action'};
84 $action =~ s/\$($check)/$paramRef->{$1}/g;
85
86 #print $action, "\n";
87 my $in = undef;
88 my $out;
89 my $err;
90 my $rc;
91 eval {
92 my $process =
93 IPC::Run::start( [ "sh", "--login", "-c", "HOME=/usr/lib/obs $action", "2>&1" ],
94 \$in, \$out, \$err );
95 $process->finish();
96 $rc = $process->result(0);
97 };
98 if ($@) {
99
100 #print "eval: ", $@;
101 $out = join( "\n", $@ );
102 $rc = 127;
103 }
104
105 #print "rc: ", $rc, "\n";
106 #print "out: ", $out, "\n";
107 chomp($out);
108
109 #print "err: ", $err, "\n";
110 $extra = {
111 'action_template' => $entry->{'action'},
112 'action' => $action,
113 'returncode' => $rc,
114 'output' => $out,
115 };
116 }
117 if ( $entry->{'log'} ) {
118 $self->log( $type, $paramRef, $extra );
119 }
120 } else {
121
122 #print "no match\n";
123 }
124 }
125
126}
127
128sub error_msg
129{
130 print "FAILED: ", join( " ", @_ ) . "\n";
131}
132
133sub log
134{
135 my ( $self, $type, $paramRef, $extra ) = @_;
136
137 my $fh;
138 my $file_opened = 0;
139
140 if ( $self->{'logfile'} eq "STDOUT" ) {
141 $fh = *STDOUT;
142 } else {
143
144 if ( open( $fh, '>>', $self->{'logfile'} ) ) {
145 $file_opened = 1;
146 } else {
147 error_msg( "failed to open log file " . $self->{'logfile'} );
148 return 1;
149 }
150 }
151
152 if ($extra) {
153 print $fh "TYPE=$type\n";
154 print $fh Dumper( { 'param' => $paramRef, 'extra' => $extra } );
155 print $fh "\n";
156 } else {
157 print $fh "TYPE=$type ";
158 print $fh Data::Dumper->new( [$paramRef] )->Indent(0)->Dump;
159 print $fh "\n";
160 }
161 if ($file_opened) {
162 close $fh;
163 }
164}
165
166# sub notify_setting {
167# my ($self, $project) = @_;
168# my ($xml, $attributes);
169#
170# $xml = $self->call_obs_api("/source/$project/_attribute/$notify_email_config::notify_attr");
171#
172# if ($xml) {
173# $attributes = XMLin($xml, KeyAttr => { }, ForceArray =>0);
174# }
175#
176# return $attributes->{attribute}->{'value'} || 0;
177# }
178
179# sub unique {
180# my ($self, @a) = @_;
181# return keys %{{ map { $_ => 1 } @a }};
182# }
183
184# sub get_obs_maintainer {
185# my ($self, $project, $package) = @_;
186# my ($request, @persons, $xml, $meta);
187# if ($package) {
188# $request = "/source/$project/$package/_meta";
189# $xml = $self->call_obs_api($request);
190# $meta = XMLin($xml, KeyAttr => {}, ForceArray => [ 'person'] );
191#
192# @persons = grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
193# }
194# $request = "/source/$project/_meta";
195#
196# $xml = $self->call_obs_api($request);
197# $meta = XMLin($xml, KeyAttr => {}, ForceArray => ['person'] );
198#
199# push @persons, grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
200#
201# @persons = $self->unique(map{ $_->{'userid'} } @persons);
202#
203# return @persons;
204# }
205#
206# sub get_obs_email {
207# my ($self, $user) = @_;
208#
209# my $xml = $self->call_obs_api("/person/$user");
210#
211# $xml = XMLin($xml, KeyAttr => {}, ForceArray => 0);
212#
213# return $xml->{'email'};
214# }
215
216# sub call_obs_api {
217# my ($self, $api_call) = @_;
218# 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`;
219# }
220
2211;
Note: See TracBrowser for help on using the repository browser.