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 File::Copy;
|
---|
88 | use File::Basename;
|
---|
89 | use File::Path;
|
---|
90 | use File::Glob ':glob';
|
---|
91 | use Getopt::Long;
|
---|
92 | use Pod::Usage;
|
---|
93 | use Data::Dumper;
|
---|
94 | use List::Util qw(first);
|
---|
95 | use ProjectBuilder::Base;
|
---|
96 | use ProjectBuilder::Conf;
|
---|
97 | use ProjectBuilder::VCS;
|
---|
98 | use DBI;
|
---|
99 | use DBD::SQLite;
|
---|
100 |
|
---|
101 | # settings
|
---|
102 | my $debug = 0;
|
---|
103 | my $help = undef;
|
---|
104 | my $man = undef;
|
---|
105 | my $source = undef;
|
---|
106 | my $machine = undef;
|
---|
107 | my $plugin = undef;
|
---|
108 | my $quiet = undef;
|
---|
109 | my $force = undef;
|
---|
110 | my $log = undef;
|
---|
111 | my $LOG = undef;
|
---|
112 |
|
---|
113 | my ($cbver,$cbrev) = cb_version_init();
|
---|
114 | my $appname = "cb";
|
---|
115 | $ENV{'PBPROJ'} = $appname;
|
---|
116 | pb_temp_init();
|
---|
117 |
|
---|
118 | # Initialize the syntax string
|
---|
119 | pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n");
|
---|
120 |
|
---|
121 | # parse command-line options
|
---|
122 | GetOptions(
|
---|
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 |
|
---|
134 | if (defined $help) {
|
---|
135 | pb_syntax(0,1);
|
---|
136 | }
|
---|
137 | if (defined $man) {
|
---|
138 | pb_syntax(0,2);
|
---|
139 | }
|
---|
140 | if (defined $quiet) {
|
---|
141 | $debug=-1;
|
---|
142 | }
|
---|
143 | if (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;
|
---|
150 | pb_log_init($debug, $LOG);
|
---|
151 | pb_log(0,"Starting cb\n");
|
---|
152 |
|
---|
153 | # Get conf file in context
|
---|
154 | pb_conf_init($appname);
|
---|
155 | # The personal one if there is such
|
---|
156 | pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc");
|
---|
157 | # The system one
|
---|
158 | pb_conf_add(cb_env_conffile());
|
---|
159 |
|
---|
160 | # Get configuration parameters
|
---|
161 | my %cb;
|
---|
162 | my $cb = \%cb;
|
---|
163 | ($cb->{'basedir'},$cb->{'usemachines'},$cb->{'cms'},$cb->{'database'}) = pb_conf_get("cbbasedir","cbusemachines","cbcms","cbdatabase");
|
---|
164 | pb_log(2,"%cb: ",Dumper($cb));
|
---|
165 |
|
---|
166 | # Check for mandatory params
|
---|
167 |
|
---|
168 | if (defined $plugin) {
|
---|
169 | # Load plugins
|
---|
170 | cb_plugin_load();
|
---|
171 | }
|
---|
172 |
|
---|
173 | my $basedir = $cb->{'basedir'}->{$appname};
|
---|
174 | eval { $basedir =~ s/(\$ENV.+\})/$1/eeg };
|
---|
175 |
|
---|
176 | pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\n");
|
---|
177 |
|
---|
178 | # Use potentially a remote account if defined
|
---|
179 | my $account = undef;
|
---|
180 | my $remote = $machine;
|
---|
181 | ($account) = pb_conf_get_if("cbaccount") if (defined $machine);
|
---|
182 | $remote = $account->{$machine}."@".$machine if ((defined $account) && (defined $account->{$machine}));
|
---|
183 | pb_log(1, "DEBUG: remote = $remote\n") if (defined $remote);
|
---|
184 |
|
---|
185 | # Create basedir if it doesn't exist
|
---|
186 | die "Unable to find base directory at $basedir" if (not -d $basedir);
|
---|
187 |
|
---|
188 | # Now handle plugins if any
|
---|
189 | my $cbp = ();
|
---|
190 |
|
---|
191 | if (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 |
|
---|
199 | my $db = "$cb->{'basedir'}/$cb->{'database'}";
|
---|
200 |
|
---|
201 | my $precmd = "";
|
---|
202 | if (! -f $db) {
|
---|
203 | $precmd = "CREATE TABLE dates (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, file VARCHAR[65535])";
|
---|
204 | }
|
---|
205 |
|
---|
206 | my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
|
---|
207 | { RaiseError => 1, AutoCommit => 1 })
|
---|
208 | || die "Unable to connect to $db";
|
---|
209 |
|
---|
210 | if ($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 |
|
---|
220 | my $dest = "$ENV{'TMPDIR'}/vcs.$$";
|
---|
221 | pb_vcs_export(,$cb->{'basedir'},$dest);
|
---|
222 |
|
---|
223 | # Is the source a file or a dir ? Split the source parameter in 2
|
---|
224 | my $srcdir = undef;
|
---|
225 | my $srcfile = undef;
|
---|
226 | my $cmd = undef;
|
---|
227 |
|
---|
228 | if (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 |
|
---|
249 | pb_log(1,"DEBUG: Found srcdir = $srcdir\n");
|
---|
250 | if (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
|
---|
257 | my $target = $basedir;
|
---|
258 | $target .= "/$machine" if defined ($machine);
|
---|
259 | $target .= "$srcdir";
|
---|
260 |
|
---|
261 | my $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
|
---|
267 | if ((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
|
---|
278 | my $cmdopt = "";
|
---|
279 |
|
---|
280 | # Recursive if we copy dirs
|
---|
281 | $cmdopt = "-r" if (not defined $srcfile);
|
---|
282 |
|
---|
283 | if (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
|
---|
290 | if (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 | }
|
---|
313 | pb_log(2,"Exiting cb_busterize\n");
|
---|
314 | }
|
---|