1 | package LATMOS::Accounts::Task::Iprecover; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base qw(LATMOS::Accounts::Task); |
---|
6 | use LATMOS::Accounts; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | use DateTime; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::Task::Iprecover - Recover IP from disabled hosts |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | This module check for disabled hosts and remove the IP set on it to free them |
---|
18 | |
---|
19 | =head1 PARAMETER |
---|
20 | |
---|
21 | =head2 netzone |
---|
22 | |
---|
23 | The C<netzone> object containing concerned hosts |
---|
24 | |
---|
25 | =head2 filter |
---|
26 | |
---|
27 | A filter limiting host search. |
---|
28 | |
---|
29 | =head2 modifDelay |
---|
30 | |
---|
31 | The number of day hosts has not be modified in database. |
---|
32 | Hosts having recent changes are left unmodified. |
---|
33 | |
---|
34 | =head2 test |
---|
35 | |
---|
36 | If set only log are emitted but changes are not done and mail not really sent |
---|
37 | |
---|
38 | =cut |
---|
39 | |
---|
40 | sub runDelay { 2 * 60 * 60 } |
---|
41 | |
---|
42 | # Wait 30 minutes |
---|
43 | sub waitDelay { 30 * 60 } |
---|
44 | |
---|
45 | sub init { |
---|
46 | my ($self) = @_; |
---|
47 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
48 | my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base; |
---|
49 | $labase && $labase->load or die "Cannot load base"; |
---|
50 | |
---|
51 | $self->{_la} = $LA; |
---|
52 | $self->{_base} = $labase; |
---|
53 | |
---|
54 | 1; |
---|
55 | } |
---|
56 | |
---|
57 | sub _filters { |
---|
58 | my ($self) = @_; |
---|
59 | |
---|
60 | my $filteredHosts; |
---|
61 | |
---|
62 | if (my @cfilters = $self->{syncm}->ini->val($self->{name}, 'filter')) { |
---|
63 | $filteredHosts = {}; |
---|
64 | foreach ($self->{_base}->search_objects('nethost', @cfilters)) { |
---|
65 | $filteredHosts->{$_} = 1; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | my $hosts; |
---|
70 | |
---|
71 | if (my @zones = $self->{syncm}->ini->val($self->{name}, 'netzone')) { |
---|
72 | |
---|
73 | $hosts = {}; |
---|
74 | |
---|
75 | my @filters; |
---|
76 | |
---|
77 | foreach my $zone (@zones) { |
---|
78 | la_log(LA_DEBUG, "Limiting affected host to zone %s", $zone); |
---|
79 | my $oZone = $self->{_base}->get_object('netzone', $zone) or do { |
---|
80 | la_log(LA_ERR, "Cannot get zone $zone named in config"); |
---|
81 | return; |
---|
82 | }; |
---|
83 | |
---|
84 | my @zonehosts = $oZone->get_attributes('hosts'); |
---|
85 | |
---|
86 | push(@filters, [ 'name=' . join('||', @zonehosts) ]); |
---|
87 | |
---|
88 | if ($oZone->get_attributes('allow_dyn')) { |
---|
89 | la_log(LA_DEBUG, "Zone %s has allow_dyn set, searching w/o ip", $zone); |
---|
90 | push(@filters, [ 'ip=NULL' ]); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | foreach my $filter (@filters) { |
---|
95 | foreach ($self->{_base}->search_objects('nethost', @$filter)) { |
---|
96 | $filteredHosts && !$filteredHosts->{$_} and next; |
---|
97 | $hosts->{$_} = 1; |
---|
98 | } |
---|
99 | } |
---|
100 | } else { |
---|
101 | $hosts = $filteredHosts; |
---|
102 | } |
---|
103 | |
---|
104 | return $hosts; |
---|
105 | } |
---|
106 | |
---|
107 | sub run { |
---|
108 | my ($self) = @_; |
---|
109 | |
---|
110 | $self->{_base}->temp_switch_unexported(sub { |
---|
111 | |
---|
112 | my $test = $self->{syncm}->ini->val($self->{name}, 'test'); |
---|
113 | |
---|
114 | my $modifDelay = $self->{syncm}->ini->val($self->{name}, 'modifdelay', 366 * 2); |
---|
115 | my $waitAfterModif = DateTime->now->subtract( days => $modifDelay )->iso8601; |
---|
116 | |
---|
117 | my $hosts = $self->_filters; |
---|
118 | |
---|
119 | my @HostToDisable = $self->{_base}->search_objects('nethost', 'exported=0', 'ip=*', 'date<' . $waitAfterModif); |
---|
120 | |
---|
121 | foreach my $hostname (@HostToDisable) { |
---|
122 | if ($hosts) { |
---|
123 | $hosts->{$hostname} or next; |
---|
124 | } |
---|
125 | my $Host = $self->{_base}->get_object('nethost', $hostname) or next; |
---|
126 | $self->{_base}->log( |
---|
127 | LA_NOTICE, |
---|
128 | '%sRecovering IP for host %s because is disable from a long time', |
---|
129 | ($test ? '(Test) ' : ''), |
---|
130 | $hostname, |
---|
131 | ); |
---|
132 | $Host->ReportChange('Update', "Recovering IP because host is disable from a long time"); |
---|
133 | $Host->_set_c_fields( ip => undef ); |
---|
134 | } |
---|
135 | |
---|
136 | $self->{_base}->commit() |
---|
137 | unless($test); |
---|
138 | |
---|
139 | }, 1); |
---|
140 | } |
---|
141 | |
---|
142 | 1; |
---|
143 | |
---|
144 | __END__ |
---|
145 | |
---|
146 | =head1 SEE ALSO |
---|
147 | |
---|
148 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Task> |
---|
149 | |
---|
150 | =head1 AUTHOR |
---|
151 | |
---|
152 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
153 | |
---|
154 | =head1 COPYRIGHT AND LICENSE |
---|
155 | |
---|
156 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
157 | |
---|
158 | This library is free software; you can redistribute it and/or modify |
---|
159 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
160 | at your option, any later version of Perl 5 you may have available. |
---|
161 | |
---|
162 | =cut |
---|
163 | |
---|