1 | package LATMOS::Accounts::Bases::Sql::Nethost; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | |
---|
10 | our $VERSION = (q$Rev: 2104 $ =~ /^Rev: (\d+) /)[0]; |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | LATMOS::Accounts::Bases::Sql::Nethost - A Network Host entry |
---|
15 | |
---|
16 | =cut |
---|
17 | |
---|
18 | sub _object_table { 'nethost' } |
---|
19 | |
---|
20 | sub _key_field { 'name' } |
---|
21 | |
---|
22 | sub _has_extended_attributes { 1 } |
---|
23 | |
---|
24 | sub _get_attr_schema { |
---|
25 | my ($class, $base) = @_; |
---|
26 | |
---|
27 | $class->SUPER::_get_attr_schema($base, |
---|
28 | { |
---|
29 | name => { ro => 1, inline => 1, }, |
---|
30 | cn => { ro => 1, inline => 1, iname => 'name' }, |
---|
31 | date => { ro => 1, inline => 1, }, |
---|
32 | create => { ro => 1, inline => 1, }, |
---|
33 | ip => { |
---|
34 | monitored => 1, |
---|
35 | multiple => 1, |
---|
36 | uniq => 1, |
---|
37 | input => sub { |
---|
38 | my $ip = $_[0]; |
---|
39 | $ip =~ s/(\D|^)0+/$1/g; |
---|
40 | $ip |
---|
41 | }, |
---|
42 | }, |
---|
43 | ipFrom => { |
---|
44 | multiple => 1, |
---|
45 | managed => 1, |
---|
46 | can_values => sub { |
---|
47 | $base->search_objects('netzone', 'type=dhcp') |
---|
48 | }, |
---|
49 | }, |
---|
50 | macaddr => { |
---|
51 | monitored => 1, |
---|
52 | multiple => 1, |
---|
53 | uniq => 1, |
---|
54 | input => sub { |
---|
55 | my @elem = split(/[:-]/, $_[0]); |
---|
56 | return join(':', map { sprintf("%02x", hex($_)) } @elem); |
---|
57 | }, |
---|
58 | }, |
---|
59 | cname => { |
---|
60 | monitored => 1, |
---|
61 | multiple => 1, uniq => 1, |
---|
62 | input => sub { lc($_[0]) } |
---|
63 | }, |
---|
64 | owner => { |
---|
65 | monitored => 1, |
---|
66 | reference => 'user', |
---|
67 | delayed => 1, |
---|
68 | }, |
---|
69 | user => { |
---|
70 | reference => 'user', |
---|
71 | delayed => 1, |
---|
72 | }, |
---|
73 | netZone => { |
---|
74 | multiple => 1, ro => 1, managed => 1, |
---|
75 | reference => 'netzone', |
---|
76 | }, |
---|
77 | puppetClass => { multiple => 1, }, |
---|
78 | netZoneExclude => { |
---|
79 | multiple => 1, ro => 1, managed => 1, |
---|
80 | reference => 'netzone', |
---|
81 | }, |
---|
82 | noDynamic => { |
---|
83 | formtype => 'CHECKBOX', |
---|
84 | }, |
---|
85 | exported => { formtype => 'CHECKBOX', }, |
---|
86 | noInheritPuppet => { formtype => 'CHECKBOX', }, |
---|
87 | otherName => { |
---|
88 | multiple => 1, |
---|
89 | input => sub { lc($_[0]) }, |
---|
90 | }, |
---|
91 | sshfp => { multiple => 1, }, |
---|
92 | sshfpUpdate => { |
---|
93 | managed => 1, |
---|
94 | }, |
---|
95 | } |
---|
96 | ) |
---|
97 | } |
---|
98 | |
---|
99 | sub get_field { |
---|
100 | my ($self, $field) = @_; |
---|
101 | |
---|
102 | if ($field eq 'netZone') { |
---|
103 | my $find = $self->base->db->prepare_cached(q{ |
---|
104 | select name from netzone where ikey in |
---|
105 | ( |
---|
106 | select netzone_attributes.okey from netzone_attributes join |
---|
107 | nethost_attributes_ips on netzone_attributes.attr='net' |
---|
108 | and |
---|
109 | nethost_attributes_ips.value::inet <<= |
---|
110 | netzone_attributes.value::inet |
---|
111 | where nethost_attributes_ips.okey = $1 |
---|
112 | except |
---|
113 | select netzone_attributes.okey from netzone_attributes join |
---|
114 | nethost_attributes_ips on netzone_attributes.attr='netExclude' |
---|
115 | and nethost_attributes_ips.value::inet <<= netzone_attributes.value::inet |
---|
116 | where nethost_attributes_ips.okey = $1 |
---|
117 | ) |
---|
118 | order by name |
---|
119 | }); |
---|
120 | $find->execute($self->get_attributes('ikey')); |
---|
121 | my @zones; |
---|
122 | while (my $res = $find->fetchrow_hashref) { |
---|
123 | push(@zones, $res->{name}); |
---|
124 | } |
---|
125 | return @zones ? [ @zones ] : undef; |
---|
126 | } elsif ($field eq 'netZoneExclude') { |
---|
127 | my $find = $self->base->db->prepare_cached(q{ |
---|
128 | select name from netzone where ikey in |
---|
129 | ( |
---|
130 | select netzone_attributes.okey from netzone_attributes join |
---|
131 | nethost_attributes_ips on netzone_attributes.attr='netExclude' |
---|
132 | and |
---|
133 | nethost_attributes_ips.value::inet <<= |
---|
134 | netzone_attributes.value::inet |
---|
135 | where nethost_attributes_ips.okey = $1 |
---|
136 | ) |
---|
137 | order by name |
---|
138 | }); |
---|
139 | $find->execute($self->get_attributes('ikey')); |
---|
140 | my @zones; |
---|
141 | while (my $res = $find->fetchrow_hashref) { |
---|
142 | push(@zones, $res->{name}); |
---|
143 | } |
---|
144 | return @zones ? [ @zones ] : undef; |
---|
145 | } else { |
---|
146 | return $self->SUPER::get_field($field); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | sub set_fields { |
---|
151 | my ($self, %data) = @_; |
---|
152 | if ($data{ipFrom}) { |
---|
153 | my @currentips = grep { $_ } $self->get_attributes('ip'); |
---|
154 | foreach (ref $data{ipFrom} ? @{$data{ipFrom}} : $data{ipFrom}) { |
---|
155 | my $zone = $self->base->get_object('netzone', $_) |
---|
156 | or next; |
---|
157 | my @freeips = $zone->get_attributes('freeIP') or next; |
---|
158 | my $idx = rand(scalar(@freeips)); |
---|
159 | push(@currentips, $freeips[$idx]); |
---|
160 | $self->base->log(LA_NOTICE, "Assigning ip %s to host %s", |
---|
161 | $freeips[$idx], $self->id); |
---|
162 | } |
---|
163 | push(@currentips, ref $data{ip} ? @{$data{ip}} : $data{ip}) |
---|
164 | if ($data{ip}); |
---|
165 | $data{ip} = \@currentips; |
---|
166 | delete($data{ipFrom}); |
---|
167 | } |
---|
168 | if ($data{sshfpUpdate}) { |
---|
169 | open my $h, '<', \$data{sshfpUpdate}; |
---|
170 | my @sshfps; |
---|
171 | while (my $line = <$h>) { |
---|
172 | my ($type, $mode, $key) = $line =~ /^\S+\s+IN\s+SSHFP\s+(\d)\s+(\d)\s+(\S+)/i |
---|
173 | or do { |
---|
174 | la_log(LA_ERR, "Seems to not be a ssh-keygen line: %s", $line); |
---|
175 | return; |
---|
176 | }; |
---|
177 | la_log(LA_DEBUG, "found sshfp %s %s %s", $type, $mode, $key); |
---|
178 | push(@sshfps, "$type $mode $key"); |
---|
179 | } |
---|
180 | close($h); |
---|
181 | delete($data{sshfpUpdate}); |
---|
182 | $data{sshfp} = \@sshfps; |
---|
183 | } |
---|
184 | |
---|
185 | $self->SUPER::set_fields(%data) |
---|
186 | } |
---|
187 | |
---|
188 | 1; |
---|
189 | |
---|
190 | __END__ |
---|
191 | |
---|
192 | =head1 SEE ALSO |
---|
193 | |
---|
194 | L<LATMOS::Accounts::Bases::Sql::Netzones>, L<LATMOS::Accounts::Bases::Sql> |
---|
195 | |
---|
196 | =head1 AUTHOR |
---|
197 | |
---|
198 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
199 | |
---|
200 | =head1 COPYRIGHT AND LICENSE |
---|
201 | |
---|
202 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
203 | |
---|
204 | This library is free software; you can redistribute it and/or modify |
---|
205 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
206 | at your option, any later version of Perl 5 you may have available. |
---|
207 | |
---|
208 | |
---|
209 | =cut |
---|