This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Makefile
523 lines (464 loc) · 20.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#
# Copyright (c) 2017-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL:=/bin/bash
CURRENT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
export CURRENT_DIR_BASE:=$(shell basename $(CURRENT_DIR))
BRANCH:=$(shell git branch| grep \*| cut -d ' ' -f2)
export TEST = 0
COMPOSE_PROJECT_NAME?="oisp"
CONTAINERS?=$(shell docker-compose --log-level ERROR config --services)
EXT_CONTAINERS=cassandra;gcr.io/cassandra-operator/cassandra-3.11.6:v6.4.0 cassandra;gcr.io/cassandra-operator/cassandra-operator:v6.7.0 sidecar;gcr.io/cassandra-operator/instaclustr-icarus:1.0.1 kafka;confluentinc/cp-kafka:5.0.1
CONTAINERS_AGENT=oisp-testsensor oisp-iot-agent
DOCKER_COMPOSE_ARGS?=
K3S_NODE=$(shell docker ps --format '{{.Names}}' | grep k3s_agent)
export K3S_IMAGE?=rancher/k3s:v1.24.10-k3s1-amd64
export DOCKER_TAG?=latest
export DOCKER_PREFIX?=oisp
# Try setting this to true if you are getting cgroups error in k3s image
export K3D_FIX_CGROUPV2?=false
export GIT_COMMIT_PLATFORM_LAUNCHER=$(git rev-parse HEAD)
export GIT_COMMIT_FRONTEND=$(git -C oisp-frontend rev-parse HEAD)
export GIT_COMMIT_GEARPUMP=$(git -C oisp-gearpump-rule-engine rev-parse HEAD)
export GIT_COMMIT_BACKEND=$(git -C oisp-backend rev-parse HEAD)
NOBACKUP?=false
DEBUG?=false
USE_LOCAL_REGISTRY?=false
ifeq ($(DEBUG),true)
CONTAINERS:=$(CONTAINERS) debugger
endif
export NAMESPACE?=oisp
# Name for HELM deployment
NAME?=$(NAMESPACE)
DEPLOYMENT?=debugger
DEBUGGER_POD:=$(shell kubectl -n $(NAMESPACE) get pods -o custom-columns=:metadata.name | grep debugger | head -n 1)
SELECTED_POD:=$(shell kubectl -n $(NAMESPACE) get pods -o custom-columns=:metadata.name | grep $(DEPLOYMENT) | head -n 1)
FRONTEND_POD:=$(shell kubectl -n $(NAMESPACE) get pods -o custom-columns=:metadata.name | grep frontend | head -n 1)
BACKUP_EXCLUDE:=sh.helm stolon default-token oisp-stolon-token
.init:
@$(call msg,"Initializing ...");
@$(call msg,"Currently on branch ${BRANCH}");
@if [ "${BRANCH}" != master ]; then \
if [ "${TEST}" != "1" ]; then \
echo -e "Non-master branch detected! Submodules will not be updated automatically. \nYou have to run 'make update' for submodules from develop branch and update manually otherwise"; \
read -r -p "Continue? [Y/n]: " response; \
case $$response in \
[Nn]* ) echo "Bye!"; exit 1; \
esac \
fi; \
else \
git submodule update --init --recursive; \
fi;
@touch $@
# ===================
# DEPLOYMENT COMMANDS
# ===================
check-docker-cred-env:
@if [ "$$NODOCKERLOGIN" = "true" ]; then \
echo "No docker login. Images must be imported"; exit 0; \
elif [ "$$USE_LOCAL_REGISTRY" = "true" ]; then \
echo "Using local registry. Images must be imported"; exit 0; \
else \
if [ "$$DOCKERUSER" = "" ]; then \
echo "DOCKERUSER env var is undefined"; exit 1; \
fi; \
if [ "$$DOCKERPASS" = "" ]; then \
echo "DOCKERPASS env var is undefined"; exit 1; \
fi; \
docker login -u $$DOCKERUSER -p $$DOCKERPASS; \
fi
# ftp pass checking is disabled, as service jars are hosted in services-server now
# re-enable this if an ftp server is used instead.
# if [ "$$FTPPASS" = "" ]; then \
# echo "FTPPASS env var is undefined"; exit 1; \
# fi; \
fi
deploy-operators:
@echo "Deploying operators"; \
cd util && bash ./deploy_operators.sh
## deploy-oisp-test: Deploy repository as HELM chart,
## create an ethereal address, and make sure there is
## a debugger container.
## This also takes a $DOCKER_TAG parameter
##
deploy-oisp-test: check-docker-cred-env
make deploy-oisp
kubectl -n $(NAMESPACE) scale deployment debugger --replicas=1
# wait for debugger container to become ready (otherwise the next "make" command might failing finding debugger)
@printf "Waiting for debugger pod to be created";
@while ! kubectl -n $(NAMESPACE) get pods -l=app=debugger 2> /dev/null | grep debugger >> /dev/null; \
do printf "."; sleep 5; done;
@echo
@printf "Waiting for debugger to become ready ";
@while kubectl -n $(NAMESPACE) get pods -l=app=debugger -o \
jsonpath="{.items[*].status.containerStatuses[*].ready}" | grep false >> /dev/null; \
do printf "."; sleep 5; done;
@echo
## deploy-oisp: Deploy repository as HELM chart
##
deploy-oisp: check-docker-cred-env deploy-operators wait-until-operators-ready
@echo "Create Namespace"
kubectl create namespace $(NAMESPACE) --dry-run=client -o yaml | kubectl apply -f -
@echo "Prepare helm"
cd kubernetes && helm dependency update --namespace $(NAMESPACE)
@$(call msg,"Starting first deploy");
cd kubernetes && \
POSTGRES_PASSWORD="$(call randomPass)" && \
if [ "$$USE_LOCAL_REGISTRY" = "true" ]; then \
LOCAL_REGISTRY=k3d-iff.localhost:12345/ ; \
fi && \
helm install $(NAME) . --namespace $(NAMESPACE) \
--timeout 1200s \
--set imageCredentials.username="$$DOCKERUSER" \
--set imageCredentials.password="$$DOCKERPASS" \
--set systemuser.password="$(call randomPass)" \
--set grafana.password="$(call randomPass)" \
--set mqtt.broker.password="$(call randomPass)" \
--set postgres.password="$(call randomPass)" \
--set tag=$(DOCKER_TAG) \
--set imagePrefix=$(DOCKER_PREFIX) \
--set emqx.initContainers[0].name=wait-for-mqtt-auth-service \
--set emqx.initContainers[0].image=$${LOCAL_REGISTRY}$(DOCKER_PREFIX)/wait-for-it:$(DOCKER_TAG) \
--set emqx.initContainers[0].args='{mqtt-gateway:3025,--,echo,mqtt auth service is up}' \
--set emqxtest.initContainers[0].name=wait-for-mqtt-auth-service \
--set emqxtest.initContainers[0].image=$${LOCAL_REGISTRY}$(DOCKER_PREFIX)/wait-for-it:$(DOCKER_TAG) \
--set emqxtest.initContainers[0].args='{mqtt-gateway:3025,--,echo,mqtt auth service is up}' \
--set use_local_registry=$(USE_LOCAL_REGISTRY) \
$(HELM_ARGS)
## upgrade-oisp: Upgrade already deployed HELM chart
##
upgrade-oisp: check-docker-cred-env backup
@if [ "$$NO_OPERATOR_DEPLOY" = "true" ]; then \
make deploy-operators; \
make wait-until-operators-ready; \
fi
@echo "Preparing Helm"
cd kubernetes && helm dependency update --namespace $(NAMESPACE)
@$(call msg,"Starting upgrade");
@source util/get_oisp_credentials.sh && \
cd kubernetes && \
if [ "$$USE_LOCAL_REGISTRY" = "true" ]; then \
LOCAL_REGISTRY=k3d-iff.localhost:12345/ ; \
fi && \
helm upgrade $(NAME) . --namespace $(NAMESPACE) \
--timeout 600s \
--set imageCredentials.username="$$DOCKERUSER" \
--set imageCredentials.password="$$DOCKERPASS" \
--set systemuser.password="$${SYSTEMUSER_PASSWORD}" \
--set grafana.password="$${GRAFANA_PASSWORD}" \
--set mqtt.broker.password="$${MQTT_BROKER_PASSWORD}" \
--set postgres.password="$${POSTGRES_PASSWORD}" \
--set tag=$(DOCKER_TAG) \
--set imagePrefix=$(DOCKER_PREFIX) \
--set emqx.initContainers[0].name=wait-for-mqtt-auth-service \
--set emqx.initContainers[0].image=$${LOCAL_REGISTRY}$(DOCKER_PREFIX)/wait-for-it:$(DOCKER_TAG) \
--set emqx.initContainers[0].args='{mqtt-gateway:3025,--,echo,mqtt auth service is up}' \
--set emqxtest.initContainers[0].name=wait-for-mqtt-auth-service \
--set emqxtest.initContainers[0].image=$${LOCAL_REGISTRY}$(DOCKER_PREFIX)/wait-for-it:$(DOCKER_TAG) \
--set emqxtest.initContainers[0].args='{mqtt-gateway:3025,--,echo,mqtt auth service is up}' \
--set use_local_registry=$(USE_LOCAL_REGISTRY) \
$(HELM_ARGS)
## undeploy-oisp: Remove OISP deployment by HELM chart
##
undeploy-oisp:
@cd kubernetes && \
(kubectl delete -n $(NAMESPACE) "beamservices.$${NAMESPACE}.org" --all || echo "Beam services not (or already) deleted") && \
(kubectl delete -n $(NAMESPACE) "beamsqlstatementsets.$${NAMESPACE}.org" --all || echo "Beam SQL services not (or already) deleted") && \
( helm uninstall $(NAME) --namespace $(NAMESPACE) || echo helm uninstall failed) && \
(kubectl delete namespace $(NAMESPACE) || echo "namespace not (or already) deleted") && \
(kubectl delete cassdcs -n cassandra --all || echo "cassandra dc not (or already) deleted")
# Beam services must be deleted first, otherwise the finalizer (operator) is down, and deletation does not work.
# =====
# UTILS
# =====
## reset-db: Reset database via admin tool in frontend
##
reset-db: wait-until-ready
kubectl -n $(NAMESPACE) exec $(FRONTEND_POD) --container frontend -- node admin resetDB
## add-test-user: Add a test user via admin tool in frontend
##
add-test-user:
for i in $(shell seq 1 1); do kubectl -n $(NAMESPACE) exec $(FRONTEND_POD) -c frontend -- node admin addUser user$${i}@example.com password; done;
wait-until-operators-ready:
@{ printf "\nWaiting for readiness of operators\n"; \
cd ./tests/bats; \
if [ ! -d "./lib" ]; then \
$(SHELL) ./prepare-bats.sh; \
fi; \
bats ./test-operators/*.bats; }
## wait-until-ready: Wait until the platform is up and running
## As of now, this is assumed if all frontend and backend containers
## are ready.
## In case of timeout, the application will be redeployed ONLY if TEST is set
wait-until-ready:
@{ printf "\nWaiting for readiness of platform"; \
DOCKER_TAG=$(DOCKER_TAG); \
if [ "$${DOCKER_TAG}" = "latest" ]; then \
export DOCKER_TAG=test; \
fi; \
$(SHELL) ./wait-until-ready.sh $(NAMESPACE); \
retval=$$?; \
if [ $${retval} -eq 2 ]; then \
printf "\nTimeout while waiting for platform\n"; \
exit 2; \
fi }
## import-images: Import images listed in CONTAINERS into local cluster
##
import-images:
$(foreach image, $(CONTAINERS), \
printf $(image) && \
docker tag $(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) && \
docker exec k3d-iff-cluster-server-0 sh -c "ctr image rm k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG)" && \
docker push k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) && \
docker rmi k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) && \
printf ", imported\n" \
)
## import-images-agent: Import images to deploy OISP-Agent
##
import-images-agent:
@$(foreach image,$(CONTAINERS_AGENT), \
printf $(image) && \
docker tag $(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) && \
docker push k3d-iff.localhost:12345/$(DOCKER_PREFIX)/$(image):$(DOCKER_TAG) && \
printf ", imported\n"
)
## open-shell: Open a shell to a random pod in DEPLOYMENT.
## By default thi will try to open a shell to a debugger pod.
##
open-shell:
@$(call msg, "Opening shell to pod: $(SELECTED_POD)")
kubectl -n $(NAMESPACE) exec -it $(SELECTED_POD) /bin/bash
## install the dependencies for OISP
##
restart-cluster:
@cd util && \
./restart-cluster.sh
## delete and recreate registry, used for the CI setup
## needs make restart-cluster command after it
##
recreate-registry:
@k3d registry delete k3d-iff.localhost
@k3d registry create iff.localhost -p 12345
test:
## TO-DO: adjust bats tests to not use OISP
## @cd tests/bats && bats test-*
# ==============
# BUILD COMMANDS
# ==============
## build: Build OISP images locally.
## You can specify a version using the $DOCKER_TAG argument.
## $CONTAINERS arg (as whitespace seperated list) specifies which containers should be built,
## if it is left blank, all containers will be built.
## If $DEBUG is set to true, debugger will be added to CONTAINERS
## This command also accepts the argument $DOCKER_COMPOSE_ARGS, which is passed directly to compose.
##
build: .init
@$(call msg,"Building OISP containers");
@docker-compose -f docker-compose.yml -f docker/debugger/docker-compose-debugger.yml build --parallel $(DOCKER_COMPOSE_ARGS) $(CONTAINERS);
## update: Update all subrepositories to latest origin/develop
## For competabilty, this will also backup and remove setup-environment.sh
##
update: clean
@$(call msg,"Git Update (dev only)");
# Normally, the following should be enough, but it can crash in merge conflicts
# Everything is run twice to allow submodules of submodules, if the .gitmodules
# file is different at HEAD than latest develop.
# @git submodule update --init --recursive --remote --merge
@$(call msg,"Git Update (dev only)");
@git submodule update --init
@git submodule foreach --recursive "git fetch origin && git checkout origin/develop && git submodule update --init"
@git submodule foreach --recursive "git fetch origin && git checkout origin/develop && git stash -a"
## docker-clean: Remove all docker images and containers.
## This also includes non-oisp containers.
##
docker-clean:
@$(call msg,"Removing all docker images and containers ...");
@read -r -p "Continue? [y/N]: " response; \
case $$response in \
[Yy]* ) docker stop $(docker ps -a -q); \
docker rm -f $(docker ps -a -q); \
/bin/bash -c "docker images -q | xargs -n 1 -I {} docker rmi -f {}";; \
[Nn]* ) echo "Not removing containers";; \
esac \
# TODO fix this
## test-prep-only: Prepare test for 3rd party apps like oisp-iot-agent but do not start full e2e test
## Creates a user with $USERNAME and $PASSWORD, and creates a device with id 00-11-22-33-44-55,
## dumps the result in oisp-prep-only.conf
##
test-prep-only: prepare-tests
kubectl -n $(NAMESPACE) exec $(DEBUGGER_POD) -c debugger \
-- /bin/bash -c "cd /home/$(CURRENT_DIR_BASE)/tests && make test-prepare-only TERM=xterm NAMESPACE=$(NAMESPACE)"
kubectl cp $(NAMESPACE)/$(DEBUGGER_POD):/home/$(CURRENT_DIR_BASE)/tests/oisp-prep-only.conf tests/oisp-prep-only.conf
## remove: Remove all OISP images from local machine.
## $CONTAINERS arg (as whitespace seperated list) specifies which containers should be removed,
## if it is left blank, all containers will removed.
##
remove:
@$(call msg,"Removing $(CONTAINERS)")
ifeq ($(CONTAINERS),"")
make stop;\
$(eval images:=$(shell docker images --filter label=oisp -q)) \
docker rmi -f $(images)
else
@$(foreach container,$(CONTAINERS), \
docker rm -f -v $(container); \
docker images | grep "^.*$(container)" | awk '{print $$3}' | xargs -n 1 -I {} docker rmi -f {}; \
)
endif
## test-backup: Test backup
## Assumes test-prep-only executed before
## then make backup, redeploying OISP and restore backup. Check the users, devices, etc.
## NOTE: NAMESPACE and DOCKERTAG need to be set
##
test-backup: prepare-tests test-prep-only
@$(call msg,"Testing backup and restore ...");
$(eval ACCOUNTID := $(shell jq ".accountId" tests/oisp-prep-only.conf))
$(eval ACTIVATIONCODE := $(shell jq ".activationCode" tests/oisp-prep-only.conf))
$(eval USERNAME := $(shell jq ".username" tests/oisp-prep-only.conf))
$(eval PASSWORD := $(shell jq ".password" tests/oisp-prep-only.conf))
kubectl -n $(NAMESPACE) exec $(DEBUGGER_POD) -c debugger \
-- /bin/bash -c "cd /home/$(CURRENT_DIR_BASE)/tests && \
make test-backup-before TERM=xterm NAMESPACE=$(NAMESPACE) \
ACCOUNTID=$(ACCOUNTID) ACTIVATIONCODE=$(ACTIVATIONCODE) USERNAME=$(USERNAME) PASSWORD=$(PASSWORD)"
$(MAKE) backup
$(MAKE) undeploy-oisp
$(MAKE) NAMESPACE=$(NAMESPACE) DEBUG=$(DEBUG) DOCKER_TAG=$(DOCKER_TAG) deploy-oisp-test
$(MAKE) restore
FRONTEND=$$(kubectl -n $(NAMESPACE) get pods | grep frontend| cut -d " " -f 1) && \
DEBUGGER_POD=$$(kubectl -n $(NAMESPACE) get pods -o custom-columns=:metadata.name | grep debugger | head -n 1) && \
$(MAKE) NAMESPACE=$(NAMESPACE) DEBUGGER_POD=$${DEBUGGER_POD} prepare-tests && \
kubectl -n $(NAMESPACE) exec $${DEBUGGER_POD} -c debugger \
-- /bin/bash -c "cd /home/$(CURRENT_DIR_BASE)/tests && \
make test-backup-after TERM=xterm NAMESPACE=$(NAMESPACE) \
ACCOUNTID=$(ACCOUNTID) ACTIVATIONCODE=$(ACTIVATIONCODE) USERNAME=$(USERNAME) PASSWORD=$(PASSWORD)"
## logs:
## Create a .zip archive containing logs from all containers.
## The result will be saved in platform-lancuher-logs_{data}.zip
## This can only be used if the cluster is running locally on k3s.
##
logs:
$(eval LOGS_ARCHIVE := oisp-logs_$(shell date +'%Y-%m-%d_%H-%M-%S'))
@$(call msg,"Making one archive file with all the logs within ( ./$(LOGS_ARCHIVE).zip ) ...");
@rm -rf /tmp/$(LOGS_ARCHIVE)* && mkdir -p /tmp/$(LOGS_ARCHIVE)
@docker cp $(shell docker ps --format "{{.Names}}" | grep k3s_node):/var/log/pods /tmp/$(LOGS_ARCHIVE)
@cd /tmp && zip -q -r ./$(LOGS_ARCHIVE).zip $(LOGS_ARCHIVE)/*
@mv /tmp/$(LOGS_ARCHIVE).zip .
## clean: Remove .init, forcing a clean-up on the next run.
##
clean:
@$(call msg,"Cleaning ...");
@rm -f .init
## push images: Push containers to dockerhub.
## This obviously requires docker login,
## $CONTAINERS arg selects which containers (whitespace seperated list) should be pushed,
## if left empty, all containers will be pushed.
##
push-images:
@$(call msg,"Pushing docker images to registry");
@docker-compose -f docker-compose.yml -f docker/debugger/docker-compose-debugger.yml push $(CONTAINERS)
## backup: backup database, configmaps and secrets.
## This requires either default K8s config or KUBECONFIG set
## A tar file is created containing the files
##
backup:
ifeq ($(NOBACKUP),false)
@$(call msg,"Creating backup");
@mkdir -p backups
@$(eval TMPDIR := backup_$(NAMESPACE)_$(shell date +'%Y-%m-%d_%H-%M-%S'))
@$(eval TARGETNAME := backup_$(NAMESPACE)_daily_$(shell date -Iseconds).tgz)
@if [ -d "/tmp/$(TMPDIR)" ]; then echo "Backup file already exists. Not overwriting. Bye"; exit 1; fi
@mkdir -p /tmp/$(TMPDIR)
@util/backup/db_dump.sh /tmp/$(TMPDIR) $(NAMESPACE) 2>&1 || exit 1
@util/backup/cm_dump.sh /tmp/$(TMPDIR) $(NAMESPACE) "$(BACKUP_EXCLUDE)" 2>&1 || exit 1
@tar cvzf backups/$(TARGETNAME) -C /tmp $(TMPDIR)
@rm -rf /tmp/$(TMPDIR)
endif
ifdef S3BUCKET
s3cmd put backups/$(TARGETNAME) $(S3BUCKET)/$(TARGETNAME)
endif
## purge-backups: looks into S3 Bucket or local backups folder and purges
## daily backups older than 7 days. Keeps one backup per month.
## This requires either default K8s config or KUBECONFIG set
## A tar file is created containing the files
##
purge-backups:
@$(call msg,"Purging backups");
ifdef S3BUCKET
@echo Purging S3 backups in $(S3BUCKET)
@util/backup/purge_backup.sh $(S3BUCKET)
else
@echo purging local backups
@util/backup/purge_backup.sh backups
endif
## restore: restore database, configmaps and secrets.
## This requires either default K8s config or KUBECONFIG set
## Parameters: BACKUPFILE var must be set or the most recent timestamp is selected
##
restore:
ifdef S3BUCKET
@if [ -z "$(BACKUPFILE)" ]; then echo "BACKUPFILE should be set when S3BUCKET is set"; exit 1; fi
endif
ifndef BACKUPFILE
@echo Look for most recent backup file
@$(eval BACKUPFILE := $(shell ls backups/backup_*|sort -V| head -n 1))
endif
@$(eval TMPDIR := backup_$(NAMESPACE)_$(shell date +'%Y-%m-%d_%H-%M-%S'))
@echo using backup file $(BACKUPFILE) and copying to localfile $(TMPDIR).tgz
ifdef S3BUCKET
@echo Copy $(BACKUPFILE) from bucket $(S3BUCKET) to /tmp/$(BACKUPFILE)
@s3cmd get $(S3BUCKET)/$(BACKUPFILE) /tmp/$(TMPDIR).tgz || exit 1
else
@cp $(BACKUPFILE) /tmp/$(TMPDIR).tgz
endif
@mkdir /tmp/$(TMPDIR)
@tar xvzf /tmp/$(TMPDIR).tgz --strip-components=1 -C /tmp/$(TMPDIR)
@util/backup/cm_check.sh /tmp/$(TMPDIR) $(NAMESPACE)
@util/backup/db_restore.sh /tmp/$(TMPDIR) $(NAMESPACE)
@util/backup/cm_restore.sh /tmp/$(TMPDIR) $(NAMESPACE)
ifdef S3BUCKET
@rm -rf /tmp/$(BACKUPFILE)
else
@rm -rf /tmp/${TMPDIR} /tmp/$(TMPDIR).tgz
endif
## backup-db only, backup is saved in TMPDIR/database.sql
##
backup-db:
$(call msg, "Creating DB backup");
mkdir -p backups;
util/backup/db_dump.sh $(TMPDIR) $(NAMESPACE)
## restore-db only, from TMPDIR/database.sql
##
restore-db:
ifndef BACKUPFILE
$(eval BACKUPFIL := backups/database.sql)
endif
$(call msg, "Restoring DB backup");
export DBONLY=true && util/backup/db_restore.sh $(TMPDIR) $(NAMESPACE)
## help: Show this help message
##
help:
@grep "^##" Makefile | cut -c4-
#-----------------
# helper functions
#-----------------
define msg
@tput setaf 2 && \
for i in $(shell seq 1 60 ); do echo -n "-"; done; echo "" && \
echo -e "\t"$1 && \
for i in $(shell seq 1 60 ); do echo -n "-"; done; echo "" && \
tput sgr0
endef
define randomPass
$$(< /dev/urandom tr -dc A-Za-z0-9 | head -c$${1:-64})
endef