source: projects/casparbuster/devel/bin/cbusterize @ 1466

Last change on this file since 1466 was 1466, checked in by bruno, 7 years ago
  • Property svn:executable set to *
File size: 9.1 KB
Line 
1#!/usr/bin/perl -w
2#
3=head1 NAME
4
5cbusterize - Creates the correct CasparBuster structure in your CMS environment
6
7=head1 SYNOPSIS
8
9cbusterize.pl [options] --source /path/to/file/to/CasparBusterize
10
11 Options:
12   --debug  |-d         debug mode
13   --help   |-h         brief help message
14   --man            full documentation
15   --source |-s <file/dir>  directory or file to copy in the CasparBuster tree
16   --machine|-m <machine>   machine to consider in the subtree
17
18=head1 OPTIONS
19
20=over 4
21
22=item B<--debug>
23
24Enter debug mode. This will print what would be done. No commands are executed,
25so this is safe to use when testing.
26
27=item B<--help>
28
29Print a brief help message and exits.
30
31=item B<--man>
32
33Prints the manual page and exits.
34
35=item B<--machine> I<machine name>
36
37Specify the machine to consider when dealing with the CasparBuster structure.
38The file will be taken from this machine, and a subdirectory named after the machine
39will be used under the basedir to host the directory structure to manage
40
41=item B<--source> I<path>
42
43Specify the path to the source file or directory to manage with CasparBuster.
44
45=back
46
47=head1 DESCRIPTION
48
49Creates a directory under the machine dir passed as parameter in working
50directory or directory passed as parameter named like the last path
51element of the parameter, and creates the standard CasparBuster setup
52that refers to the parameter path in the new directory, and configuration
53files when possible. It also copies the original config file into the new dir.
54Is reasonably picky about path names, tries to avoid common errors.
55
56Schema looks like:
57
58Base dir
59   |
60   |- machine1 (optional)
61   |     |
62   |     |-- conf dir1
63   |     |       |
64   |     |       |- conf file 1
65   |   [...]    [...]
66   |
67   |- machine2 (optional)
68   |     |
69   |     |-- conf dir2
70   |     |       |
71   |     |       |- conf file 2
72   |   [...]    [...]
73
74Use of machines require use of option -m (if using cbusemachines in cb.conf)
75If not, the conf dirs are directly attached to the base dir
76
77=head1 EXAMPLES
78
79    # this will create the appropriate CasparBuster environment
80    # under the base ~/prj/musique-ancienne.org directory (Cf cbbasedir in cb.conf)
81    # containing the directory victoria2 for this machine
82    # under which it will copy the required structure if needed (/etc/ssh)
83    # to finaly put a copy of the file sshd_conf in it from the victoria2 machine
84
85    cbusterize -m victoria2 -s /etc/ssh/sshd_config
86
87=head1 AUTHOR
88
89=over 4
90
91Bruno Cornec, http://brunocornec.wordpress.com
92
93=back
94
95=head1 LICENSE
96
97Copyright (C) 2012  Bruno Cornec <bruno@project-builder.org>
98Released under the GPLv2 or the Artistic license at your will.
99
100=cut
101use strict;
102use Cwd 'realpath';
103use File::Find;
104use File::Copy;
105use File::Basename;
106use File::Path;
107use File::Glob ':glob';
108use Getopt::Long;
109use Pod::Usage;
110use List::Util qw(first);
111use ProjectBuilder::Base;
112use ProjectBuilder::Conf;
113use CasparBuster::Env;
114
115# settings
116my $debug = 0;
117my $help = 0;
118my $man = 0;
119my $source = undef;
120my $machine = undef;
121my $quiet = undef;
122my $log = undef;
123my $LOG = undef;
124
125my ($cbver,$cbrev) = pb_version_init();
126my $appname = "cb";
127$ENV{'PBPROJ'} = $appname;
128pb_temp_init();
129
130# Initialize the syntax string
131pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
132
133# parse command-line options
134GetOptions(
135    'machine|m=s' => \$machine,
136    'debug|d+'    => \$debug,
137    'help|h'      => \$help,
138    'quiet|q'     => \$quiet,
139    'man'         => \$man,
140    'log-files|l=s' => \$log,
141    'source|s=s'  => \$source,
142) || pb_syntax(-1,0);
143
144if (defined $help) {
145    pb_syntax(0,1);
146}
147if (defined $man) {
148    pb_syntax(0,2);
149}
150if (defined $quiet) {
151    $debug=-1;
152}
153if (defined $log) {
154    open(LOG,"> $log") || die "Unable to log to $log: $!";
155    $LOG = \*LOG;
156    $debug = 0  if ($debug == -1);
157}
158pb_log_init($debug, $LOG);
159pb_log(0,"Starting cbusterize\n");
160
161# Get conf file in context
162pb_conf_init($appname);
163# The system one
164pb_conf_add(cb_env_conffile());
165# The personal one if there is such
166pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
167
168# Get configuration parameters
169my ($basedir,$opt,$usemach,$pluginsdir,$cms) = pb_conf_get("cbbasedir","cbdatabase","cbusemachines","cbpluginssubdir","cbcms");
170
171# Check for mandatory params
172pod2usage("Error: --source is a mandatory argument\n") (if not defined $source);
173pod2usage("Error: --machine is a mandatory argument when configure with cbusemachines = true\n") if ($usemach->{$appname} =~ /true/) && (not defined $machine));
174
175# Are the source and basedir full path names ? if not, make it such
176$source = realpath($source);
177$basedir = realpath($basedir->{$appname});
178
179# debug mode overview
180if ($debug) {
181    print <<EOF;
182DEBUG MODE, not doing anything, just printing
183DEBUG: basedir = $basedir
184DEBUG: source  = $source
185EOF
186    if (defined ($machine)) {
187        print "DEBUG: machine = $machine\n";
188    }
189}
190
191# Create basedir if it doesn't exist
192if (not -d $basedir) {
193    if ($debug) {
194        print "DEBUG: Creating recursively directory $basedir\n";
195    } else {
196        mkpath($basedir,0,0755) or die "Unable to recursively create $basedir";
197        # TODO: Add it to the CMS
198    }
199}
200
201# Is the source a file or a dir ? Split the source parameter in 2
202my $srcdir = undef;
203my $srcfile = undef;
204if (-d $source) {
205    $srcdir = $source;
206} else {
207    $srcdir = dirname($source);
208    $srcfile = basename($source);
209    }
210
211if ($debug) {
212    print "DEBUG: Found srcdir = $srcdir\n";
213    if (defined $srcfile) {
214        print "DEBUG: Found srcfile = $srcfile\n";
215    } else {
216        print "DEBUG: Found no srcfile\n";
217    }
218}
219
220# Find the include/install.mk file by looking up the tree
221# from basedir/machine or basedir if no machine given
222my $sdir1 = $basedir;
223$sdir1 .= "/$machine" if defined ($machine);
224my $sdir = $sdir1;
225while (! -f "$sdir/include/install.mk") {
226    $sdir = dirname($sdir);
227    if ($sdir eq "/") {
228        die <<EOF;
229ERROR: No include/install.mk file was found while
230       parsing $sdir1 and its parents
231       Your tree is not CasparBuster compliant.
232       Please fix it before relaunching CasparBuster.pl
233EOF
234    }
235}
236if ($debug) {
237    print "DEBUG: Found sdir = $sdir\n";
238}
239# Now checking conformity
240print "CHECK: $sdir/include/install.mk\n" if ($debug);
241my $cf = 0;
242my $remote = undef;
243open(MF,"$sdir/include/install.mk");
244while (<MF>) {
245    if (($_ =~ /csp_UHOST\s*=/) && ($_ =~ /(\w+\@$machine.*)/)) {
246        $remote = $1;
247        $cf++; 
248        if ($debug) {
249            print "DEBUG: csp_UHOST conform line found with ref to $machine\n";
250            print "DEBUG: remote configuration is $remote\n";
251        }
252    }
253    if (/include\s+CasparBuster\/mk\/CasparBuster.mk/) {
254        $cf++; 
255        if ($debug) {
256            print "DEBUG: include conform line found with ref to CasparBuster.mk\n";
257        }
258    }
259}
260close(MF);
261if ($cf < 2) {
262    print "WARNING: Non conform $sdir/include/install.mk file found\n";
263}
264die "No remote configuration defined in $sdir/include/install.mk" if (not defined $remote);
265
266my $target = "$sdir1/$srcdir";
267# If both source and target are dirs, then copy into the parent of the target
268$target = basename($target) if ((not defined $srcfile) && (-d $target));
269
270# Create Makefiles if none exists also in parents, warn else
271my $tdir = $target;
272my $sd = $srcdir;
273while (! -f "$tdir/Makefile") {
274
275    # Create target if it doesn't exist
276    if (not -d $tdir) {
277        if ($debug) {
278            print "DEBUG: Creating recursively directory $tdir\n";
279        } else {
280            mkpath($tdir,0,0755) or die "Unable to recursively create $tdir";
281            print "INFO: Created $tdir you may want to add it to your CMS\n";
282        }
283    }
284
285    if (-f "$tdir/Makefile") {
286        print "CHECK: $tdir/Makefile\n" if ($debug);
287        # Now checking conformity
288        $cf = 0;
289        open(MF,"$tdir/Makefile") || die "Unable to open $tdir/Makefile";
290        while (<MF>) {
291            my $sd1 = $sd;
292            $sd1 =~ s|/|\/|g;
293            if (/csp_DIR\s*=\s*$sd1/) {
294                $cf++; 
295                if ($debug) {
296                    print "DEBUG: csp_DIR conform line found with ref to $sd\n";
297                }
298            }
299            $sd1 = $sdir;
300            $sd1 =~ s|/|\/|g;
301            if (/include\s+$sd1[\/]*install.mk/) {
302                $cf++; 
303                if ($debug) {
304                    print "DEBUG: include conform line found with ref to install.mk\n";
305                }
306            }
307            close(MF);
308   
309            if ($cf < 2) {
310                print "WARNING: Non conform $tdir/Makefile file found\n";
311            }
312        }
313    } else {
314        # In this case we create one
315        if ($debug) {
316            print <<EOF;
317DEBUG: Creating $tdir/Makefile with the following content:
318csp_DIR = $sd
319include $sdir/include/install.mk
320EOF
321        } else {
322            my $mf = "$tdir/Makefile";
323            $mf =~ s|//|/|g;
324            open(MF,"> $mf") || die "Unable to create $mf";
325            print MF <<EOF;
326# Created by CasparBuster.pl - \$Id\$
327csp_DIR = $sd
328# That include should be last of these declarations
329include $sdir/include/install.mk
330EOF
331            close(MF);
332            print "INFO: Created $mf you may want to add it to your CMS\n";
333        }
334    }
335    $tdir = dirname($tdir);
336    $sd = dirname($sd);
337    if ($debug) {
338        print "DEBUG: Next round, tdir = $tdir and sd = $sd\n";
339    }
340    # Stop at basedir
341    last if ($tdir eq $basedir);
342}
343
344# We need to know where to get the content from
345my $cmd;
346my $cmdopt = "";
347
348# Recursive if we copy dirs
349$cmdopt = "-r" if (not defined $srcfile);
350
351if (defined $machine) {
352    $cmd = "scp -p -q $cmdopt $remote:$source $target";
353} else {
354    $cmd = "cp -p $cmdopt $source $target";
355}
356# Now add content if not already there
357if ((defined $srcfile) && (! -f "$target/$srcfile")) {
358    if ($debug) {
359        print "DEBUG: launching $cmd\n";
360    } else {
361        system($cmd);   
362        print "INFO: Created $target/$srcfile you may want to add it to your CMS\n";
363    }
364}
Note: See TracBrowser for help on using the repository browser.