2008-12-15 02:31:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This static publisher can be used to deploy static content to multiple hosts, by generating the cache files locally and then rsyncing then to
|
|
|
|
* each destination box. This can be used to set up a load-balanced collection of static servers.
|
2009-02-04 21:18:12 +01:00
|
|
|
*
|
|
|
|
* @see http://doc.silverstripe.com/doku.php?id=staticpublisher
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage publishers
|
2008-12-15 02:31:01 +01:00
|
|
|
*/
|
|
|
|
class RsyncMultiHostPublisher extends FilesystemPublisher {
|
|
|
|
/**
|
|
|
|
* Array of rsync targets to publish to. These can either be local file names, or scp-style targets, in the form "user@server:path"
|
|
|
|
*/
|
2010-10-15 03:38:31 +02:00
|
|
|
protected static $targets = array();
|
2008-12-15 02:31:01 +01:00
|
|
|
|
2010-10-15 03:38:38 +02:00
|
|
|
protected static $excluded_folders = array();
|
|
|
|
|
2008-12-15 02:31:01 +01:00
|
|
|
/**
|
|
|
|
* Set the targets to publish to.
|
2009-02-04 21:18:12 +01:00
|
|
|
* If target is an scp-style remote path, no password is accepted - we assume key-based authentication to be set up on the application server
|
|
|
|
* initiating the publication.
|
|
|
|
*
|
2008-12-15 02:31:01 +01:00
|
|
|
* @param $targets An array of targets to publish to. These can either be local file names, or scp-style targets, in the form "user@server:path"
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function set_targets($targets) {
|
2008-12-15 02:31:01 +01:00
|
|
|
self::$targets = $targets;
|
|
|
|
}
|
2010-10-15 03:38:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify folders to exclude from the rsync
|
|
|
|
* For example, you could exclude assets.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function set_excluded_folders($folders) {
|
2010-10-15 03:38:38 +02:00
|
|
|
self::$excluded_folders = $folders;
|
|
|
|
}
|
2008-12-15 02:31:01 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function publishPages($urls) {
|
2008-12-15 02:31:01 +01:00
|
|
|
parent::publishPages($urls);
|
|
|
|
$base = Director::baseFolder();
|
2012-04-12 10:36:25 +02:00
|
|
|
$framework = FRAMEWORK_DIR;
|
2008-12-15 02:31:01 +01:00
|
|
|
|
|
|
|
// Get variable that can turn off the rsync component of publication
|
|
|
|
if(isset($_GET['norsync']) && $_GET['norsync']) return;
|
|
|
|
|
2010-10-15 03:38:38 +02:00
|
|
|
$extraArg = "";
|
|
|
|
if(self::$excluded_folders) foreach(self::$excluded_folders as $folder) {
|
|
|
|
$extraArg .= " --exclude " . escapeshellarg($folder);
|
|
|
|
}
|
|
|
|
|
2008-12-15 02:31:01 +01:00
|
|
|
foreach(self::$targets as $target) {
|
|
|
|
// Transfer non-PHP content from everything to the target; that will ensure that we have all the JS/CSS/etc
|
2010-10-15 03:38:38 +02:00
|
|
|
$rsyncOutput = `cd $base; rsync -av -e ssh --exclude /.htaccess --exclude /web.config --exclude '*.php' --exclude '*.svn' --exclude '*.git' --exclude '*~' $extraArg --delete . $target`;
|
2008-12-15 02:31:01 +01:00
|
|
|
// Then transfer "safe" PHP from the cache/ directory
|
2010-10-15 03:38:38 +02:00
|
|
|
$rsyncOutput .= `cd $base; rsync -av -e ssh --exclude '*.svn' --exclude '*~' $extraArg --delete cache $target`;
|
2012-04-12 10:36:25 +02:00
|
|
|
// Transfer framework/static-main.php to the target
|
|
|
|
$rsyncOutput .= `cd $base; rsync -av -e ssh --delete $framework/static-main.php $target/$framework`;
|
2008-12-15 02:31:01 +01:00
|
|
|
if(StaticPublisher::echo_progress()) echo $rsyncOutput;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|