| 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 [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 CasparBuster::Version; |
|---|
| 103 | use CasparBuster::Env; |
|---|
| 104 | #use Cwd 'realpath'; |
|---|
| 105 | use File::Find; |
|---|
| 106 | use File::Copy; |
|---|
| 107 | use File::Basename; |
|---|
| 108 | use File::Path; |
|---|
| 109 | use File::Glob ':glob'; |
|---|
| 110 | use Getopt::Long; |
|---|
| 111 | use Pod::Usage; |
|---|
| 112 | use Data::Dumper; |
|---|
| 113 | use List::Util qw(first); |
|---|
| 114 | use ProjectBuilder::Base; |
|---|
| 115 | use ProjectBuilder::Conf; |
|---|
| 116 | use ProjectBuilder::VCS; |
|---|
| 117 | |
|---|
| 118 | # settings |
|---|
| 119 | my $debug = 0; |
|---|
| 120 | my $help = undef; |
|---|
| 121 | my $man = undef; |
|---|
| 122 | my $source = undef; |
|---|
| 123 | my $machine = undef; |
|---|
| 124 | my $plugin = undef; |
|---|
| 125 | my $quiet = undef; |
|---|
| 126 | my $log = undef; |
|---|
| 127 | my $LOG = undef; |
|---|
| 128 | |
|---|
| 129 | my ($cbver,$cbrev) = cb_version_init(); |
|---|
| 130 | my $appname = "cb"; |
|---|
| 131 | $ENV{'PBPROJ'} = $appname; |
|---|
| 132 | pb_temp_init(); |
|---|
| 133 | |
|---|
| 134 | # Initialize the syntax string |
|---|
| 135 | pb_syntax_init("$appname (aka CasparBuster) Version $cbver-$cbrev\n"); |
|---|
| 136 | |
|---|
| 137 | # parse command-line options |
|---|
| 138 | GetOptions( |
|---|
| 139 | 'machine|m=s' => \$machine, |
|---|
| 140 | 'debug|d+' => \$debug, |
|---|
| 141 | 'help|h' => \$help, |
|---|
| 142 | 'quiet|q' => \$quiet, |
|---|
| 143 | 'man' => \$man, |
|---|
| 144 | 'log-files|l=s' => \$log, |
|---|
| 145 | 'source|s=s' => \$source, |
|---|
| 146 | 'plugin|p=s' => \$plugin, |
|---|
| 147 | ) || pb_syntax(-1,0); |
|---|
| 148 | |
|---|
| 149 | if (defined $help) { |
|---|
| 150 | pb_syntax(0,1); |
|---|
| 151 | } |
|---|
| 152 | if (defined $man) { |
|---|
| 153 | pb_syntax(0,2); |
|---|
| 154 | } |
|---|
| 155 | if (defined $quiet) { |
|---|
| 156 | $debug=-1; |
|---|
| 157 | } |
|---|
| 158 | if (defined $log) { |
|---|
| 159 | open(LOG,"> $log") || die "Unable to log to $log: $!"; |
|---|
| 160 | $LOG = \*LOG; |
|---|
| 161 | $debug = 0 if ($debug == -1); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | $pbdebug = $debug; |
|---|
| 165 | pb_log_init($debug, $LOG); |
|---|
| 166 | pb_log(0,"Starting cbusterize\n"); |
|---|
| 167 | |
|---|
| 168 | # Get conf file in context |
|---|
| 169 | pb_conf_init($appname); |
|---|
| 170 | # The personal one if there is such |
|---|
| 171 | pb_conf_add("$ENV{'HOME'}/.cbrc") if (-f "$ENV{'HOME'}/.cbrc"); |
|---|
| 172 | # The system one |
|---|
| 173 | pb_conf_add(cb_env_conffile()); |
|---|
| 174 | |
|---|
| 175 | # Get configuration parameters |
|---|
| 176 | my %cb; |
|---|
| 177 | my $cb = \%cb; |
|---|
| 178 | ($cb->{'basedir'},$cb->{'database'},$cb->{'usemachines'},$cb->{'pluginsdir'},$cb->{'cms'}) = pb_conf_get("cbbasedir","cbdatabase","cbusemachines","cbpluginssubdir","cbcms"); |
|---|
| 179 | pb_log(2,"%cb: ",Dumper($cb)); |
|---|
| 180 | |
|---|
| 181 | # Check for mandatory params |
|---|
| 182 | pod2usage("Error: --source or --plugin is a mandatory argument\n") if ((not defined $source) && (not defined $plugin)); |
|---|
| 183 | pod2usage("Error: --machine is a mandatory argument when configure with cbusemachines = true\n") if (($cb->{'usemachines'}->{$appname} =~ /true/) && (not defined $machine)); |
|---|
| 184 | |
|---|
| 185 | if (defined $plugin) { |
|---|
| 186 | # Load plugin conf |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | my $basedir = $cb->{'basedir'}->{$appname}; |
|---|
| 190 | eval { $basedir =~ s/(\$ENV.+\})/$1/eeg }; |
|---|
| 191 | |
|---|
| 192 | pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\nDEBUG: source = $source\n"); |
|---|
| 193 | pb_log(1, "DEBUG: machine = $machine\n") if (defined ($machine)); |
|---|
| 194 | |
|---|
| 195 | # Create basedir if it doesn't exist |
|---|
| 196 | if (not -d $basedir) { |
|---|
| 197 | if ($debug) { |
|---|
| 198 | pb_log(1, "DEBUG: Creating recursively directory $basedir\n"); |
|---|
| 199 | } else { |
|---|
| 200 | pb_mkdir_p($basedir) || die "Unable to recursively create $basedir"; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | # Is the source a file or a dir ? Split the source parameter in 2 |
|---|
| 205 | my $srcdir = undef; |
|---|
| 206 | my $srcfile = undef; |
|---|
| 207 | # TODO: That should be remote !! |
|---|
| 208 | if (-d $source) { |
|---|
| 209 | $srcdir = $source; |
|---|
| 210 | } else { |
|---|
| 211 | $srcdir = dirname($source); |
|---|
| 212 | $srcfile = basename($source); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | pb_log(1,"DEBUG: Found srcdir = $srcdir\n"); |
|---|
| 216 | if (defined $srcfile) { |
|---|
| 217 | pb_log(1,"DEBUG: Found srcfile = $srcfile\n"); |
|---|
| 218 | } else { |
|---|
| 219 | pb_log(1,"DEBUG: Found no srcfile\n"); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | # Deduce the target directory from the local structure and the source |
|---|
| 223 | my $target = $basedir; |
|---|
| 224 | $target .= "/$machine" if defined ($machine); |
|---|
| 225 | $target .= "$srcdir"; |
|---|
| 226 | |
|---|
| 227 | my $scheme = $cb->{'cms'}->{$appname}; |
|---|
| 228 | |
|---|
| 229 | # If both source and target are dirs, then copy into the parent of the target |
|---|
| 230 | $target = basename($target) if ((not defined $srcfile) && (-d $target)); |
|---|
| 231 | |
|---|
| 232 | # Create target if it doesn't exist |
|---|
| 233 | if (not -d $target) { |
|---|
| 234 | if ($debug) { |
|---|
| 235 | pb_log(1,"DEBUG: Creating recursively directory $target\n"); |
|---|
| 236 | } else { |
|---|
| 237 | pb_mkdir_p($target) || die "Unable to recursively create $target"; |
|---|
| 238 | pb_vcs_add($scheme,$target); |
|---|
| 239 | pb_log(0,"INFO: Created $target and added it to your $scheme system\n"); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | # We need to know where to get the content from |
|---|
| 244 | my $cmd; |
|---|
| 245 | my $cmdopt = ""; |
|---|
| 246 | |
|---|
| 247 | # Recursive if we copy dirs |
|---|
| 248 | $cmdopt = "-r" if (not defined $srcfile); |
|---|
| 249 | |
|---|
| 250 | if (defined $machine) { |
|---|
| 251 | $cmd = "scp -p -q $cmdopt $machine:$source $target"; |
|---|
| 252 | } else { |
|---|
| 253 | $cmd = "cp -p $cmdopt $source $target"; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | # Now add content if not already there |
|---|
| 257 | if (defined $srcfile) { |
|---|
| 258 | # File case |
|---|
| 259 | if (! -f "$target/$srcfile") { |
|---|
| 260 | if ($debug) { |
|---|
| 261 | pb_log(1,"DEBUG: launching $cmd\n"); |
|---|
| 262 | } else { |
|---|
| 263 | pb_system($cmd); |
|---|
| 264 | pb_vcs_add($scheme,"$target/$srcfile"); |
|---|
| 265 | pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system\n"); |
|---|
| 266 | } |
|---|
| 267 | } else { |
|---|
| 268 | pb_log(0,"INFO: File $target/$srcfile already there\n"); |
|---|
| 269 | } |
|---|
| 270 | } else { |
|---|
| 271 | # Directory case |
|---|
| 272 | # TODO: if targetlocal dir alredy exists, take the parent |
|---|
| 273 | if ($debug) { |
|---|
| 274 | pb_log(1,"DEBUG: launching $cmd\n"); |
|---|
| 275 | } else { |
|---|
| 276 | pb_system($cmd); |
|---|
| 277 | pb_vcs_add($scheme,"$target"); |
|---|
| 278 | pb_log(0,"INFO: Created $target and added it to your $scheme system\n"); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|