-
Notifications
You must be signed in to change notification settings - Fork 6
/
DatabaseDownloadTrait.php
202 lines (186 loc) · 6.53 KB
/
DatabaseDownloadTrait.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Usher\Robo\Plugin\Traits;
use AsyncAws\S3\S3Client;
use Robo\Exception\TaskException;
use Robo\Result;
/**
* Trait to provide database download functionality to Robo commands.
*/
trait DatabaseDownloadTrait
{
/**
* Default S3 region.
*
* @var string
*/
protected $s3DefaultRegion = 'us-east-1';
/**
* Download the latest database dump for the site.
*
* @param string $siteName
* The site name.
*
* @aliases dbdl
*
* @throws \Robo\Exception\TaskException
*/
public function databaseDownload(string $siteName = 'default'): string|\Robo\Result
{
$this->io()->title('database download.');
$awsConfigDirPath = getenv('HOME') . '/.aws';
$awsConfigFilePath = "$awsConfigDirPath/credentials";
if (!is_dir($awsConfigDirPath) || !file_exists($awsConfigFilePath)) {
$result = $this->configureAwsCredentials($awsConfigDirPath, $awsConfigFilePath);
if ($result->wasCancelled()) {
return Result::cancelled();
}
}
$s3 = new S3Client([
'region' => $this->s3RegionForSite($siteName),
]);
$objects = $s3->listObjectsV2($this->s3BucketRequestConfig($siteName));
$objects = iterator_to_array($objects);
if (count($objects) == 0) {
throw new TaskException($this, "No database dumps found for '$siteName'.");
}
// Ensure objects are sorted by last modified date.
usort(
array: $objects,
/** @var \AsyncAws\S3\ValueObject\AwsObject $a */
callback: fn($a, $b) => $a->getLastModified()->getTimestamp() <=> $b->getLastModified()->getTimestamp(),
);
/** @var \AsyncAws\S3\ValueObject\AwsObject $latestDatabaseDump */
$latestDatabaseDump = array_pop(array: $objects);
$dbFilename = $latestDatabaseDump->getKey();
$downloadFileName = $this->sanitizeFileNameForWindows($dbFilename);
if (file_exists($downloadFileName)) {
$this->say("Skipping download. Latest database dump file exists >>> $downloadFileName");
} else {
$result = $s3->getObject([
'Bucket' => $this->s3BucketForSite($siteName),
'Key' => $dbFilename,
]);
stream_copy_to_stream(
from: $result->getBody()->getContentAsResource(),
to: fopen($downloadFileName, 'wb'),
);
$this->say("Database dump file downloaded >>> $downloadFileName");
}
return $downloadFileName;
}
/**
* Configure AWS credentials.
*
* @param string $awsConfigDirPath
* Path to the AWS configuration directory.
* @param string $awsConfigFilePath
* Path to the AWS configuration file.
*/
protected function configureAwsCredentials(string $awsConfigDirPath, string $awsConfigFilePath): Result
{
$yes = $this->io()->confirm('AWS S3 credentials not detected. Do you wish to configure them?');
if (!$yes) {
return Result::cancelled();
}
if (!is_dir($awsConfigDirPath)) {
$this->_mkdir($awsConfigDirPath);
}
if (!file_exists($awsConfigFilePath)) {
$this->_touch($awsConfigFilePath);
}
$awsKeyId = $this->ask("AWS Access Key ID:");
$awsSecretKey = $this->askHidden("AWS Secret Access Key:");
return $this->taskWriteToFile($awsConfigFilePath)
->line('[default]')
->line("aws_access_key_id = $awsKeyId")
->line("aws_secret_access_key = $awsSecretKey")
->run();
}
/**
* Build S3 request configuration from sites config.
*
* @param string $siteName
* The site name.
*/
protected function s3BucketRequestConfig(string $siteName): array
{
$s3ConfigArray = ['Bucket' => $this->s3BucketForSite($siteName)];
try {
$s3KeyPrefix = $this->getSiteConfigItem('database_s3_key_prefix_string', $siteName);
$this->say("'$siteName' S3 Key prefix: '$s3KeyPrefix'");
$s3ConfigArray['Prefix'] = $s3KeyPrefix;
} catch (TaskException) {
$this->say("No S3 Key prefix found for $siteName.");
}
return $s3ConfigArray;
}
/**
* Get S3 Bucket for site.
*
* @param string $siteName
* The site name.
*
* @throws \Robo\Exception\TaskException
*/
protected function s3BucketForSite(string $siteName): string
{
if (!is_string($bucket = $this->getSiteConfigItem('database_s3_bucket', $siteName))) {
throw new TaskException($this, "database_s3_bucket value not set for '$siteName'.");
}
$this->say("'$siteName' S3 bucket: $bucket");
return $bucket;
}
/**
* Get S3 region for site.
*
* @param string $siteName
* The site name.
*/
protected function s3RegionForSite(string $siteName): string
{
try {
$region = $this->getSiteConfigItem('database_s3_region', $siteName);
$this->say("'$siteName' database_s3_region set to $region.");
} catch (TaskException) {
// Set default region if one is not set.
$defaultRegion = $this->s3DefaultRegion;
$this->say("'$siteName' database_s3_region not set. Defaulting to $defaultRegion.");
$region = $defaultRegion;
}
return $region;
}
/**
* Sanitizes a file name for the Windows file system.
*
* @param string $fileName
* The file name to sanitize.
*/
public function sanitizeFileNameForWindows(string $fileName): string
{
if (PHP_OS_FAMILY === 'Windows') {
$fileName = preg_replace(
'~
# File system reserved characters.
# @link https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
[<>:"/\\\|?*]|
# Control characters.
# @link https://docs.microsoft.com/en-gb/windows/win32/fileio/naming-a-file
[\x00-\x1F]|
# Non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN.
[\x7F\xA0\xAD]
~x',
'_',
$fileName
);
}
return $fileName;
}
/**
* Delete the specified database.
*/
protected function deleteDatabase(string $dbPath): Result
{
$this->say("Deleting $dbPath");
return $this->taskExec('rm')->args($dbPath)->run();
}
}