[1466] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
| 3 | =head1 NAME
|
---|
| 4 |
|
---|
[1485] | 5 | cb - CasparBuster looks at the structure in your CMS environment and deploy it to the target systems as needed
|
---|
[1466] | 6 |
|
---|
| 7 | =head1 SYNOPSIS
|
---|
| 8 |
|
---|
[1485] | 9 | cb [options]
|
---|
[1466] | 10 |
|
---|
[1485] | 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.
|
---|
[1466] | 19 |
|
---|
| 20 | =head1 OPTIONS
|
---|
| 21 |
|
---|
| 22 | =over 4
|
---|
| 23 |
|
---|
[1485] | 24 | =item B<--debug>
|
---|
[1466] | 25 |
|
---|
[1485] | 26 | Enter debug mode. This will print what would be done. No commands are executed,
|
---|
| 27 | so this is safe to use when testing.
|
---|
[1466] | 28 |
|
---|
[1485] | 29 | =item B<--help>
|
---|
[1466] | 30 |
|
---|
| 31 | Print a brief help message and exits.
|
---|
| 32 |
|
---|
| 33 | =item B<--man>
|
---|
| 34 |
|
---|
| 35 | Prints the manual page and exits.
|
---|
| 36 |
|
---|
[1485] | 37 | =item B<--machine> I<machine name>
|
---|
[1466] | 38 |
|
---|
[1485] | 39 | Specify the machine to consider when dealing with the CasparBuster structure.
|
---|
| 40 | The files will be pushed to this machine, and a subdirectory named after the machine
|
---|
| 41 | will be used under the basedir to look at the directory structure to deploy
|
---|
[1466] | 42 |
|
---|
[1485] | 43 | =item B<--source> I<path>
|
---|
[1466] | 44 |
|
---|
[1485] | 45 | Specify the path of the source file or directory to deploy with CasparBuster. Multiple paths can be specified separated by ','.
|
---|
[1466] | 46 |
|
---|
[1485] | 47 | =item B<--plugin> I<name>
|
---|
[1466] | 48 |
|
---|
[1485] | 49 | Specify the name of the plugin to deploy with CasparBuster. Multiple plugins can be specified separated by ','.
|
---|
| 50 | A 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.
|
---|
[1466] | 51 |
|
---|
[1485] | 52 | =back
|
---|
[1466] | 53 |
|
---|
[1485] | 54 | =head1 DESCRIPTION
|
---|
[1466] | 55 |
|
---|
[1485] | 56 | Deploy 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.
|
---|
[1466] | 57 |
|
---|
[1485] | 58 | =head1 EXAMPLES
|
---|
[1466] | 59 |
|
---|
[1485] | 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
|
---|
[1466] | 64 |
|
---|
[1485] | 65 | cb -m victoria2 -p dhcpd
|
---|
[1466] | 66 |
|
---|
[1485] | 67 | =head1 AUTHOR
|
---|
[1466] | 68 |
|
---|
| 69 | =over 4
|
---|
| 70 |
|
---|
[1485] | 71 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
[1466] | 72 |
|
---|
[1485] | 73 | =back
|
---|
[1466] | 74 |
|
---|
[1485] | 75 | =head1 LICENSE
|
---|
[1466] | 76 |
|
---|
[1485] | 77 | Copyright (C) 2012 Bruno Cornec <bruno@project-builder.org>
|
---|
| 78 | Released under the GPLv2 or the Artistic license at your will.
|
---|
[1466] | 79 |
|
---|
| 80 | =cut
|
---|
[1485] | 81 | use strict;
|
---|
| 82 | use CasparBuster::Version;
|
---|
| 83 | use CasparBuster::Env;
|
---|
| 84 | use CasparBuster::Plugin;
|
---|
| 85 | #use Cwd 'realpath';
|
---|
| 86 | use File::Find;
|
---|
[1487] | 87 | use Archive::Tar;
|
---|
[1485] | 88 | use Getopt::Long;
|
---|
| 89 | use Pod::Usage;
|
---|
| 90 | use Data::Dumper;
|
---|
| 91 | use ProjectBuilder::Base;
|
---|
| 92 | use ProjectBuilder::Conf;
|
---|
| 93 | use ProjectBuilder::VCS;
|
---|
| 94 | use DBI;
|
---|
| 95 | use DBD::SQLite;
|
---|
[1466] | 96 |
|
---|
[1485] | 97 | # settings
|
---|
| 98 | my $debug = 0;
|
---|
| 99 | my $help = undef;
|
---|
| 100 | my $man = undef;
|
---|
| 101 | my $source = undef;
|
---|
| 102 | my $machine = undef;
|
---|
| 103 | my $plugin = undef;
|
---|
| 104 | my $quiet = undef;
|
---|
| 105 | my $force = undef;
|
---|
| 106 | my $log = undef;
|
---|
| 107 | my $LOG = undef;
|
---|
[1466] | 108 |
|
---|
[1485] | 109 | my ($cbver,$cbrev) = cb_version_init();
|
---|
| 110 | my $appname = "cb";
|
---|
| 111 | $ENV{'PBPROJ'} = $appname;
|
---|
| 112 | pb_temp_init();
|
---|
[1466] | 113 |
|
---|
| 114 | # Initialize the syntax string
|
---|
[1485] | 115 | pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
|
---|
[1466] | 116 |
|
---|
[1485] | 117 | # parse command-line options
|
---|
| 118 | GetOptions(
|
---|
| 119 | 'machine|m=s' => \$machine,
|
---|
| 120 | 'debug|d+' => \$debug,
|
---|
| 121 | 'help|h' => \$help,
|
---|
| 122 | 'quiet|q' => \$quiet,
|
---|
| 123 | 'force|f' => \$force,
|
---|
| 124 | 'man' => \$man,
|
---|
| 125 | 'logfile|l=s' => \$log,
|
---|
| 126 | 'source|s=s' => \$source,
|
---|
| 127 | 'plugin|p=s' => \$plugin,
|
---|
[1466] | 128 | ) || pb_syntax(-1,0);
|
---|
| 129 |
|
---|
[1485] | 130 | if (defined $help) {
|
---|
[1466] | 131 | pb_syntax(0,1);
|
---|
| 132 | }
|
---|
[1485] | 133 | if (defined $man) {
|
---|
[1466] | 134 | pb_syntax(0,2);
|
---|
| 135 | }
|
---|
[1485] | 136 | if (defined $quiet) {
|
---|
| 137 | $debug=-1;
|
---|
[1466] | 138 | }
|
---|
[1485] | 139 | if (defined $log) {
|
---|
| 140 | open(LOG,"> $log") || die "Unable to log to $log: $!";
|
---|
| 141 | $LOG = \*LOG;
|
---|
| 142 | $debug = 0 if ($debug == -1);
|
---|
[1466] | 143 | }
|
---|
| 144 |
|
---|
[1485] | 145 | $pbdebug = $debug;
|
---|
| 146 | pb_log_init($debug, $LOG);
|
---|
| 147 | pb_log(0,"Starting cb\n");
|
---|
[1466] | 148 |
|
---|
[1485] | 149 | # Get conf file in context
|
---|
| 150 | pb_conf_init($appname);
|
---|
| 151 | # The personal one if there is such
|
---|
| 152 | pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
|
---|
| 153 | # The system one
|
---|
| 154 | pb_conf_add(cb_env_conffile());
|
---|
[1466] | 155 |
|
---|
[1485] | 156 | # Get configuration parameters
|
---|
| 157 | my %cb;
|
---|
| 158 | my $cb = \%cb;
|
---|
| 159 | ($cb->{'basedir'},$cb->{'usemachines'},$cb->{'cms'},$cb->{'database'}) = pb_conf_get("cbbasedir","cbusemachines","cbcms","cbdatabase");
|
---|
| 160 | pb_log(2,"%cb: ",Dumper($cb));
|
---|
[1466] | 161 |
|
---|
[1487] | 162 | die "No machine should be given when using machine structure" if (($cb->{'usemachines'}->{$appname} !~ /true/) && (defined $machine));
|
---|
[1466] | 163 |
|
---|
[1485] | 164 | if (defined $plugin) {
|
---|
| 165 | # Load plugins
|
---|
| 166 | cb_plugin_load();
|
---|
[1466] | 167 | }
|
---|
| 168 |
|
---|
[1485] | 169 | my $basedir = $cb->{'basedir'}->{$appname};
|
---|
| 170 | eval { $basedir =~ s/(\$ENV.+\})/$1/eeg };
|
---|
[1466] | 171 |
|
---|
[1485] | 172 | # Create basedir if it doesn't exist
|
---|
| 173 | die "Unable to find base directory at $basedir" if (not -d $basedir);
|
---|
[1466] | 174 |
|
---|
[1487] | 175 | pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\n");
|
---|
[1466] | 176 |
|
---|
[1487] | 177 | # Create database if not existing and give a handler
|
---|
| 178 | my $db = "$basedir/$cb->{'database'}->{$appname}";
|
---|
[1466] | 179 |
|
---|
[1485] | 180 | my $precmd = "";
|
---|
| 181 | if (! -f $db) {
|
---|
[1487] | 182 | $precmd = "CREATE TABLE dates (id INTEGER PRIMARY KEY AUTOINCREMENT, date INTEGER, file VARCHAR[65535], machine VARCHAR[65535])";
|
---|
[1466] | 183 | }
|
---|
| 184 |
|
---|
[1485] | 185 | my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
|
---|
| 186 | { RaiseError => 1, AutoCommit => 1 })
|
---|
| 187 | || die "Unable to connect to $db";
|
---|
[1466] | 188 |
|
---|
[1485] | 189 | if ($precmd ne "") {
|
---|
| 190 | my $sth = $dbh->prepare(qq{$precmd}) || die "Unable to create table into $db";
|
---|
| 191 | if ($debug) {
|
---|
[1487] | 192 | pb_log(1,"DEBUG: Creating DB $db\n");
|
---|
| 193 | pb_log(1,"DEBUG: with command $precmd\n");
|
---|
[1466] | 194 | } else {
|
---|
[1485] | 195 | $sth->execute();
|
---|
[1466] | 196 | }
|
---|
[1487] | 197 | $sth->finish();
|
---|
[1466] | 198 | }
|
---|
| 199 |
|
---|
[1487] | 200 | # Define destination dir and populate with a VCS export
|
---|
[1485] | 201 | my $dest = "$ENV{'TMPDIR'}/vcs.$$";
|
---|
[1487] | 202 | my $scheme = $cb->{'cms'}->{$appname};
|
---|
| 203 | pb_vcs_export(pb_vcs_get_uri($scheme,$basedir),$basedir,$dest);
|
---|
[1466] | 204 |
|
---|
[1487] | 205 | # Now distribute to the right machines
|
---|
| 206 | if defined ($machine) {
|
---|
| 207 | cb_distribute($machine);
|
---|
[1485] | 208 | } else {
|
---|
[1487] | 209 | if ($cb->{'usemachines'}->{$appname} =~ /true/) {
|
---|
| 210 | # First dir level is the machine, then the content
|
---|
| 211 | opendir(DIR,$dest) || die "Unable to open $dest: $!";
|
---|
| 212 | foreach my $m (readdir(DIR)) {
|
---|
| 213 | next if ($m =~ /^\./);
|
---|
| 214 | next if (! -d $m);
|
---|
| 215 | # Machine name
|
---|
| 216 | cb_distribute($m);
|
---|
| 217 | }
|
---|
| 218 | closedir(DIR);
|
---|
[1466] | 219 | } else {
|
---|
[1487] | 220 | cb_distribute(undef);
|
---|
[1466] | 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[1487] | 224 | # Cleanup
|
---|
| 225 | pb_rm_rf($dest);
|
---|
| 226 | $dbh->disconnect;
|
---|
| 227 |
|
---|
| 228 | # End of Main
|
---|
| 229 |
|
---|
| 230 | # Distribute files to target machines
|
---|
| 231 | sub cb_distribute {
|
---|
| 232 |
|
---|
| 233 | my $machine = shift;
|
---|
| 234 |
|
---|
| 235 | pb_log(2,"Entering into cb_distribute with machine $machine\n");
|
---|
| 236 |
|
---|
| 237 | # Use potentially a remote account if defined
|
---|
| 238 | my $account = undef;
|
---|
| 239 | my $remote = $machine;
|
---|
| 240 | ($account) = pb_conf_get_if("cbaccount") if (defined $machine);
|
---|
| 241 | $remote = $account->{$machine}."@".$machine if ((defined $account) && (defined $account->{$machine}));
|
---|
| 242 | pb_log(1, "DEBUG: remote = $remote\n") if (defined $remote);
|
---|
| 243 |
|
---|
| 244 | # Now handle plugins if any
|
---|
| 245 | my $cbp = ();
|
---|
| 246 |
|
---|
| 247 | # Handle plugins
|
---|
| 248 | if (defined $plugin) {
|
---|
| 249 | foreach my $p (split(/,/,$plugin)) {
|
---|
| 250 | pb_log(1,"Getting context for plugin $p\n");
|
---|
| 251 | $cbp = cb_plugin_get($p,$cbp);
|
---|
| 252 | # Adds mtime info to the plugin structure
|
---|
| 253 | foreach my $type ('files','dirs') {
|
---|
| 254 | foreach my $f (keys %{$cbp->{$p}->{$type}}) {
|
---|
| 255 | my $tdir = $dest;
|
---|
| 256 | if (defined $machine) {
|
---|
| 257 | $tdir = "$dest/$machine";
|
---|
| 258 | }
|
---|
| 259 | if (-r "$tdir/$f") {
|
---|
| 260 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$tdir/$f") || die "Unable to stat $tdir: $!";
|
---|
| 261 | $cbp->{$p}->{$type}->{$f}->{'mtime'} = $mtime;
|
---|
| 262 | } else {
|
---|
| 263 | pb_log(0,"WARNING: Unable to read $tdir/$f from plugin $p\n");
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
[1466] | 268 | }
|
---|
| 269 |
|
---|
[1487] | 270 | # Handle this source
|
---|
| 271 | if (defined $source) {
|
---|
| 272 | my $fullsource = "$source";
|
---|
| 273 | $fullsource = "$machine/$source" if (defined $machine);
|
---|
| 274 | pb_log(2,"fullsource is $fullsource\n");
|
---|
| 275 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$dest/$fullsource") || die "Unable to stat $fullsource: $!";
|
---|
| 276 | my $type = 'files';
|
---|
| 277 | if (-d $fullsource) {
|
---|
| 278 | $type = 'dirs';
|
---|
| 279 | }
|
---|
| 280 | die "ERROR: Only able to handle files or dirs with option --source\n" if ((! -d $fullsource) && (! -f $fullsource));
|
---|
[1466] | 281 |
|
---|
[1487] | 282 | $cbp->{"cb.source"}->{$type}->{$source}->{'uid'} = $uid;
|
---|
| 283 | $cbp->{"cb.source"}->{$type}->{$source}->{'gid'} = $gid;
|
---|
| 284 | $cbp->{"cb.source"}->{$type}->{$source}->{'mode'} = $mode;
|
---|
| 285 | $cbp->{"cb.source"}->{$type}->{$source}->{'mtime'} = $mtime;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
[1466] | 288 |
|
---|
[1487] | 289 | if ((not defined $source) && (not defined $plugin)) {
|
---|
| 290 | # Here we need to take all content under $dest considering that machine
|
---|
| 291 | if (defined $machine) {
|
---|
| 292 | find(\&cb_add_to_cbp,("$dest/$machine"));
|
---|
[1466] | 293 | } else {
|
---|
[1487] | 294 | find(\&cb_add_to_cbp,($dest));
|
---|
[1466] | 295 | }
|
---|
| 296 | }
|
---|
[1487] | 297 | pb_log(2,"INFO: RAW cbp: ".Dumper($cbp)."\n");
|
---|
[1466] | 298 |
|
---|
[1487] | 299 | # Clean up cbp structure by comparing with data stored in the DB
|
---|
| 300 | # Only keep the more recent modified content
|
---|
| 301 | my $mac = $machine;
|
---|
| 302 | $mac = "localhost" if (not defined $machine);
|
---|
| 303 | my $all = $dbh->selectall_arrayref("SELECT id,date,file,machine FROM dates WHERE machine=$mac");
|
---|
| 304 | # Check what in cbp is in the DB and deploy only if necessary
|
---|
| 305 | foreach my $k (keys %{$cbp}) {
|
---|
| 306 | foreach my $type ('files','dirs') {
|
---|
| 307 | foreach my $o (keys %{$cbp->{$k}->{$type}}) {
|
---|
| 308 | # Compare with info from DB
|
---|
| 309 | foreach my $row (@$all) {
|
---|
| 310 | my ($id, $date, $file, $mac1) = @$row;
|
---|
| 311 | # If less recent than in the DB remove it
|
---|
| 312 | delete($cbp->{$k}->{$type}->{$o}) if (($file eq $o) && ($date > $cbp->{$k}->{$type}->{$o}->{'mtime'}));
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | pb_log(2,"INFO: cleaned cbp: ".Dumper($cbp)."\n");
|
---|
[1466] | 319 |
|
---|
[1487] | 320 | # Now create a tar containing all the relevant content
|
---|
| 321 | my $tar = Archive::Tar->new;
|
---|
| 322 | $tar->add_files((keys %{$cbp->{$k}->{'files'}}),(keys %{$cbp->{$k}->{'dirs'}}));
|
---|
| 323 | $tar->write("$ENV{'TMPDIR'}/content$$.tar");
|
---|
[1466] | 324 |
|
---|
[1487] | 325 | # Copy the tar file and extract it + set up modes/uids/gids
|
---|
[1485] | 326 | if (defined $machine) {
|
---|
[1487] | 327 | $cmd = "scp -p -q $remote:$source $target";
|
---|
[1466] | 328 | } else {
|
---|
[1487] | 329 | $cmd = "cp -p $source $target";
|
---|
[1466] | 330 | }
|
---|
| 331 |
|
---|
[1487] | 332 | if ($debug) {
|
---|
| 333 | pb_log(1,"DEBUG: launching $cmd\n");
|
---|
[1466] | 334 | } else {
|
---|
[1487] | 335 | pb_system($cmd);
|
---|
| 336 | pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system\n");
|
---|
[1466] | 337 | }
|
---|
[1487] | 338 | pb_log(2,"Exiting cb_distribute\n");
|
---|
[1466] | 339 | }
|
---|
[1487] | 340 |
|
---|
| 341 | sub cb_add_to_cbp {
|
---|
| 342 |
|
---|
| 343 | my $type = 'files';
|
---|
| 344 | if (-d $File::Find::name) {
|
---|
| 345 | $type = 'dirs';
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | my $destname = $File::Find::name;
|
---|
| 349 | # Target name is without the $dest part
|
---|
| 350 | $destname =~ s|^$dest||;
|
---|
| 351 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$File::Find::name") || die "Unable to stat $File::Find::name: $!";
|
---|
| 352 | $cbp->{"cb.full"}->{$type}->{$destname}->{'uid'} = $uid;
|
---|
| 353 | $cbp->{"cb.full"}->{$type}->{$destname}->{'gid'} = $gid;
|
---|
| 354 | $cbp->{"cb.full"}->{$type}->{$destname}->{'mode'} = $mode;
|
---|
| 355 | $cbp->{"cb.full"}->{$type}->{$destname}->{'mtime'} = $mtime;
|
---|
| 356 | pb_log(2,"Adding $destname ($uid,$gid,$mode) to cbp\n");
|
---|
| 357 | }
|
---|