Completed
Pull Request — master (#1)
by Jindun
04:17
created

NewDeployKubernetesJobEventCommand::getEventName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Question\GitLabCICommonQuestions;
13
use TheAentMachine\Aenthill\CommonEvents;
14
use TheAentMachine\Aenthill\CommonMetadata;
15
use TheAentMachine\Aenthill\Manifest;
16
use TheAentMachine\Command\AbstractEventCommand;
17
use TheAentMachine\Exception\ManifestException;
18
use TheAentMachine\Exception\MissingEnvironmentVariableException;
19
20
final class NewDeployKubernetesJobEventCommand extends AbstractEventCommand
21
{
22
    /** @var string */
23
    private $envName;
24
25
    /** @var string */
26
    private $registryDomainName;
27
28
    /** @var string */
29
    private $k8sPathname;
30
31
    protected function getEventName(): string
32
    {
33
        return CommonEvents::NEW_DEPLOY_KUBERNETES_JOB_EVENT;
34
    }
35
36
    /**
37
     * @param null|string $payload
38
     * @return null|string
39
     * @throws GitLabCIFileException
40
     * @throws JobException
41
     * @throws ManifestException
42
     * @throws MissingEnvironmentVariableException
43
     * @throws PayloadException
44
     */
45
    protected function executeEvent(?string $payload): ?string
46
    {
47
        $aentHelper = $this->getAentHelper();
48
49
        $aentHelper->title('GitLab CI: adding a deploy stage');
50
51
        if (empty($payload)) {
52
            throw PayloadException::missingKubernetesPathname();
53
        }
54
55
        $this->envName = Manifest::mustGetMetadata(CommonMetadata::ENV_NAME_KEY);
56
        $this->registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY);
57
        $this->k8sPathname = $payload;
58
59
        $this->output->writeln("🦊×☸️ Kubernetes path: <info>$this->k8sPathname</info>");
60
        $aentHelper->spacer();
61
62
        $deployJob = $this->askForDeployType();
63
        $cleanUpJob = $this->createCleanupOnGCloud();
64
65
        $file = new GitLabCIFile();
66
        $file->findOrCreate();
67
        $file->addDeploy($deployJob);
68
        $file->addCleanUp($cleanUpJob);
69
70
71
        $this->output->writeln('🦊 <info>' . GitLabCIFile::DEFAULT_FILENAME . '</info> has been successfully updated!');
72
73
        return null;
74
    }
75
76
    /**
77
     * @return DeployKubernetesJob
78
     * @throws JobException
79
     */
80
    private function askForDeployType(): DeployKubernetesJob
81
    {
82
        $deployType = Manifest::getMetadata(Metadata::DEPLOY_TYPE_KEY);
83
84
        if (null === $deployType) {
85
            $deployType = $this->getAentHelper()
86
                ->choiceQuestion('Select on which provider you want to deploy your stack', [
87
                    Metadata::DEPLOY_TYPE_GCLOUD
88
                ])
89
                ->ask();
90
        }
91
92
        switch ($deployType) {
93
            case Metadata::DEPLOY_TYPE_GCLOUD:
94
                return $this->createDeployOnGCloud();
95
            default:
96
                throw JobException::unknownDeployType($deployType);
97
        }
98
    }
99
100
    /**
101
     * @return DeployKubernetesJob
102
     * @throws JobException
103
     */
104
    private function createDeployOnGCloud(): DeployKubernetesJob
105
    {
106
        Manifest::addMetadata(Metadata::DEPLOY_TYPE_KEY, Metadata::DEPLOY_TYPE_GCLOUD);
107
108
        $gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper());
109
110
        $remoteBasePath = $gitlabCICommonQuestions->askForRemoteBasePath();
111
        $isManual = $gitlabCICommonQuestions->askForManual();
112
113
        return DeployKubernetesJob::newDeployOnGCloud(
114
            $this->envName,
115
            $remoteBasePath,
116
            $isManual
117
        );
118
    }
119
120
    /**
121
     * @return CleanupKubernetesJob
122
     * @throws JobException
123
     * @throws ManifestException
124
     */
125
    private function createCleanupOnGCloud(): CleanupKubernetesJob
126
    {
127
        $gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper());
128
        $projectGroup = Manifest::mustGetMetadata(Metadata::PROJECT_GROUP_KEY);
129
        $projectName = Manifest::mustGetMetadata(Metadata::PROJECT_NAME_KEY);
130
        $isManual = $gitlabCICommonQuestions->askForManual();
131
132
        return CleanupKubernetesJob::newCleanup(
133
            $this->envName,
134
            $projectGroup,
135
            $projectName,
136
            $isManual
137
        );
138
    }
139
}
140