source: ProjectBuilder/projects/casparbuster/devel/bin/cbusterize@ 1470

Last change on this file since 1470 was 1470, checked in by Bruno Cornec, 12 years ago
  • Improve again cbusterize with more usage of pb functions, including VCS ones now that they are available
  • Property svn:executable set to *
File size: 7.2 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;
116use ProjectBuilder::VCS;
117
118# settings
119my $debug = 0;
120my $help = undef;
121my $man = undef;
122my $source = undef;
123my $machine = undef;
124my $plugin = undef;
125my $quiet = undef;
126my $log = undef;
127my $LOG = undef;
128
129my ($cbver,$cbrev) = cb_version_init();
130my $appname = "cb";
131$ENV{'PBPROJ'} = $appname;
132pb_temp_init();
133
134# Initialize the syntax string
135pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
136
137# parse command-line options
138GetOptions(
139 'machine|m=s' => \$machine,
140 'debug|d+' => \$debug,
141 'help|h' => \$help,
142 'quiet|q' => \$quiet,
143 'man' => \$man,
144 'log-files|l=s' => \$log,
145 'source|s=s' => \$source,
146 'plugin|p=s' => \$plugin,
147) || pb_syntax(-1,0);
148
149if (defined $help) {
150 pb_syntax(0,1);
151}
152if (defined $man) {
153 pb_syntax(0,2);
154}
155if (defined $quiet) {
156 $debug=-1;
157}
158if (defined $log) {
159 open(LOG,"> $log") || die "Unable to log to $log: $!";
160 $LOG = \*LOG;
161 $debug = 0 if ($debug == -1);
162}
163
164$pbdebug = $debug;
165pb_log_init($debug, $LOG);
166pb_log(0,"Starting cbusterize\n");
167
168# Get conf file in context
169pb_conf_init($appname);
170# The personal one if there is such
171pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
172# The system one
173pb_conf_add(cb_env_conffile());
174
175# Get configuration parameters
176my %cb;
177my $cb = \%cb;
178($cb->{'basedir'},$cb->{'database'},$cb->{'usemachines'},$cb->{'pluginsdir'},$cb->{'cms'}) = pb_conf_get("cbbasedir","cbdatabase","cbusemachines","cbpluginssubdir","cbcms");
179pb_log(2,"%cb: ",Dumper($cb));
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
192pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\nDEBUG: source = $source\n");
193pb_log(1, "DEBUG: machine = $machine\n") if (defined ($machine));
194
195# Create basedir if it doesn't exist
196if (not -d $basedir) {
197 if ($debug) {
198 pb_log(1, "DEBUG: Creating recursively directory $basedir\n");
199 } else {
200 pb_mkdir_p($basedir) || die "Unable to recursively create $basedir";
201 }
202}
203
204# Is the source a file or a dir ? Split the source parameter in 2
205my $srcdir = undef;
206my $srcfile = undef;
207# TODO: That should be remote !!
208if (-d $source) {
209 $srcdir = $source;
210} else {
211 $srcdir = dirname($source);
212 $srcfile = basename($source);
213 }
214
215pb_log(1,"DEBUG: Found srcdir = $srcdir\n");
216if (defined $srcfile) {
217 pb_log(1,"DEBUG: Found srcfile = $srcfile\n");
218} else {
219 pb_log(1,"DEBUG: Found no srcfile\n");
220}
221
222# Deduce the target directory from the local structure and the source
223my $target = $basedir;
224$target .= "/$machine" if defined ($machine);
225$target .= "$srcdir";
226
227my $scheme = $cb->{'cms'}->{$appname};
228
229# If both source and target are dirs, then copy into the parent of the target
230$target = basename($target) if ((not defined $srcfile) && (-d $target));
231
232# Create target if it doesn't exist
233if (not -d $target) {
234 if ($debug) {
235 pb_log(1,"DEBUG: Creating recursively directory $target\n");
236 } else {
237 pb_mkdir_p($target) || die "Unable to recursively create $target";
238 pb_vcs_add($scheme,$target);
239 pb_log(0,"INFO: Created $target and added it to your $scheme system\n");
240 }
241}
242
243# We need to know where to get the content from
244my $cmd;
245my $cmdopt = "";
246
247# Recursive if we copy dirs
248$cmdopt = "-r" if (not defined $srcfile);
249
250if (defined $machine) {
251 $cmd = "scp -p -q $cmdopt $machine:$source $target";
252} else {
253 $cmd = "cp -p $cmdopt $source $target";
254}
255
256# Now add content if not already there
257if (defined $srcfile) {
258 # File case
259 if (! -f "$target/$srcfile") {
260 if ($debug) {
261 pb_log(1,"DEBUG: launching $cmd\n");
262 } else {
263 pb_system($cmd);
264 pb_vcs_add($scheme,"$target/$srcfile");
265 pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system\n");
266 }
267 } else {
268 pb_log(0,"INFO: File $target/$srcfile already there\n");
269 }
270} else {
271 # Directory case
272 # TODO: if targetlocal dir alredy exists, take the parent
273 if ($debug) {
274 pb_log(1,"DEBUG: launching $cmd\n");
275 } else {
276 pb_system($cmd);
277 pb_vcs_add($scheme,"$target");
278 pb_log(0,"INFO: Created $target and added it to your $scheme system\n");
279 }
280}
Note: See TracBrowser for help on using the repository browser.