1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Runs a deployment via the most appropriate backend |
5
|
|
|
*/ |
6
|
|
|
class DeployJob { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
public $args; |
12
|
|
|
|
13
|
|
|
public function setUp() { |
14
|
|
|
$this->updateStatus('Started'); |
15
|
|
|
chdir(BASE_PATH); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function tearDown() { |
19
|
|
|
$this->updateStatus('Finished'); |
20
|
|
|
chdir(BASE_PATH); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function perform() { |
24
|
|
|
echo "[-] DeployJob starting" . PHP_EOL; |
25
|
|
|
$log = new DeploynautLogFile($this->args['logfile']); |
26
|
|
|
|
27
|
|
|
$deployment = DNDeployment::get()->byID($this->args['deploymentID']); |
28
|
|
|
$environment = $deployment->Environment(); |
29
|
|
|
$project = $environment->Project(); |
30
|
|
|
// This is a bit icky, but there is no easy way of capturing a failed deploy by using the PHP Resque |
31
|
|
|
try { |
32
|
|
|
// Disallow concurrent deployments (don't rely on queuing implementation to restrict this) |
33
|
|
|
// Only consider deployments started in the last 30 minutes (older jobs probably got stuck) |
34
|
|
|
$runningDeployments = DNDeployment::get() |
35
|
|
|
->filter(array( |
36
|
|
|
'EnvironmentID' => $environment->ID, |
37
|
|
|
'Status' => array('Queued', 'Started'), |
38
|
|
|
'Created:GreaterThan' => strtotime('-30 minutes') |
39
|
|
|
)) |
40
|
|
|
->exclude('ID', $this->args['deploymentID']); |
41
|
|
|
|
42
|
|
|
if($runningDeployments->count()) { |
43
|
|
|
$runningDeployment = $runningDeployments->first(); |
44
|
|
|
$log->write(sprintf( |
45
|
|
|
'[-] Error: another deployment is in progress (started at %s by %s)', |
46
|
|
|
$runningDeployment->dbObject('Created')->Nice(), |
47
|
|
|
$runningDeployment->Deployer()->Title |
48
|
|
|
)); |
49
|
|
|
throw new RuntimeException(sprintf( |
50
|
|
|
'Another deployment is in progress (started at %s by %s)', |
51
|
|
|
$runningDeployment->dbObject('Created')->Nice(), |
52
|
|
|
$runningDeployment->Deployer()->Title |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$environment->Backend()->deploy( |
57
|
|
|
$environment, |
58
|
|
|
$log, |
59
|
|
|
$project, |
60
|
|
|
// Pass all args to give the backend full visibility. These args also contain |
61
|
|
|
// all options from the DeploymentStrategy merged in, including sha. |
62
|
|
|
$this->args |
63
|
|
|
); |
64
|
|
|
} catch(Exception $e) { |
65
|
|
|
$this->updateStatus('Failed'); |
66
|
|
|
echo "[-] DeployJob failed" . PHP_EOL; |
67
|
|
|
throw $e; |
68
|
|
|
} |
69
|
|
|
echo "[-] DeployJob finished" . PHP_EOL; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $status |
74
|
|
|
* @global array $databaseConfig |
75
|
|
|
*/ |
76
|
|
|
protected function updateStatus($status) { |
77
|
|
|
global $databaseConfig; |
78
|
|
|
DB::connect($databaseConfig); |
79
|
|
|
$dnDeployment = DNDeployment::get()->byID($this->args['deploymentID']); |
80
|
|
|
$dnDeployment->Status = $status; |
81
|
|
|
$dnDeployment->write(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return DNData |
86
|
|
|
*/ |
87
|
|
|
protected function DNData() { |
88
|
|
|
return DNData::inst(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|