DeployKubernetesJob::newDeployOnGCloud()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 29
nc 4
nop 5
dl 0
loc 36
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
4
5
use TheAentMachine\AentGitLabCI\Context\BaseGitLabCIContext;
6
use TheAentMachine\AentGitLabCI\Exception\JobException;
7
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel;
8
9
final class DeployKubernetesJob extends AbstractDeployJob
10
{
11
    /**
12
     * @param string $k8sDirName
13
     * @param BaseGitLabCIContext $context
14
     * @param BranchesModel $branchesModel
15
     * @param string $cleanupJobIdentifier
16
     * @param bool $isManual
17
     * @return DeployKubernetesJob
18
     * @throws JobException
19
     */
20
    public static function newDeployOnGCloud(string $k8sDirName, BaseGitLabCIContext $context, BranchesModel $branchesModel, string $cleanupJobIdentifier, bool $isManual): self
21
    {
22
        $self = new self($context->getEnvironmentName());
23
        $self->image = 'thecodingmachine/k8s-gitlabci:latest';
24
        $self->variables = [
25
            'GCLOUD_SERVICE_KEY_BASE64' => 'You should put this value in your secrets CI variables!',
26
            'GCLOUD_PROJECT' => 'You should put this value in your secrets CI variables!',
27
            'GKE_CLUSTER' => 'You should put this value in your secrets CI variables!',
28
            'ZONE' => 'You should put this value in your secrets CI variables!',
29
            'KUBECONFIG' => '/root/.kube/config',
30
            'K8S_DIRNAME' => $k8sDirName,
31
        ];
32
        $self->script = [
33
            'echo $GCLOUD_SERVICE_KEY_BASE64 | base64 -d > /secret.json',
34
            'gcloud auth activate-service-account --key-file /secret.json',
35
            'gcloud config set project $GCLOUD_PROJECT',
36
            'gcloud container clusters get-credentials $GKE_CLUSTER --zone $ZONE --project $GCLOUD_PROJECT',
37
            'kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true',
38
            'kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
39
            'cd ${K8S_DIRNAME}',
40
            'for template_file in $(find . -type f -name "*.template"); do sed -e "s/#ENVIRONMENT#/${CI_COMMIT_REF_SLUG}/g" $template_file > ${template_file::-9}; done',
41
            'for yml_file in $(find . -type f -name "*.yml" -or -name "*.yaml"); do kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} apply -f ${yml_file}; done'
42
        ];
43
        foreach ($branchesModel->getBranches() as $branch) {
44
            $self->addOnly($branch);
45
        }
46
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
47
            $self->addExcept($branch);
48
        }
49
        $self->environment = [
50
            'name' => 'review/$CI_COMMIT_REF_NAME',
51
            'url' => '# updates this with your environment URL',
52
            'on_stop' => $cleanupJobIdentifier
53
        ];
54
        $self->manual = $isManual;
55
        return $self;
56
    }
57
58
    /**
59
     * @param string $k8sDirName
60
     * @param BaseGitLabCIContext $context
61
     * @param BranchesModel $branchesModel
62
     * @param string $cleanupJobIdentifier
63
     * @param bool $isManual
64
     * @return DeployKubernetesJob
65
     * @throws JobException
66
     */
67
    public static function newDeployOnRancher(string $k8sDirName, BaseGitLabCIContext $context, BranchesModel $branchesModel, string $cleanupJobIdentifier, bool $isManual): self
68
    {
69
        $self = new self($context->getEnvironmentName());
70
        $self->image = 'thecodingmachine/k8s-gitlabci:latest';
71
        $self->variables = [
72
            'KUBECONFIG' => '/root/.kube/config',
73
            'K8S_DIRNAME' => $k8sDirName,
74
        ];
75
        $self->script = [
76
            'mkdir ~/.kube',
77
            'echo "$KUBE_CONFIG" > ~/.kube/config',
78
            'kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true',
79
            'kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
80
            'cd ${K8S_DIRNAME}',
81
            // TODO: add a docker-registry secret?
82
            'for template_file in $(find . -type f -name "*.template"); do sed -e "s/#ENVIRONMENT#/${CI_COMMIT_REF_SLUG}/g" $template_file > ${template_file::-9}; done',
83
            'for yml_file in $(find . -type f -name "*.yml" -or -name "*.yaml"); do kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} apply -f ${yml_file}; done'
84
        ];
85
        foreach ($branchesModel->getBranches() as $branch) {
86
            $self->addOnly($branch);
87
        }
88
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
89
            $self->addExcept($branch);
90
        }
91
        $self->environment = [
92
            'name' => 'review/$CI_COMMIT_REF_NAME',
93
            'url' => '# updates this with your environment URL',
94
            'on_stop' => $cleanupJobIdentifier
95
        ];
96
        $self->manual = $isManual;
97
        return $self;
98
    }
99
}
100