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

Last change on this file since 1468 was 1468, checked in by bruno, 7 years ago
  • Still working to have a first cbusterize version working
  • Property svn:executable set to *
File size: 6.7 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 [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 CasparBuster::Version;
103use CasparBuster::Env;
104#use Cwd 'realpath';
105use File::Find;
106use File::Copy;
107use File::Basename;
108use File::Path;
109use File::Glob ':glob';
110use Getopt::Long;
111use Pod::Usage;
112use Data::Dumper;
113use List::Util qw(first);
114use ProjectBuilder::Base;
115use ProjectBuilder::Conf;
116
117# settings
118my $debug = 0;
119my $help = undef;
120my $man = undef;
121my $source = undef;
122my $machine = undef;
123my $plugin = undef;
124my $quiet = undef;
125my $log = undef;
126my $LOG = undef;
127
128my ($cbver,$cbrev) = cb_version_init();
129my $appname = "cb";
130$ENV{'PBPROJ'} = $appname;
131pb_temp_init();
132
133# Initialize the syntax string
134pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
135
136# parse command-line options
137GetOptions(
138    'machine|m=s' => \$machine,
139    'debug|d+'    => \$debug,
140    'help|h'      => \$help,
141    'quiet|q'     => \$quiet,
142    'man'         => \$man,
143    'log-files|l=s' => \$log,
144    'source|s=s'  => \$source,
145    'plugin|p=s'  => \$plugin,
146) || pb_syntax(-1,0);
147
148if (defined $help) {
149    pb_syntax(0,1);
150}
151if (defined $man) {
152    pb_syntax(0,2);
153}
154if (defined $quiet) {
155    $debug=-1;
156}
157if (defined $log) {
158    open(LOG,"> $log") || die "Unable to log to $log: $!";
159    $LOG = \*LOG;
160    $debug = 0  if ($debug == -1);
161}
162
163$pbdebug = $debug;
164pb_log_init($debug, $LOG);
165pb_log(0,"Starting cbusterize\n");
166
167# Get conf file in context
168pb_conf_init($appname);
169# The personal one if there is such
170pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
171# The system one
172pb_conf_add(cb_env_conffile());
173
174# Get configuration parameters
175my %cb;
176my $cb = \%cb;
177($cb->{'basedir'},$cb->{'database'},$cb->{'usemachines'},$cb->{'pluginsdir'},$cb->{'cms'}) = pb_conf_get("cbbasedir","cbdatabase","cbusemachines","cbpluginssubdir","cbcms");
178
179print Dumper($cb) if ($debug);
180
181# Check for mandatory params
182pod2usage("Error: --source or --plugin is a mandatory argument\n") if ((not defined $source) && (not defined $plugin));
183pod2usage("Error: --machine is a mandatory argument when configure with cbusemachines = true\n") if (($cb->{'usemachines'}->{$appname} =~ /true/) && (not defined $machine));
184
185if (defined $plugin) {
186    # Load plugin conf
187}
188
189my $basedir = $cb->{'basedir'}->{$appname};
190eval { $basedir =~ s/(\$ENV.+\})/$1/eeg };
191
192# debug mode overview
193if ($debug) {
194    print <<EOF;
195DEBUG MODE, not doing anything, just printing
196DEBUG: basedir = $basedir
197DEBUG: source  = $source
198EOF
199    if (defined ($machine)) {
200        print "DEBUG: machine = $machine\n";
201    }
202}
203
204# Create basedir if it doesn't exist
205if (not -d $basedir) {
206    if ($debug) {
207        print "DEBUG: Creating recursively directory $basedir\n";
208    } else {
209        mkpath($basedir,0,0755) or die "Unable to recursively create $basedir";
210        # TODO: Add it to the CMS
211    }
212}
213
214# Is the source a file or a dir ? Split the source parameter in 2
215my $srcdir = undef;
216my $srcfile = undef;
217if (-d $source) {
218    $srcdir = $source;
219} else {
220    $srcdir = dirname($source);
221    $srcfile = basename($source);
222    }
223
224if ($debug) {
225    print "DEBUG: Found srcdir = $srcdir\n";
226    if (defined $srcfile) {
227        print "DEBUG: Found srcfile = $srcfile\n";
228    } else {
229        print "DEBUG: Found no srcfile\n";
230    }
231}
232
233# Deduce the target directory from the local structure and the source
234my $target = $basedir;
235$target .= "/$machine" if defined ($machine);
236$target .= "$srcdir";
237
238# If both source and target are dirs, then copy into the parent of the target
239$target = basename($target) if ((not defined $srcfile) && (-d $target));
240
241# Create target if it doesn't exist
242if (not -d $target) {
243    if ($debug) {
244        print "DEBUG: Creating recursively directory $target\n";
245    } else {
246        mkpath($target,0,0755) or die "Unable to recursively create $target";
247        print "INFO: Created $target you may want to add it to your CMS\n";
248        # TODO: Add it to the CMS
249    }
250}
251
252# We need to know where to get the content from
253my $cmd;
254my $cmdopt = "";
255
256# Recursive if we copy dirs
257$cmdopt = "-r" if (not defined $srcfile);
258
259if (defined $machine) {
260    $cmd = "scp -p -q $cmdopt $machine:$source $target";
261} else {
262    $cmd = "cp -p $cmdopt $source $target";
263}
264
265# Now add content if not already there
266if ((defined $srcfile) && (! -f "$target/$srcfile")) {
267    if ($debug) {
268        print "DEBUG: launching $cmd\n";
269    } else {
270        system($cmd);   
271        print "INFO: Created $target/$srcfile you may want to add it to your CMS\n";
272    }
273}
Note: See TracBrowser for help on using the repository browser.