Skip to content

Commit c5a0bfa

Browse files
committed
Changes the way git-deploy works so that it only has 1 active connection at any time.
First it tests that it can connect to every provided server to check if all logins are correct and working. Then it starts deploying, one by one.
1 parent 4f2ff00 commit c5a0bfa

File tree

4 files changed

+87
-39
lines changed

4 files changed

+87
-39
lines changed

git-deploy

7.22 KB
Binary file not shown.

tools/src/Ftp.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
<?php
22

33
namespace Brunodebarros\Gitdeploy;
4+
45
use Brunodebarros\Gitdeploy\Helpers;
56

67
class Ftp extends Server {
78

8-
public function connect($server) {
9-
9+
public function connect($test = false) {
1010
if (!extension_loaded('ftp')) {
1111
Helpers::error("You need the FTP extension to be enabled if you want to deploy via FTP.");
1212
}
1313

14-
$this->connection = @ftp_connect($server['host'], $server['port'], 30);
14+
if (!$this->connection or $test) {
15+
$server = $this->server;
16+
$this->connection = @ftp_connect($server['host'], $server['port'], 30);
1517

16-
if (!$this->connection) {
17-
Helpers::error("Could not connect to {$this->host}");
18-
} else {
19-
if (!@ftp_login($this->connection, $server['user'], $server['pass'])) {
20-
Helpers::error("Could not login to {$this->host}");
21-
}
18+
if (!$this->connection) {
19+
Helpers::error("Could not connect to {$this->host}");
20+
} else {
21+
if (!@ftp_login($this->connection, $server['user'], $server['pass'])) {
22+
Helpers::error("Could not login to {$this->host}");
23+
}
2224

23-
ftp_pasv($this->connection, $server['passive']);
25+
ftp_pasv($this->connection, $server['passive']);
2426

25-
if (!ftp_chdir($this->connection, $server['path'])) {
26-
Helpers::error("Could not change the directory to {$server['path']} on {$this->host}");
27+
if (!ftp_chdir($this->connection, $server['path'])) {
28+
Helpers::error("Could not change the directory to {$server['path']} on {$this->host}");
29+
}
2730
}
31+
32+
Helpers::logmessage("Connected to: {$this->host}");
33+
$this->current_commit = $this->get_file('REVISION', true);
2834
}
2935

30-
Helpers::logmessage("Connected to: {$this->host}");
36+
if ($test) {
37+
$this->disconnect();
38+
}
39+
}
40+
41+
public function disconnect() {
42+
ftp_close($this->connection);
43+
$this->connection = null;
44+
Helpers::logmessage("Disconnected from: {$this->host}");
3145
}
3246

3347
public function get_file($file, $ignore_if_error = false) {
48+
$this->connect();
49+
3450
$tmpFile = tempnam(sys_get_temp_dir(), 'GITDEPLOYPHP');
3551

3652
if ($ignore_if_error) {
@@ -53,6 +69,8 @@ public function get_file($file, $ignore_if_error = false) {
5369
}
5470

5571
public function set_file($file, $contents, $die_if_fail = false) {
72+
$this->connect();
73+
5674
# Make sure the folder exists in the FTP server.
5775

5876
$dir = explode("/", dirname($file));
@@ -99,6 +117,8 @@ public function set_file($file, $contents, $die_if_fail = false) {
99117
}
100118

101119
protected function recursive_remove($file_or_directory, $die_if_fail = false) {
120+
$this->connect();
121+
102122
if (!(@ftp_rmdir($this->connection, $file_or_directory) || @ftp_delete($this->connection, $file_or_directory))) {
103123

104124
if ($die_if_fail) {
@@ -118,11 +138,15 @@ protected function recursive_remove($file_or_directory, $die_if_fail = false) {
118138
}
119139

120140
public function mkdir($file) {
141+
$this->connect();
142+
121143
ftp_mkdir($this->connection, $file);
122144
Helpers::logmessage("Created directory: $file");
123145
}
124146

125147
public function unset_file($file) {
148+
$this->connect();
149+
126150
$this->recursive_remove($file);
127151
Helpers::logmessage("Deleted: $file");
128152
}

tools/src/Server.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ public function __construct($server, $deploy_script = 'deploy.ini') {
2424
$this->ignore_directories = $server['ignore_directories'];
2525
$this->upload_untracked = $server['upload_untracked'];
2626
$this->host = "{$server['scheme']}://{$server['user']}@{$server['host']}:{$server['port']}{$server['path']}";
27-
$this->connect($server);
28-
$this->current_commit = $this->get_file('REVISION', true);
27+
$this->connect(true);
2928
}
3029

3130
public function deploy(Git $git, $target_commit, $is_revert = false, $list_only = false) {
32-
3331
if ($target_commit == $this->current_commit) {
3432
Helpers::logmessage("Nothing to update on: $this->host");
3533
return;
@@ -171,6 +169,7 @@ protected function set_current_commit($target_commit, $list_only = false) {
171169
}
172170

173171
Helpers::logmessage("Finished working on: {$this->host}");
172+
$this->disconnect();
174173
}
175174

176175
}

tools/src/Sftp.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,61 @@
11
<?php
22

33
namespace Brunodebarros\Gitdeploy;
4+
45
use Brunodebarros\Gitdeploy\Helpers;
56

67
class Sftp extends Server {
78

8-
public function connect($server) {
9-
include('Crypt/RSA.php');
10-
include('Net/SFTP.php');
11-
12-
$this->connection = new \Net_SFTP($server['host'], $server['port'], 10);
13-
$logged_in = false;
14-
15-
if (isset($server['sftp_key'])) {
16-
$key = new \Crypt_RSA();
17-
if (isset($server['pass']) and ! empty($server['pass'])) {
18-
$key->setPassword($server['pass']);
19-
}
20-
$key->loadKey(file_get_contents($server['sftp_key']));
21-
$logged_in = $this->connection->login($server['user'], $key);
9+
public function connect($test = false) {
10+
if (!$this->connection or $test) {
11+
$server = $this->server;
12+
require_once('Crypt/RSA.php');
13+
require_once('Net/SFTP.php');
14+
15+
$this->connection = new \Net_SFTP($server['host'], $server['port'], 10);
16+
$logged_in = false;
17+
18+
if (isset($server['sftp_key'])) {
19+
$key = new \Crypt_RSA();
20+
if (isset($server['pass']) and ! empty($server['pass'])) {
21+
$key->setPassword($server['pass']);
22+
}
23+
$key->loadKey(file_get_contents($server['sftp_key']));
24+
$logged_in = $this->connection->login($server['user'], $key);
2225

23-
if (!$logged_in) {
24-
Helpers::error("Could not login to {$this->host}. It may be because the key requires a passphrase, which you need to specify it as the 'pass' attribute.");
26+
if (!$logged_in) {
27+
Helpers::error("Could not login to {$this->host}. It may be because the key requires a passphrase, which you need to specify it as the 'pass' attribute.");
28+
}
29+
} else {
30+
$logged_in = $this->connection->login($server['user'], $server['pass']);
31+
32+
if (!$logged_in) {
33+
Helpers::error("Could not login to {$this->host}");
34+
}
2535
}
26-
} else {
27-
$logged_in = $this->connection->login($server['user'], $server['pass']);
2836

29-
if (!$logged_in) {
30-
Helpers::error("Could not login to {$this->host}");
37+
if (!$this->connection->chdir($server['path'])) {
38+
Helpers::error("Could not change the directory to {$server['path']} on {$this->host}");
3139
}
40+
41+
Helpers::logmessage("Connected to: {$this->host}");
42+
$this->current_commit = $this->get_file('REVISION', true);
3243
}
3344

34-
if (!$this->connection->chdir($server['path'])) {
35-
Helpers::error("Could not change the directory to {$server['path']} on {$this->host}");
45+
if ($test) {
46+
$this->disconnect();
3647
}
48+
}
3749

38-
Helpers::logmessage("Connected to: {$this->host}");
50+
public function disconnect() {
51+
$this->connection->disconnect();
52+
$this->connection = null;
53+
Helpers::logmessage("Disconnected from: {$this->host}");
3954
}
4055

4156
public function get_file($file, $ignore_if_error = false) {
57+
$this->connect();
58+
4259
$contents = $this->connection->get($file);
4360
if ($contents) {
4461
return $contents;
@@ -53,6 +70,8 @@ public function get_file($file, $ignore_if_error = false) {
5370
}
5471

5572
public function set_file($file, $contents) {
73+
$this->connect();
74+
5675
$file = $file;
5776
$dir = explode("/", dirname($file));
5877
$path = "";
@@ -87,6 +106,8 @@ public function set_file($file, $contents) {
87106
}
88107

89108
protected function recursive_remove($file_or_directory, $if_dir = false) {
109+
$this->connect();
110+
90111
$parent = dirname($file_or_directory);
91112
if ($this->connection->delete($file_or_directory, $if_dir)) {
92113
$filelist = $this->connection->nlist($parent);
@@ -101,11 +122,15 @@ protected function recursive_remove($file_or_directory, $if_dir = false) {
101122
}
102123

103124
public function mkdir($file) {
125+
$this->connect();
126+
104127
$this->connection->mkdir($file);
105128
Helpers::logmessage("Created directory: $file");
106129
}
107130

108131
public function unset_file($file) {
132+
$this->connect();
133+
109134
$this->recursive_remove($file, false);
110135
Helpers::logmessage("Deleted: $file");
111136
}

0 commit comments

Comments
 (0)