Completed
Push — master ( f1cf0f...b33238 )
by Jindun
10s
created

DeployKubernetesJob::newDeployOnRancher()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 29
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 4
1
<?php
2
3
4
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
5
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 $identifier
13
     * @param string $k8sDirName
14
     * @param BranchesModel $branchesModel
15
     * @param bool $isManual
16
     * @return DeployKubernetesJob
17
     * @throws JobException
18
     */
19
    public static function newDeployOnGCloud(string $identifier, string $k8sDirName, BranchesModel $branchesModel, bool $isManual): self
20
    {
21
        $self = new self($identifier);
22
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
            'chmod +x /kubectl',
38
            '/kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true',
39
            '/kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
40
            'cd ${K8S_DIRNAME}',
41
            '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',
42
            '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'
43
        ];
44
45
        foreach ($branchesModel->getBranches() as $branch) {
46
            $self->addOnly($branch);
47
        }
48
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
49
            $self->addExcept($branch);
50
        }
51
        $self->manual = $isManual;
52
53
        return $self;
54
    }
55
56
    /**
57
     * @param string $identifier
58
     * @param string $k8sDirName
59
     * @param BranchesModel $branchesModel
60
     * @param bool $isManual
61
     * @return DeployKubernetesJob
62
     * @throws JobException
63
     */
64
    public static function newDeployOnRancher(string $identifier, string $k8sDirName, BranchesModel $branchesModel, bool $isManual): self
65
    {
66
        $self = new self($identifier);
67
68
        $self->image = 'lwolf/kubectl_deployer:latest';
69
        $self->variables = [
70
            'KUBECONFIG' => '/root/.kube/config',
71
            'K8S_DIRNAME' => $k8sDirName,
72
        ];
73
        $self->script = [
74
            'mkdir ~/.kube',
75
            'echo "$KUBE_CONFIG" > ~/.kube/config',
76
            'kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true',
77
            'kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
78
            'cd ${K8S_DIRNAME}',
79
            // TODO: add a docker-registry secret?
80
            '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',
81
            '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'
82
        ];
83
84
        foreach ($branchesModel->getBranches() as $branch) {
85
            $self->addOnly($branch);
86
        }
87
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
88
            $self->addExcept($branch);
89
        }
90
        $self->manual = $isManual;
91
92
        return $self;
93
    }
94
}
95