forked from camptocamp/devops-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
131 lines (120 loc) · 4.57 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
TERRAFORM_VERSION := 0.13.4
DOCKER_HOST := "tcp://127.0.0.1:2376/"
UID_NUMBER := $(shell id -u $$USER)
GID_NUMBER := $(shell id -g $$USER)
DOCKER_GID_NUMBER := $(shell stat -c %g /var/run/docker.sock)
ifneq ($(CI_PROJECT_URL),)
REPO_URL = $(CI_PROJECT_URL)
REMOTE_BRANCH = $(CI_COMMIT_REF_NAME)
else
ifneq ($(GITHUB_SERVER_URL),)
REPO_URL = "$(GITHUB_SERVER_URL)/$(GITHUB_REPOSITORY).git"
REMOTE_BRANCH = $(shell echo $(GITHUB_REF) | rev | cut -f1 -d/ | rev)
else
REMOTE := $(shell git status -sb|sed -Ene's@.. ([^\.]*)\.\.\.([^/]*)/(.*)@\2@p')
REMOTE_BRANCH := $(shell git status -sb|sed -Ene's@.. ([^\.]*)\.\.\.([^/]*)/(.*)@\3@p'|cut -f1 -d' ')
REMOTE_URL := $(shell git remote get-url $(REMOTE))
ifeq ($(findstring "https",$(REMOTE_URL)),)
REPO_URL = "https://github.com/$(shell echo $(REMOTE_URL) | sed -Ene's|[email protected]:([^/]*)/(.*).git|\1/\2|p').git"
else
REPO_URL = $(REMOTE_URL)
endif
endif
endif
CLUSTER_NAME := $(REMOTE_BRANCH)
ARTIFACTS_DIR := "terraform/terraform.tfstate.d/$(CLUSTER_NAME)"
.PHONY: test deploy clean debug
test: deploy
docker run --rm \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v $$PWD:/workdir \
--network k3s-$(CLUSTER_NAME) \
--env BASE_DOMAIN=$(BASE_DOMAIN) \
--env HOME=/tmp \
--entrypoint "" \
--workdir /workdir \
curlimages/curl /workdir/scripts/test.sh
deploy: $(ARTIFACTS_DIR)/kubeconfig.yaml get-base-domain
docker run --rm \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v $$PWD:/workdir \
-v $$PWD/$(ARTIFACTS_DIR)/kubeconfig.yaml:/tmp/.kube/config \
--network k3s-$(CLUSTER_NAME) \
--env HOME=/tmp \
--env KUBECTL_COMMAND=apply \
--env ARGOCD_OPTS="--plaintext --port-forward --port-forward-namespace argocd" \
--env ARTIFACTS_DIR=$(ARTIFACTS_DIR) \
--entrypoint "" \
--workdir /workdir \
argoproj/argocd:v1.7.6 /workdir/scripts/deploy.sh & \
docker run --rm \
--group-add $(DOCKER_GID_NUMBER) \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $$PWD:/workdir \
-v $$PWD/$(ARTIFACTS_DIR)/kubeconfig.yaml:/tmp/.kube/config \
-v $$HOME/.terraformrc:/tmp/.terraformrc \
-v $$HOME/.terraform.d:/tmp/.terraform.d \
--network k3s-$(CLUSTER_NAME) \
--env HOME=/tmp \
--env VAULT_ADDR="https://vault.apps.$(BASE_DOMAIN)" \
--env CLUSTER_NAME=$(CLUSTER_NAME) \
--env ARTIFACTS_DIR=$(ARTIFACTS_DIR) \
--entrypoint "" \
--workdir /workdir \
hashicorp/terraform:$(TERRAFORM_VERSION) /workdir/scripts/configure-vault.sh & \
wait
# Get kubernetes context
$(ARTIFACTS_DIR)/kubeconfig.yaml: $(ARTIFACTS_DIR)/terraform.tfstate get-base-domain
docker cp k3s-server-$(CLUSTER_NAME):/etc/rancher/k3s/k3s.yaml $(ARTIFACTS_DIR)/kubeconfig.yaml
sed -i -e "s/127.0.0.1/$(API_IP_ADDRESS)/" $$PWD/$(ARTIFACTS_DIR)/kubeconfig.yaml
get-base-domain:
$(eval API_IP_ADDRESS = $(shell docker run --rm \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v $$PWD:/workdir \
stedolan/jq -r '.values.root_module.resources[]|select(.type=="docker_container" and .name=="k3s_server").values.ip_address' /workdir/terraform/terraform.tfstate.d/$(CLUSTER_NAME)/terraform.tfstate.json))
$(eval BASE_DOMAIN = $(shell echo $(API_IP_ADDRESS)|tr '.' '-').nip.io)
$(ARTIFACTS_DIR)/terraform.tfstate: terraform/*
echo $(REPO_URL)
touch $$HOME/.terraformrc
docker run --rm \
--group-add $(DOCKER_GID_NUMBER) \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $$PWD:$$PWD \
-v $$HOME/.terraformrc:/tmp/.terraformrc \
-v $$HOME/.terraform.d:/tmp/.terraform.d \
--env HOME=/tmp \
--env REPO_URL=$(REPO_URL) \
--env CLUSTER_NAME=$(CLUSTER_NAME) \
--env ARTIFACTS_DIR=$(ARTIFACTS_DIR) \
--entrypoint "" \
--workdir $$PWD \
hashicorp/terraform:$(TERRAFORM_VERSION) $$PWD/scripts/provision.sh
clean:
touch $$HOME/.terraformrc
docker run --rm \
--group-add $(DOCKER_GID_NUMBER) \
--user $(UID_NUMBER):$(GID_NUMBER) \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $$PWD:/workdir \
-v $$HOME/.terraformrc:/tmp/.terraformrc \
-v $$HOME/.terraform.d:/tmp/.terraform.d \
--env HOME=/tmp \
--env CLUSTER_NAME=$(CLUSTER_NAME) \
--entrypoint "" \
--workdir /workdir \
hashicorp/terraform:$(TERRAFORM_VERSION) /workdir/scripts/destroy.sh
rm -rf $$PWD/$(ARTIFACTS_DIR)
debug: get-base-domain
@echo CLUSTER_NAME=$(CLUSTER_NAME)
@echo BASE_DOMAIN=$(BASE_DOMAIN)
@echo DOCKER_HOST=$(DOCKER_HOST)
@echo UID_NUMBER=$(UID_NUMBER)
@echo GID_NUMBER=$(GID_NUMBER)
@echo DOCKER_GID_NUMBER=$(DOCKER_GID_NUMBER)
@echo ARTIFACTS_DIR="terraform/terraform.tfstate.d/$(CLUSTER_NAME)"
@echo REMOTE=$(REMOTE)
@echo REMOTE_BRANCH=$(REMOTE_BRANCH)
@echo REMOTE_URL=$(REMOTE_URL)
@echo REPO_URL=$(REPO_URL)