|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentGitLabCI\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\FilesystemException; |
|
6
|
|
|
use TheAentMachine\Aent\Event\CI\AbstractCIKubernetesDeployJobEvent; |
|
7
|
|
|
use TheAentMachine\Aent\K8SProvider\Provider; |
|
8
|
|
|
use TheAentMachine\Aent\Payload\CI\KubernetesReplyDeployJobPayload; |
|
9
|
|
|
use TheAentMachine\AentGitLabCI\Context\BaseGitLabCIContext; |
|
10
|
|
|
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException; |
|
11
|
|
|
use TheAentMachine\AentGitLabCI\Exception\JobException; |
|
12
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\GitLabCIFile; |
|
13
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\CleanupKubernetesJob; |
|
14
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\DeployKubernetesJob; |
|
15
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel; |
|
16
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
17
|
|
|
use TheAentMachine\Prompt\Helper\ValidatorHelper; |
|
18
|
|
|
|
|
19
|
|
|
final class KubernetesDeployJobEvent extends AbstractCIKubernetesDeployJobEvent |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string[] */ |
|
22
|
|
|
private $deployBranches = []; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string[] */ |
|
25
|
|
|
private $ignoredBranches = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $directoryName |
|
29
|
|
|
* @param Provider $provider |
|
30
|
|
|
* @return KubernetesReplyDeployJobPayload |
|
31
|
|
|
* @throws FilesystemException |
|
32
|
|
|
* @throws GitLabCIFileException |
|
33
|
|
|
* @throws JobException |
|
34
|
|
|
* @throws MissingEnvironmentVariableException |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function addDeployJob(string $directoryName, Provider $provider): KubernetesReplyDeployJobPayload |
|
37
|
|
|
{ |
|
38
|
|
|
$this->output->writeln("\n🦊 Currently, we only support a deploy on branches when using Kubernetes as orchestrator!"); |
|
39
|
|
|
$branchesModel = $this->getBranches(); |
|
40
|
|
|
$isManual = $this->deployManually(); |
|
41
|
|
|
$this->prompt->printAltBlock("GitLab: adding deploy and cleanup jobs..."); |
|
42
|
|
|
$context = new BaseGitLabCIContext(); |
|
43
|
|
|
$context->setBranchesModel($branchesModel); |
|
44
|
|
|
$context->toMetadata(); |
|
45
|
|
|
if ($provider->getName() === Provider::GOOGLE_CLOUD) { |
|
46
|
|
|
$cleanupJob = CleanupKubernetesJob::newCleanupForGCloud($context, $branchesModel, $isManual); |
|
47
|
|
|
} else { |
|
48
|
|
|
$cleanupJob = CleanupKubernetesJob::newCleanupForRancher($context, $branchesModel, $isManual); |
|
49
|
|
|
} |
|
50
|
|
|
if ($provider->getName() === Provider::GOOGLE_CLOUD) { |
|
51
|
|
|
$deployJob = DeployKubernetesJob::newDeployOnGCloud($directoryName, $context, $branchesModel, $cleanupJob->getJobName(), $isManual); |
|
52
|
|
|
} else { |
|
53
|
|
|
$deployJob = DeployKubernetesJob::newDeployOnRancher($directoryName, $context, $branchesModel, $cleanupJob->getJobName(), $isManual); |
|
54
|
|
|
} |
|
55
|
|
|
$file = new GitLabCIFile(); |
|
56
|
|
|
$file->findOrCreate(); |
|
57
|
|
|
$file->addDeploy($deployJob); |
|
58
|
|
|
$file->addCleanUp($cleanupJob); |
|
59
|
|
|
return new KubernetesReplyDeployJobPayload(!$branchesModel->isSingleBranch()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return BranchesModel |
|
64
|
|
|
* @throws JobException |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getBranches(): BranchesModel |
|
67
|
|
|
{ |
|
68
|
|
|
$text = "\nBranch for deploy (keep empty if you don't want to use a specific branch)"; |
|
69
|
|
|
$helpText = "A branch for which GitLab will deploy your stack and create images from Dockerfiles."; |
|
70
|
|
|
$branchName = $this->getDeployBranch($text, $helpText); |
|
71
|
|
|
if (!empty($branchName)) { |
|
72
|
|
|
$this->deployBranches[] = $branchName; |
|
73
|
|
|
$text = $text = "\nBranch (keep empty to skip)"; |
|
|
|
|
|
|
74
|
|
|
do { |
|
75
|
|
|
$branchName = $this->getDeployBranch($text, $helpText); |
|
76
|
|
|
if (!empty($branchName)) { |
|
77
|
|
|
$this->deployBranches[] = $branchName; |
|
78
|
|
|
} |
|
79
|
|
|
} while (!empty($branchName)); |
|
80
|
|
|
} |
|
81
|
|
|
$text = "\nBranch to ignore (keep empty to skip)"; |
|
82
|
|
|
$helpText = "A branch for which GitLab will NOT deploy your stack and create images from Dockerfiles."; |
|
83
|
|
|
$branchName = $this->getIgnoredBranch($text, $helpText); |
|
84
|
|
|
if (!empty($branchName)) { |
|
85
|
|
|
$this->ignoredBranches[] = $branchName; |
|
86
|
|
|
do { |
|
87
|
|
|
$branchName = $this->getIgnoredBranch($text, $helpText); |
|
88
|
|
|
if (!empty($branchName)) { |
|
89
|
|
|
$this->ignoredBranches[] = $branchName; |
|
90
|
|
|
} |
|
91
|
|
|
} while (!empty($branchName)); |
|
92
|
|
|
} |
|
93
|
|
|
if (empty($this->deployBranches)) { |
|
94
|
|
|
$this->deployBranches = [ 'branches' ]; |
|
95
|
|
|
} |
|
96
|
|
|
return new BranchesModel($this->deployBranches, $this->ignoredBranches); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $text |
|
101
|
|
|
* @param string $helpText |
|
102
|
|
|
* @return null|string |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getDeployBranch(string $text, string $helpText): ?string |
|
105
|
|
|
{ |
|
106
|
|
|
$validator = ValidatorHelper::merge( |
|
107
|
|
|
ValidatorHelper::getFuncShouldNotReturnTrueValidator([$this, 'doesDeployBranchExist'], 'Deploy branch "%s" does already exist!'), |
|
108
|
|
|
ValidatorHelper::getAlphaWithAdditionalCharactersValidator(['_', '.', '-']) |
|
109
|
|
|
); |
|
110
|
|
|
return $this->prompt->input($text, $helpText, null, false, $validator); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $text |
|
115
|
|
|
* @param string $helpText |
|
116
|
|
|
* @return null|string |
|
117
|
|
|
*/ |
|
118
|
|
|
private function getIgnoredBranch(string $text, string $helpText): ?string |
|
119
|
|
|
{ |
|
120
|
|
|
$validator = ValidatorHelper::merge( |
|
121
|
|
|
ValidatorHelper::getFuncShouldNotReturnTrueValidator([$this, 'doesIgnoredBranchExist'], 'Branch "%s" is already ignored or used for deploy!'), |
|
122
|
|
|
ValidatorHelper::getAlphaWithAdditionalCharactersValidator(['_', '.', '-']) |
|
123
|
|
|
); |
|
124
|
|
|
return $this->prompt->input($text, $helpText, null, false, $validator); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string|null $branchName |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function doesDeployBranchExist(?string $branchName): bool |
|
132
|
|
|
{ |
|
133
|
|
|
if (empty($branchName)) { |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
foreach ($this->deployBranches as $deployBranchName) { |
|
137
|
|
|
if ($deployBranchName === $branchName) { |
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string|null $branchName |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function doesIgnoredBranchExist(?string $branchName): bool |
|
149
|
|
|
{ |
|
150
|
|
|
$resp = $this->doesDeployBranchExist($branchName); |
|
151
|
|
|
if ($resp) { |
|
152
|
|
|
return $resp; |
|
153
|
|
|
} |
|
154
|
|
|
foreach ($this->ignoredBranches as $ignoredBranchName) { |
|
155
|
|
|
if ($ignoredBranchName === $branchName) { |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
private function deployManually(): bool |
|
166
|
|
|
{ |
|
167
|
|
|
$text = "\nDo you want to deploy your stack <info>manually</info>?"; |
|
168
|
|
|
return $this->prompt->confirm($text, null, null, true); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|