-
Notifications
You must be signed in to change notification settings - Fork 0
/
functs.mk
77 lines (60 loc) · 2.22 KB
/
functs.mk
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
# Author: rsharo <[email protected]>
#
# Call a single-argument function on every element in a list
# usage:
# $(call myFunction,file1 file2 ... fileN)
map1arg = $(foreach a,$(2),$(call $(1),$(a)))
#
# Remove containers from docker daemon
# usage:
# $(call clean-container,myContainerName)
clean-container = $(DOCKER) ps -aq -f "name=$(1)" | xargs -r $(DOCKER) rm -f
#
# Remove images from docker daemon
# usage:
# $(call clean-image, myImageName)
clean-image = $(DOCKER) images -q "$(1)" | xargs -r $(DOCKER) rmi -f
#
# Remove volumes from docker daemon
# usage:
# $(call clean-volume, myVolumeName)
clean-volume = $(DOCKER) volume ls -qf "name=$(1)" | xargs -r $(DOCKER) volume rm
#
# Prompt user, retain fail code if user doesn't press "y"
# usage:
# @$(call check-confirm,"Are you sure you want to do that?")
# @echo
check-confirm = bash -c 'read -n 1 -t 20 -p "$(1) [y/N] " response ; [[ "$$response" == "y" ]]'
#
# Return fail code if container already exists
# usage:
# @$(call check-new-container,containerName)
# $(DOCKER) run --name containerName ...
check-new-container = $(DOCKER) ps -aq -f "name=$(1)" | xargs -r false
#
# Return fail code if local image repository exists
# usage:
# @if ( $(call check-new-image,$(IMAGE)) ) ; then \
# echo "Building $(IMAGE)..." ; \
# $(DOCKER) build -t $(IMAGE) $(IMAGE) ; \
# fi
check-new-image = $(DOCKER) images -q "$(1)" | xargs -r false
#
# Variants that accept a list of arguments
# usage:
# $(call XXXs,arg1 arg2 ... argN)
clean-containers = $(call map1arg,clean-container,$(1))
clean-images = $(call map1arg,clean-image,$(1))
clean-volumes = $(call map1arg,clean-volume,$(1))
check-new-images = $(call check-new-image,$(1))
check-new-containers = $(call check-new-container,$(1))
#
# Variants that print an error message on failure
# usage:
# @$(call XXX-msg,arg,"ERROR: command failed.")
# $(DOCKER) build ...
call-with-msg = ( $(call $(1),$(2)) ) || ( echo $(3)>&2 ; false )
check-new-container-msg = $(call call-with-msg,check-new-container,$(1),$(2))
check-new-containers-msg = $(call call-with-msg,check-new-containers,$(1),$(2))
check-new-image-msg = $(call call-with-msg,check-new-image,$(1),$(2))
check-new-images-msg = $(call call-with-msg,check-new-images,$(1),$(2))