Completed
Push — master ( 540fce...b26f23 )
by Sean
04:04
created
code/backends/CapistranoDeploymentBackend.php 1 patch
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 
26 26
 		$currentBuild = $environment->CurrentBuild();
27 27
 		$currentSha = $currentBuild ? $currentBuild->SHA : '-';
28
-		if($currentSha !== $options['sha']) {
28
+		if ($currentSha !== $options['sha']) {
29 29
 			$strategy->setChange('Code version', $currentSha, $options['sha']);
30 30
 		}
31 31
 		$strategy->setActionTitle('Confirm deployment');
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 
67 67
 		// Use a package generator if specified, otherwise run a direct deploy, which is the default behaviour
68 68
 		// if build_filename isn't specified
69
-		if($this->packageGenerator) {
69
+		if ($this->packageGenerator) {
70 70
 			$log->write(sprintf('Using package generator "%s"', get_class($this->packageGenerator)));
71 71
 
72 72
 			try {
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 				throw $e;
77 77
 			}
78 78
 
79
-			if(empty($args['build_filename'])) {
79
+			if (empty($args['build_filename'])) {
80 80
 				throw new RuntimeException('Failed to generate package.');
81 81
 			}
82 82
 		}
@@ -119,14 +119,14 @@  discard block
 block discarded – undo
119 119
 		$cleanup->run(function($type, $buffer) use($log) {
120 120
 			$log->write($buffer);
121 121
 		});
122
-		if(!$cleanup->isSuccessful()) {
122
+		if (!$cleanup->isSuccessful()) {
123 123
 			$this->extend('cleanupFailure', $environment, $sha, $log, $project);
124 124
 			$log->write('Warning: Cleanup failed, but fine to continue. Needs manual cleanup sometime.');
125 125
 		}
126 126
 
127 127
 		$this->extend('deployEnd', $environment, $sha, $log, $project);
128 128
 
129
-		if ($error!==null) {
129
+		if ($error !== null) {
130 130
 			throw new RuntimeException($error);
131 131
 		}
132 132
 
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 		$command->run(function($type, $buffer) use($log) {
181 181
 			$log->write($buffer);
182 182
 		});
183
-		if(!$command->isSuccessful()) {
183
+		if (!$command->isSuccessful()) {
184 184
 			$this->extend('maintenanceEnableFailure', $environment, $log);
185 185
 			throw new RuntimeException($command->getErrorOutput());
186 186
 		}
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
 		$command->run(function($type, $buffer) use($log) {
197 197
 			$log->write($buffer);
198 198
 		});
199
-		if(!$command->isSuccessful()) {
199
+		if (!$command->isSuccessful()) {
200 200
 			$this->extend('maintenanceDisableFailure', $environment, $log);
201 201
 			throw new RuntimeException($command->getErrorOutput());
202 202
 		}
@@ -218,25 +218,25 @@  discard block
 block discarded – undo
218 218
 	 * @inheritdoc
219 219
 	 */
220 220
 	public function dataTransfer(\DNDataTransfer $dataTransfer, \DeploynautLogFile $log) {
221
-		if($dataTransfer->Direction == 'get') {
221
+		if ($dataTransfer->Direction == 'get') {
222 222
 			$this->dataTransferBackup($dataTransfer, $log);
223 223
 		} else {
224 224
 			$environment = $dataTransfer->Environment();
225 225
 			$project = $environment->Project();
226
-			$workingDir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'deploynaut-transfer-' . $dataTransfer->ID;
226
+			$workingDir = TEMP_FOLDER.DIRECTORY_SEPARATOR.'deploynaut-transfer-'.$dataTransfer->ID;
227 227
 			$archive = $dataTransfer->DataArchive();
228 228
 
229 229
 			// extract the sspak contents, we'll need these so capistrano can restore that content
230 230
 			try {
231 231
 				$archive->extractArchive($workingDir);
232
-			} catch(Exception $e) {
232
+			} catch (Exception $e) {
233 233
 				$log->write($e->getMessage());
234 234
 				throw new RuntimeException($e->getMessage());
235 235
 			}
236 236
 
237 237
 			// validate the contents match the requested transfer mode
238 238
 			$result = $archive->validateArchiveContents($dataTransfer->Mode);
239
-			if(!$result->valid()) {
239
+			if (!$result->valid()) {
240 240
 				// do some cleaning, get rid of the extracted archive lying around
241 241
 				$process = new AbortableProcess(sprintf('rm -rf %s', escapeshellarg($workingDir)));
242 242
 				$process->setTimeout(120);
@@ -266,41 +266,41 @@  discard block
 block discarded – undo
266 266
 		$name = $environment->getFullName();
267 267
 		$env = $environment->Project()->getProcessEnv();
268 268
 
269
-		if(!$args) {
269
+		if (!$args) {
270 270
 			$args = array();
271 271
 		}
272
-		$args['history_path'] = realpath(DEPLOYNAUT_LOG_PATH . '/');
272
+		$args['history_path'] = realpath(DEPLOYNAUT_LOG_PATH.'/');
273 273
 		$args['environment_id'] = $environment->ID;
274 274
 
275 275
 		// Inject env string directly into the command.
276 276
 		// Capistrano doesn't like the $process->setEnv($env) we'd normally do below.
277 277
 		$envString = '';
278
-		if(!empty($env)) {
278
+		if (!empty($env)) {
279 279
 			$envString .= 'env ';
280
-			foreach($env as $key => $value) {
280
+			foreach ($env as $key => $value) {
281 281
 				$envString .= "$key=\"$value\" ";
282 282
 			}
283 283
 		}
284 284
 
285 285
 		$data = DNData::inst();
286 286
 		// Generate a capfile from a template
287
-		$capTemplate = file_get_contents(BASE_PATH . '/deploynaut/Capfile.template');
287
+		$capTemplate = file_get_contents(BASE_PATH.'/deploynaut/Capfile.template');
288 288
 		$cap = str_replace(
289 289
 			array('<config root>', '<ssh key>', '<base path>'),
290 290
 			array($data->getEnvironmentDir(), DEPLOYNAUT_SSH_KEY, BASE_PATH),
291 291
 			$capTemplate
292 292
 		);
293 293
 
294
-		if(defined('DEPLOYNAUT_CAPFILE')) {
294
+		if (defined('DEPLOYNAUT_CAPFILE')) {
295 295
 			$capFile = DEPLOYNAUT_CAPFILE;
296 296
 		} else {
297
-			$capFile = ASSETS_PATH . '/Capfile';
297
+			$capFile = ASSETS_PATH.'/Capfile';
298 298
 		}
299 299
 		file_put_contents($capFile, $cap);
300 300
 
301
-		$command = "{$envString}cap -f " . escapeshellarg($capFile) . " -vv $name $action ROLES=$roles";
302
-		foreach($args as $argName => $argVal) {
303
-			$command .= ' -s ' . escapeshellarg($argName) . '=' . escapeshellarg($argVal);
301
+		$command = "{$envString}cap -f ".escapeshellarg($capFile)." -vv $name $action ROLES=$roles";
302
+		foreach ($args as $argName => $argVal) {
303
+			$command .= ' -s '.escapeshellarg($argName).'='.escapeshellarg($argVal);
304 304
 		}
305 305
 
306 306
 		$log->write(sprintf('Running command: %s', $command));
@@ -334,16 +334,16 @@  discard block
 block discarded – undo
334 334
 		$filepathBase = $dataArchive->generateFilepath($dataTransfer);
335 335
 		mkdir($filepathBase, 0700, true);
336 336
 
337
-		$databasePath = $filepathBase . DIRECTORY_SEPARATOR . 'database.sql.gz';
337
+		$databasePath = $filepathBase.DIRECTORY_SEPARATOR.'database.sql.gz';
338 338
 
339 339
 		// Backup database
340
-		if(in_array($dataTransfer->Mode, array('all', 'db'))) {
340
+		if (in_array($dataTransfer->Mode, array('all', 'db'))) {
341 341
 			$log->write(sprintf('Backup of database from "%s" started', $name));
342 342
 			$command = $this->getCommand('data:getdb', 'db', $environment, array('data_path' => $databasePath), $log);
343 343
 			$command->run(function($type, $buffer) use($log) {
344 344
 				$log->write($buffer);
345 345
 			});
346
-			if(!$command->isSuccessful()) {
346
+			if (!$command->isSuccessful()) {
347 347
 				$this->extend('dataTransferFailure', $environment, $log);
348 348
 				throw new RuntimeException($command->getErrorOutput());
349 349
 			}
@@ -351,13 +351,13 @@  discard block
 block discarded – undo
351 351
 		}
352 352
 
353 353
 		// Backup assets
354
-		if(in_array($dataTransfer->Mode, array('all', 'assets'))) {
354
+		if (in_array($dataTransfer->Mode, array('all', 'assets'))) {
355 355
 			$log->write(sprintf('Backup of assets from "%s" started', $name));
356 356
 			$command = $this->getCommand('data:getassets', 'web', $environment, array('data_path' => $filepathBase), $log);
357 357
 			$command->run(function($type, $buffer) use($log) {
358 358
 				$log->write($buffer);
359 359
 			});
360
-			if(!$command->isSuccessful()) {
360
+			if (!$command->isSuccessful()) {
361 361
 				$this->extend('dataTransferFailure', $environment, $log);
362 362
 				throw new RuntimeException($command->getErrorOutput());
363 363
 			}
@@ -372,12 +372,12 @@  discard block
 block discarded – undo
372 372
 		$log->write('Creating sspak...');
373 373
 
374 374
 		$sspakFilename = sprintf('%s.sspak', $dataArchive->generateFilename($dataTransfer));
375
-		$sspakFilepath = $filepathBase . DIRECTORY_SEPARATOR . $sspakFilename;
375
+		$sspakFilepath = $filepathBase.DIRECTORY_SEPARATOR.$sspakFilename;
376 376
 
377 377
 		try {
378 378
 			$dataArchive->attachFile($sspakFilepath, $dataTransfer);
379 379
 			$dataArchive->setArchiveFromFiles($filepathBase);
380
-		} catch(Exception $e) {
380
+		} catch (Exception $e) {
381 381
 			$log->write($e->getMessage());
382 382
 			throw new RuntimeException($e->getMessage());
383 383
 		}
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
 		$process = new AbortableProcess(sprintf('rm -rf %s/assets && rm -f %s', escapeshellarg($filepathBase), escapeshellarg($databasePath)));
389 389
 		$process->setTimeout(120);
390 390
 		$process->run();
391
-		if(!$process->isSuccessful()) {
391
+		if (!$process->isSuccessful()) {
392 392
 			$log->write('Could not delete temporary files');
393 393
 			throw new RuntimeException($process->getErrorOutput());
394 394
 		}
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 		$command->run(function($type, $buffer) use($log) {
408 408
 			$log->write($buffer);
409 409
 		});
410
-		if(!$command->isSuccessful()) {
410
+		if (!$command->isSuccessful()) {
411 411
 			$log->write(sprintf('Rebuild of "%s" failed: %s', $name, $command->getErrorOutput()));
412 412
 			throw new RuntimeException($command->getErrorOutput());
413 413
 		}
@@ -437,14 +437,14 @@  discard block
 block discarded – undo
437 437
 		};
438 438
 
439 439
 		// Restore database into target environment
440
-		if(in_array($dataTransfer->Mode, array('all', 'db'))) {
440
+		if (in_array($dataTransfer->Mode, array('all', 'db'))) {
441 441
 			$log->write(sprintf('Restore of database to "%s" started', $name));
442
-			$args = array('data_path' => $workingDir . DIRECTORY_SEPARATOR . 'database.sql');
442
+			$args = array('data_path' => $workingDir.DIRECTORY_SEPARATOR.'database.sql');
443 443
 			$command = $this->getCommand('data:pushdb', 'db', $environment, $args, $log);
444 444
 			$command->run(function($type, $buffer) use($log) {
445 445
 				$log->write($buffer);
446 446
 			});
447
-			if(!$command->isSuccessful()) {
447
+			if (!$command->isSuccessful()) {
448 448
 				$cleanupFn();
449 449
 				$log->write(sprintf('Restore of database to "%s" failed: %s', $name, $command->getErrorOutput()));
450 450
 				$this->extend('dataTransferFailure', $environment, $log);
@@ -454,14 +454,14 @@  discard block
 block discarded – undo
454 454
 		}
455 455
 
456 456
 		// Restore assets into target environment
457
-		if(in_array($dataTransfer->Mode, array('all', 'assets'))) {
457
+		if (in_array($dataTransfer->Mode, array('all', 'assets'))) {
458 458
 			$log->write(sprintf('Restore of assets to "%s" started', $name));
459
-			$args = array('data_path' => $workingDir . DIRECTORY_SEPARATOR . 'assets');
459
+			$args = array('data_path' => $workingDir.DIRECTORY_SEPARATOR.'assets');
460 460
 			$command = $this->getCommand('data:pushassets', 'web', $environment, $args, $log);
461 461
 			$command->run(function($type, $buffer) use($log) {
462 462
 				$log->write($buffer);
463 463
 			});
464
-			if(!$command->isSuccessful()) {
464
+			if (!$command->isSuccessful()) {
465 465
 				$cleanupFn();
466 466
 				$log->write(sprintf('Restore of assets to "%s" failed: %s', $name, $command->getErrorOutput()));
467 467
 				$this->extend('dataTransferFailure', $environment, $log);
@@ -486,7 +486,7 @@  discard block
 block discarded – undo
486 486
 		$timeout = 600;
487 487
 		$tick = 60;
488 488
 
489
-		if(!$url) {
489
+		if (!$url) {
490 490
 			$log->write('Skipping site accessible check: no URL found.');
491 491
 			return true;
492 492
 		}
@@ -517,7 +517,7 @@  discard block
 block discarded – undo
517 517
 		// query the site every second. Note that if the URL doesn't respond,
518 518
 		// curl_exec will take 5 seconds to timeout (see CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT above)
519 519
 		do {
520
-			if(time() > $start + $timeout) {
520
+			if (time() > $start + $timeout) {
521 521
 				$log->write(sprintf(' * Failed: check for %s timed out after %smin', $url, $timeout / 60));
522 522
 				return false;
523 523
 			}
@@ -526,13 +526,13 @@  discard block
 block discarded – undo
526 526
 
527 527
 			// check the HTTP response code for HTTP protocols
528 528
 			$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
529
-			if($status && !in_array($status, [500, 501, 502, 503, 504])) {
529
+			if ($status && !in_array($status, [500, 501, 502, 503, 504])) {
530 530
 				$success = true;
531 531
 			}
532 532
 
533 533
 			// check for any curl errors, mostly for checking the response state of non-HTTP protocols,
534 534
 			// but applies to checks of any protocol
535
-			if($response && !curl_errno($ch)) {
535
+			if ($response && !curl_errno($ch)) {
536 536
 				$success = true;
537 537
 			}
538 538
 
@@ -560,7 +560,7 @@  discard block
 block discarded – undo
560 560
 			}
561 561
 
562 562
 			sleep(1);
563
-		} while(!$success);
563
+		} while (!$success);
564 564
 
565 565
 		curl_close($ch);
566 566
 		$log->write(' * Success: site is accessible!');
Please login to merge, or discard this patch.