Skip to content

Commit

Permalink
v1.4.0 - Revision syncing implemented.
Browse files Browse the repository at this point in the history
Signed-off-by: Baki Goxhaj <banago@gmail.com>
  • Loading branch information
banago committed Mar 14, 2014
1 parent 22091fd commit e22cb8d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions deploy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ passive = true
[quickmode]
staging = ftp://example:password@staging-example.com:21/path/to/installation
production = ftp://example:password@production-example.com:21/path/to/installation

68 changes: 49 additions & 19 deletions phploy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Bruno De Barros <bruno@terraduo.com>
* @link http://wplancer.com
* @licence MIT Licence
* @version 1.3.5
* @version 1.4.0
*/

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ class PHPloy
/**
* @var array $longopts
*/
protected $longopts = array('list', 'rollback::', 'server:');
protected $longopts = array('list', 'rollback::', 'server:', 'sync::');

/**
* @var bool|resource $connection
Expand Down Expand Up @@ -92,6 +92,11 @@ class PHPloy
*/
protected $listFiles = false;

/**
* @var bool $listFiles
*/
protected $sync = false;

/**
* Constructor
*/
Expand Down Expand Up @@ -122,6 +127,10 @@ class PHPloy
$this->server = isset($options['s']) ? $options['s'] : $options['server'];
}

if (isset($options['sync'])) {
$this->sync = empty($options['sync']) ? 'sync' : $options['sync'];
}

if (isset($options['rollback'])) {
$this->revision = ($options['rollback'] == '') ? 'HEAD^' : $options['rollback'];
} else {
Expand Down Expand Up @@ -310,6 +319,12 @@ class PHPloy
if ($this->server != '' && $this->server != $name) continue;

$this->connect($server);

if( $this->sync ) {
$this->setRevision();
continue;
}

$files = $this->compare($revision);

if ($this->listFiles === true) {
Expand Down Expand Up @@ -384,7 +399,7 @@ class PHPloy

$pathsThatExist = array();

$connection = ftp_connect($server['host'], $server['port']);
$connection = @ftp_connect($server['host'], $server['port']);

if ($connection) {
if (! ftp_login($connection, $server['user'], $server['pass'])) {
Expand All @@ -393,7 +408,7 @@ class PHPloy

ftp_pasv($connection, $server['passive']);

if (ftp_chdir($connection, $server['path'])) {
if (@ftp_chdir($connection, $server['path'])) {
$this->connection = $connection;
$this->output("\r\n+ ---------- ☻ ---------- +");
} else {
Expand Down Expand Up @@ -435,7 +450,7 @@ class PHPloy
if (! isset($pathsThatExist[$path])) {
$origin = ftp_pwd($this->connection);

if (! ftp_chdir($this->connection, $path)) {
if (! @ftp_chdir($this->connection, $path)) {
if (! ftp_mkdir($this->connection, $path)) {
$ret = false;

Expand All @@ -451,7 +466,7 @@ class PHPloy
$pathsThatExist[$path] = true;
}

ftp_chdir($this->connection, $origin);
@ftp_chdir($this->connection, $origin);
}
}

Expand Down Expand Up @@ -494,19 +509,9 @@ class PHPloy
}
}

if (count($filesToUpload) > 0 or count($filesToDelete) > 0) {
$temp = tempnam(sys_get_temp_dir(), 'gitRevision');
$command = "git --git-dir='$this->repo/.git' --work-tree='$this->repo\' rev-parse HEAD";
exec(escapeshellcmd($command), $locRev);

file_put_contents($temp, $locRev);

if (ftp_put($this->connection, $this->dotRevision, $temp, FTP_BINARY)) {
unlink($temp);
$this->output("+ ---------- ✓ ---------- +\r\n");
} else {
throw new Exception("Oh Snap: Could not update the revision file on server.");
}
if (count($filesToUpload) > 0 or count($filesToDelete) > 0) {
// Set revision on server
$this->setRevision();
} else {
$this->output("No files to upload.");
}
Expand All @@ -520,6 +525,31 @@ class PHPloy
}
}

/**
* Sets version hash on the server.
*
*/
protected function setRevision()
{
if ( $this->sync == 'sync' or $this->sync == false ) {
$command = "git --git-dir='$this->repo/.git' --work-tree='$this->repo\' rev-parse HEAD";
exec(escapeshellcmd($command), $locRev);
} else {
$locRev = $this->sync;
}

$temp = tempnam(sys_get_temp_dir(), 'gitRevision');
file_put_contents($temp, $locRev);

if (ftp_put($this->connection, $this->dotRevision, $temp, FTP_BINARY)) {
unlink($temp);
if( $this->sync ) $this->output("⟲ revision synced");
$this->output("+ ---------- ✓ ---------- +\r\n");
} else {
throw new Exception("Oh Snap: Could not update the revision file on server.");
}
}

/**
* Helper method to display messages on the screen.
*
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ Or:

phploy --list

## Sync Revision

If you want to sync the `.revision` file on the server with the current revision you are currently on locally, run:

phploy --sync

If you want to set it to another previous commit revision, you just repecify the revision like this:

phploy --sync="your-revision-hash-here"

## How It Works

PHPloy stores a file called `.revision` on your server. This file contains the hash of the commit that you have deployed to that server. When you run phploy, it downloads that file and compares the commit reference in it with the commit you are trying to deploy to find out which files to upload.
Expand Down

0 comments on commit e22cb8d

Please sign in to comment.