|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Action; |
|
4
|
|
|
|
|
5
|
|
|
use App\Action\AbstractAction; |
|
6
|
|
|
use App\Action\ActionInterface; |
|
7
|
|
|
use App\Action\Context; |
|
8
|
|
|
use App\Facades\Log; |
|
9
|
|
|
use App\Model\Deployment; |
|
10
|
|
|
use Ronanchilvers\Foundation\Config; |
|
11
|
|
|
use Ronanchilvers\Orm\Orm; |
|
12
|
|
|
use Ronanchilvers\Utility\File; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Action to clean up old deployments after deployment |
|
16
|
|
|
* |
|
17
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class CleanupAction extends AbstractAction |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @see \App\Action\ActionInterface::run() |
|
23
|
|
|
*/ |
|
24
|
|
|
public function run(Config $configuration, Context $context) |
|
25
|
|
|
{ |
|
26
|
|
|
$thisDeployment = $context->getOrThrow('deployment', 'Invalid or missing deployment'); |
|
27
|
|
|
$deploymentBaseDir = $context->getOrThrow('deployment_base_dir', 'Invalid or missing deployment dir'); |
|
28
|
|
|
$project = $context->getOrThrow('project', 'Invalid or missing project'); |
|
29
|
|
|
$number = $project->keep_deployments; |
|
30
|
|
|
$deployments = Orm::finder(Deployment::class)->earlierThan( |
|
31
|
|
|
$project, |
|
32
|
|
|
$number |
|
33
|
|
|
); |
|
34
|
|
|
$count = count($deployments); |
|
35
|
|
|
Log::debug(sprintf("Found %d deployments to clean", $count)); |
|
36
|
|
|
if (0 == $count) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
foreach ($deployments as $deployment) { |
|
40
|
|
|
$deploymentDir = File::join($deploymentBaseDir, $deployment->number); |
|
41
|
|
|
Log::debug('Cleaning old deployment', [ |
|
42
|
|
|
'deployment_dir' => $deploymentDir, |
|
43
|
|
|
]); |
|
44
|
|
|
if (!File::rm($deploymentDir)) { |
|
45
|
|
|
if (!is_dir($deploymentDir)) { |
|
46
|
|
|
$method = 'info'; |
|
47
|
|
|
$message = 'Deployment folder not found for deployment ' . $deployment->number; |
|
48
|
|
|
} else { |
|
49
|
|
|
$method = 'error'; |
|
50
|
|
|
$message = 'Unable to remove deployment folder for deployment ' . $deployment->number; |
|
51
|
|
|
} |
|
52
|
|
|
$this->$method( |
|
53
|
|
|
$thisDeployment, |
|
54
|
|
|
[ |
|
55
|
|
|
$message, |
|
56
|
|
|
"Deployment Folder - " . $deploymentDir |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
Log::error($message, [ |
|
60
|
|
|
'deployment_dir' => $deploymentDir |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
if (!$deployment->delete()) { |
|
64
|
|
|
$this->error( |
|
65
|
|
|
$thisDeployment, |
|
66
|
|
|
'Unable to remove database entry for deployment ' . $deployment->number |
|
67
|
|
|
); |
|
68
|
|
|
Log::error('Unable to remove old deployment', [ |
|
69
|
|
|
'deployment' => $deployment->toArray(), |
|
70
|
|
|
]); |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
$this->info( |
|
74
|
|
|
$thisDeployment, |
|
75
|
|
|
[ |
|
76
|
|
|
"Cleaned deployment {$deployment->number} @ {$deploymentDir}" |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|