-
Notifications
You must be signed in to change notification settings - Fork 1
/
dc.sh
executable file
·112 lines (91 loc) · 2.39 KB
/
dc.sh
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
#!/usr/bin/env bash
tt_dc() {
dc $@
}
dc_file() {
echo $(get_workspace_dir)/docker-compose.yml
}
is_dc_v2() {
[[ $(docker-compose -v) == *' v2.'* ]]
}
get_tt_env() {
(set -o posix; set) | grep -E 'TT_' | paste -sd " " -
}
dc() {
env $(get_tt_env) docker-compose -p $(get_workspace) -f $(dc_file) $*
}
dc_get_links() {
local key=services_"$1"_links
# `docker-compose config` output wreaks havoc with list indentation,
# until we find a non-hack way to deal with this, parse file directly
parse_yaml $(dc_file) | sed -ne "s|$key+=(\"\(.*\)\")|\1|p"
}
dc_get_services_with_source() {
dc_get_sources | sed -e 's/^\[\(.*\)\]=.*$/\1/'
}
dc_get_service_and_dependencies_with_source() {
# intersect service & dependencies with source backed services
sort <(dc_get_service_and_dependencies $@) <(dc_get_services_with_source) | uniq -d
}
dc_get_service_and_dependencies() {
if [[ $# -gt 0 ]]; then
services=
result=$@ # start with the ones passed in
while [ "$services" != "$result" ]; do
services=$result
result=
# for each service...
for service in $services; do
# add service and linked services to result
result="$result $service $(dc_get_links $service)"
done
# remove duplicates and normalize whitespace
result=$(echo $result | awk -v RS="[\n ]" '!a[$0]++')
done
if [ ! -z "$result" ]; then
echo "$result"
fi
else
echo "$(dc_get_services)"
fi
}
get_workspace_cache_dir() {
local cachedir=$(get_workspace_dir)/.cache
mkdir -p $cachedir
echo $cachedir
}
dc_get_services() {
if [ "$TT_IS_GLOBAL" = true ]; then
generate_compose_file_caches
cat "$(get_workspace_cache_dir)/services"
else
dc config --services
fi
}
dc_get_sources() {
get_compose_yaml | parse_yaml |
grep '^services_.*_labels_com.tarantino.source=' |
sed -e 's/^services_\(.*\)_labels_com.tarantino.source=(\(.*\))$/[\1]=\2/'
}
generate_compose_file_caches() {
local cachedir=$(get_workspace_cache_dir)
local md5check=$cachedir/dc_file.md5
if [ -f "$md5check" ]; then
if $(md5sum -c "$md5check" --status); then
return 0
fi
fi
local yaml_cache_file=$cachedir/yaml
dc config > "$yaml_cache_file"
local services_cache_file=$cachedir/services
dc config --services > "$services_cache_file"
md5sum "$(dc_file)" > "$md5check"
}
get_compose_yaml() {
if [ "$TT_IS_GLOBAL" = true ]; then
generate_compose_file_caches
cat "$(get_workspace_cache_dir)/yaml"
else
dc config
fi
}