1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DNEnvironment |
5
|
|
|
* |
6
|
|
|
* This dataobject represents a target environment that source code can be deployed to. |
7
|
|
|
* Permissions are controlled by environment, see the various many-many relationships. |
8
|
|
|
* |
9
|
|
|
* @property string $Filename |
10
|
|
|
* @property string $Name |
11
|
|
|
* @property string $URL |
12
|
|
|
* @property string $BackendIdentifier |
13
|
|
|
* @property bool $Usage |
14
|
|
|
* |
15
|
|
|
* @method DNProject Project() |
16
|
|
|
* @property int $ProjectID |
17
|
|
|
* |
18
|
|
|
* @method HasManyList Deployments() |
19
|
|
|
* @method HasManyList DataArchives() |
20
|
|
|
* |
21
|
|
|
* @method ManyManyList Viewers() |
22
|
|
|
* @method ManyManyList ViewerGroups() |
23
|
|
|
* @method ManyManyList Deployers() |
24
|
|
|
* @method ManyManyList DeployerGroups() |
25
|
|
|
* @method ManyManyList CanRestoreMembers() |
26
|
|
|
* @method ManyManyList CanRestoreGroups() |
27
|
|
|
* @method ManyManyList CanBackupMembers() |
28
|
|
|
* @method ManyManyList CanBackupGroups() |
29
|
|
|
* @method ManyManyList ArchiveUploaders() |
30
|
|
|
* @method ManyManyList ArchiveUploaderGroups() |
31
|
|
|
* @method ManyManyList ArchiveDownloaders() |
32
|
|
|
* @method ManyManyList ArchiveDownloaderGroups() |
33
|
|
|
* @method ManyManyList ArchiveDeleters() |
34
|
|
|
* @method ManyManyList ArchiveDeleterGroups() |
35
|
|
|
*/ |
36
|
|
|
class DNEnvironment extends DataObject { |
|
|
|
|
37
|
|
|
|
38
|
|
|
const UAT = 'UAT'; |
39
|
|
|
|
40
|
|
|
const PRODUCTION = 'Production'; |
41
|
|
|
|
42
|
|
|
const UNSPECIFIED = 'Unspecified'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
public static $db = [ |
48
|
|
|
"Filename" => "Varchar(255)", |
49
|
|
|
"Name" => "Varchar(255)", |
50
|
|
|
"URL" => "Varchar(255)", |
51
|
|
|
"BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
52
|
|
|
"Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')", |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
public static $has_many = [ |
59
|
|
|
"Deployments" => "DNDeployment", |
60
|
|
|
"DataArchives" => "DNDataArchive", |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
public static $many_many = [ |
67
|
|
|
"Viewers" => "Member", // Who can view this environment |
68
|
|
|
"ViewerGroups" => "Group", |
69
|
|
|
"Deployers" => "Member", // Who can deploy to this environment |
70
|
|
|
"DeployerGroups" => "Group", |
71
|
|
|
"CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
72
|
|
|
"CanRestoreGroups" => "Group", |
73
|
|
|
"CanBackupMembers" => "Member", // Who can backup archive files from this environment |
74
|
|
|
"CanBackupGroups" => "Group", |
75
|
|
|
"ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
76
|
|
|
"ArchiveUploaderGroups" => "Group", |
77
|
|
|
"ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
78
|
|
|
"ArchiveDownloaderGroups" => "Group", |
79
|
|
|
"ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
80
|
|
|
"ArchiveDeleterGroups" => "Group", |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
public static $summary_fields = [ |
87
|
|
|
"Name" => "Environment Name", |
88
|
|
|
"Usage" => "Usage", |
89
|
|
|
"URL" => "URL", |
90
|
|
|
"DeployersList" => "Can Deploy List", |
91
|
|
|
"CanRestoreMembersList" => "Can Restore List", |
92
|
|
|
"CanBackupMembersList" => "Can Backup List", |
93
|
|
|
"ArchiveUploadersList" => "Can Upload List", |
94
|
|
|
"ArchiveDownloadersList" => "Can Download List", |
95
|
|
|
"ArchiveDeletersList" => "Can Delete List", |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
public static $searchable_fields = [ |
102
|
|
|
"Name", |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
private static $singular_name = 'Capistrano Environment'; |
|
|
|
|
106
|
|
|
|
107
|
|
|
private static $plural_name = 'Capistrano Environments'; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
private static $default_sort = 'Name'; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
public static $has_one = [ |
118
|
|
|
"Project" => "DNProject", |
119
|
|
|
"CreateEnvironment" => "DNCreateEnvironment" |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* If this is set to a full pathfile, it will be used as template |
124
|
|
|
* file when creating a new capistrano environment config file. |
125
|
|
|
* |
126
|
|
|
* If not set, the default 'environment.template' from the module |
127
|
|
|
* root is used |
128
|
|
|
* |
129
|
|
|
* @config |
130
|
|
|
* @var string |
131
|
|
|
*/ |
132
|
|
|
private static $template_file = ''; |
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set this to true to allow editing of the environment files via the web admin |
136
|
|
|
* |
137
|
|
|
* @var bool |
138
|
|
|
*/ |
139
|
|
|
private static $allow_web_editing = false; |
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @var array |
143
|
|
|
*/ |
144
|
|
|
private static $casting = [ |
|
|
|
|
145
|
|
|
'DeployHistory' => 'Text' |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Allowed backends. A map of Injector identifier to human-readable label. |
150
|
|
|
* |
151
|
|
|
* @config |
152
|
|
|
* @var array |
153
|
|
|
*/ |
154
|
|
|
private static $allowed_backends = []; |
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Used by the sync task |
158
|
|
|
* |
159
|
|
|
* @param string $path |
160
|
|
|
* @return \DNEnvironment |
161
|
|
|
*/ |
162
|
|
|
public static function create_from_path($path) { |
163
|
|
|
$e = DNEnvironment::create(); |
|
|
|
|
164
|
|
|
$e->Filename = $path; |
165
|
|
|
$e->Name = basename($e->Filename, '.rb'); |
166
|
|
|
|
167
|
|
|
// add each administrator member as a deployer of the new environment |
168
|
|
|
$adminGroup = Group::get()->filter('Code', 'administrators')->first(); |
169
|
|
|
$e->DeployerGroups()->add($adminGroup); |
|
|
|
|
170
|
|
|
return $e; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the deployment backend used for this environment. |
175
|
|
|
* |
176
|
|
|
* Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
177
|
|
|
* illegal then that value is ignored. |
178
|
|
|
* |
179
|
|
|
* @return DeploymentBackend |
180
|
|
|
*/ |
181
|
|
|
public function Backend() { |
182
|
|
|
$backends = array_keys($this->config()->get('allowed_backends', Config::FIRST_SET)); |
183
|
|
|
switch (sizeof($backends)) { |
184
|
|
|
// Nothing allowed, use the default value "DeploymentBackend" |
185
|
|
|
case 0: |
186
|
|
|
$backend = "DeploymentBackend"; |
187
|
|
|
break; |
188
|
|
|
|
189
|
|
|
// Only 1 thing allowed, use that |
190
|
|
|
case 1: |
191
|
|
|
$backend = $backends[0]; |
192
|
|
|
break; |
193
|
|
|
|
194
|
|
|
// Multiple choices, use our choice if it's legal, otherwise default to the first item on the list |
195
|
|
|
default: |
196
|
|
|
$backend = $this->BackendIdentifier; |
197
|
|
|
if (!in_array($backend, $backends)) { |
198
|
|
|
$backend = $backends[0]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return Injector::inst()->get($backend); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param SS_HTTPRequest $request |
207
|
|
|
* |
208
|
|
|
* @return DeploymentStrategy |
209
|
|
|
*/ |
210
|
|
|
public function getDeployStrategy(\SS_HTTPRequest $request) { |
211
|
|
|
return $this->Backend()->planDeploy($this, $request->requestVars()); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function Menu() { |
215
|
|
|
$list = new ArrayList(); |
216
|
|
|
|
217
|
|
|
$controller = Controller::curr(); |
218
|
|
|
$actionType = $controller->getField('CurrentActionType'); |
219
|
|
|
|
220
|
|
|
$list->push(new ArrayData([ |
221
|
|
|
'Link' => sprintf('naut/project/%s/environment/%s', $this->Project()->Name, $this->Name), |
222
|
|
|
'Title' => 'Deployments', |
223
|
|
|
'IsCurrent' => $this->isCurrent(), |
224
|
|
|
'IsSection' => $this->isSection() && $actionType == DNRoot::ACTION_DEPLOY |
225
|
|
|
])); |
226
|
|
|
|
227
|
|
|
$this->extend('updateMenu', $list); |
228
|
|
|
|
229
|
|
|
return $list; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return the current object from $this->Menu() |
234
|
|
|
* Good for making titles and things |
235
|
|
|
*/ |
236
|
|
|
public function CurrentMenu() { |
237
|
|
|
return $this->Menu()->filter('IsSection', true)->First(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Return a name for this environment. |
242
|
|
|
* |
243
|
|
|
* @param string $separator The string used when concatenating project with env name |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function getFullName($separator = ':') { |
247
|
|
|
return sprintf('%s%s%s', $this->Project()->Name, $separator, $this->Name); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* URL for the environment that can be used if no explicit URL is set. |
252
|
|
|
*/ |
253
|
|
|
public function getDefaultURL() { |
254
|
|
|
return null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function getBareURL() { |
258
|
|
|
$url = parse_url($this->URL); |
259
|
|
|
if (isset($url['host'])) { |
260
|
|
|
return strtolower($url['host']); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getBareDefaultURL() { |
265
|
|
|
$url = parse_url($this->getDefaultURL()); |
266
|
|
|
if (isset($url['host'])) { |
267
|
|
|
return strtolower($url['host']); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Environments are only viewable by people that can view the environment. |
273
|
|
|
* |
274
|
|
|
* @param Member|null $member |
275
|
|
|
* @return boolean |
276
|
|
|
*/ |
277
|
|
|
public function canView($member = null) { |
278
|
|
|
if (!$member) { |
279
|
|
|
$member = Member::currentUser(); |
280
|
|
|
} |
281
|
|
|
if (!$member) { |
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
// Must be logged in to check permissions |
285
|
|
|
|
286
|
|
|
if (Permission::checkMember($member, 'ADMIN')) { |
287
|
|
|
return true; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// if no Viewers or ViewerGroups defined, fallback to DNProject::canView permissions |
291
|
|
|
if ($this->Viewers()->exists() || $this->ViewerGroups()->exists()) { |
292
|
|
|
return $this->Viewers()->byID($member->ID) |
293
|
|
|
|| $member->inGroups($this->ViewerGroups()); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $this->Project()->canView($member); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Allow deploy only to some people. |
301
|
|
|
* |
302
|
|
|
* @param Member|null $member |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
public function canDeploy($member = null) { |
|
|
|
|
306
|
|
|
if (!$member) { |
307
|
|
|
$member = Member::currentUser(); |
308
|
|
|
} |
309
|
|
|
if (!$member) { |
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
// Must be logged in to check permissions |
313
|
|
|
|
314
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
315
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_DEPLOYMENT, $member)) { |
|
|
|
|
316
|
|
|
return true; |
317
|
|
|
} |
318
|
|
|
} else { |
319
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) { |
320
|
|
|
return true; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $this->Deployers()->byID($member->ID) |
325
|
|
|
|| $member->inGroups($this->DeployerGroups()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Provide reason why the user cannot deploy. |
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
public function getCannotDeployMessage() { |
334
|
|
|
return 'You cannot deploy to this environment.'; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
339
|
|
|
* {@link DNEnvironment}. |
340
|
|
|
* |
341
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
342
|
|
|
* @return boolean true if $member can restore, and false if they can't. |
343
|
|
|
*/ |
344
|
|
View Code Duplication |
public function canRestore($member = null) { |
|
|
|
|
345
|
|
|
if (!$member) { |
346
|
|
|
$member = Member::currentUser(); |
347
|
|
|
} |
348
|
|
|
if (!$member) { |
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
// Must be logged in to check permissions |
352
|
|
|
|
353
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
354
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
355
|
|
|
return true; |
356
|
|
|
} |
357
|
|
|
} else { |
358
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
359
|
|
|
return true; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $this->CanRestoreMembers()->byID($member->ID) |
364
|
|
|
|| $member->inGroups($this->CanRestoreGroups()); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
369
|
|
|
* file. |
370
|
|
|
* |
371
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
372
|
|
|
* @return boolean true if $member can backup, and false if they can't. |
373
|
|
|
*/ |
374
|
|
View Code Duplication |
public function canBackup($member = null) { |
|
|
|
|
375
|
|
|
$project = $this->Project(); |
376
|
|
|
if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
377
|
|
|
return false; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if (!$member) { |
381
|
|
|
$member = Member::currentUser(); |
382
|
|
|
} |
383
|
|
|
// Must be logged in to check permissions |
384
|
|
|
if (!$member) { |
385
|
|
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
389
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
390
|
|
|
return true; |
391
|
|
|
} |
392
|
|
|
} else { |
393
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
394
|
|
|
return true; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $this->CanBackupMembers()->byID($member->ID) |
399
|
|
|
|| $member->inGroups($this->CanBackupGroups()); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
404
|
|
|
* {@link DNEnvironment}. |
405
|
|
|
* |
406
|
|
|
* Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
407
|
|
|
* process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
408
|
|
|
* {@link self::canRestore()}. |
409
|
|
|
* |
410
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
411
|
|
|
* @return boolean true if $member can upload archives linked to this environment, false if they can't. |
412
|
|
|
*/ |
413
|
|
View Code Duplication |
public function canUploadArchive($member = null) { |
|
|
|
|
414
|
|
|
$project = $this->Project(); |
415
|
|
|
if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
416
|
|
|
return false; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
if (!$member) { |
420
|
|
|
$member = Member::currentUser(); |
421
|
|
|
} |
422
|
|
|
if (!$member) { |
423
|
|
|
return false; |
424
|
|
|
} |
425
|
|
|
// Must be logged in to check permissions |
426
|
|
|
|
427
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
428
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
429
|
|
|
return true; |
430
|
|
|
} |
431
|
|
|
} else { |
432
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
433
|
|
|
return true; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return $this->ArchiveUploaders()->byID($member->ID) |
438
|
|
|
|| $member->inGroups($this->ArchiveUploaderGroups()); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
443
|
|
|
* {@link DNEnvironment}. |
444
|
|
|
* |
445
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
446
|
|
|
* @return boolean true if $member can download archives from this environment, false if they can't. |
447
|
|
|
*/ |
448
|
|
View Code Duplication |
public function canDownloadArchive($member = null) { |
|
|
|
|
449
|
|
|
if (!$member) { |
450
|
|
|
$member = Member::currentUser(); |
451
|
|
|
} |
452
|
|
|
if (!$member) { |
453
|
|
|
return false; |
454
|
|
|
} |
455
|
|
|
// Must be logged in to check permissions |
456
|
|
|
|
457
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
458
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
459
|
|
|
return true; |
460
|
|
|
} |
461
|
|
|
} else { |
462
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
463
|
|
|
return true; |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
return $this->ArchiveDownloaders()->byID($member->ID) |
468
|
|
|
|| $member->inGroups($this->ArchiveDownloaderGroups()); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
473
|
|
|
* {@link DNEnvironment}. |
474
|
|
|
* |
475
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
476
|
|
|
* @return boolean true if $member can delete archives from this environment, false if they can't. |
477
|
|
|
*/ |
478
|
|
View Code Duplication |
public function canDeleteArchive($member = null) { |
|
|
|
|
479
|
|
|
if (!$member) { |
480
|
|
|
$member = Member::currentUser(); |
481
|
|
|
} |
482
|
|
|
if (!$member) { |
483
|
|
|
return false; |
484
|
|
|
} |
485
|
|
|
// Must be logged in to check permissions |
486
|
|
|
|
487
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
488
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
489
|
|
|
return true; |
490
|
|
|
} |
491
|
|
|
} else { |
492
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
493
|
|
|
return true; |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
return $this->ArchiveDeleters()->byID($member->ID) |
498
|
|
|
|| $member->inGroups($this->ArchiveDeleterGroups()); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Get a string of groups/people that are allowed to deploy to this environment. |
503
|
|
|
* Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
504
|
|
|
* |
505
|
|
|
* @return string |
506
|
|
|
*/ |
507
|
|
|
public function getDeployersList() { |
508
|
|
|
return implode( |
509
|
|
|
", ", |
510
|
|
|
array_merge( |
511
|
|
|
$this->DeployerGroups()->column("Title"), |
512
|
|
|
$this->Deployers()->column("FirstName") |
513
|
|
|
) |
514
|
|
|
); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
519
|
|
|
* |
520
|
|
|
* @return string |
521
|
|
|
*/ |
522
|
|
|
public function getCanRestoreMembersList() { |
523
|
|
|
return implode( |
524
|
|
|
", ", |
525
|
|
|
array_merge( |
526
|
|
|
$this->CanRestoreGroups()->column("Title"), |
527
|
|
|
$this->CanRestoreMembers()->column("FirstName") |
528
|
|
|
) |
529
|
|
|
); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
534
|
|
|
* |
535
|
|
|
* @return string |
536
|
|
|
*/ |
537
|
|
|
public function getCanBackupMembersList() { |
538
|
|
|
return implode( |
539
|
|
|
", ", |
540
|
|
|
array_merge( |
541
|
|
|
$this->CanBackupGroups()->column("Title"), |
542
|
|
|
$this->CanBackupMembers()->column("FirstName") |
543
|
|
|
) |
544
|
|
|
); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
549
|
|
|
* objects linked to this environment. |
550
|
|
|
* |
551
|
|
|
* @return string |
552
|
|
|
*/ |
553
|
|
|
public function getArchiveUploadersList() { |
554
|
|
|
return implode( |
555
|
|
|
", ", |
556
|
|
|
array_merge( |
557
|
|
|
$this->ArchiveUploaderGroups()->column("Title"), |
558
|
|
|
$this->ArchiveUploaders()->column("FirstName") |
559
|
|
|
) |
560
|
|
|
); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
565
|
|
|
* |
566
|
|
|
* @return string |
567
|
|
|
*/ |
568
|
|
|
public function getArchiveDownloadersList() { |
569
|
|
|
return implode( |
570
|
|
|
", ", |
571
|
|
|
array_merge( |
572
|
|
|
$this->ArchiveDownloaderGroups()->column("Title"), |
573
|
|
|
$this->ArchiveDownloaders()->column("FirstName") |
574
|
|
|
) |
575
|
|
|
); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
580
|
|
|
* |
581
|
|
|
* @return string |
582
|
|
|
*/ |
583
|
|
|
public function getArchiveDeletersList() { |
584
|
|
|
return implode( |
585
|
|
|
", ", |
586
|
|
|
array_merge( |
587
|
|
|
$this->ArchiveDeleterGroups()->column("Title"), |
588
|
|
|
$this->ArchiveDeleters()->column("FirstName") |
589
|
|
|
) |
590
|
|
|
); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* @return DNData |
595
|
|
|
*/ |
596
|
|
|
public function DNData() { |
597
|
|
|
return DNData::inst(); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Get the current deployed build for this environment |
602
|
|
|
* |
603
|
|
|
* Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
604
|
|
|
* a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
605
|
|
|
* |
606
|
|
|
* @return false|DNDeployment |
607
|
|
|
*/ |
608
|
|
|
public function CurrentBuild() { |
609
|
|
|
// The DeployHistory function is far too slow to use for this |
610
|
|
|
|
611
|
|
|
/** @var DNDeployment $deploy */ |
612
|
|
|
$deploy = DNDeployment::get()->filter([ |
613
|
|
|
'EnvironmentID' => $this->ID, |
614
|
|
|
'State' => DNDeployment::STATE_COMPLETED |
615
|
|
|
])->sort('LastEdited DESC')->first(); |
616
|
|
|
|
617
|
|
|
if (!$deploy || (!$deploy->SHA)) { |
|
|
|
|
618
|
|
|
return false; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
$repo = $this->Project()->getRepository(); |
622
|
|
|
if (!$repo) { |
623
|
|
|
return $deploy; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
try { |
627
|
|
|
$commit = $this->getCommit($deploy->SHA); |
628
|
|
|
if ($commit) { |
629
|
|
|
$deploy->Message = Convert::raw2xml($this->getCommitMessage($commit)); |
|
|
|
|
630
|
|
|
$deploy->Committer = Convert::raw2xml($commit->getCommitterName()); |
|
|
|
|
631
|
|
|
$deploy->CommitDate = $commit->getCommitterDate()->Format('d/m/Y g:ia'); |
|
|
|
|
632
|
|
|
$deploy->Author = Convert::raw2xml($commit->getAuthorName()); |
|
|
|
|
633
|
|
|
$deploy->AuthorDate = $commit->getAuthorDate()->Format('d/m/Y g:ia'); |
|
|
|
|
634
|
|
|
} |
635
|
|
|
// We can't find this SHA, so we ignore adding a commit message to the deployment |
636
|
|
|
} catch (Exception $ex) { |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
return $deploy; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* This is a proxy call to gitonmy that caches the information per project and sha |
644
|
|
|
* |
645
|
|
|
* @param string $sha |
646
|
|
|
* @return \Gitonomy\Git\Commit |
647
|
|
|
*/ |
648
|
|
|
public function getCommit($sha) { |
649
|
|
|
return $this->Project()->getCommit($sha); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
653
|
|
|
return $this->Project()->getCommitMessage($commit); |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
657
|
|
|
return $this->Project()->getCommitTags($commit); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* A list of past deployments. |
662
|
|
|
* @param string $orderBy - the name of a DB column to sort in descending order |
663
|
|
|
* @return \ArrayList |
664
|
|
|
*/ |
665
|
|
|
public function DeployHistory($orderBy = '') { |
666
|
|
|
$sort = []; |
667
|
|
|
if ($orderBy != '') { |
668
|
|
|
$sort[$orderBy] = 'DESC'; |
669
|
|
|
} |
670
|
|
|
// default / fallback sort order |
671
|
|
|
$sort['LastEdited'] = 'DESC'; |
672
|
|
|
|
673
|
|
|
return $this->Deployments() |
674
|
|
|
->where('"SHA" IS NOT NULL') |
675
|
|
|
->filter('State', [ |
676
|
|
|
DNDeployment::STATE_COMPLETED, |
677
|
|
|
DNDeployment::STATE_FAILED, |
678
|
|
|
DNDeployment::STATE_INVALID |
679
|
|
|
]) |
680
|
|
|
->sort($sort); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* A list of upcoming or current deployments. |
685
|
|
|
* @return ArrayList |
686
|
|
|
*/ |
687
|
|
|
public function UpcomingDeployments() { |
688
|
|
|
return $this->Deployments() |
689
|
|
|
->where('"SHA" IS NOT NULL') |
690
|
|
|
->filter('State', [ |
691
|
|
|
DNDeployment::STATE_NEW, |
692
|
|
|
DNDeployment::STATE_SUBMITTED, |
693
|
|
|
DNDeployment::STATE_APPROVED, |
694
|
|
|
DNDeployment::STATE_REJECTED, |
695
|
|
|
DNDeployment::STATE_ABORTING, |
696
|
|
|
DNDeployment::STATE_QUEUED, |
697
|
|
|
DNDeployment::STATE_DEPLOYING, |
698
|
|
|
]) |
699
|
|
|
->sort('LastEdited DESC'); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* @param string $action |
704
|
|
|
* |
705
|
|
|
* @return string |
706
|
|
|
*/ |
707
|
|
|
public function Link($action = '') { |
708
|
|
|
return \Controller::join_links($this->Project()->Link(), "environment", $this->Name, $action); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Is this environment currently at the root level of the controller that handles it? |
713
|
|
|
* @return bool |
714
|
|
|
*/ |
715
|
|
|
public function isCurrent() { |
716
|
|
|
return $this->isSection() && Controller::curr()->getAction() == 'environment'; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Is this environment currently in a controller that is handling it or performing a sub-task? |
721
|
|
|
* @return bool |
722
|
|
|
*/ |
723
|
|
|
public function isSection() { |
724
|
|
|
$controller = Controller::curr(); |
725
|
|
|
$environment = $controller->getField('CurrentEnvironment'); |
726
|
|
|
return $environment && $environment->ID == $this->ID; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* @return FieldList |
731
|
|
|
*/ |
732
|
|
|
public function getCMSFields() { |
733
|
|
|
$fields = new FieldList(new TabSet('Root')); |
734
|
|
|
|
735
|
|
|
$project = $this->Project(); |
736
|
|
|
if ($project && $project->exists()) { |
737
|
|
|
$viewerGroups = $project->Viewers(); |
738
|
|
|
$groups = $viewerGroups->sort('Title')->map()->toArray(); |
739
|
|
|
$members = []; |
740
|
|
|
foreach ($viewerGroups as $group) { |
741
|
|
|
foreach ($group->Members()->map() as $k => $v) { |
742
|
|
|
$members[$k] = $v; |
743
|
|
|
} |
744
|
|
|
} |
745
|
|
|
asort($members); |
746
|
|
|
} else { |
747
|
|
|
$groups = []; |
748
|
|
|
$members = []; |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
// Main tab |
752
|
|
|
$fields->addFieldsToTab('Root.Main', [ |
753
|
|
|
// The Main.ProjectID |
754
|
|
|
TextField::create('ProjectName', 'Project') |
755
|
|
|
->setValue(($project = $this->Project()) ? $project->Name : null) |
756
|
|
|
->performReadonlyTransformation(), |
757
|
|
|
|
758
|
|
|
// The Main.Name |
759
|
|
|
TextField::create('Name', 'Environment name') |
760
|
|
|
->setDescription('A descriptive name for this environment, e.g. staging, uat, production'), |
761
|
|
|
|
762
|
|
|
$this->obj('Usage')->scaffoldFormField('Environment usage'), |
763
|
|
|
|
764
|
|
|
// The Main.URL field |
765
|
|
|
TextField::create('URL', 'Server URL') |
766
|
|
|
->setDescription('This url will be used to provide the front-end with a link to this environment'), |
767
|
|
|
|
768
|
|
|
// The Main.Filename |
769
|
|
|
TextField::create('Filename') |
770
|
|
|
->setDescription('The capistrano environment file name') |
771
|
|
|
->performReadonlyTransformation(), |
772
|
|
|
]); |
773
|
|
|
|
774
|
|
|
// Backend identifier - pick from a named list of configurations specified in YML config |
775
|
|
|
$backends = $this->config()->get('allowed_backends', Config::FIRST_SET); |
776
|
|
|
// If there's only 1 backend, then user selection isn't needed |
777
|
|
|
if (sizeof($backends) > 1) { |
778
|
|
|
$fields->addFieldToTab('Root.Main', DropdownField::create('BackendIdentifier', 'Deployment backend') |
779
|
|
|
->setSource($backends) |
780
|
|
|
->setDescription('What kind of deployment system should be used to deploy to this environment')); |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
$fields->addFieldsToTab('Root.UserPermissions', [ |
784
|
|
|
// The viewers of the environment |
785
|
|
|
$this |
786
|
|
|
->buildPermissionField('ViewerGroups', 'Viewers', $groups, $members) |
787
|
|
|
->setTitle('Who can view this environment?') |
788
|
|
|
->setDescription('Groups or Users who can view this environment'), |
789
|
|
|
|
790
|
|
|
// The Main.Deployers |
791
|
|
|
$this |
792
|
|
|
->buildPermissionField('DeployerGroups', 'Deployers', $groups, $members) |
793
|
|
|
->setTitle('Who can deploy?') |
794
|
|
|
->setDescription('Groups or Users who can deploy to this environment'), |
795
|
|
|
|
796
|
|
|
// A box to select all snapshot options. |
797
|
|
|
$this |
798
|
|
|
->buildPermissionField('TickAllSnapshotGroups', 'TickAllSnapshot', $groups, $members) |
799
|
|
|
->setTitle("<em>All snapshot permissions</em>") |
800
|
|
|
->addExtraClass('tickall') |
801
|
|
|
->setDescription('UI shortcut to select all snapshot-related options - not written to the database.'), |
802
|
|
|
|
803
|
|
|
// The Main.CanRestoreMembers |
804
|
|
|
$this |
805
|
|
|
->buildPermissionField('CanRestoreGroups', 'CanRestoreMembers', $groups, $members) |
806
|
|
|
->setTitle('Who can restore?') |
807
|
|
|
->setDescription('Groups or Users who can restore archives from Deploynaut into this environment'), |
808
|
|
|
|
809
|
|
|
// The Main.CanBackupMembers |
810
|
|
|
$this |
811
|
|
|
->buildPermissionField('CanBackupGroups', 'CanBackupMembers', $groups, $members) |
812
|
|
|
->setTitle('Who can backup?') |
813
|
|
|
->setDescription('Groups or Users who can backup archives from this environment into Deploynaut'), |
814
|
|
|
|
815
|
|
|
// The Main.ArchiveDeleters |
816
|
|
|
$this |
817
|
|
|
->buildPermissionField('ArchiveDeleterGroups', 'ArchiveDeleters', $groups, $members) |
818
|
|
|
->setTitle('Who can delete?') |
819
|
|
|
->setDescription("Groups or Users who can delete archives from this environment's staging area."), |
820
|
|
|
|
821
|
|
|
// The Main.ArchiveUploaders |
822
|
|
|
$this |
823
|
|
|
->buildPermissionField('ArchiveUploaderGroups', 'ArchiveUploaders', $groups, $members) |
824
|
|
|
->setTitle('Who can upload?') |
825
|
|
|
->setDescription( |
826
|
|
|
'Users who can upload archives linked to this environment into Deploynaut.<br />' . |
827
|
|
|
'Linking them to an environment allows limiting download permissions (see below).' |
828
|
|
|
), |
829
|
|
|
|
830
|
|
|
// The Main.ArchiveDownloaders |
831
|
|
|
$this |
832
|
|
|
->buildPermissionField('ArchiveDownloaderGroups', 'ArchiveDownloaders', $groups, $members) |
833
|
|
|
->setTitle('Who can download?') |
834
|
|
|
->setDescription(<<<PHP |
835
|
|
|
Users who can download archives from this environment to their computer.<br /> |
836
|
|
|
Since this implies access to the snapshot, it is also a prerequisite for restores |
837
|
|
|
to other environments, alongside the "Who can restore" permission.<br> |
838
|
|
|
Should include all users with upload permissions, otherwise they can't download |
839
|
|
|
their own uploads. |
840
|
|
|
PHP |
841
|
|
|
) |
842
|
|
|
|
843
|
|
|
]); |
844
|
|
|
|
845
|
|
|
// The Main.DeployConfig |
846
|
|
|
if ($this->Project()->exists()) { |
847
|
|
|
$this->setDeployConfigurationFields($fields); |
848
|
|
|
} |
849
|
|
|
|
850
|
|
|
// The DataArchives |
851
|
|
|
$dataArchiveConfig = GridFieldConfig_RecordViewer::create(); |
852
|
|
|
$dataArchiveConfig->removeComponentsByType('GridFieldAddNewButton'); |
853
|
|
|
if (class_exists('GridFieldBulkManager')) { |
854
|
|
|
$dataArchiveConfig->addComponent(new GridFieldBulkManager()); |
855
|
|
|
} |
856
|
|
|
$dataArchive = GridField::create('DataArchives', 'Data Archives', $this->DataArchives(), $dataArchiveConfig); |
857
|
|
|
$fields->addFieldToTab('Root.DataArchive', $dataArchive); |
858
|
|
|
|
859
|
|
|
// Deployments |
860
|
|
|
$deploymentsConfig = GridFieldConfig_RecordEditor::create(); |
861
|
|
|
$deploymentsConfig->removeComponentsByType('GridFieldAddNewButton'); |
862
|
|
|
if (class_exists('GridFieldBulkManager')) { |
863
|
|
|
$deploymentsConfig->addComponent(new GridFieldBulkManager()); |
864
|
|
|
} |
865
|
|
|
$deployments = GridField::create('Deployments', 'Deployments', $this->Deployments(), $deploymentsConfig); |
866
|
|
|
$fields->addFieldToTab('Root.Deployments', $deployments); |
867
|
|
|
|
868
|
|
|
Requirements::javascript('deploynaut/javascript/environment.js'); |
869
|
|
|
|
870
|
|
|
// Add actions |
871
|
|
|
$action = new FormAction('check', 'Check Connection'); |
872
|
|
|
$action->setUseButtonTag(true); |
873
|
|
|
$dataURL = Director::absoluteBaseURL() . 'naut/api/' . $this->Project()->Name . '/' . $this->Name . '/ping'; |
874
|
|
|
$action->setAttribute('data-url', $dataURL); |
875
|
|
|
$fields->insertBefore($action, 'Name'); |
|
|
|
|
876
|
|
|
|
877
|
|
|
// Allow extensions |
878
|
|
|
$this->extend('updateCMSFields', $fields); |
879
|
|
|
return $fields; |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
/** |
883
|
|
|
*/ |
884
|
|
|
public function onBeforeWrite() { |
885
|
|
|
parent::onBeforeWrite(); |
886
|
|
|
if ($this->Name && $this->Name . '.rb' != $this->Filename) { |
887
|
|
|
$this->Filename = $this->Name . '.rb'; |
888
|
|
|
} |
889
|
|
|
$this->checkEnvironmentPath(); |
890
|
|
|
$this->writeConfigFile(); |
891
|
|
|
} |
892
|
|
|
|
893
|
|
|
public function onAfterWrite() { |
894
|
|
|
parent::onAfterWrite(); |
895
|
|
|
|
896
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UAT) { |
897
|
|
|
$conflicting = DNEnvironment::get() |
|
|
|
|
898
|
|
|
->filter('ProjectID', $this->ProjectID) |
899
|
|
|
->filter('Usage', $this->Usage) |
900
|
|
|
->exclude('ID', $this->ID); |
901
|
|
|
|
902
|
|
|
foreach ($conflicting as $otherEnvironment) { |
903
|
|
|
$otherEnvironment->Usage = self::UNSPECIFIED; |
904
|
|
|
$otherEnvironment->write(); |
905
|
|
|
} |
906
|
|
|
} |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
/** |
910
|
|
|
* Delete any related config files |
911
|
|
|
*/ |
912
|
|
|
public function onAfterDelete() { |
913
|
|
|
parent::onAfterDelete(); |
914
|
|
|
|
915
|
|
|
// Create a basic new environment config from a template |
916
|
|
|
if ($this->config()->get('allow_web_editing') && $this->envFileExists()) { |
917
|
|
|
unlink($this->getConfigFilename()); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
$deployments = $this->Deployments(); |
921
|
|
|
if ($deployments && $deployments->exists()) { |
922
|
|
|
foreach ($deployments as $deployment) { |
923
|
|
|
$deployment->delete(); |
924
|
|
|
} |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
$archives = $this->DataArchives(); |
928
|
|
|
if ($archives && $archives->exists()) { |
929
|
|
|
foreach ($archives as $archive) { |
930
|
|
|
$archive->delete(); |
931
|
|
|
} |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
$create = $this->CreateEnvironment(); |
|
|
|
|
935
|
|
|
if ($create && $create->exists()) { |
936
|
|
|
$create->delete(); |
937
|
|
|
} |
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
/** |
941
|
|
|
* Returns the path to the ruby config file |
942
|
|
|
* |
943
|
|
|
* @return string |
944
|
|
|
*/ |
945
|
|
|
public function getConfigFilename() { |
946
|
|
|
if (!$this->Project()->exists()) { |
947
|
|
|
return ''; |
948
|
|
|
} |
949
|
|
|
if (!$this->Filename) { |
950
|
|
|
return ''; |
951
|
|
|
} |
952
|
|
|
return $this->DNData()->getEnvironmentDir() . '/' . $this->Project()->Name . '/' . $this->Filename; |
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
/** |
956
|
|
|
* Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
957
|
|
|
* {@link ArrayData} object structure, so that values can be used in templates. |
958
|
|
|
* |
959
|
|
|
* @param array $array The (single- or multi-dimensional) array to convert |
960
|
|
|
* @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
961
|
|
|
* isn't an array. |
962
|
|
|
*/ |
963
|
|
|
public static function array_to_viewabledata($array) { |
964
|
|
|
// Don't transform non-arrays |
965
|
|
|
if (!is_array($array)) { |
966
|
|
|
return $array; |
967
|
|
|
} |
968
|
|
|
|
969
|
|
|
// Figure out whether this is indexed or associative |
970
|
|
|
$keys = array_keys($array); |
971
|
|
|
$assoc = ($keys != array_keys($keys)); |
972
|
|
|
if ($assoc) { |
973
|
|
|
// Treat as viewable data |
974
|
|
|
$data = new ArrayData([]); |
975
|
|
|
foreach ($array as $key => $value) { |
976
|
|
|
$data->setField($key, self::array_to_viewabledata($value)); |
977
|
|
|
} |
978
|
|
|
return $data; |
979
|
|
|
} else { |
980
|
|
|
// Treat this as basic non-associative list |
981
|
|
|
$list = new ArrayList(); |
982
|
|
|
foreach ($array as $value) { |
983
|
|
|
$list->push(self::array_to_viewabledata($value)); |
984
|
|
|
} |
985
|
|
|
return $list; |
986
|
|
|
} |
987
|
|
|
} |
988
|
|
|
|
989
|
|
|
/** |
990
|
|
|
* Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
991
|
|
|
* if an old deployment is stuck. |
992
|
|
|
* |
993
|
|
|
* @return DataList |
994
|
|
|
*/ |
995
|
|
|
public function runningDeployments() { |
996
|
|
|
return DNDeployment::get() |
997
|
|
|
->filter([ |
998
|
|
|
'EnvironmentID' => $this->ID, |
999
|
|
|
'State' => [ |
1000
|
|
|
DNDeployment::STATE_QUEUED, |
1001
|
|
|
DNDeployment::STATE_DEPLOYING, |
1002
|
|
|
DNDeployment::STATE_ABORTING |
1003
|
|
|
], |
1004
|
|
|
'Created:GreaterThan' => strtotime('-1 hour') |
1005
|
|
|
]); |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
/** |
1009
|
|
|
* @param string $sha |
1010
|
|
|
* @return array |
1011
|
|
|
*/ |
1012
|
|
|
protected function getCommitData($sha) { |
1013
|
|
|
try { |
1014
|
|
|
$repo = $this->Project()->getRepository(); |
1015
|
|
|
if ($repo !== false) { |
1016
|
|
|
$commit = new \Gitonomy\Git\Commit($repo, $sha); |
1017
|
|
|
return [ |
1018
|
|
|
'AuthorName' => (string) Convert::raw2xml($commit->getAuthorName()), |
1019
|
|
|
'AuthorEmail' => (string) Convert::raw2xml($commit->getAuthorEmail()), |
1020
|
|
|
'Message' => (string) Convert::raw2xml($this->getCommitMessage($commit)), |
1021
|
|
|
'ShortHash' => Convert::raw2xml($commit->getFixedShortHash(8)), |
1022
|
|
|
'Hash' => Convert::raw2xml($commit->getHash()) |
1023
|
|
|
]; |
1024
|
|
|
} |
1025
|
|
|
} catch (\Gitonomy\Git\Exception\ReferenceNotFoundException $exc) { |
1026
|
|
|
SS_Log::log($exc, SS_Log::WARN); |
1027
|
|
|
} |
1028
|
|
|
return [ |
1029
|
|
|
'AuthorName' => '(unknown)', |
1030
|
|
|
'AuthorEmail' => '(unknown)', |
1031
|
|
|
'Message' => '(unknown)', |
1032
|
|
|
'ShortHash' => $sha, |
1033
|
|
|
'Hash' => '(unknown)', |
1034
|
|
|
]; |
1035
|
|
|
} |
1036
|
|
|
|
1037
|
|
|
/** |
1038
|
|
|
* Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
1039
|
|
|
* |
1040
|
|
|
* @param string $groupField Group field name |
1041
|
|
|
* @param string $memberField Member field name |
1042
|
|
|
* @param array $groups List of groups |
1043
|
|
|
* @param array $members List of members |
1044
|
|
|
* @return FieldGroup |
1045
|
|
|
*/ |
1046
|
|
|
protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
1047
|
|
|
return FieldGroup::create( |
1048
|
|
|
ListboxField::create($groupField, false, $groups) |
1049
|
|
|
->setMultiple(true) |
1050
|
|
|
->setAttribute('data-placeholder', 'Groups') |
1051
|
|
|
->setAttribute('placeholder', 'Groups') |
1052
|
|
|
->setAttribute('style', 'width: 400px;'), |
1053
|
|
|
|
1054
|
|
|
ListboxField::create($memberField, false, $members) |
1055
|
|
|
->setMultiple(true) |
1056
|
|
|
->setAttribute('data-placeholder', 'Members') |
1057
|
|
|
->setAttribute('placeholder', 'Members') |
1058
|
|
|
->setAttribute('style', 'width: 400px;') |
1059
|
|
|
); |
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
/** |
1063
|
|
|
* @param FieldList $fields |
1064
|
|
|
*/ |
1065
|
|
|
protected function setDeployConfigurationFields(&$fields) { |
1066
|
|
|
if (!$this->config()->get('allow_web_editing')) { |
1067
|
|
|
return; |
1068
|
|
|
} |
1069
|
|
|
|
1070
|
|
|
if ($this->envFileExists()) { |
1071
|
|
|
$deployConfig = new TextareaField('DeployConfig', 'Deploy config', $this->getEnvironmentConfig()); |
1072
|
|
|
$deployConfig->setRows(40); |
1073
|
|
|
$fields->insertAfter($deployConfig, 'Filename'); |
|
|
|
|
1074
|
|
|
return; |
1075
|
|
|
} |
1076
|
|
|
|
1077
|
|
|
$warning = 'Warning: This environment doesn\'t have deployment configuration.'; |
1078
|
|
|
$noDeployConfig = new LabelField('noDeployConfig', $warning); |
1079
|
|
|
$noDeployConfig->addExtraClass('message warning'); |
1080
|
|
|
$fields->insertAfter($noDeployConfig, 'Filename'); |
|
|
|
|
1081
|
|
|
$createConfigField = new CheckboxField('CreateEnvConfig', 'Create Config'); |
1082
|
|
|
$createConfigField->setDescription('Would you like to create the capistrano deploy configuration?'); |
1083
|
|
|
$fields->insertAfter($createConfigField, 'noDeployConfig'); |
|
|
|
|
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
/** |
1087
|
|
|
* Ensure that environment paths are setup on the local filesystem |
1088
|
|
|
*/ |
1089
|
|
|
protected function checkEnvironmentPath() { |
1090
|
|
|
// Create folder if it doesn't exist |
1091
|
|
|
$configDir = dirname($this->getConfigFilename()); |
1092
|
|
|
if (!file_exists($configDir) && $configDir) { |
1093
|
|
|
mkdir($configDir, 0777, true); |
1094
|
|
|
} |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* Write the deployment config file to filesystem |
1099
|
|
|
*/ |
1100
|
|
|
protected function writeConfigFile() { |
1101
|
|
|
if (!$this->config()->get('allow_web_editing')) { |
1102
|
|
|
return; |
1103
|
|
|
} |
1104
|
|
|
|
1105
|
|
|
// Create a basic new environment config from a template |
1106
|
|
|
if (!$this->envFileExists() |
1107
|
|
|
&& $this->Filename |
1108
|
|
|
&& $this->CreateEnvConfig |
|
|
|
|
1109
|
|
|
) { |
1110
|
|
|
$templateFile = $this->config()->template_file ?: BASE_PATH . '/deploynaut/environment.template'; |
1111
|
|
|
file_put_contents($this->getConfigFilename(), file_get_contents($templateFile)); |
1112
|
|
|
} else if ($this->envFileExists() && $this->DeployConfig) { |
|
|
|
|
1113
|
|
|
file_put_contents($this->getConfigFilename(), $this->DeployConfig); |
|
|
|
|
1114
|
|
|
} |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
/** |
1118
|
|
|
* @return string |
1119
|
|
|
*/ |
1120
|
|
|
protected function getEnvironmentConfig() { |
1121
|
|
|
if (!$this->envFileExists()) { |
1122
|
|
|
return ''; |
1123
|
|
|
} |
1124
|
|
|
return file_get_contents($this->getConfigFilename()); |
1125
|
|
|
} |
1126
|
|
|
|
1127
|
|
|
/** |
1128
|
|
|
* @return boolean |
1129
|
|
|
*/ |
1130
|
|
|
protected function envFileExists() { |
1131
|
|
|
if (!$this->getConfigFilename()) { |
1132
|
|
|
return false; |
1133
|
|
|
} |
1134
|
|
|
return file_exists($this->getConfigFilename()); |
1135
|
|
|
} |
1136
|
|
|
|
1137
|
|
|
protected function validate() { |
1138
|
|
|
$result = parent::validate(); |
1139
|
|
|
$backend = $this->Backend(); |
1140
|
|
|
|
1141
|
|
|
if (strcasecmp('test', $this->Name) === 0 && get_class($backend) == 'CapistranoDeploymentBackend') { |
1142
|
|
|
$result->error('"test" is not a valid environment name when using Capistrano backend.'); |
1143
|
|
|
} |
1144
|
|
|
|
1145
|
|
|
return $result; |
1146
|
|
|
} |
1147
|
|
|
|
1148
|
|
|
} |
1149
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.