silverstripe-staticpublisher/code/extensions/RsyncMultiHostPublisher.php

86 lines
2.9 KiB
PHP
Raw Normal View History

2012-09-21 07:47:26 +02:00
<?php
/**
2013-04-06 06:24:51 +02:00
* This static publisher can be used to deploy static content to multiple
2012-09-25 10:02:26 +02:00
* hosts, by generating the cache files locally and then rsyncing then to
2013-04-06 06:24:51 +02:00
* each destination box. This can be used to set up a load-balanced
2012-09-25 10:02:26 +02:00
* collection of static servers.
2012-09-21 07:47:26 +02:00
*
2012-09-25 10:02:26 +02:00
* @package staticpublisher
2012-09-21 07:47:26 +02:00
*/
class RsyncMultiHostPublisher extends FilesystemPublisher {
2012-09-25 10:02:26 +02:00
2012-09-21 07:47:26 +02:00
/**
2013-04-06 06:24:51 +02:00
* @config
*
* Array of rsync targets to publish to. These can either be local
2012-09-25 10:02:26 +02:00
* file names, or scp-style targets, in the form "user@server:path"
*
* @var array
2012-09-21 07:47:26 +02:00
*/
2013-04-06 06:24:51 +02:00
private static $targets = array();
2012-09-21 07:47:26 +02:00
2013-04-06 06:24:51 +02:00
/**
* @config
*
* @var array
*/
private static $excluded_folders = array();
2012-09-21 07:47:26 +02:00
/**
* Set the targets to publish to.
2012-09-25 10:02:26 +02:00
*
2013-04-06 06:24:51 +02:00
* If target is an scp-style remote path, no password is accepted - we
2012-09-25 10:02:26 +02:00
* assume key-based authentication to be set up on the application server
2012-09-21 07:47:26 +02:00
* initiating the publication.
2013-04-06 06:24:51 +02:00
*
* @deprecated 3.2 Use the "RsyncMultiHostPublisher.targets" config setting instead
*
* @param $targets An array of targets to publish to.
2012-09-21 07:47:26 +02:00
*/
2012-09-25 10:02:26 +02:00
public static function set_targets($targets) {
2013-04-06 06:24:51 +02:00
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.targets" config setting instead');
Config::inst()->update('RsyncMultiHostPublisher', 'targets', $targets);
2012-09-21 07:47:26 +02:00
}
/**
2013-04-06 06:24:51 +02:00
* Specify folders to exclude from the rsync
* For example, you could exclude assets.
2012-09-25 10:02:26 +02:00
*
2013-04-06 06:24:51 +02:00
* @deprecated 3.2 Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead
2012-09-21 07:47:26 +02:00
*/
2012-09-25 10:02:26 +02:00
public static function set_excluded_folders($folders) {
2013-04-06 06:24:51 +02:00
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead');
Config::inst()->update('RsyncMultiHostPublisher', 'excluded_folders', $folders);
2012-09-21 07:47:26 +02:00
}
2012-09-25 10:02:26 +02:00
public function publishPages($urls) {
2012-09-21 07:47:26 +02:00
parent::publishPages($urls);
$base = Director::baseFolder();
$framework = FRAMEWORK_DIR;
// Get variable that can turn off the rsync component of publication
if(isset($_GET['norsync']) && $_GET['norsync']) return;
$extraArg = "";
2013-04-06 06:24:51 +02:00
if($this->config()->excluded_folders) foreach($this->config()->excluded_folders as $folder) {
2012-09-21 07:47:26 +02:00
$extraArg .= " --exclude " . escapeshellarg($folder);
}
2013-04-06 06:24:51 +02:00
foreach((array)$this->config()->targets as $target) {
2012-09-21 07:47:26 +02:00
// Transfer non-PHP content from everything to the target; that will ensure that we have all the JS/CSS/etc
$rsyncOutput = `cd $base; rsync -av -e ssh --exclude /.htaccess --exclude /web.config --exclude '*.php' --exclude '*.svn' --exclude '*.git' --exclude '*~' $extraArg --delete . $target`;
// Then transfer "safe" PHP from the cache/ directory
$rsyncOutput .= `cd $base; rsync -av -e ssh --exclude '*.svn' --exclude '*~' $extraArg --delete cache $target`;
// Transfer framework/static-main.php to the target
$rsyncOutput .= `cd $base; rsync -av -e ssh --delete $framework/static-main.php $target/$framework`;
2013-04-06 06:24:51 +02:00
if(Config::inst()->get('StaticPublisher', 'echo_progress')) {
echo $rsyncOutput;
}
2012-09-21 07:47:26 +02:00
}
}
}