source: projects/casparbuster/devel/bin/cb @ 1485

Last change on this file since 1485 was 1485, checked in by bruno, 7 years ago
  • Adds a Plugin module to support plugin management
  • cbusterize now works with both -s and -p options
  • One plugin example added (dhcpd)

-cb script started. Not working now

  • Property svn:executable set to *
File size: 8.2 KB
Line 
1#!/usr/bin/perl -w
2#
3=head1 NAME
4
5cb - CasparBuster looks at the structure in your CMS environment and deploy it to the target systems as needed
6
7=head1 SYNOPSIS
8
9cb [options]
10
11 Options:
12   --debug  |-d         debug mode
13   --help   |-h         brief help message
14   --man            full documentation
15   --force   |-f            force copy of files, even if they exist
16   --source |-s <file/dir>  directory or files to copy from the CasparBuster tree (',' separated if many) to the target
17   --plugin |-p <plugin name>   plugin defining what to copy from the CasparBuster tree (',' separated if many) to the target
18   --machine|-m <machine>   machine to deploy on.
19
20=head1 OPTIONS
21
22=over 4
23
24=item B<--debug>
25
26Enter debug mode. This will print what would be done. No commands are executed,
27so this is safe to use when testing.
28
29=item B<--help>
30
31Print a brief help message and exits.
32
33=item B<--man>
34
35Prints the manual page and exits.
36
37=item B<--machine> I<machine name>
38
39Specify the machine to consider when dealing with the CasparBuster structure.
40The files will be pushed to this machine, and a subdirectory named after the machine
41will be used under the basedir to look at the directory structure to deploy
42
43=item B<--source> I<path>
44
45Specify the path of the source file or directory to deploy with CasparBuster. Multiple paths can be specified separated by ','.
46
47=item B<--plugin> I<name>
48
49Specify the name of the plugin to deploy with CasparBuster. Multiple plugins can be specified separated by ','.
50A plugin defines a set of files (with their mode and owner), a set of directories (with their mode and owner) and a set of scripts to launch once the files are copied remotely.
51
52=back
53
54=head1 DESCRIPTION
55
56Deploy the standard CasparBuster structure created by I<cbusterize>. It will reinstall all files and directory in the plugin, with correct owner, group and mode, and launch at the end the script to re-enable potentially the service using the updated files.
57
58=head1 EXAMPLES
59
60    # this will deploy the appropriate CasparBuster environment for DHCP
61    # from the base ~/prj/musique-ancienne.org directory (Cf cbbasedir in cb.conf)
62    # containing the directory victoria2 for this machine
63    # to which it will copy the required files
64
65    cb -m victoria2 -p dhcpd
66
67=head1 AUTHOR
68
69=over 4
70
71Bruno Cornec, http://brunocornec.wordpress.com
72
73=back
74
75=head1 LICENSE
76
77Copyright (C) 2012  Bruno Cornec <bruno@project-builder.org>
78Released under the GPLv2 or the Artistic license at your will.
79
80=cut
81use strict;
82use CasparBuster::Version;
83use CasparBuster::Env;
84use CasparBuster::Plugin;
85#use Cwd 'realpath';
86use File::Find;
87use File::Copy;
88use File::Basename;
89use File::Path;
90use File::Glob ':glob';
91use Getopt::Long;
92use Pod::Usage;
93use Data::Dumper;
94use List::Util qw(first);
95use ProjectBuilder::Base;
96use ProjectBuilder::Conf;
97use ProjectBuilder::VCS;
98use DBI;
99use DBD::SQLite;
100
101# settings
102my $debug = 0;
103my $help = undef;
104my $man = undef;
105my $source = undef;
106my $machine = undef;
107my $plugin = undef;
108my $quiet = undef;
109my $force = undef;
110my $log = undef;
111my $LOG = undef;
112
113my ($cbver,$cbrev) = cb_version_init();
114my $appname = "cb";
115$ENV{'PBPROJ'} = $appname;
116pb_temp_init();
117
118# Initialize the syntax string
119pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
120
121# parse command-line options
122GetOptions(
123    'machine|m=s' => \$machine,
124    'debug|d+'    => \$debug,
125    'help|h'      => \$help,
126    'quiet|q'     => \$quiet,
127    'force|f'     => \$force,
128    'man'         => \$man,
129    'logfile|l=s' => \$log,
130    'source|s=s'  => \$source,
131    'plugin|p=s'  => \$plugin,
132) || pb_syntax(-1,0);
133
134if (defined $help) {
135    pb_syntax(0,1);
136}
137if (defined $man) {
138    pb_syntax(0,2);
139}
140if (defined $quiet) {
141    $debug=-1;
142}
143if (defined $log) {
144    open(LOG,"> $log") || die "Unable to log to $log: $!";
145    $LOG = \*LOG;
146    $debug = 0  if ($debug == -1);
147}
148
149$pbdebug = $debug;
150pb_log_init($debug, $LOG);
151pb_log(0,"Starting cb\n");
152
153# Get conf file in context
154pb_conf_init($appname);
155# The personal one if there is such
156pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
157# The system one
158pb_conf_add(cb_env_conffile());
159
160# Get configuration parameters
161my %cb;
162my $cb = \%cb;
163($cb->{'basedir'},$cb->{'usemachines'},$cb->{'cms'},$cb->{'database'}) = pb_conf_get("cbbasedir","cbusemachines","cbcms","cbdatabase");
164pb_log(2,"%cb: ",Dumper($cb));
165
166# Check for mandatory params
167
168if (defined $plugin) {
169    # Load plugins
170    cb_plugin_load();
171}
172
173my $basedir = $cb->{'basedir'}->{$appname};
174eval { $basedir =~ s/(\$ENV.+\})/$1/eeg };
175
176pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\n");
177
178# Use potentially a remote account if defined
179my $account = undef;
180my $remote = $machine;
181($account) = pb_conf_get_if("cbaccount") if (defined $machine);
182$remote = $account->{$machine}."@".$machine if ((defined $account) && (defined $account->{$machine}));
183pb_log(1, "DEBUG: remote = $remote\n") if (defined $remote);
184
185# Create basedir if it doesn't exist
186die "Unable to find base directory at $basedir" if (not -d $basedir);
187
188# Now handle plugins if any
189my $cbp = ();
190
191if (defined $plugin) {
192    foreach my $p (split(/,/,$plugin)) {   
193        pb_log(1,"Getting context for plugin $p\n");
194        $cbp = cb_plugin_get($p,$cbp);
195        pb_log(2,"cbp: ".Dumper($cbp)."\n");
196    }
197}
198
199my $db = "$cb->{'basedir'}/$cb->{'database'}";
200
201my $precmd = "";
202if (! -f $db) {
203    $precmd = "CREATE TABLE dates (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, file VARCHAR[65535])";
204}
205
206my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
207            { RaiseError => 1, AutoCommit => 1 })
208            || die "Unable to connect to $db";
209
210if ($precmd ne "") {
211    my $sth = $dbh->prepare(qq{$precmd}) || die "Unable to create table into $db";
212    if ($debug) {
213            pb_log(1,"DEBUG: Creatung DB $db\n");
214            pb_log(1,"DEBUG: with command $precmd\n");
215    } else {
216        $sth->execute();
217    }
218}
219
220my $dest = "$ENV{'TMPDIR'}/vcs.$$";
221pb_vcs_export(,$cb->{'basedir'},$dest);
222
223# Is the source a file or a dir ? Split the source parameter in 2
224my $srcdir = undef;
225my $srcfile = undef;
226my $cmd = undef;
227
228if (not defined $machine) {
229    if (-d $source) {
230        $srcdir = $source;
231    } else {
232        $srcdir = dirname($source);
233        $srcfile = basename($source);
234    }
235} else {
236    $cmd = "ssh -q $remote \'echo \"if [ -d $source ]; then exit 0; else exit -1; fi\" | sudo bash\'";
237    my $res = pb_system($cmd,"","quiet");
238    pb_log(2,"DEBUG: Found res = $res\n");
239    if ($res == 0) {
240        $srcdir = $source;
241        pb_log(1,"DEBUG: Found remote dir = $source\n");
242    } else {
243        $srcdir = dirname($source);
244        $srcfile = basename($source);
245        pb_log(1,"DEBUG: Found remote file = $source\n");
246    }
247}
248
249pb_log(1,"DEBUG: Found srcdir = $srcdir\n");
250if (defined $srcfile) {
251    pb_log(1,"DEBUG: Found srcfile = $srcfile\n");
252} else {
253    pb_log(1,"DEBUG: Found no srcfile\n");
254}
255
256# Deduce the target directory from the local structure and the source
257my $target = $basedir;
258$target .= "/$machine" if defined ($machine);
259$target .= "$srcdir";
260
261my $scheme = $cb->{'cms'}->{$appname};
262
263# If both source and target are dirs, then copy into the parent of the target
264$target = basename($target) if ((not defined $srcfile) && (-d $target));
265
266# Create target if it doesn't exist when we have to copy a file
267if ((not -d $target) && (defined $srcfile)) {
268    if ($debug) {
269        pb_log(1,"DEBUG: Creating recursively directory $target\n");
270    } else {
271        pb_mkdir_p($target) || die "Unable to recursively create $target: $!";
272        pb_vcs_add($scheme,$target);
273        pb_log(0,"INFO: Created $target and added it to your $scheme system\n");
274    }
275}
276
277# We need to know where to get the content from
278my $cmdopt = "";
279
280# Recursive if we copy dirs
281$cmdopt = "-r" if (not defined $srcfile);
282
283if (defined $machine) {
284    $cmd = "scp -p -q $cmdopt $remote:$source $target";
285} else {
286    $cmd = "cp -p $cmdopt $source $target";
287}
288
289# Now add content if not already there
290if (defined $srcfile) {
291    # File case
292    if ((! -f "$target/$srcfile") || (defined $force)){
293        if ($debug) {
294            pb_log(1,"DEBUG: launching $cmd\n");
295        } else {
296            pb_system($cmd);
297            pb_vcs_add($scheme,"$target/$srcfile");
298            pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system\n");
299        }
300    } else {
301        pb_log(0,"INFO: File $target/$srcfile already there\n");
302    }
303} else {
304    # Directory case
305    if ($debug) {
306        pb_log(1,"DEBUG: launching $cmd\n");
307    } else {
308        pb_system($cmd);
309        pb_vcs_add($scheme,"$target");
310        pb_log(0,"INFO: Created $target and added it to your $scheme system\n");
311    }
312}
313pb_log(2,"Exiting cb_busterize\n");
314}
Note: See TracBrowser for help on using the repository browser.