|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage Workflow |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Workflow; |
|
16
|
|
|
use Netresearch\Kite\Task; |
|
17
|
|
|
|
|
18
|
|
|
use Netresearch\Kite\Workflow; |
|
19
|
|
|
use Netresearch\Kite\Exception; |
|
20
|
|
|
|
|
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Deploy the current application to a certain stage |
|
25
|
|
|
* |
|
26
|
|
|
* @category Netresearch |
|
27
|
|
|
* @package Netresearch\Kite |
|
28
|
|
|
* @subpackage Workflow |
|
29
|
|
|
* @author Christian Opitz <[email protected]> |
|
30
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
31
|
|
|
* @link http://www.netresearch.de |
|
32
|
|
|
*/ |
|
33
|
|
|
class Deployment extends Workflow |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string Current release name |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $release; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Configures the arguments/options |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function configureVariables() |
|
46
|
|
|
{ |
|
47
|
|
|
return array( |
|
48
|
|
|
'rollback' => array( |
|
49
|
|
|
'type' => 'bool', |
|
50
|
|
|
'label' => 'Makes previous release current and current release next', |
|
51
|
|
|
'option' => true, |
|
52
|
|
|
'mode' => InputOption::VALUE_NONE, |
|
53
|
|
|
'shortcut' => 'r' |
|
54
|
|
|
), |
|
55
|
|
|
'activate' => array( |
|
56
|
|
|
'type' => 'bool', |
|
57
|
|
|
'label' => 'Makes next release current and current release previous', |
|
58
|
|
|
'option' => true, |
|
59
|
|
|
'shortcut' => 'a', |
|
60
|
|
|
'mode' => InputOption::VALUE_NONE, |
|
61
|
|
|
), |
|
62
|
|
|
'rsync' => array( |
|
63
|
|
|
'type' => 'array', |
|
64
|
|
|
'label' => 'Options for the rsync task (can contain keys options, exclude, and include - see rsync task for their descriptions)' |
|
65
|
|
|
), |
|
66
|
|
|
'shared' => array( |
|
67
|
|
|
'type' => 'array', |
|
68
|
|
|
'label' => 'Array of files (in key "files") and directories (in key "dirs") to share between releases - share directory is in node.deployDir/shared', |
|
69
|
|
|
'default' => array() |
|
70
|
|
|
), |
|
71
|
|
|
'--' |
|
72
|
|
|
) + parent::configureVariables(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Assemble this workflow |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function assemble() |
|
81
|
|
|
{ |
|
82
|
|
|
$rollback = $this->get('rollback'); |
|
83
|
|
|
$restore = $this->get('activate'); |
|
84
|
|
|
|
|
85
|
|
|
if (!$rollback && !$restore) { |
|
86
|
|
|
$this->checkout(); |
|
87
|
|
|
$this->release = date($this->get('releaseFormat', 'YmdHis')); |
|
88
|
|
|
$this->set('releaseDir', 'releases/' . $this->release); |
|
89
|
|
|
$this->release(); |
|
90
|
|
|
$this->shareResources(); |
|
91
|
|
|
} |
|
92
|
|
|
if ($rollback) { |
|
93
|
|
|
$this->rollback(); |
|
94
|
|
|
} else { |
|
95
|
|
|
$this->activate(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Checkout (forwards branch and merge in the stage configuration) |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Netresearch\Kite\Workflow\Composer\Checkout |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function checkout() |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$this->get('job.initialBranch', null)) { |
|
107
|
|
|
$this->set('job.initialBranch', $this->get('composer.rootPackage.branch')); |
|
108
|
|
|
|
|
109
|
|
|
// Assert a clean state and a valid lock... |
|
110
|
|
|
$this->sub('Netresearch\Kite\Workflow\Composer\Diagnose') |
|
111
|
|
|
->message('<step>Diagnosing package states</step>') |
|
112
|
|
|
->set('fix', true); |
|
113
|
|
|
|
|
114
|
|
|
// ...then backup this lock... |
|
115
|
|
|
$this->fs()->copy('composer.lock', '{config["workspace"]}/composer.lock.tmp'); |
|
116
|
|
|
|
|
117
|
|
|
// ...restore it at the end of the job and install the clean state again |
|
118
|
|
|
$this->after('@all')->restoreInitialState()->force(); |
|
119
|
|
|
} elseif ($this->get('merge', false)) { |
|
120
|
|
|
// We want to merge the initial branch and not the previously checked out |
|
121
|
|
|
$this->restoreInitialState(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->sub( |
|
|
|
|
|
|
125
|
|
|
'Netresearch\\Kite\\Workflow\\Composer\\Checkout', |
|
126
|
|
|
array( |
|
127
|
|
|
'branch' => $this->get('branch', null), |
|
128
|
|
|
'merge' => $this->get('merge', false), |
|
129
|
|
|
'create' => $this->get('createBranch', false) |
|
130
|
|
|
) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Check out initial branch and install the state before the first checkout |
|
136
|
|
|
* |
|
137
|
|
|
* @return Task\SubTask |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function restoreInitialState() |
|
140
|
|
|
{ |
|
141
|
|
|
$cleanup = $this->sub() |
|
142
|
|
|
->message('<step>Restoring initial state</step>') |
|
143
|
|
|
->when('job.initialBranch != composer.rootPackage.branch'); |
|
144
|
|
|
|
|
145
|
|
|
$cleanup->git('checkout', null, '{job.initialBranch}'); |
|
146
|
|
|
$cleanup->fs()->copy('{config["workspace"]}/composer.lock.tmp', 'composer.lock'); |
|
147
|
|
|
$cleanup->composer('install'); |
|
148
|
|
|
|
|
149
|
|
|
return $cleanup; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Create the next release from the current code base |
|
154
|
|
|
* |
|
155
|
|
|
* @return \Netresearch\Kite\Task\SubTask |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function release() |
|
158
|
|
|
{ |
|
159
|
|
|
$sub = $this->sub(); |
|
|
|
|
|
|
160
|
|
|
$sub->message("<step>Preparing release <comment>{$this->release}</comment></step>"); |
|
161
|
|
|
|
|
162
|
|
|
// Assert required directory structure |
|
163
|
|
|
$sub->remoteShell('mkdir -p {node.deployPath}/{releaseDir}'); |
|
164
|
|
|
|
|
165
|
|
|
$sub->remoteShell( |
|
166
|
|
|
'if [ -h current ]; then rsync --recursive --links `readlink current`/ {releaseDir}; fi', |
|
167
|
|
|
'{node.deployPath}' |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
$sub->remoteShell('if [ -h next ]; then rm -rf `readlink next` next; fi', '{node.deployPath}'); |
|
171
|
|
|
$sub->remoteShell('ln -s {releaseDir} next;', '{node.deployPath}'); |
|
172
|
|
|
|
|
173
|
|
|
$sub->output('<step>Synchronizing sources</step>'); |
|
174
|
|
|
$sub->rsync( |
|
175
|
|
|
'.', '{node}:{node.deployPath}/next', |
|
176
|
|
|
$this->get('rsync.options', array()), |
|
177
|
|
|
$this->get('rsync.exclude', array()), |
|
178
|
|
|
$this->get('rsync.include', array()) |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
return $sub; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Activate the next release |
|
186
|
|
|
* |
|
187
|
|
|
* @return \Netresearch\Kite\Task\SubTask |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function activate() |
|
190
|
|
|
{ |
|
191
|
|
|
$sub = $this->iterate('{nodes}', 'node'); |
|
|
|
|
|
|
192
|
|
|
$sub->message('<step>Activating ' . ($this->release ? 'new' : 'latest') . ' release</step>'); |
|
193
|
|
|
$sub->callback( |
|
194
|
|
|
function (Task\IterateTask $iterator) { |
|
195
|
|
|
$links = $iterator->remoteShell('echo "`readlink previous`;`readlink current`;`readlink next`"', '{node.deployPath}'); |
|
196
|
|
|
list($previous, $current, $next) = explode(';', $links); |
|
197
|
|
|
|
|
198
|
|
|
if (!$next) { |
|
199
|
|
|
$iterator->doBreak('<warning>Could not find next release on {node}</warning> - aborting'); |
|
200
|
|
|
} else { |
|
201
|
|
|
$nextRelease = basename($next); |
|
202
|
|
|
if (!$this->release) { |
|
203
|
|
|
$this->release = $nextRelease; |
|
204
|
|
|
} elseif ($nextRelease !== $this->release) { |
|
205
|
|
|
$iterator->doBreak("<warning>Next release on {node} is $nextRelease and not {$this->release} as expected</warning> - aborting"); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$commands = array("ln -sfn $next current; rm next"); |
|
209
|
|
|
if ($current) { |
|
210
|
|
|
$from = '<comment>' . basename($current) . '</comment>'; |
|
211
|
|
|
array_unshift($commands, "ln -s $current previous"); |
|
212
|
|
|
if ($previous) { |
|
213
|
|
|
array_unshift($commands, "rm previous; rm -rf $previous"); |
|
214
|
|
|
} |
|
215
|
|
|
} else { |
|
216
|
|
|
$from = '<warning>none</warning>'; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$iterator->output("<comment>{node}</comment>: $from -> <comment>$nextRelease</comment>"); |
|
220
|
|
|
|
|
221
|
|
|
$iterator->remoteShell($commands, '{node.deployPath}'); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Rollback to the previous release (makes current next again) |
|
229
|
|
|
* |
|
230
|
|
|
* In general this is as easy as: |
|
231
|
|
|
* |
|
232
|
|
|
* <code> |
|
233
|
|
|
* $this->remoteShell('if [ -h current ] && [ -h previous ]; then ln -sfn `readlink current` next; fi', '{node.deployPath}'); |
|
234
|
|
|
* $this->remoteShell('if [ -h previous ]; then ln -sfn `readlink previous` current; rm previous; fi', '{node.deployPath}'); |
|
235
|
|
|
* </code> |
|
236
|
|
|
* |
|
237
|
|
|
* but we want to output which release was switched to which on each node, |
|
238
|
|
|
* thus the code is a little more complex |
|
239
|
|
|
* |
|
240
|
|
|
* @return \Netresearch\Kite\Task\SubTask |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function rollback() |
|
243
|
|
|
{ |
|
244
|
|
|
$firstPreviousRelease = null; |
|
245
|
|
|
|
|
246
|
|
|
$sub = $this->iterate('{nodes}', 'node'); |
|
|
|
|
|
|
247
|
|
|
$sub->message('<step>Restoring previous release</step>'); |
|
248
|
|
|
$sub->callback( |
|
249
|
|
|
function (Task\IterateTask $iterator) use (&$firstPreviousRelease) { |
|
250
|
|
|
$links = $iterator->remoteShell('echo "`readlink previous`;`readlink current`;`readlink next`"', '{node.deployPath}'); |
|
251
|
|
|
list($previous, $current, $next) = explode(';', $links); |
|
|
|
|
|
|
252
|
|
|
if (!$previous) { |
|
253
|
|
|
$this->doBreak('<warning>Could not find previous release on {node}</warning> - aborting'); |
|
254
|
|
|
} else { |
|
255
|
|
|
$previousRelease = basename($previous); |
|
256
|
|
|
|
|
257
|
|
|
if (!$firstPreviousRelease) { |
|
258
|
|
|
$firstPreviousRelease = $previousRelease; |
|
259
|
|
|
} elseif ($previousRelease !== $firstPreviousRelease) { |
|
260
|
|
|
$iterator->doBreak("<warning>Previous release on {node} is $previousRelease and not $firstPreviousRelease as on the previous node(s)</warning> - aborting"); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$commands = array("ln -sfn $previous current; rm previous"); |
|
264
|
|
|
if ($current) { |
|
265
|
|
|
array_unshift($commands, "ln -s $current next"); |
|
266
|
|
|
$from = '<comment>' . basename($current) . '</comment>'; |
|
267
|
|
|
} else { |
|
268
|
|
|
$from = '<warning>none</warning>'; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$iterator->output("<comment>{node}</comment>: $from -> <comment>$previousRelease</comment>"); |
|
272
|
|
|
|
|
273
|
|
|
$iterator->remoteShell($commands, '{node.deployPath}'); |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Setup shared resources |
|
281
|
|
|
* |
|
282
|
|
|
* @return void |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function shareResources() |
|
285
|
|
|
{ |
|
286
|
|
|
$sub = $this->iterate('{shared}', array('type' => 'entries')); |
|
|
|
|
|
|
287
|
|
|
$sub->message('<step>Linking shared resources</step>'); |
|
288
|
|
|
$sub->callback( |
|
289
|
|
|
function (Task\IterateTask $iterator) { |
|
290
|
|
|
$type = $iterator->get('type'); |
|
291
|
|
|
if (!in_array($type, array('dirs', 'files'), true)) { |
|
292
|
|
|
$iterator->doExit('shared may only contain keys "dirs" or "files"', 1); |
|
293
|
|
|
} |
|
294
|
|
|
$isFile = substr($type, 0, 4) === 'file'; |
|
295
|
|
|
$entries = (array) $iterator->get('entries'); |
|
296
|
|
|
$shareDir = 'shared'; |
|
297
|
|
|
foreach ($entries as $entry) { |
|
298
|
|
|
$dirName = strpos($entry, '/') !== false ? dirname($entry) : null; |
|
299
|
|
|
$subDirCount = substr_count($this->get('releaseDir'), '/') + 1; |
|
300
|
|
|
$commands = array(); |
|
301
|
|
|
if ($isFile) { |
|
302
|
|
|
$commands[] = "if [ ! -f $shareDir/$entry ]; then mkdir -p $shareDir/$dirName; touch $shareDir/$entry; fi"; |
|
303
|
|
|
} else { |
|
304
|
|
|
$commands[] = "mkdir -p $shareDir/$entry"; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$commands[] = 'cd {releaseDir}'; |
|
308
|
|
|
$commands[] = 'rm -rf ' . $entry; |
|
309
|
|
|
if ($dirName) { |
|
|
|
|
|
|
310
|
|
|
$commands[] = 'mkdir -p ' . $dirName; |
|
311
|
|
|
$commands[] = 'cd ' . $dirName; |
|
312
|
|
|
$subDirCount += substr_count($dirName, '/') + 1; |
|
313
|
|
|
} |
|
314
|
|
|
$commands[] = 'ln -s ' . str_repeat('../', $subDirCount) . $shareDir . '/' . $entry; |
|
315
|
|
|
|
|
316
|
|
|
$iterator->remoteShell($commands, '{node.deployPath}'); |
|
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
?> |
|
323
|
|
|
|