| 1 | # | ||
| 2 | # (c) Jean-Marie RENOUARD <jmrenouard@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Rex::Group::Lookup::DBI - read hostnames and groups from a DBI source | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | With this module you can define hostgroups out of an DBI source. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Rex::Group::Lookup::DBI; | ||
| 16 | groups_dbi "dsn", "user", "password", "SQL request"; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 EXPORTED FUNCTIONS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Rex::Group::Lookup::DBI; | ||
| 24 | |||
| 25 | use v5.14.4; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | our $VERSION = '1.16.1'; # VERSION | ||
| 29 | |||
| 30 | use Rex -base; | ||
| 31 | use Carp; | ||
| 32 | |||
| 33 | require Exporter; | ||
| 34 | use base qw(Exporter); | ||
| 35 | use vars qw(@EXPORT); | ||
| 36 | |||
| 37 | use Rex::Helper::DBI; | ||
| 38 | @EXPORT = qw(groups_dbi); | ||
| 39 | |||
| 40 | =head2 groups_dbi($dsn, $user, $password, $sql) | ||
| 41 | |||
| 42 | With this function you can read groups from DBI source. Example: | ||
| 43 | |||
| 44 | groups_dbi( 'DBI:mysql:rex;host=db01', | ||
| 45 | user => 'username', | ||
| 46 | password => 'password', | ||
| 47 | sql => "SELECT * FROM HOST", | ||
| 48 | create_all_group => TRUE); | ||
| 49 | |||
| 50 | =head2 Database sample for MySQL | ||
| 51 | |||
| 52 | CREATE TABLE IF NOT EXISTS `HOST` ( | ||
| 53 | `ID` int(11) NOT NULL, | ||
| 54 | `GROUP` varchar(255) DEFAULT NULL, | ||
| 55 | `HOST` varchar(255) NOT NULL, | ||
| 56 | PRIMARY KEY (`ID`) | ||
| 57 | ); | ||
| 58 | |||
| 59 | =head2 Data sample for MySQL | ||
| 60 | |||
| 61 | INSERT INTO `HOST` (`ID`, `GROUP`, `HOST`) VALUES | ||
| 62 | (1, 'db', 'db01'), | ||
| 63 | (2, 'db', 'db02'), | ||
| 64 | (3, 'was', 'was01'), | ||
| 65 | (4, 'was', 'was02'); | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub groups_dbi { | ||
| 70 | my ( $dsn, %option ) = @_; # $user, $pass, $sql) = @_; | ||
| 71 | |||
| 72 | confess "You have to define the sql." if ( !exists $option{sql} ); | ||
| 73 | |||
| 74 | my $user = $option{user}; | ||
| 75 | my $pass = $option{password}; | ||
| 76 | my $sql = $option{sql}; | ||
| 77 | |||
| 78 | my $hash = Rex::Helper::DBI::perform_request( $dsn, $user, $pass, $sql ); | ||
| 79 | |||
| 80 | my %group; | ||
| 81 | my %all_hosts; | ||
| 82 | for my $k ( keys %{$hash} ) { | ||
| 83 | my $add = {}; | ||
| 84 | my $rex_host = | ||
| 85 | Rex::Group::Entry::Server->new( name => $hash->{$k}->{'HOST'}, %{$add} ); | ||
| 86 | push @{ $group{ $hash->{$k}->{'GROUP'} } }, $rex_host; | ||
| 87 | |||
| 88 | $all_hosts{ $hash->{$k}->{'HOST'} } = $rex_host; | ||
| 89 | } | ||
| 90 | |||
| 91 | for my $g ( keys %group ) { | ||
| 92 | group( "$g" => @{ $group{$g} } ); | ||
| 93 | } | ||
| 94 | |||
| 95 | if ( exists $option{create_all_group} && $option{create_all_group} ) { | ||
| 96 | group( "all", values %all_hosts ); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; |