per-agent configuration of batch processors, #71837
[freeside.git] / torrus / bin / ttproclist.in
1 #!@PERL@
2 #  Copyright (C) 2005  Stanislav Sinyagin
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 # $Id: ttproclist.in,v 1.1 2010-12-27 00:04:03 ivan Exp $
19 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20
21
22 use strict;
23 use Template;
24 use Getopt::Long;
25
26 my $template;
27 my $outfile;
28 my $nodelist;
29 my $parameters;
30
31
32 my $creator = "Torrus version @VERSION@\n" .
33     "This file was generated by command:\n" .
34     $0 . " \\\n";
35 foreach my $arg ( @ARGV )
36 {
37     if( $arg =~ /^--/ )
38     {
39         $creator .= ' ' . $arg . ' ';
40     }
41     else
42     {
43         $creator .= "\'" . $arg . "\'\\\n";
44     }
45 }
46 $creator .= "\nOn " . scalar(localtime(time));
47
48
49 my $ok = GetOptions( 'tmpl=s'   => \$template,
50                      'out=s'    => \$outfile,
51                      'nodes=s'  => \$nodelist,
52                      'param=s'  => \$parameters );
53
54 if( not $ok or not $template or not $outfile or not $nodelist)
55 {
56     print STDERR "Process a template with a nodelist\n\n";
57
58     print STDERR "Usage: $0 options...\n",
59     "Mandatory options:\n",
60     " --tmpl=filename                 template file name\n",
61     " --out=filename                  output file\n",
62     " --nodes=filename                file with nodes\n",
63     "Options:\n",
64     " --param=NAME:VALUE,NAME:VALUE...  parameters passed to template\n";
65     exit 1;
66 }
67
68 my @rawnodes;
69
70 if( not open( NODES, $nodelist ) )
71 {
72     print STDERR "Cannot open $nodelist: $!\n";
73     exit 1;
74 }
75 while(<NODES>)
76 {
77     s/^\s+//;
78     s/\s+$//;
79     push( @rawnodes, split( /\s+/ ) );
80 }
81 close( NODES );
82
83 my %nodes;
84 foreach my $node ( @rawnodes )
85 {
86     my $symname = $node;
87     if( $node =~ /([^:]+):(.+)/ )
88     {
89         $node = $1;
90         $symname = $2;
91     }
92
93     $nodes{$node} = $symname;
94 }
95
96 my %params;
97 if( defined( $parameters ) )
98 {
99     foreach my $pair ( split( '\s*,\s*', $parameters ) )
100     {
101         my ($name, $val) = split( '\s*:\s*', $pair );
102         $params{$name} = $val;
103     }
104 }
105
106 my $tt = new Template( INCLUDE_PATH => '@tmpluserdir@',
107                        ABSOLUTE => 1,
108                        RELATIVE => 1,
109                        TRIM => 1 );
110
111 my $vars =
112 {
113     'nodes'   => \%nodes,
114     'param'   => \%params,
115     'nodesfile' => $nodelist,
116     'creator' => $creator,
117     'lc' => sub{ return lc $_[0] },
118     'uc' => sub{ return uc $_[0] }
119 };
120
121 my $result = $tt->process($template, $vars, $outfile);
122
123 if( not $result )
124 {
125     print STDERR "Error while processing template: ".$tt->error()."\n";
126 }
127
128 exit( $result ? 0:1 );
129
130
131 # Local Variables:
132 # mode: perl
133 # indent-tabs-mode: nil
134 # perl-indent-level: 4
135 # End: