Skip to content

Commit

Permalink
Adds ability to configure multiple servers
Browse files Browse the repository at this point in the history
You can now configure and use mutliple servers and deply to any of them by name.
Example below:
    phploy -s staging
Or:
    phploy --server staging

Hope it helps: fixes #1

Signed-off-by: Baki Goxhaj <[email protected]>
  • Loading branch information
banago committed Feb 11, 2014
1 parent 16966c2 commit 1e4ef5c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 68 deletions.
23 changes: 18 additions & 5 deletions deploy.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
; This is a sample deploy.ini file.
; You can specify as many servers as you need
; and use whichever configuration way you like.

[example]
[staging]

skip = false
user = example
pass = password
host = example.com
host = staging-example.com
path = /path/to/installation
port = 21
passive = true


[production]

user = example
pass = password
host = production-example.com
path = /path/to/installation
port = 21
passive = true

; If that seemed too long for you, you can specify servers like this:
[ftp://example:[email protected]:21/path/to/installation]
; If that seemed too long for you, you can use quickmode instead:
[quickmode]
staging = ftp://example:[email protected]:21/path/to/installation
production = ftp://example:[email protected]:21/path/to/installation
104 changes: 57 additions & 47 deletions phploy
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ class PHPloy {
public $revision;
public $files_to_ignore = array('.gitignore', '.gitmodules');


protected $connection = false;
protected $print = true;
protected $server = '';
protected $repo;
protected $main_repo;
protected $is_submodule = false;
Expand All @@ -55,6 +57,10 @@ class PHPloy {
$this->list_files = true;
}

if( isset( $options['s'] ) or isset( $options['server'] ) ) {
$this->server = isset( $options['s'] ) ? $options['s'] : $options['server'];
}

$this->revision = isset( $options['revision'] ) ? $options['revision'] : 'HEAD';
$this->repo = isset( $opts['repo'] ) ? rtrim( $opts['repo'], '/') : getcwd();

Expand Down Expand Up @@ -103,51 +109,51 @@ class PHPloy {
*/
function parseCredentials($deploy)
{
if( ! file_exists( $deploy ) ) {
$this->output("'$deploy' does not exist.");
exit;
} else {
$servers = parse_ini_file($deploy, true);
if( ! $servers ) {
$this->output("'$deploy' is not a valid .ini file.");
exit;
} else {
$this->checkServers($servers);
}
}
$this->ignore($deploy);
}
if( ! file_exists( $deploy ) ) {
$this->output("'$deploy' does not exist.");
exit;
} else {
$servers = parse_ini_file($deploy, true);
if( ! $servers ) {
$this->output("'$deploy' is not a valid .ini file.");
exit;
} else {
$this->ignore($deploy);
return $servers;
}
}
}

/**
* Add Servers
*/
function checkServers($servers)
{
foreach($servers as $uri => $options) {
function prepareServers()
{
$servers = $this->parseCredentials('deploy.ini');

foreach($servers as $name => $options) {

if(substr($uri, 0, 6) === 'ftp://') {
$options = array_merge($options, parse_url($uri));
if($name === 'quickmode') {
foreach($options as $env => $creds) {
$options = parse_url($creds);
$this->servers[$env] = $options;
}
break;
}

// Defaults
$options = array_merge(array(
'skip' => false,
'host' => '',
'user' => '',
'pass' => '',
'port' => 21,
'path' => '/',
'passive' => true,
'clean_directories' => array()
), $options);

if ($options['skip']) {
continue;
} else {
$this->servers[$uri] = $options;
}
}
}
$options = array_merge(array(
'host' => '',
'user' => '',
'pass' => '',
'port' => 21,
'path' => '/',
'passive' => true,
'clean_directories' => array()
), $options);

$this->servers[$name] = $options;
}
}

/**
* Ignore files
Expand Down Expand Up @@ -176,7 +182,7 @@ class PHPloy {
$remote_revision = trim(fread($tmp_file, 1024));
fclose($tmp_file);
} else {
$this->output('No revision file exists on the server.');
$this->output('Fresh deployment - grab a ☕');
}

if( ! empty( $remote_revision ) ) {
Expand Down Expand Up @@ -226,14 +232,19 @@ class PHPloy {
*/
function deploy($revision = 'HEAD')
{
$this->parseCredentials('deploy.ini');
foreach( $this->servers as $server )
{
$this->prepareServers();

foreach( $this->servers as $name => $server )
{
// Skip servers not specified, if specified.
if( $this->server != '' && $this->server != $name ) continue;

$this->connect( $server );
$files = $this->compare( $revision );
if( $this->list_files === true ) {
$this->listFiles($files);
} else {
$this->output("" . $name);
$this->push( $files );
}

Expand Down Expand Up @@ -280,14 +291,13 @@ class PHPloy {
*/
protected function connect( $server )
{
# Let's make sure the $path ends with a slash.
// Let's make sure the $path ends with a slash.
if(substr($server['path'], strlen($server['path']) - 1, strlen($server['path'])) !== '/') {
$server['path'] = $server['path'] . '/';
}
$paths_that_exist = array();

// Okay, let's connect to the server.

// Let's connect to the server.
$connection = @ftp_connect($server['host'], $server['port']);

if (!$connection) {
Expand All @@ -296,7 +306,7 @@ class PHPloy {
if (!@ftp_login($connection, $server['user'], $server['pass'])) {
throw new Exception("Could not login to {$server['host']} (Tried to login as {$server['user']}).");
}
ftp_pasv($connection, $server['passive']);
ftp_pasv($connection, true);
if(ftp_chdir($connection, $server['path'])) {
$this->connection = $connection;
$this->output("\r\n+ ---------- ☻ ---------- +");
Expand Down Expand Up @@ -403,4 +413,4 @@ class PHPloy {
}
}

} //PHPloy
} //PHPloy
58 changes: 42 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,51 @@ As any script, you can use PHPloy globally, from your `bin` directory or locally

## deploy.ini

The `deploy.ini` file hold your credentials and it must be in the root directory of your project.

; Deploy Settings

[Production]

skip = false
user = example
pass = password
host = example.com
port = 21
path = /path/to/installation
passive = true

; If that seemed too long for you, you can specify servers like this:
[ftp://example:[email protected]:21/path/to/installation]
The `deploy.ini` file hold your credentials and it must be in the root directory of your project. Use as many servers as you need and whichever configuration type you prefer.

; This is a sample deploy.ini file.
; You can specify as many servers as you need
; and use whichever configuration way you like.

[staging]

user = example
pass = password
host = staging-example.com
path = /path/to/installation
port = 21
passive = true

[production]

user = example
pass = password
host = production-example.com
path = /path/to/installation
port = 21
passive = true

; If that seemed too long for you, you can use quickmode instead:
[quickmode]
staging = ftp://example:[email protected]:21/path/to/installation
production = ftp://example:[email protected]:21/path/to/installation


The first time it's executed, PHPloy will assume that your deployment server is empty, and will upload all the files of your project.

## Specify Server

PHPloy allows you to configure multilple servers in the deploy file and deploy to any of them with ease. By default it will deploy to all specified servers.
To specify one server run:

phploy -s servername

Or:

phploy --server servername

`servername` stands for the name you have given to the server in the `deploy.ini` configuration file.

## View files

PHPloy allows you to check out what are going to be uploaded/deleted before you actually push them. Just run:
Expand Down

0 comments on commit 1e4ef5c

Please sign in to comment.