-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path.docker-misc.bash
More file actions
executable file
·162 lines (138 loc) · 5.33 KB
/
.docker-misc.bash
File metadata and controls
executable file
·162 lines (138 loc) · 5.33 KB
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
#!/usr/bin/env bash
# shellcheck shell=bash
direxpand() {
v="$(pwd)"
while [[ -n "$v" ]]; do
if [[ -f "$v/Dockerfile" ]] && [[ $(<"$v/Dockerfile") =~ salt ]]; then
path="${v}"
dir="${v##*/}"
echo "$path" "$dir"
return 0
fi
v="${v%/*}";
done
echo "Not in project's directory or there is no \`Dockerfile'" 1>&2
return 1
}
gtpl() {
start=${EPOCHREALTIME/./}
git pull || return $?
git submodule sync || return $?
git submodule update --init --recursive --force || return $?
local submodules
declare -a pids=()
declare -A subnames
declare ECODE=0
mapfile -t submodules < <(git submodule --quiet foreach "pwd")
for submodule in "${submodules[@]}"; do
(cd "$submodule" || exit 123; git fetch origin master && git checkout --force -B master origin/master) &
pid="$!"
pids+=("$pid")
subnames["$pid"]=$submodule
done
resp=()
for pid in "${pids[@]}"; do
wait "$pid"; ecode=$?; [[ $ecode -gt $ECODE ]] && ECODE=$ecode
resp+=("Job ${pid} exited with $ecode for submodule ${subnames[$pid]}")
done
end=${EPOCHREALTIME/./}
resp+=("$FUNCNAME returned $ECODE in $(( (end - start)/1000 ))ms")
printf "%s\n" "${resp[@]}"
return $ECODE
}
dbuild() {
read -ra _dirs < <(direxpand) || return 1
local path=${_dirs[0]}
local name=${_dirs[1]}
docker stop -t 0 "${name}-$USER" > /dev/null 2>&1
docker build -t "${name}:$USER" "${path}" "$@";
}
drun() {
read -ra _dirs < <(direxpand) || return 1
local path=${_dirs[0]}
local name=${_dirs[1]}
local top_file=/tmp/$USER-$name-top-sls.file
cat "${path}/pillar/top_sls/_top.sls" <(echo) > "$top_file"
find "${path}/pillar/top_sls" -not -type d -print0 | grep -vzP '\.swp$|_?top\.sls$' | sort -z | xargs -0 -I{} bash -c "cat {} <(echo) >> \"$top_file\""
if [[ ! $(docker ps --format "{{.Names}}" --filter "name=${name}-$USER") =~ ${name}-$USER ]] ; then
docker run --hostname salt --detach --rm --name "${name}-$USER" \
--volume "$top_file:/srv/pillar/top.sls" \
--volume "${path}/:/srv/" \
--volume "$SSH_AUTH_SOCK:/root/.ssh-agent" \
--env SSH_AUTH_SOCK=/root/.ssh-agent \
"${name}:$USER" \
-- \
bash -c "sleep 2h && kill -s 15 1; rm /srv/pillar/top.sls" ;
fi
if test -t 0; then opt+="i"; fi
if test -t 1; then opt+="t"; fi
if [[ -n ${opt} ]]; then opt="-${opt}"; fi
if [[ "$1" == check ]]; then
docker exec "${name}-$USER" "/.check_pillar_for_roster.sh" ;
elif [[ ${@} =~ grains= ]]; then
docker cp "${path}/etc/salt" "${name}-$USER:/etc/" >/dev/null 2>&1
docker exec ${opt} "${name}-$USER" /entrypoint.sh "$@"
else
docker exec ${opt} "${name}-$USER" "$@"
fi
}
_drun_compl()
{
read -ra _dirs < <(direxpand) || return 1
local path=${_dirs[0]}
local name=${_dirs[1]}
for i in "${!COMP_WORDS[@]}"; do # arguments cleanup
if [[ ${COMP_WORDS[$i]} == "=" ]]; then # equals split string to three parts
unset 'COMP_WORDS[$i-1]' # and we want to delete all those arguments
unset 'COMP_WORDS[$i]'
unset 'COMP_WORDS[$i+1]'
fi
if [[ ${COMP_WORDS[$i]} =~ ^--user$ ]]; then # delete --user user
unset 'COMP_WORDS[$i]'
unset 'COMP_WORDS[$i+1]'
fi
if [[ ${COMP_WORDS[$i]} =~ ^- ]]; then # delete all args that starts with dash
unset 'COMP_WORDS[$i]'
fi
done
COMP_WORDS=("${COMP_WORDS[@]}")
if [[ ${#COMP_WORDS[@]} -eq 2 ]]; then
local _args
_args="salt-ssh check"
mapfile -t COMPREPLY < <(compgen -W "${_args}" "${COMP_WORDS[1]}")
fi
if [[ ${#COMP_WORDS[@]} -eq 3 ]]; then
local hosts
hosts=$(grep -ohP "^[^\s:]+" "${path}"/etc/salt/roster*)
mapfile -t COMPREPLY < <(compgen -W "${hosts}" "${COMP_WORDS[2]}")
fi
if [[ ${#COMP_WORDS[@]} -eq 4 ]]; then
local _args
_args="test.ping state.apply state.highstate pillar.items pillar.item grains.items grains.item cmd.run"
mapfile -t COMPREPLY < <(compgen -W "${_args}" "${COMP_WORDS[3]}")
fi
if [[ ${#COMP_WORDS[@]} -eq 5 && ${COMP_WORDS[3]} == state.apply ]]; then
local _args cachefile prefix
prefix='/tmp/druncomprep-'
cachefile="${prefix}$(md5sum <<<"$path" | cut -f 1 -d ' ')"
if [[ -f $cachefile ]]; then
_args=$(<"$cachefile")
else
_args=$(find "${path}" -regex '.*formulas/microdevops-formula/.*sls' | awk -F/ '{if (/pillar/) {next}; gsub(/^.*clients\/[^/]+/,"",$0); gsub(/init.sls/,"",$5); gsub(/.sls/,"",$5); if ($5) {print $4"."$5} else { print $4}}')
_args+="\n"
_args+=$(find "${path}" -regex '.*salt_local/.*sls' | awk -F/ '{if (/pillar/) {next}; gsub(/^.*clients/,"",$0); gsub(/init.sls/,"",$5); gsub(/.sls/,"",$5); if ($5) {print $4"."$5} else { print $4}}')
echo -e "$_args" > "$cachefile"
fi
mapfile -t COMPREPLY < <(compgen -W "${_args}" "${COMP_WORDS[4]}")
fi
}
if (return 0 2>/dev/null) ; then
complete -F _drun_compl drun
else
fnname=${0##*/}
if declare -F "$fnname" > /dev/null; then
$fnname "$@"
else
echo "No such function"
fi
fi