bin/zone-mk
changeset 88 0e1e5027e9c0
parent 87 6d624831079f
child 94 54252e5987f2
equal deleted inserted replaced
52:53c95f2ff0ac 88:0e1e5027e9c0
       
     1 #!/usr/bin/perl 
       
     2 
       
     3 use 5.010;
       
     4 use warnings;
       
     5 use strict;
       
     6 use Pod::Usage;
       
     7 use if $ENV{DEBUG} => "Smart::Comments";
       
     8 use Cwd qw(abs_path);
       
     9 use File::Path qw(make_path);
       
    10 use File::Basename;
       
    11 use Getopt::Long;
       
    12 use Net::LibIDN qw(:all);
       
    13 use DNStools::Config qw(get_config);
       
    14 use Template;
       
    15 
       
    16 my $CHARSET = "UTF-8";
       
    17 
       
    18 my $opt_force = 0;
       
    19 
       
    20 MAIN: {
       
    21 
       
    22     my %cf = get_config;
       
    23 
       
    24     GetOptions(
       
    25         "f|force" => \$opt_force,
       
    26         "h|help"  => sub { pod2usage(-verbose => 1, -exit => 0) },
       
    27         "m|man"   => sub {
       
    28             pod2usage(
       
    29                 -verbose   => 2,
       
    30                 -noperldoc => system("perldoc -V &>/dev/null"),
       
    31                 -exit      => 0
       
    32             );
       
    33         },
       
    34       )
       
    35       and @ARGV >= 2
       
    36       or pod2usage;
       
    37 
       
    38     my $customer = shift;
       
    39 
       
    40     die "$cf{master_dir}: $!"    if not -d -r -x $cf{master_dir};
       
    41     die "$cf{zone_conf_dir}: $!" if not -d -r -x $cf{zone_conf_dir};
       
    42 
       
    43     # legt fuer jede domain in @ARGV ein verzeichnis in $master_dir an.
       
    44     # schreibt aus den angegebenen templates die dateien $zonefile und $config
       
    45     # in die entsprechenden verzeichnisse.
       
    46     for my $utf8domain (@ARGV) {
       
    47 
       
    48         my $domain     = idn_to_ascii($utf8domain, $CHARSET);
       
    49         my $zonefile   = "$cf{master_dir}/$domain/$domain";
       
    50         my $configfile = "$cf{zone_conf_dir}/$domain";
       
    51         my $now        = time;
       
    52 
       
    53         make_path dirname $zonefile;
       
    54 
       
    55         if (-f $zonefile and not $opt_force) {
       
    56             say "skipping $utf8domain: zone file '$zonefile' exists.";
       
    57             next;
       
    58         }
       
    59 
       
    60         if (-f $configfile and not $opt_force) {
       
    61             say "skipping $utf8domain: config file '$configfile' exists.";
       
    62             next;
       
    63         }
       
    64 
       
    65         say "domain $utf8domain ($domain) for $customer.";
       
    66 
       
    67         my %vars = (
       
    68             domain     => $domain,
       
    69             utf8domain => $utf8domain,
       
    70             now        => $now,
       
    71             zonefile   => abs_path($zonefile),
       
    72             customer   => $customer,
       
    73             hostmaster => $cf{hostmaster},
       
    74             primary    => $cf{primary},
       
    75             secondary  => $cf{secondary},
       
    76         );
       
    77 
       
    78         my $tt = Template->new(INCLUDE_PATH => $cf{template_dir})
       
    79           or die "$Template::ERROR\n";
       
    80 
       
    81         $tt->process("named.zone",   \%vars, $zonefile)   || die $tt->error;
       
    82         $tt->process("named.config", \%vars, $configfile) || die $tt->error;
       
    83 
       
    84     }
       
    85 
       
    86 }
       
    87 
       
    88 __END__
       
    89 
       
    90 =head1 NAME
       
    91 
       
    92     zone-mk - create a new zone
       
    93 
       
    94 =head1 SYNOPSIS
       
    95 
       
    96     zone-mk [-f|--force] <customer-id> <domain>...
       
    97 
       
    98 =head1 DESCRIPTION
       
    99 
       
   100 B<zone-mk> creates a new DNS zone file and the config snipped.
       
   101 
       
   102 =head1 OPTIONS
       
   103 
       
   104 =over
       
   105 
       
   106 =item B<-f>|B<--force>
       
   107 
       
   108 Crate zone file and config even if they exist. (default: off)
       
   109 
       
   110 =item B<-h>|B<--help>
       
   111 
       
   112 =item B<-m>|B<--man>
       
   113 
       
   114 =back
       
   115 
       
   116 =head1 FILES
       
   117 
       
   118 The F<dnstools.conf> is used.
       
   119 
       
   120 =cut