forked from brookhong/obs_event_trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify_jenkins.pm
More file actions
115 lines (99 loc) · 3.09 KB
/
notify_jenkins.pm
File metadata and controls
115 lines (99 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
# Copyright (c) 2010 Anas Nashif, Intel Inc.
# Copyright (c) 2010 David Greaves, Nokia.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
#
# Wrap events up into a BOSS workitem and launch a process to handle
# the event. Analysis and logic is handled in the participant.
#
# BSConfig should define:
# BOSS_host : hostname for server running BOSS AMQP
# BOSS_user : AMQP username
# BOSS_passwd : AMQP password (cleartext)
#
package notify_boss;
use Net::RabbitMQ;
use BSConfig;
use JSON::XS;
use Data::Dumper;
use strict;
sub new {
my $self = {};
bless $self, shift;
# As load grows maybe put a persistent AMQP connection in here and
# handle disconnects. For now a reconnect on each message is reliable.
return $self;
}
sub notify() {
my ($self, $type, $paramRef ) = @_;
$type = "UNKNOWN" unless $type;
my $prefix = $BSConfig::notification_namespace || "OBS";
$type = "${prefix}_$type";
if ($paramRef) {
# reserve original 'type' field at first
$paramRef->{'origintype'} = $paramRef->{'type'};
$paramRef->{'type'} = $type;
$paramRef->{'time'} = time();
my @boss_hosts = split(/\s+/, $BSConfig::BOSS_host);
my @mq = ();
for my $i (0..@boss_hosts-1) {
$mq[$i] = Net::RabbitMQ->new();
eval {
($mq[$i])->connect($boss_hosts[$i], { user => $BSConfig::BOSS_user,
password => $BSConfig::BOSS_passwd,
vhost => "boss" });
};
if ($@) {
warn("BOSS Plugin: $@");
return;
}
}
for my $i (0..@boss_hosts-1) {
($mq[$i])->channel_open(1);
}
my $definition = <<EOS;
Ruote.process_definition :name => 'OBS Raw Event' do
obs_event
end
EOS
my $fields = {obsEvent => $paramRef};
my $msg={"definition" => $definition,
"fields" => $fields};
my $body;
eval {
$body = encode_json($msg);
};
if ($@) {
print "\n#################################################################################\n";
print "Dumper\n";
print Dumper($msg);
print "\n#################################################################################\n";
print "JSON\n";
print $body;
warn "Error decoding event\n".Dumper($msg);
} else {
for my $i (0..@boss_hosts-1) {
($mq[$i])->publish(1, "ruote_workitems", encode_json($msg), { exchange => '' });
}
}
for my $i (0..@boss_hosts-1) {
($mq[$i])->disconnect();
}
}
}
1;