This repository was archived by the owner on Apr 22, 2020. It is now read-only.
forked from kairops/docker-command-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkd.sh
More file actions
executable file
·166 lines (145 loc) · 5.03 KB
/
Copy pathkd.sh
File metadata and controls
executable file
·166 lines (145 loc) · 5.03 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
163
164
165
166
#!/bin/bash
set -eo pipefail
function echo_err () {
echo >&2 -e "$*"
}
function echo_debug () {
if [ "${KD_DEBUG}" == "1" ]; then
echo >&2 -e ">>>> DEBUG >>>>> $(date "+%Y-%m-%d %H:%M:%S") docker-command-launcher: " "$*"
fi
}
echo_debug "begin"
echo_debug "Docker Command Launcher (c) 2019 TIC para Bien"
# Functions
function removeSentinel() {
local sentinel
sentinel=$1
echo_debug "Removing sentinel image(s) ${sentinel}"
docker rmi "${sentinel}" > /dev/null 2>&1
sentinel=""
}
function assertCommandExist () {
local search
search="$1"
for command in ${commandList[*]}; do
[[ "$(echo "${command}"|cut -f 1 -d ':')" == "${search}" ]] && return 0
done
echo_err "The Docker Command '${search}' does not exist. Aborting"
echo_debug "end"
exit 1
}
# Initialize
commandList=(
commit-validator:1.0.0
get-next-release-number:2.0.0
git-changelog-generator:2.0.0
hello-world:1.0.1
md2html:2.0.0
mdline:1.0.0
)
imagePrefix="tpbtools/dc-"
commandCacheSeconds=86400
# Parameters check
if [ $# -eq 0 ]; then
echo_err "Usage: kd command [file_or_folder] [parameter] [parameter] [...] [-v]
command: The Docker Command to execute, like 'hello-world' or 'git-changelog-generator'
Options:
[file_or_folder]: Optional file or folder to give to the container
[parameter]: Optional parameters. Depends on the 'docker-command' you are running
Examples:
kd hello-world
kd git-changelog-generator .
You can also concatenate two or more Docker Commands through a pipe
Examples:
kd git-changelog-generator . | kd md2html - > changelog.html
Available commands:
"
for item in ${commandList[*]}; do
echo -n "* $(echo "${item}"|cut -f 1 -d ':')"
if [ "${KD_EDGE}" == "1" ]; then
echo " (latest)"
else
echo " v$(echo "${item}"|cut -f 2 -d ':')"
fi
done
echo_err
echo_err "Other options:
- KD_DEBUG=1 to enable verbose debug info (``export KD_DEBUG=1``)
- KD_EDGE=1 to use the latest release of the commands (``export KD_EDGE=1``)
- KD_SENTINEL=1 to enable a 24h image cache sentinel (``export KD_SENTINEL=1``)
"
echo_debug "end"
exit 0
fi
# Command check
command=$1
assertCommandExist "${command}"
if [ "${KD_EDGE}" == "1" ]; then
command=$(echo "${command}"|cut -f 1 -d ':')
command="${command}:latest"
echo "${command}"
fi
image="${imagePrefix}${command}"
shift
# Parameter (file and folder) check
mountFolder=""
file=""
if [ $# -gt 0 ]; then
if [ "$1" != "-" ]; then
fileOrDirectory=$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1")
if [ -f "${fileOrDirectory}" ]; then
mountFolder=$(dirname "${fileOrDirectory}")
file=$(basename "${fileOrDirectory}")
else
if [ -d "${fileOrDirectory}" ]; then
mountFolder=$(dirname "${fileOrDirectory}/.")
fi
fi
shift
fi
fi
mountInfo=""
if [ "${mountFolder}" != "" ]; then
mountInfo=$(echo "-v \"${mountFolder}\":/workspace"|sed "s/ /\\ /g")
fi
# Update command cache
if [ "${KD_SENTINEL}" == "1" ]; then
sentinel=$(docker images ${imagePrefix}sentinel-*|awk 'NR>1 {print $1}')
if [ "${sentinel}" != "" ]; then
# If more then one sentinel cache images exists, drop all
if [ "$(echo "${sentinel}"|wc -l)" -gt 1 ]; then
removeSentinel "${sentinel}"
else
# Check time elapsed since sentinel cache image creation
sentinelTimestamp=$(echo "${sentinel}"|awk -F '-' '{print $NF}')
currentTimestamp=$(date +%s)
echo_debug "Sentinel timestamp: ${sentinelTimestamp}"
echo_debug "Current timesamp: ${currentTimestamp}"
secondsElapsed=$((currentTimestamp - sentinelTimestamp))
echo_debug "Seconds Elapsed: ${secondsElapsed}"
if [ ${secondsElapsed} -gt ${commandCacheSeconds} ]; then
removeSentinel "${sentinel}"
fi
fi
fi
if [ "${sentinel}" == "" ]; then
# Retrieve all command cache
sentinelTimestamp=$(date +%s)
echo_debug "Creating new sentinel image ${sentinelTimestamp}"
echo -e "FROM alpine\nRUN echo ${sentinelTimestamp} > /.sentinel.lock" | docker build -t "${imagePrefix}sentinel-${sentinelTimestamp}" - > /dev/null 2>&1
for command in ${commandList[*]}; do
echo_debug "Removing docker-command image for '${command}'"
docker rmi "${imagePrefix}${command}" > /dev/null 2>&1 || true
done
fi
fi
# Get image if not exist
if [ "$(docker image ls -q "${image}")" == "" ]; then
echo_debug "Retrieving docker-command image ${image}"
docker pull "${image}" > /dev/null 2>&1 || (echo_err "The docker image for the '${command}' can't be retrieved. Aborting"; exit 1)
fi
# Execute Docker Command with optional volume injection and input parameters
dockerCommand="docker run -i --rm -e KD_DEBUG=${KD_DEBUG} ${mountInfo} ${image} ${file} $*"
echo_debug "Executing: '${dockerCommand}'"
eval "${dockerCommand}"
echo_debug "end"