forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncViaHttpCommands.php
96 lines (89 loc) · 3.72 KB
/
SyncViaHttpCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Drush\Commands;
use Consolidation\AnnotatedCommand\CommandData;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Symfony\Component\Filesystem\Filesystem;
/**
* Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples`
*/
class SyncViaHttpCommands extends DrushCommands
{
/**
* When a hook extends a command with additional options, it must
* implement declare those option(s) in a @hook option like this one. Doing so will add
* the option to the help text for the modified command, and will also
* allow the new option to be specified on the command line. Without
* this, Drush will fail with an error when a user attempts to use
* an unknown option.
*
* @hook option sql-sync
* @option http-sync Copy the database via http instead of rsync. Value is the url that the existing database dump can be found at.
* @option http-sync-user Username for the protected directory containing the sql dump.
* @option http-sync-password Password for the same directory.
*/
public function optionsetSqlSync()
{
}
/**
* During the pre hook, determine if the http-sync option has been
* specified. If it has been, then disable the normal ssh + rsync
* dump-and-transfer that sql-sync usually does, and transfer the
* database dump via an http download.
*
* @hook pre-command sql-sync
*/
public function preSqlSync(CommandData $commandData)
{
$sql_dump_download_url = $commandData->input()->getOption('http-sync');
if (!empty($sql_dump_download_url)) {
$user = $commandData->input()->getOption('http-sync-user');
$password = $commandData->input()->getOption('http-sync-password');
$source_dump_file = $this->downloadFile($sql_dump_download_url, $user, $password);
$commandData->input()->setOption('target-dump', $source_dump_file);
$commandData->input()->setOption('no-dump', true);
$commandData->input()->setOption('no-sync', true);
}
}
/**
* Downloads a file.
*
* Optionally uses user authentication, using either wget or curl, as available.
*/
protected function downloadFile($url, $user = false, $password = false, $destination = false, $overwrite = true)
{
static $use_wget;
if ($use_wget === null) {
$use_wget = drush_shell_exec('which wget');
}
$destination_tmp = drush_tempnam('download_file');
if ($use_wget) {
if ($user && $password) {
drush_shell_exec("wget -q --timeout=30 --user=%s --password=%s -O %s %s", $user, $password, $destination_tmp, $url);
} else {
drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url);
}
} else {
if ($user && $password) {
drush_shell_exec("curl -s -L --connect-timeout 30 --user %s:%s -o %s %s", $user, $password, $destination_tmp, $url);
} else {
drush_shell_exec("curl -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url);
}
}
if (!Drush::simulate()) {
if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) {
@file_put_contents($destination_tmp, $file);
}
if (!drush_file_not_empty($destination_tmp)) {
// Download failed.
throw new \Exception(dt("The URL !url could not be downloaded.", ['!url' => $url]));
}
}
if ($destination) {
$fs = new Filesystem();
$fs->rename($destination_tmp, $destination, $overwrite);
return $destination;
}
return $destination_tmp;
}
}