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