1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This demo back-end doesn't actually do deployment. |
5
|
|
|
* |
6
|
|
|
* Whenever you deploy, it will track the deployment history in a text file assets/<environment>.deploy-history.txt |
7
|
|
|
* |
8
|
|
|
* It's useful for demonstrating how the system works, and how you can write deployment back-ends |
9
|
|
|
*/ |
10
|
|
|
class DemoDeploymentBackend extends Object implements DeploymentBackend { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Create a deployment strategy. |
14
|
|
|
* |
15
|
|
|
* @param \DNEnvironment $environment |
16
|
|
|
* @param array $options |
17
|
|
|
* |
18
|
|
|
* @return DeploymentStrategy |
19
|
|
|
*/ |
20
|
|
|
public function planDeploy(\DNEnvironment $environment, $options = array()) { |
21
|
|
|
return new DeploymentStrategy($environment, $options); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Deploy the given build to the given environment |
26
|
|
|
* |
27
|
|
|
* @param \DNEnvironment $environment |
28
|
|
|
* @param \DeploynautLogFile $log |
29
|
|
|
* @param \DNProject $project |
30
|
|
|
* @param array $options |
31
|
|
|
*/ |
32
|
|
|
public function deploy( |
33
|
|
|
\DNEnvironment $environment, |
34
|
|
|
\DeploynautLogFile $log, |
35
|
|
|
\DNProject $project, |
36
|
|
|
$options |
37
|
|
|
) { |
38
|
|
|
$sha = $options['sha']; |
39
|
|
|
|
40
|
|
|
$this->extend('deployStart', $environment, $sha, $log, $project); |
41
|
|
|
|
42
|
|
|
$file = sprintf('%s/%s.deploy-history.txt', DEPLOYNAUT_LOG_PATH, $environment->getFullName()); |
43
|
|
|
$CLI_file = escapeshellarg($file); |
44
|
|
|
$CLI_line = escapeshellarg(date('Y-m-d H:i:s') . " => $sha"); |
45
|
|
|
|
46
|
|
|
// Put maintenance page up |
47
|
|
|
$this->enableMaintenance($environment, $log, $project); |
48
|
|
|
|
49
|
|
|
// Do the deployment |
50
|
|
|
$log->write("Demo deployment: echo $CLI_line >> $CLI_file"); |
51
|
|
|
`echo $CLI_line >> $CLI_file`; |
52
|
|
|
$log->write("Arbitrary pause for 10s"); |
53
|
|
|
sleep(10); |
54
|
|
|
$log->write("Well, that was a waste of time"); |
55
|
|
|
|
56
|
|
|
// Once the deployment has run it's necessary to update the maintenance page status |
57
|
|
|
if(!empty($options['leaveMaintenancePage'])) { |
58
|
|
|
$this->enableMaintenance($environment, $log, $project); |
59
|
|
|
} else { |
60
|
|
|
// Remove maintenance page if we want it to |
61
|
|
|
$this->disableMaintenance($environment, $log, $project); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->extend('deployEnd', $environment, $sha, $log, $project); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \DNEnvironment $environment |
69
|
|
|
* @return ArrayList |
70
|
|
|
*/ |
71
|
|
|
public function getDeployOptions(\DNEnvironment $environment) { |
72
|
|
|
return new ArrayList(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function dataTransfer(\DNDataTransfer $dataTransfer, \DeploynautLogFile $log) { |
79
|
|
|
die('Not implemented'); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function enableMaintenance(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
83
|
|
|
$log->write(sprintf('Maintenance page enabled on "%s"', $environment->getFullName())); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function disableMaintenance(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
87
|
|
|
$log->write(sprintf('Maintenance page disabled on "%s"', $environment->getFullName())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function ping(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
91
|
|
|
$log->write(sprintf('Ping "%s"', $environment->getFullName())); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.