-
Notifications
You must be signed in to change notification settings - Fork 9
/
event_handler.pl
executable file
·53 lines (43 loc) · 1.26 KB
/
event_handler.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use Fcntl qw(:flock);
use File::Temp qw(tempfile);
use File::Copy;
while (<STDIN>) {
chomp;
my @member_fields = split("\t", $_, -1);
die "fields must include 4 elements" unless @member_fields == 4;
my $file = $ARGV[0] || '/etc/hosts';
my ($name, $address, undef, undef) = @member_fields;
my $event = $ENV{SERF_EVENT};
if ($event eq 'member-join') {
open my $fh, ">>", $file or die $!;
{
flock($fh, LOCK_EX);
print $fh "${address}\t${name}\n";
flock($fh, LOCK_UN);
}
close $fh;
}
elsif ($event eq 'member-leave') {
open my $fh, "<", $file or die $!;
my ($tmp_fh, $tmp_file) = tempfile();
{
flock($fh, LOCK_EX);
my $name_reg = quotemeta $name;
while (<$fh>) {
if ($_ !~ /\s${name_reg}$/) {
print $tmp_fh $_;
}
}
flock($fh, LOCK_UN);
}
close $fh;
close $tmp_fh;
chmod 0644, $tmp_file or
die "Failed to change permission of ${tmp_file} into 0644";
File::Copy::move($tmp_file, $file) or
die "Failed to move ${tmp_file} to ${file}";
}
}