/
ClearStalledDeployment.php
122 lines (101 loc) · 3.72 KB
/
ClearStalledDeployment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace REBELinBLUE\Deployer\Console\Commands;
use Illuminate\Console\Command;
use REBELinBLUE\Deployer\Deployment;
use REBELinBLUE\Deployer\Project;
use REBELinBLUE\Deployer\Repositories\Contracts\DeploymentRepositoryInterface;
use REBELinBLUE\Deployer\Repositories\Contracts\ProjectRepositoryInterface;
use REBELinBLUE\Deployer\Repositories\Contracts\ServerLogRepositoryInterface;
use REBELinBLUE\Deployer\ServerLog;
/**
* Clears any stalled deployments so that new deployments can be queued.
*/
class ClearStalledDeployment extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'deployer:cleanup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cancels any stalled deployments so new deployments can be run';
/**
* @var ServerLogRepositoryInterface
*/
private $logRepository;
/**
* @var DeploymentRepositoryInterface
*/
private $deploymentRepository;
/**
* @var ProjectRepositoryInterface
*/
private $projectRepository;
/**
* ClearStalledDeployment constructor.
*
* @param ServerLogRepositoryInterface $logRepository
* @param DeploymentRepositoryInterface $deploymentRepository
* @param ProjectRepositoryInterface $projectRepository
*/
public function __construct(
ServerLogRepositoryInterface $logRepository,
DeploymentRepositoryInterface $deploymentRepository,
ProjectRepositoryInterface $projectRepository
) {
parent::__construct();
$this->logRepository = $logRepository;
$this->deploymentRepository = $deploymentRepository;
$this->projectRepository = $projectRepository;
}
/**
* Execute the console command.
*/
public function handle()
{
$bring_back_up = false;
// Check the app is offline, if not ask the user if it can be brought down
if (!$this->laravel->isDownForMaintenance()) {
$this->error(
'You must switch to maintenance mode before running this command, ' .
'this will ensure that no new deployments are started'
);
if (!$this->confirm(
'Switch to maintenance mode now? The app will switch ' .
'back to live mode once cleanup is finished'
)) {
return -1;
}
$bring_back_up = true;
$this->call('down');
}
$this->cleanupDeployments();
// If we prompted the user to bring the app down, bring it back up
if ($bring_back_up) {
$this->call('up');
}
}
/**
* Cleans up any stalled deployments in the database.
*/
public function cleanupDeployments()
{
// Mark any pending steps as cancelled
$this->logRepository->updateStatusAll(ServerLog::PENDING, ServerLog::CANCELLED);
// Mark any running steps as failed
$this->logRepository->updateStatusAll(ServerLog::RUNNING, ServerLog::FAILED);
// Mark any running/pending deployments as failed
$this->deploymentRepository->updateStatusAll(Deployment::DEPLOYING, Deployment::FAILED);
$this->deploymentRepository->updateStatusAll(Deployment::PENDING, Deployment::FAILED);
// Mark any aborting deployments as aborted
$this->deploymentRepository->updateStatusAll(Deployment::ABORTING, Deployment::ABORTED);
// Mark any deploying/pending projects as failed
$this->projectRepository->updateStatusAll(Project::DEPLOYING, Project::FAILED);
$this->projectRepository->updateStatusAll(Project::PENDING, Project::FAILED);
}
}