|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentGitLabCI\Command; |
|
4
|
|
|
|
|
5
|
|
|
use TheAentMachine\AentGitLabCI\Aenthill\Metadata; |
|
6
|
|
|
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException; |
|
7
|
|
|
use TheAentMachine\AentGitLabCI\Exception\JobException; |
|
8
|
|
|
use TheAentMachine\AentGitLabCI\Exception\PayloadException; |
|
9
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\GitLabCIFile; |
|
10
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\CleanupKubernetesJob; |
|
11
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\DeployKubernetesJob; |
|
12
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel; |
|
13
|
|
|
use TheAentMachine\AentGitLabCI\Question\GitLabCICommonQuestions; |
|
14
|
|
|
use TheAentMachine\Aenthill\CommonEvents; |
|
15
|
|
|
use TheAentMachine\Aenthill\CommonMetadata; |
|
16
|
|
|
use TheAentMachine\Aenthill\Manifest; |
|
17
|
|
|
use TheAentMachine\Command\AbstractEventCommand; |
|
18
|
|
|
use TheAentMachine\Exception\ManifestException; |
|
19
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
20
|
|
|
|
|
21
|
|
|
final class NewDeployKubernetesJobEventCommand extends AbstractEventCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $envName; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $registryDomainName; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $k8sDirName; |
|
31
|
|
|
|
|
32
|
|
|
protected function getEventName(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return CommonEvents::NEW_DEPLOY_KUBERNETES_JOB_EVENT; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param null|string $k8sDirName |
|
39
|
|
|
* @return null|string |
|
40
|
|
|
* @throws GitLabCIFileException |
|
41
|
|
|
* @throws JobException |
|
42
|
|
|
* @throws ManifestException |
|
43
|
|
|
* @throws MissingEnvironmentVariableException |
|
44
|
|
|
* @throws PayloadException |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function executeEvent(?string $k8sDirName): ?string |
|
47
|
|
|
{ |
|
48
|
|
|
$aentHelper = $this->getAentHelper(); |
|
49
|
|
|
|
|
50
|
|
|
$aentHelper->title('GitLab CI: adding a deploy stage'); |
|
51
|
|
|
|
|
52
|
|
|
if (null === $k8sDirName) { |
|
53
|
|
|
throw PayloadException::missingKubernetesPathname(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->envName = Manifest::mustGetMetadata(CommonMetadata::ENV_NAME_KEY); |
|
57
|
|
|
$this->registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY); |
|
58
|
|
|
$this->k8sDirName = $k8sDirName; |
|
59
|
|
|
|
|
60
|
|
|
$this->output->writeln("🦊×☸️ Kubernetes folder: <info>$this->k8sDirName</info>"); |
|
61
|
|
|
$aentHelper->spacer(); |
|
62
|
|
|
|
|
63
|
|
|
$deployJob = $this->askForDeployType(); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$file = new GitLabCIFile(); |
|
67
|
|
|
$file->findOrCreate(); |
|
68
|
|
|
$file->addDeploy($deployJob); |
|
69
|
|
|
switch (Manifest::mustGetMetadata(Metadata::DEPLOY_TYPE_KEY)) { |
|
70
|
|
|
case Metadata::DEPLOY_TYPE_GCLOUD: |
|
71
|
|
|
$cleanUpJob = $this->createCleanupOnGCloud(); |
|
72
|
|
|
$file->addCleanUp($cleanUpJob); |
|
73
|
|
|
break; |
|
74
|
|
|
case Metadata::DEPLOY_TYPE_RANCHER: |
|
75
|
|
|
$cleanUpJob = $this->createCleanupForRancher(); |
|
76
|
|
|
$file->addCleanUp($cleanUpJob); |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->output->writeln('🦊 <info>' . GitLabCIFile::DEFAULT_FILENAME . '</info> has been successfully updated!'); |
|
81
|
|
|
|
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return DeployKubernetesJob |
|
87
|
|
|
* @throws JobException |
|
88
|
|
|
* @throws ManifestException |
|
89
|
|
|
*/ |
|
90
|
|
|
private function askForDeployType(): DeployKubernetesJob |
|
91
|
|
|
{ |
|
92
|
|
|
$deployType = Manifest::getMetadata(Metadata::DEPLOY_TYPE_KEY); |
|
93
|
|
|
|
|
94
|
|
|
if (null === $deployType) { |
|
95
|
|
|
$deployType = $this->getAentHelper() |
|
96
|
|
|
->choiceQuestion('Select on which provider you want to deploy your stack', [ |
|
97
|
|
|
Metadata::DEPLOY_TYPE_GCLOUD, |
|
98
|
|
|
Metadata::DEPLOY_TYPE_RANCHER |
|
99
|
|
|
]) |
|
100
|
|
|
->ask(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
switch ($deployType) { |
|
104
|
|
|
case Metadata::DEPLOY_TYPE_GCLOUD: |
|
105
|
|
|
return $this->createDeployOnGCloud(); |
|
106
|
|
|
case Metadata::DEPLOY_TYPE_RANCHER: |
|
107
|
|
|
return $this->createDeployOnRancher(); |
|
108
|
|
|
default: |
|
109
|
|
|
throw JobException::unknownDeployType($deployType); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return DeployKubernetesJob |
|
115
|
|
|
* @throws JobException |
|
116
|
|
|
* @throws ManifestException |
|
117
|
|
|
*/ |
|
118
|
|
|
private function createDeployOnGCloud(): DeployKubernetesJob |
|
119
|
|
|
{ |
|
120
|
|
|
Manifest::addMetadata(Metadata::DEPLOY_TYPE_KEY, Metadata::DEPLOY_TYPE_GCLOUD); |
|
121
|
|
|
|
|
122
|
|
|
$gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper()); |
|
123
|
|
|
$branchesModel = BranchesModel::newFromMetadata(); |
|
124
|
|
|
$isManual = $gitlabCICommonQuestions->askForManual(); |
|
125
|
|
|
|
|
126
|
|
|
return DeployKubernetesJob::newDeployOnGCloud( |
|
127
|
|
|
$this->envName, |
|
128
|
|
|
$this->k8sDirName, |
|
129
|
|
|
$branchesModel, |
|
130
|
|
|
$isManual |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return CleanupKubernetesJob |
|
136
|
|
|
* @throws JobException |
|
137
|
|
|
* @throws ManifestException |
|
138
|
|
|
*/ |
|
139
|
|
|
private function createCleanupOnGCloud(): CleanupKubernetesJob |
|
140
|
|
|
{ |
|
141
|
|
|
$gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper()); |
|
142
|
|
|
$registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY); |
|
143
|
|
|
$projectGroup = Manifest::mustGetMetadata(Metadata::PROJECT_GROUP_KEY); |
|
144
|
|
|
$projectName = Manifest::mustGetMetadata(Metadata::PROJECT_NAME_KEY); |
|
145
|
|
|
$branchesModel = BranchesModel::newFromMetadata(); |
|
146
|
|
|
$isManual = $gitlabCICommonQuestions->askForManual(); |
|
147
|
|
|
|
|
148
|
|
|
return CleanupKubernetesJob::newCleanupForGCloud( |
|
149
|
|
|
$this->envName, |
|
150
|
|
|
$registryDomainName, |
|
151
|
|
|
$projectGroup, |
|
152
|
|
|
$projectName, |
|
153
|
|
|
$branchesModel, |
|
154
|
|
|
$isManual |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return DeployKubernetesJob |
|
160
|
|
|
* @throws JobException |
|
161
|
|
|
* @throws ManifestException |
|
162
|
|
|
*/ |
|
163
|
|
|
private function createDeployOnRancher(): DeployKubernetesJob |
|
164
|
|
|
{ |
|
165
|
|
|
Manifest::addMetadata(Metadata::DEPLOY_TYPE_KEY, Metadata::DEPLOY_TYPE_RANCHER); |
|
166
|
|
|
|
|
167
|
|
|
$gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper()); |
|
168
|
|
|
$branchesModel = BranchesModel::newFromMetadata(); |
|
169
|
|
|
$isManual = $gitlabCICommonQuestions->askForManual(); |
|
170
|
|
|
|
|
171
|
|
|
return DeployKubernetesJob::newDeployOnRancher( |
|
172
|
|
|
$this->envName, |
|
173
|
|
|
$this->k8sDirName, |
|
174
|
|
|
$branchesModel, |
|
175
|
|
|
$isManual |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return CleanupKubernetesJob |
|
181
|
|
|
* @throws JobException |
|
182
|
|
|
* @throws ManifestException |
|
183
|
|
|
*/ |
|
184
|
|
|
private function createCleanupForRancher(): CleanupKubernetesJob |
|
185
|
|
|
{ |
|
186
|
|
|
$gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper()); |
|
187
|
|
|
$registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY); |
|
188
|
|
|
$projectGroup = Manifest::mustGetMetadata(Metadata::PROJECT_GROUP_KEY); |
|
189
|
|
|
$projectName = Manifest::mustGetMetadata(Metadata::PROJECT_NAME_KEY); |
|
190
|
|
|
$branchesModel = BranchesModel::newFromMetadata(); |
|
191
|
|
|
$isManual = $gitlabCICommonQuestions->askForManual(); |
|
192
|
|
|
|
|
193
|
|
|
return CleanupKubernetesJob::newCleanupForRancher( |
|
194
|
|
|
$this->envName, |
|
195
|
|
|
$registryDomainName, |
|
196
|
|
|
$projectGroup, |
|
197
|
|
|
$projectName, |
|
198
|
|
|
$branchesModel, |
|
199
|
|
|
$isManual |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|