1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | cbusterize - Creates the correct CasparBuster structure in your CMS environment
|
---|
6 |
|
---|
7 | =head1 SYNOPSIS
|
---|
8 |
|
---|
9 | cbusterize.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 |
|
---|
24 | Enter debug mode. This will print what would be done. No commands are executed,
|
---|
25 | so this is safe to use when testing.
|
---|
26 |
|
---|
27 | =item B<--help>
|
---|
28 |
|
---|
29 | Print a brief help message and exits.
|
---|
30 |
|
---|
31 | =item B<--man>
|
---|
32 |
|
---|
33 | Prints the manual page and exits.
|
---|
34 |
|
---|
35 | =item B<--machine> I<machine name>
|
---|
36 |
|
---|
37 | Specify the machine to consider when dealing with the CasparBuster structure.
|
---|
38 | The file will be taken from this machine, and a subdirectory named after the machine
|
---|
39 | will be used under the basedir to host the directory structure to manage
|
---|
40 |
|
---|
41 | =item B<--source> I<path>
|
---|
42 |
|
---|
43 | Specify the path to the source file or directory to manage with CasparBuster.
|
---|
44 |
|
---|
45 | =back
|
---|
46 |
|
---|
47 | =head1 DESCRIPTION
|
---|
48 |
|
---|
49 | Creates a directory under the machine dir passed as parameter in working
|
---|
50 | directory or directory passed as parameter named like the last path
|
---|
51 | element of the parameter, and creates the standard CasparBuster setup
|
---|
52 | that refers to the parameter path in the new directory, and configuration
|
---|
53 | files when possible. It also copies the original config file into the new dir.
|
---|
54 | Is reasonably picky about path names, tries to avoid common errors.
|
---|
55 |
|
---|
56 | Schema looks like:
|
---|
57 |
|
---|
58 | Base 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 |
|
---|
74 | Use of machines require use of option -m (if using cbusemachines in cb.conf)
|
---|
75 | If 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 |
|
---|
91 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
92 |
|
---|
93 | =back
|
---|
94 |
|
---|
95 | =head1 LICENSE
|
---|
96 |
|
---|
97 | Copyright (C) 2012 Bruno Cornec <bruno@project-builder.org>
|
---|
98 | Released under the GPLv2 or the Artistic license at your will.
|
---|
99 |
|
---|
100 | =cut
|
---|
101 | use strict;
|
---|
102 | use Cwd 'realpath';
|
---|
103 | use File::Find;
|
---|
104 | use File::Copy;
|
---|
105 | use File::Basename;
|
---|
106 | use File::Path;
|
---|
107 | use File::Glob ':glob';
|
---|
108 | use Getopt::Long;
|
---|
109 | use Pod::Usage;
|
---|
110 | use List::Util qw(first);
|
---|
111 | use ProjectBuilder::Base;
|
---|
112 | use ProjectBuilder::Conf;
|
---|
113 | use CasparBuster::Env;
|
---|
114 |
|
---|
115 | # settings
|
---|
116 | my $debug = 0;
|
---|
117 | my $help = 0;
|
---|
118 | my $man = 0;
|
---|
119 | my $source = undef;
|
---|
120 | my $machine = undef;
|
---|
121 | my $quiet = undef;
|
---|
122 | my $log = undef;
|
---|
123 | my $LOG = undef;
|
---|
124 |
|
---|
125 | my ($cbver,$cbrev) = pb_version_init();
|
---|
126 | my $appname = "cb";
|
---|
127 | $ENV{'PBPROJ'} = $appname;
|
---|
128 | pb_temp_init();
|
---|
129 |
|
---|
130 | # Initialize the syntax string
|
---|
131 | pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
|
---|
132 |
|
---|
133 | # parse command-line options
|
---|
134 | GetOptions(
|
---|
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 |
|
---|
144 | if (defined $help) {
|
---|
145 | pb_syntax(0,1);
|
---|
146 | }
|
---|
147 | if (defined $man) {
|
---|
148 | pb_syntax(0,2);
|
---|
149 | }
|
---|
150 | if (defined $quiet) {
|
---|
151 | $debug=-1;
|
---|
152 | }
|
---|
153 | if (defined $log) {
|
---|
154 | open(LOG,"> $log") || die "Unable to log to $log: $!";
|
---|
155 | $LOG = \*LOG;
|
---|
156 | $debug = 0 if ($debug == -1);
|
---|
157 | }
|
---|
158 | pb_log_init($debug, $LOG);
|
---|
159 | pb_log(0,"Starting cbusterize\n");
|
---|
160 |
|
---|
161 | # Get conf file in context
|
---|
162 | pb_conf_init($appname);
|
---|
163 | # The system one
|
---|
164 | pb_conf_add(cb_env_conffile());
|
---|
165 | # The personal one if there is such
|
---|
166 | pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
|
---|
167 |
|
---|
168 | # Get configuration parameters
|
---|
169 | my ($basedir,$opt,$usemach,$pluginsdir,$cms) = pb_conf_get("cbbasedir","cbdatabase","cbusemachines","cbpluginssubdir","cbcms");
|
---|
170 |
|
---|
171 | # Check for mandatory params
|
---|
172 | pod2usage("Error: --source is a mandatory argument\n") (if not defined $source);
|
---|
173 | pod2usage("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
|
---|
180 | if ($debug) {
|
---|
181 | print <<EOF;
|
---|
182 | DEBUG MODE, not doing anything, just printing
|
---|
183 | DEBUG: basedir = $basedir
|
---|
184 | DEBUG: source = $source
|
---|
185 | EOF
|
---|
186 | if (defined ($machine)) {
|
---|
187 | print "DEBUG: machine = $machine\n";
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | # Create basedir if it doesn't exist
|
---|
192 | if (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
|
---|
202 | my $srcdir = undef;
|
---|
203 | my $srcfile = undef;
|
---|
204 | if (-d $source) {
|
---|
205 | $srcdir = $source;
|
---|
206 | } else {
|
---|
207 | $srcdir = dirname($source);
|
---|
208 | $srcfile = basename($source);
|
---|
209 | }
|
---|
210 |
|
---|
211 | if ($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
|
---|
222 | my $sdir1 = $basedir;
|
---|
223 | $sdir1 .= "/$machine" if defined ($machine);
|
---|
224 | my $sdir = $sdir1;
|
---|
225 | while (! -f "$sdir/include/install.mk") {
|
---|
226 | $sdir = dirname($sdir);
|
---|
227 | if ($sdir eq "/") {
|
---|
228 | die <<EOF;
|
---|
229 | ERROR: 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
|
---|
233 | EOF
|
---|
234 | }
|
---|
235 | }
|
---|
236 | if ($debug) {
|
---|
237 | print "DEBUG: Found sdir = $sdir\n";
|
---|
238 | }
|
---|
239 | # Now checking conformity
|
---|
240 | print "CHECK: $sdir/include/install.mk\n" if ($debug);
|
---|
241 | my $cf = 0;
|
---|
242 | my $remote = undef;
|
---|
243 | open(MF,"$sdir/include/install.mk");
|
---|
244 | while (<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 | }
|
---|
260 | close(MF);
|
---|
261 | if ($cf < 2) {
|
---|
262 | print "WARNING: Non conform $sdir/include/install.mk file found\n";
|
---|
263 | }
|
---|
264 | die "No remote configuration defined in $sdir/include/install.mk" if (not defined $remote);
|
---|
265 |
|
---|
266 | my $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
|
---|
271 | my $tdir = $target;
|
---|
272 | my $sd = $srcdir;
|
---|
273 | while (! -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;
|
---|
317 | DEBUG: Creating $tdir/Makefile with the following content:
|
---|
318 | csp_DIR = $sd
|
---|
319 | include $sdir/include/install.mk
|
---|
320 | EOF
|
---|
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\$
|
---|
327 | csp_DIR = $sd
|
---|
328 | # That include should be last of these declarations
|
---|
329 | include $sdir/include/install.mk
|
---|
330 | EOF
|
---|
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
|
---|
345 | my $cmd;
|
---|
346 | my $cmdopt = "";
|
---|
347 |
|
---|
348 | # Recursive if we copy dirs
|
---|
349 | $cmdopt = "-r" if (not defined $srcfile);
|
---|
350 |
|
---|
351 | if (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
|
---|
357 | if ((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 | }
|
---|