source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Nethost.pm @ 1064

Last change on this file since 1064 was 1064, checked in by nanardon, 12 years ago

Add puppet zones

This patch create a new zone type "puppet" which generate manifest files for a
set of computers.

Puppet class can be set in the zone or in the host, both settings are merged.

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