source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Unusedhosts.pm @ 2024

Last change on this file since 2024 was 2024, checked in by nanardon, 7 years ago

Fix filter in task

  • Property svn:executable set to *
File size: 7.6 KB
Line 
1package LATMOS::Accounts::Task::Unusedhosts;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use LATMOS::Accounts::Log;
8use LATMOS::Accounts::Utils;
9use DateTime;
10
11=head1 NAME
12
13LATMOS::Accounts::Task::Unusedhosts - Disable host owned by inactive users
14
15=head1 DESCRIPTION
16
17This module disable nethost when owned by an expired or disabled user account, removing it from network
18
19=head1 PARAMETER
20
21=head2 netzone
22
23The C<netzone> object containing concerned hosts
24
25=head2 filter
26
27A filter limiting host search.
28
29=head2 disableMode
30
31The rules to disable hosts:
32
33=over 4
34
35=item C<owner>: C<owner> is set and is inactive
36
37=item C<user>: C<user> is set and is inactive
38
39=item C<both>: C<owner> or C<user> (if set) is inactive
40
41=item C<effective> (default): If C<user> is set and is inactive otherwise if
42C<owner> is inactive
43
44=back
45
46=head2 test
47
48If set only log are emitted but changes are not done and mail not really sent
49
50=cut
51
52# Wait 30 minutes
53sub waitDelay { 30 * 60 }
54
55sub init {
56    my ($self) = @_;
57    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
58    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
59    $labase && $labase->load or die "Cannot load base";
60
61    $self->{_la} = $LA;
62    $self->{_base} = $labase;
63
64    1;
65}
66
67sub run {
68    my ($self) = @_;
69
70    $self->_enable;
71
72    $self->_disable;
73
74    1;
75}
76
77sub _filters {
78    my ($self) = @_;
79
80    my $filteredHosts;
81
82    if (my @cfilters = $self->{syncm}->ini->val($self->{name}, 'filter')) {
83        $filteredHosts = {};
84        foreach ($self->{_base}->search_objects('nethost', @cfilters)) {
85            $filteredHosts->{$_} = 1;
86        }
87    }
88
89    my $hosts;
90
91    if (my @zones = $self->{syncm}->ini->val($self->{name}, 'netzone')) {
92
93        $hosts = {};
94
95        my @filters;
96
97        foreach my $zone (@zones) {
98            la_log(LA_DEBUG, "Limiting affected host to zone %s", $zone);
99            my $oZone = $self->{_base}->get_object('netzone', $zone) or do {
100                la_log(LA_ERR, "Cannot get zone $zone named in config");
101                return;
102            };
103
104            my @zonehosts = $oZone->get_attributes('hosts');
105
106            push(@filters, [ 'name=' . join('||', @zonehosts) ]);
107
108            if ($oZone->get_attributes('allow_dyn')) {
109                la_log(LA_DEBUG, "Zone %s has allow_dyn set, searching w/o ip", $zone);
110                push(@filters, [ 'ip=NULL' ]);
111            }
112        }
113
114        foreach my $filter (@filters) {
115            foreach ($self->{_base}->search_objects('nethost', @$filter)) {
116                $filteredHosts && !$filteredHosts->{$_} and next;
117                $hosts->{$_} = 1;
118            }
119        }
120    } else {
121        $hosts = $filteredHosts;
122    }
123
124    return $hosts;
125}
126
127sub _disable {
128    my ($self) = @_;
129
130    my $test = $self->{syncm}->ini->val($self->{name}, 'test');
131    my $mode = $self->{syncm}->ini->val($self->{name}, 'disableMode', 'effective');
132
133    la_log(LA_DEBUG, "Checking for host to disable using %s method", $mode);
134
135    my $hosts = $self->_filters;
136
137    my @HostToDisable;
138    for ($mode) {
139        /owner/i and do {
140            my @HostNoOwner = $self->{_base}->search_objects('nethost', 'exported=1', 'owner.active=0');
141            foreach (@HostNoOwner) {
142                push(@HostToDisable, $_);
143            }
144            last;
145        };
146        /user/i and do {
147            my @HostNoUser = $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0');
148            foreach (@HostNoUser) {
149                push(@HostToDisable, $_);
150            }
151            last;
152        };
153        /both/i and do {
154            my %HostNoUser  = map { $_ => } (
155                $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0'),
156                $self->{_base}->search_objects('nethost', 'exported=1', 'owner.active=0'),
157            );
158            foreach (keys %HostNoUser) {
159                push(@HostToDisable, $_);
160            }
161            last;
162        };
163        /effective/i and do {
164            my %HostNoUser  = map { $_ => } (
165                $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0'),
166                $self->{_base}->search_objects('nethost', 'exported=1', 'user=NULL', 'owner.active=0'),
167            );
168            foreach (keys %HostNoUser) {
169                push(@HostToDisable, $_);
170            }
171            last;
172        };
173
174        la_log(LA_ERR, "Unknown host check method `%s'", $mode);
175    }
176
177    foreach my $hostname (sort @HostToDisable) {
178        if ($hosts) {
179            $hosts->{$hostname} or next;
180        }
181        my $Host = $self->{_base}->get_object('nethost', $hostname) or next;
182        $self->{_base}->log(
183            LA_NOTICE,
184            '%sHost %s disable because owner or user is inactive',
185            ($test ? '(Test) ' : ''),
186            $hostname,
187        );
188        $Host->ReportChange('Update', "Disabling because user/owner is inactive");
189        $Host->_set_c_fields( unexported => 1 );
190    }
191
192    $self->{_base}->commit()
193        unless($test);
194}
195
196sub _enable {
197    my ($self) = @_;
198
199    my $test = $self->{syncm}->ini->val($self->{name}, 'test');
200    my $mode = $self->{syncm}->ini->val($self->{name}, 'disableMode', 'effective');
201
202    la_log(LA_DEBUG, "Checking for host to disable using %s method", $mode);
203
204    my $hosts = $self->_filters;
205
206    my @HostToEnable;
207    for ($mode) {
208        /owner/i and do {
209            my @HostNoOwner = $self->{_base}->search_objects('nethost', 'exported=0', 'owner.active=1');
210            foreach (@HostNoOwner) {
211                push(@HostToEnable, $_);
212            }
213            last;
214        };
215        /user/i and do {
216            my @HostNoUser = $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1');
217            foreach (@HostNoUser) {
218                push(@HostToEnable, $_);
219            }
220            last;
221        };
222        /both/i and do {
223            my %HostNoUser  = map { $_ => } (
224                $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1', 'owner.active=1'),
225                $self->{_base}->search_objects('nethost', 'exported=0', 'user=NULL', 'owner.active=1'),
226            );
227            foreach (keys %HostNoUser) {
228                push(@HostToEnable, $_);
229            }
230            last;
231        };
232        /effective/i and do {
233            my %HostNoUser  = map { $_ => } (
234                $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1'),
235                $self->{_base}->search_objects('nethost', 'exported=0', 'user=NULL', 'owner.active=1'),
236            );
237            foreach (keys %HostNoUser) {
238                push(@HostToEnable, $_);
239            }
240            last;
241        };
242
243        la_log(LA_ERR, "Unknown host check method `%s'", $mode);
244    }
245
246    foreach my $hostname (sort @HostToEnable) {
247        if ($hosts) {
248            $hosts->{$hostname} or next;
249        }
250        my $Host = $self->{_base}->get_object('nethost', $hostname) or next;
251        $self->{_base}->log(
252            LA_INFO,
253            '%sHost %s enable because owner or user is active',
254            ($test ? '(Test) ' : ''),
255            $hostname,
256        );
257        $Host->ReportChange('Update', "Enabling because user/owner is active");
258        $Host->_set_c_fields( unexported => 0 );
259    }
260
261    $self->{_base}->commit()
262        unless($test);
263}
264
2651;
266
267__END__
268
269=head1 SEE ALSO
270
271L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>
272
273=head1 AUTHOR
274
275Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
276
277=head1 COPYRIGHT AND LICENSE
278
279Copyright (C) 2012 CNRS SA/CETP/LATMOS
280
281This library is free software; you can redistribute it and/or modify
282it under the same terms as Perl itself, either Perl version 5.10.0 or,
283at your option, any later version of Perl 5 you may have available.
284
285=cut
286
Note: See TracBrowser for help on using the repository browser.