1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SSPak handler |
5
|
|
|
*/ |
6
|
|
|
class SSPak { |
7
|
|
|
protected $executor; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Create a new handler |
11
|
|
|
* @param Executor $executor The Executor object to handle command execution |
12
|
|
|
*/ |
13
|
|
|
function __construct($executor) { |
|
|
|
|
14
|
|
|
$this->executor = $executor; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function getActions() { |
|
|
|
|
18
|
|
|
return array( |
19
|
|
|
"help" => array( |
20
|
|
|
"description" => "Show this help message.", |
21
|
|
|
"method" => "help", |
22
|
|
|
), |
23
|
|
|
"save" => array( |
24
|
|
|
"description" => "Save an .sspak file from a SilverStripe site.", |
25
|
|
|
"unnamedArgs" => array("webroot", "sspak file"), |
26
|
|
|
"namedArgs" => array("identity"), |
27
|
|
|
"method" => "save", |
28
|
|
|
), |
29
|
|
|
"load" => array( |
30
|
|
|
"description" => "Load an .sspak file into a SilverStripe site. Does not backup - be careful!", |
31
|
|
|
"unnamedArgs" => array("sspak file", "[webroot]"), |
32
|
|
|
"namedArgs" => array("identity"), |
33
|
|
|
"namedFlags" => array("drop-db"), |
34
|
|
|
"method" => "load", |
35
|
|
|
), |
36
|
|
|
"saveexisting" => array( |
37
|
|
|
"description" => "Create an .sspak file from database SQL dump and/or assets. Does not require a SilverStripe site.", |
38
|
|
|
"unnamedArgs" => array("sspak file"), |
39
|
|
|
"namedArgs" => array("db", "assets"), |
40
|
|
|
"method" => "saveexisting" |
41
|
|
|
), |
42
|
|
|
"extract" => array( |
43
|
|
|
"description" => "Extract an .sspak file into the current working directory. Does not require a SilverStripe site.", |
44
|
|
|
"unnamedArgs" => array("sspak file", "destination path"), |
45
|
|
|
"method" => "extract" |
46
|
|
|
), |
47
|
|
|
/* |
|
|
|
|
48
|
|
|
"install" => array( |
49
|
|
|
"description" => "Install a .sspak file into a new environment.", |
50
|
|
|
"unnamedArgs" => array("sspak file", "new webroot"), |
51
|
|
|
"method" => "install", |
52
|
|
|
), |
53
|
|
|
"bundle" => array( |
54
|
|
|
"description" => "Bundle a .sspak file into a self-extracting executable .sspak.phar installer.", |
55
|
|
|
"unnamedArgs" => array("sspak file", "executable"), |
56
|
|
|
"method" => "bundle", |
57
|
|
|
), |
58
|
|
|
"transfer" => array( |
59
|
|
|
"description" => "Transfer db & assets from one site to another (not implemented yet).", |
60
|
|
|
"unnamedArgs" => array("src webroot", "dest webroot"), |
61
|
|
|
"method" => "transfer", |
62
|
|
|
), |
63
|
|
|
*/ |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function help($args) { |
|
|
|
|
68
|
|
|
echo "SSPak: manage SilverStripe .sspak archives.\n\nUsage:\n"; |
69
|
|
|
foreach($this->getActions() as $action => $info) { |
70
|
|
|
echo "sspak $action"; |
71
|
|
|
if(!empty($info['unnamedArgs'])) { |
72
|
|
|
foreach($info['unnamedArgs'] as $arg) echo " ($arg)"; |
73
|
|
|
} |
74
|
|
|
if(!empty($info['namedFlags'])) { |
75
|
|
|
foreach($info['namedFlags'] as $arg) echo " (--$arg)"; |
76
|
|
|
} |
77
|
|
|
if(!empty($info['namedArgs'])) { |
78
|
|
|
foreach($info['namedArgs'] as $arg) echo " --$arg=\"$arg value\""; |
79
|
|
|
} |
80
|
|
|
echo "\n {$info['description']}\n\n"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Save an existing database and/or assets into an .sspak.phar file. |
86
|
|
|
* Does the same as {@link save()} but doesn't require an existing site. |
87
|
|
|
*/ |
88
|
|
|
function saveexisting($args) { |
|
|
|
|
89
|
|
|
$executor = $this->executor; |
90
|
|
|
|
91
|
|
|
$args->requireUnnamed(array('sspak file')); |
92
|
|
|
$unnamedArgs = $args->getUnnamedArgs(); |
93
|
|
|
$namedArgs = $args->getNamedArgs(); |
94
|
|
|
|
95
|
|
|
$sspak = new SSPakFile($unnamedArgs[0], $executor); |
96
|
|
|
|
97
|
|
|
// Look up which parts of the sspak are going to be saved |
98
|
|
|
$pakParts = $args->pakParts(); |
99
|
|
|
|
100
|
|
|
$filesystem = new FilesystemEntity(null, $executor); |
101
|
|
|
|
102
|
|
|
if($pakParts['db']) { |
103
|
|
|
$dbPath = escapeshellarg($namedArgs['db']); |
104
|
|
|
$process = $filesystem->createProcess("cat $dbPath | gzip -c"); |
105
|
|
|
$sspak->writeFileFromProcess('database.sql.gz', $process); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if($pakParts['assets']) { |
109
|
|
|
$assetsParentArg = escapeshellarg(dirname($namedArgs['assets'])); |
110
|
|
|
$assetsBaseArg = escapeshellarg(basename($namedArgs['assets'])); |
111
|
|
|
$process = $filesystem->createProcess("cd $assetsParentArg && tar cfh - $assetsBaseArg | gzip -c"); |
112
|
|
|
$sspak->writeFileFromProcess('assets.tar.gz', $process); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Extracts an existing database and/or assets from a sspak into the given directory, |
118
|
|
|
* defaulting the current working directory if the destination is not given. |
119
|
|
|
*/ |
120
|
|
|
function extract($args) { |
|
|
|
|
121
|
|
|
$executor = $this->executor; |
122
|
|
|
|
123
|
|
|
$args->requireUnnamed(array('source sspak file')); |
124
|
|
|
$unnamedArgs = $args->getUnnamedArgs(); |
125
|
|
|
$file = $unnamedArgs[0]; |
126
|
|
|
$dest = !empty($unnamedArgs[1]) ? $unnamedArgs[1] : getcwd(); |
127
|
|
|
|
128
|
|
|
$sspak = new SSPakFile($file, $executor); |
129
|
|
|
|
130
|
|
|
// Validation |
131
|
|
|
if(!$sspak->exists()) throw new Exception("File '$file' doesn't exist."); |
132
|
|
|
|
133
|
|
|
$phar = $sspak->getPhar(); |
134
|
|
|
$phar->extractTo($dest); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Save a .sspak.phar file |
139
|
|
|
*/ |
140
|
|
|
function save($args) { |
|
|
|
|
141
|
|
|
$executor = $this->executor; |
142
|
|
|
|
143
|
|
|
$args->requireUnnamed(array('source webroot', 'dest sspak file')); |
144
|
|
|
|
145
|
|
|
$unnamedArgs = $args->getUnnamedArgs(); |
146
|
|
|
$namedArgs = $args->getNamedArgs(); |
147
|
|
|
|
148
|
|
|
$webroot = new Webroot($unnamedArgs[0], $executor); |
149
|
|
|
$file = $unnamedArgs[1]; |
150
|
|
|
if(file_exists($file)) throw new Exception( "File '$file' already exists."); |
151
|
|
|
|
152
|
|
|
$sspak = new SSPakFile($file, $executor); |
153
|
|
|
|
154
|
|
|
if(!empty($namedArgs['identity'])) { |
155
|
|
|
// SSH private key |
156
|
|
|
$webroot->setSSHItentityFile($namedArgs['identity']); |
157
|
|
|
} |
158
|
|
|
if(!empty($namedArgs['from-sudo'])) $webroot->setSudo($namedArgs['from-sudo']); |
159
|
|
|
else if(!empty($namedArgs['sudo'])) $webroot->setSudo($namedArgs['sudo']); |
160
|
|
|
|
161
|
|
|
// Look up which parts of the sspak are going to be saved |
162
|
|
|
$pakParts = $args->pakParts(); |
163
|
|
|
|
164
|
|
|
// Get the environment details |
165
|
|
|
$details = $webroot->sniff(); |
166
|
|
|
|
167
|
|
|
// Create a build folder for the sspak file |
168
|
|
|
$buildFolder = "/tmp/sspak-" . rand(100000,999999); |
169
|
|
|
$webroot->exec(array('mkdir', $buildFolder)); |
|
|
|
|
170
|
|
|
|
171
|
|
|
$dbFile = "$buildFolder/database.sql.gz"; |
172
|
|
|
$assetsFile = "$buildFolder/assets.tar.gz"; |
173
|
|
|
$gitRemoteFile = "$buildFolder/git-remote"; |
174
|
|
|
|
175
|
|
|
// Files to include in the .sspak.phar file |
176
|
|
|
$fileList = array(); |
|
|
|
|
177
|
|
|
|
178
|
|
|
// Save DB |
179
|
|
|
if($pakParts['db']) { |
180
|
|
|
// Check the database type |
181
|
|
|
$dbFunction = 'getdb_'.$details['db_type']; |
182
|
|
View Code Duplication |
if(!method_exists($this,$dbFunction)) { |
|
|
|
|
183
|
|
|
throw new Exception("Can't process database type '" . $details['db_type'] . "'"); |
184
|
|
|
} |
185
|
|
|
$this->$dbFunction($webroot, $details, $sspak, basename($dbFile)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Save Assets |
189
|
|
|
if($pakParts['assets']) { |
190
|
|
|
$this->getassets($webroot, $details['assets_path'], $sspak, basename($assetsFile)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Save git-remote |
194
|
|
|
if($pakParts['git-remote']) { |
195
|
|
|
$this->getgitremote($webroot, $sspak, basename($gitRemoteFile)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Remove the build folder |
199
|
|
|
$webroot->unlink($buildFolder); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
function getdb_MySQLPDODatabase($webroot, $conf, $sspak, $filename) { |
|
|
|
|
203
|
|
|
return $this->getdb_MySQLDatabase($webroot, $conf, $sspak, $filename); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
function getdb_MySQLDatabase($webroot, $conf, $sspak, $filename) { |
|
|
|
|
207
|
|
|
$usernameArg = escapeshellarg("--user=".$conf['db_username']); |
208
|
|
|
$passwordArg = escapeshellarg("--password=".$conf['db_password']); |
209
|
|
|
$databaseArg = escapeshellarg($conf['db_database']); |
210
|
|
|
|
211
|
|
|
$hostArg = ''; |
212
|
|
|
$portArg = ''; |
213
|
|
View Code Duplication |
if (!empty($conf['db_server']) && $conf['db_server'] != 'localhost') { |
|
|
|
|
214
|
|
|
if (strpos($conf['db_server'], ':')!==false) { |
215
|
|
|
// Handle "server:port" format. |
216
|
|
|
$server = explode(':', $conf['db_server'], 2); |
217
|
|
|
$hostArg = escapeshellarg("--host=".$server[0]); |
218
|
|
|
$portArg = escapeshellarg("--port=".$server[1]); |
219
|
|
|
} else { |
220
|
|
|
$hostArg = escapeshellarg("--host=".$conf['db_server']); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$filenameArg = escapeshellarg($filename); |
|
|
|
|
225
|
|
|
|
226
|
|
|
$process = $webroot->createProcess("mysqldump --skip-opt --add-drop-table --extended-insert --create-options --quick --set-charset --default-character-set=utf8 $usernameArg $passwordArg $hostArg $portArg $databaseArg | gzip -c"); |
227
|
|
|
$sspak->writeFileFromProcess($filename, $process); |
228
|
|
|
return true; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
function getdb_PostgreSQLDatabase($webroot, $conf, $sspak, $filename) { |
|
|
|
|
232
|
|
|
$usernameArg = escapeshellarg("--username=".$conf['db_username']); |
233
|
|
|
$passwordArg = "PGPASSWORD=".escapeshellarg($conf['db_password']); |
234
|
|
|
$databaseArg = escapeshellarg($conf['db_database']); |
235
|
|
|
$hostArg = escapeshellarg("--host=".$conf['db_server']); |
236
|
|
|
$filenameArg = escapeshellarg($filename); |
|
|
|
|
237
|
|
|
|
238
|
|
|
$process = $webroot->createProcess("$passwordArg pg_dump --clean --no-owner --no-tablespaces $usernameArg $hostArg $databaseArg | gzip -c"); |
239
|
|
|
$sspak->writeFileFromProcess($filename, $process); |
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
function getassets($webroot, $assetsPath, $sspak, $filename) { |
|
|
|
|
244
|
|
|
$assetsParentArg = escapeshellarg(dirname($assetsPath)); |
245
|
|
|
$assetsBaseArg = escapeshellarg(basename($assetsPath)); |
246
|
|
|
|
247
|
|
|
$process = $webroot->createProcess("cd $assetsParentArg && tar cfh - $assetsBaseArg | gzip -c"); |
248
|
|
|
$sspak->writeFileFromProcess($filename, $process); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function getgitremote($webroot, $sspak, $gitRemoteFile) { |
|
|
|
|
252
|
|
|
// Only do anything if we're copying from a git checkout |
253
|
|
|
$gitRepo = $webroot->getPath() .'/.git'; |
254
|
|
|
if($webroot->exists($gitRepo)) { |
255
|
|
|
// Identify current branch |
256
|
|
|
$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'branch')); |
257
|
|
|
if(preg_match("/\* ([^ \n]*)/", $output['output'], $matches) && strpos("(no branch)", $matches[1])===false) { |
258
|
|
|
// If there is a current branch, use that branch's remove |
259
|
|
|
$currentBranch = trim($matches[1]); |
260
|
|
|
$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config','--get',"branch.$currentBranch.remote")); |
261
|
|
|
$remoteName = trim($output['output']); |
262
|
|
|
if(!$remoteName) $remoteName = 'origin'; |
263
|
|
|
|
264
|
|
|
// Default to origin |
265
|
|
|
} else { |
266
|
|
|
$currentBranch = null; |
267
|
|
|
$remoteName = 'origin'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// Determine the URL of that remote |
271
|
|
|
$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config','--get',"remote.$remoteName.url")); |
272
|
|
|
$remoteURL = trim($output['output']); |
273
|
|
|
|
274
|
|
|
// Determine the current SHA |
275
|
|
|
$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'log','-1','--format=%H')); |
276
|
|
|
$sha = trim($output['output']); |
277
|
|
|
|
278
|
|
|
$content = "remote = $remoteURL\nbranch = $currentBranch\nsha = $sha\n"; |
279
|
|
|
|
280
|
|
|
$sspak->writeFile($gitRemoteFile, $content); |
281
|
|
|
|
282
|
|
|
return true; |
283
|
|
|
} |
284
|
|
|
return false; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Load an .sspak into an environment. |
289
|
|
|
* Does not backup - be careful! */ |
290
|
|
|
function load($args) { |
|
|
|
|
291
|
|
|
$executor = $this->executor; |
292
|
|
|
|
293
|
|
|
$args->requireUnnamed(array('source sspak file')); |
294
|
|
|
|
295
|
|
|
// Set-up |
296
|
|
|
$file = $args->unnamed(0); |
297
|
|
|
$sspak = new SSPakFile($file, $executor); |
298
|
|
|
$webroot = new Webroot(($args->unnamed(1) ?: '.'), $executor); |
299
|
|
|
$webroot->setSudo($args->sudo('to')); |
300
|
|
|
$pakParts = $args->pakParts(); |
301
|
|
|
|
302
|
|
|
$namedArgs = $args->getNamedArgs(); |
303
|
|
|
if(!empty($namedArgs['identity'])) { |
304
|
|
|
// SSH private key |
305
|
|
|
$webroot->setSSHItentityFile($namedArgs['identity']); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// Validation |
309
|
|
|
if(!$sspak->exists()) throw new Exception( "File '$file' doesn't exist."); |
310
|
|
|
|
311
|
|
|
// Push database, if necessary |
312
|
|
|
$namedArgs = $args->getNamedArgs(); |
313
|
|
|
if($pakParts['db'] && $sspak->contains('database.sql.gz')) { |
314
|
|
|
$webroot->putdb($sspak, isset($namedArgs['drop-db'])); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// Push assets, if neccessary |
318
|
|
|
if($pakParts['assets'] && $sspak->contains('assets.tar.gz')) { |
319
|
|
|
$webroot->putassets($sspak); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Install an .sspak into a new environment. |
325
|
|
|
*/ |
326
|
|
|
function install($args) { |
|
|
|
|
327
|
|
|
$executor = $this->executor; |
328
|
|
|
|
329
|
|
|
$args->requireUnnamed(array('source sspak file', 'dest new webroot')); |
330
|
|
|
|
331
|
|
|
// Set-up |
332
|
|
|
$file = $args->unnamed(0); |
333
|
|
|
$webrootDir = $args->unnamed(1); |
334
|
|
|
$sspak = new SSPakFile($file, $executor); |
335
|
|
|
$webroot = new Webroot($webrootDir, $executor); |
336
|
|
|
$webroot->setSudo($args->sudo('to')); |
337
|
|
|
$pakParts = $args->pakParts(); |
338
|
|
|
|
339
|
|
|
// Validation |
340
|
|
|
if($webroot->exists($webroot->getPath())) throw new Exception( "Webroot '$webrootDir' already exists."); |
341
|
|
|
if(!$sspak->exists()) throw new Exception( "File '$file' doesn't exist."); |
342
|
|
|
|
343
|
|
|
// Create new dir |
344
|
|
|
$webroot->exec(array('mkdir', $webroot->getPath())); |
|
|
|
|
345
|
|
|
|
346
|
|
|
if($sspak->contains('git-remote')) { |
347
|
|
|
$details = $sspak->gitRemoteDetails(); |
348
|
|
|
$webroot->putgit($details); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// TODO: composer install needed. |
352
|
|
|
|
353
|
|
|
// Push database, if necessary |
354
|
|
|
$namedArgs = $args->getNamedArgs(); |
355
|
|
|
if($pakParts['db'] && $sspak->contains('database.sql.gz')) { |
356
|
|
|
$webroot->putdb($sspak, isset($namedArgs['drop-db'])); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
// Push assets, if neccessary |
360
|
|
|
if($pakParts['assets'] && $sspak->contains('assets.tar.gz')) { |
361
|
|
|
$webroot->putassets($sspak); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Bundle a .sspak into a self-extracting executable installer. |
367
|
|
|
*/ |
368
|
|
|
function bundle($args) { |
|
|
|
|
369
|
|
|
// TODO: throws require_once errors, fix before re-enabling. |
370
|
|
|
|
371
|
|
|
$executor = $this->executor; |
372
|
|
|
|
373
|
|
|
$args->requireUnnamed(array('source sspak file', 'dest executable file')); |
374
|
|
|
|
375
|
|
|
// Set-up |
376
|
|
|
$sourceFile = $args->unnamed(0); |
377
|
|
|
$destFile = $args->unnamed(1); |
378
|
|
|
|
379
|
|
|
$sspakScript = file_get_contents($_SERVER['argv'][0]); |
380
|
|
|
// Broken up to not get detected by our sed command |
381
|
|
|
$sspakScript .= "\n__halt_compiler();\n"."//"." TAR START?>\n"; |
382
|
|
|
|
383
|
|
|
// Mark as self-extracting |
384
|
|
|
$sspakScript = str_replace('$isSelfExtracting = false;', '$isSelfExtracting = true;', $sspakScript); |
385
|
|
|
|
386
|
|
|
// Load the sniffer file |
387
|
|
|
$snifferFile = dirname(__FILE__) . '/sspak-sniffer.php'; |
388
|
|
|
$sspakScript = str_replace("\$snifferFileContent = '';\n", |
389
|
|
|
"\$snifferFileContent = '" |
390
|
|
|
. str_replace(array("\\","'"),array("\\\\", "\\'"), file_get_contents($snifferFile)) . "';\n", $sspakScript); |
391
|
|
|
|
392
|
|
|
file_put_contents($destFile, $sspakScript); |
393
|
|
|
chmod($destFile, 0775); |
394
|
|
|
|
395
|
|
|
$executor->execLocal(array('cat', $sourceFile), array( |
|
|
|
|
396
|
|
|
'outputFile' => $destFile, |
397
|
|
|
'outputFileAppend' => true |
398
|
|
|
)); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Transfer between environments without creating an sspak file |
403
|
|
|
*/ |
404
|
|
|
function transfer($args) { |
|
|
|
|
405
|
|
|
echo "Not implemented yet.\n"; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.