source: obs_notify_generic/obs_notify_generic.pm@ 1178

Last change on this file since 1178 was 1178, checked in by joergs, on Jun 3, 2015 at 6:03:44 PM

initial

File size: 6.1 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 if ( $entry->{'log'} ) {
79 $self->log( $type, $paramRef );
80 }
81 if ( $entry->{'action'} ) {
82
83 #print $entry->{'action'}, "\n";
84 my $check = join '|', keys %{$paramRef};
85 my $action = $entry->{'action'};
86 $action =~ s/\$($check)/$paramRef->{$1}/g;
87
88 #print $action, "\n";
89 my $in = undef;
90 my $out;
91 my $err;
92 my $rc;
93 eval {
94 my $process =
95 IPC::Run::start( [ "sh", "-c", $action, "2>&1" ],
96 \$in, \$out, \$err );
97 $process->finish();
98 $rc = $process->result(0);
99 };
100 if ($@) {
101
102 #print "eval: ", $@;
103 $out = join( "\n", $@ );
104 $rc = 127;
105 }
106
107 #print "rc: ", $rc, "\n";
108 #print "out: ", $out, "\n";
109 chomp($out);
110
111 #print "err: ", $err, "\n";
112 $self->log(
113 $type,
114 $paramRef,
115 {
116 'action_template' => $entry->{'action'},
117 'action' => $action,
118 'returncode' => $rc,
119 'output' => $out,
120 }
121 );
122
123 }
124 } else {
125
126 #print "no match\n";
127 }
128 }
129
130}
131
132sub error_msg
133{
134 print "FAILED: ", join( " ", @_ ) . "\n";
135}
136
137sub log
138{
139 my ( $self, $type, $paramRef, $extra ) = @_;
140
141 #my $log = "/srv/obs/log/obs-notify-bareos.log";
142 my $fh;
143 my $file_opened = 0;
144
145 if ( $self->{'logfile'} eq "STDOUT" ) {
146 $fh = *STDOUT;
147 } else {
148
149 if ( open( $fh, '>>', $self->{'logfile'} ) ) {
150 $file_opened = 1;
151 } else {
152 error_msg( "failed to open log file " . $self->{'logfile'} );
153 return 1;
154
155 #$fh = *STDOUT;
156 }
157 }
158
159 if ($extra) {
160 print $fh "TYPE=$type\n";
161 print $fh Dumper( { 'param' => $paramRef, 'extra' => $extra } );
162 print $fh "\n";
163 } else {
164 print $fh "TYPE=$type ";
165 print $fh Data::Dumper->new( [$paramRef] )->Indent(0)->Dump;
166 print $fh "\n";
167 }
168 if ($file_opened) {
169 close $fh;
170 }
171}
172
173# sub notify_setting {
174# my ($self, $project) = @_;
175# my ($xml, $attributes);
176#
177# $xml = $self->call_obs_api("/source/$project/_attribute/$notify_email_config::notify_attr");
178#
179# if ($xml) {
180# $attributes = XMLin($xml, KeyAttr => { }, ForceArray =>0);
181# }
182#
183# return $attributes->{attribute}->{'value'} || 0;
184# }
185
186# sub unique {
187# my ($self, @a) = @_;
188# return keys %{{ map { $_ => 1 } @a }};
189# }
190
191# sub get_obs_maintainer {
192# my ($self, $project, $package) = @_;
193# my ($request, @persons, $xml, $meta);
194# if ($package) {
195# $request = "/source/$project/$package/_meta";
196# $xml = $self->call_obs_api($request);
197# $meta = XMLin($xml, KeyAttr => {}, ForceArray => [ 'person'] );
198#
199# @persons = grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
200# }
201# $request = "/source/$project/_meta";
202#
203# $xml = $self->call_obs_api($request);
204# $meta = XMLin($xml, KeyAttr => {}, ForceArray => ['person'] );
205#
206# push @persons, grep { $_->{'role'} =~ /$notify_email_config::notified_roles/i } @{$meta->{person}};
207#
208# @persons = $self->unique(map{ $_->{'userid'} } @persons);
209#
210# return @persons;
211# }
212#
213# sub get_obs_email {
214# my ($self, $user) = @_;
215#
216# my $xml = $self->call_obs_api("/person/$user");
217#
218# $xml = XMLin($xml, KeyAttr => {}, ForceArray => 0);
219#
220# return $xml->{'email'};
221# }
222
223# sub call_obs_api {
224# my ($self, $api_call) = @_;
225# 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`;
226# }
227
2281;
Note: See TracBrowser for help on using the repository browser.