DockerComposeDeployJobEvent::getRemoteIP()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\Event;
4
5
use Safe\Exceptions\FilesystemException;
6
use TheAentMachine\Aent\Context\Context;
7
use TheAentMachine\Aent\Event\CI\AbstractCIDockerComposeDeployJobEvent;
8
use TheAentMachine\AentGitLabCI\Context\BaseGitLabCIContext;
9
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException;
10
use TheAentMachine\AentGitLabCI\Exception\JobException;
11
use TheAentMachine\AentGitLabCI\GitLabCI\GitLabCIFile;
12
use TheAentMachine\AentGitLabCI\GitLabCI\Job\DeployDockerComposeJob;
13
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel;
14
use TheAentMachine\Exception\MissingEnvironmentVariableException;
15
use TheAentMachine\Prompt\Helper\ValidatorHelper;
16
17
final class DockerComposeDeployJobEvent extends AbstractCIDockerComposeDeployJobEvent
18
{
19
    /**
20
     * @param string $dockerComposeFilename
21
     * @return void
22
     * @throws JobException
23
     * @throws GitLabCIFileException
24
     * @throws MissingEnvironmentVariableException
25
     * @throws FilesystemException
26
     */
27
    protected function addDeployJob(string $dockerComposeFilename): void
28
    {
29
        $this->output->writeln("\n🦊 Currently, we only support a deploy on a remote server and on a single branch when using Docker Compose as orchestrator!");
30
        $this->output->writeln("Important: If you're using a dot env file, make sure to create it on your remote server!");
31
        $branchesModel = $this->getBranch();
32
        $remoteIP = $this->getRemoteIP();
33
        $remoteUser = $this->getRemoteUser();
34
        $remoteBasePath = $this->getRemoteBasePath();
35
        $isManual = $this->deployManually();
36
        $this->prompt->printAltBlock("GitLab: adding deploy job...");
37
        $context = new BaseGitLabCIContext();
38
        $context->setBranchesModel($branchesModel);
39
        $context->toMetadata();
40
        $job = DeployDockerComposeJob::newDeployOnRemoteServer($dockerComposeFilename, $context, $remoteIP, $remoteUser, $remoteBasePath, $isManual);
41
        $file = new GitLabCIFile();
42
        $file->findOrCreate();
43
        $file->addDeploy($job);
44
    }
45
46
    /**
47
     * @return BranchesModel
48
     * @throws JobException
49
     */
50
    private function getBranch(): BranchesModel
51
    {
52
        $context = Context::fromMetadata();
53
        $text = "\nYour branch";
54
        $helpText = "The branch for which GitLab will deploy your stack and create images from Dockerfiles.";
55
        $default = null;
56
        if ($context->isTest()) {
57
            $default = 'testing';
58
        } elseif ($context->isProduction()) {
59
            $default = 'master';
60
        }
61
        $branchName = $this->prompt->input($text, $helpText, $default, true, ValidatorHelper::getAlphaWithAdditionalCharactersValidator(['_', '.', '-'])) ?? '';
62
        return new BranchesModel([$branchName]);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    private function getRemoteIP(): string
69
    {
70
        $text = "\nIP of your remote server";
71
        $helpText = "The IP of the server where you want to deploy your stack.";
72
        return $this->prompt->input($text, $helpText, null, true, ValidatorHelper::getIPv4Validator()) ?? '';
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    private function getRemoteUser(): string
79
    {
80
        $text = "\nDeploy user";
81
        $helpText = "The username of the user which will deploy over SSH your stack.";
82
        return $this->prompt->input($text, $helpText, null, true, ValidatorHelper::getAlphaWithAdditionalCharactersValidator(['_', '-'])) ?? '';
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    private function getRemoteBasePath(): string
89
    {
90
        $text = "\nAbsolute path on the server";
91
        $helpText = "The absolute path (without trailing \"/\") on the server where your stack will be deployed.";
92
        return $this->prompt->input($text, $helpText, null, true, ValidatorHelper::getAbsolutePathValidator()) ?? '';
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    private function deployManually(): bool
99
    {
100
        $text = "\nDo you want to deploy your stack <info>manually</info>?";
101
        return $this->prompt->confirm($text, null, null, true);
102
    }
103
}
104