-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc-run.sh
executable file
·165 lines (141 loc) · 4.09 KB
/
dc-run.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
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
#!/bin/bash
#
# by TS, May 2019
#
# ----------------------------------------------------------
# update Docker Image from remote repository? [true|false]
LCFG_UPDATE_REMOTE_IMAGE=true
# ----------------------------------------------------------
# @param string $1 Path
# @param int $2 Recursion level
#
# @return string Absolute path
function realpath_osx() {
local TMP_RP_OSX_RES=
[[ $1 = /* ]] && TMP_RP_OSX_RES="$1" || TMP_RP_OSX_RES="$PWD/${1#./}"
if [ -h "$TMP_RP_OSX_RES" ]; then
TMP_RP_OSX_RES="$(readlink "$TMP_RP_OSX_RES")"
# possible infinite loop...
local TMP_RP_OSX_RECLEV=$2
[ -z "$TMP_RP_OSX_RECLEV" ] && TMP_RP_OSX_RECLEV=0
TMP_RP_OSX_RECLEV=$(( TMP_RP_OSX_RECLEV + 1 ))
if [ $TMP_RP_OSX_RECLEV -gt 20 ]; then
# too much recursion
TMP_RP_OSX_RES="--error--"
else
TMP_RP_OSX_RES="$(realpath_osx "$TMP_RP_OSX_RES" $TMP_RP_OSX_RECLEV)"
fi
fi
echo "$TMP_RP_OSX_RES"
}
# @param string $1 Path
#
# @return string Absolute path
function realpath_poly() {
case "$OSTYPE" in
linux*) realpath "$1" ;;
darwin*) realpath_osx "$1" ;;
*) echo "$VAR_MYNAME: Error: Unknown OSTYPE '$OSTYPE'" >/dev/stderr; echo -n "$1" ;;
esac
}
VAR_MYNAME="$(basename "$0")"
VAR_MYDIR="$(realpath_poly "$0")"
VAR_MYDIR="$(dirname "$VAR_MYDIR")"
# ----------------------------------------------------------
# Outputs CPU architecture string
#
# @param string $1 debian_rootfs|debian_dist
#
# @return int EXITCODE
function _getCpuArch() {
case "$(uname -m)" in
x86_64*)
echo -n "amd64"
;;
i686*)
if [ "$1" = "debian_dist" ]; then
echo -n "i386"
else
echo "$VAR_MYNAME: Error: invalid arg '$1'" >/dev/stderr
return 1
fi
;;
aarch64*)
if [ "$1" = "debian_dist" ]; then
echo -n "arm64"
else
echo "$VAR_MYNAME: Error: invalid arg '$1'" >/dev/stderr
return 1
fi
;;
armv7*)
if [ "$1" = "debian_dist" ]; then
echo -n "armhf"
else
echo "$VAR_MYNAME: Error: invalid arg '$1'" >/dev/stderr
return 1
fi
;;
*)
echo "$VAR_MYNAME: Error: Unknown CPU architecture '$(uname -m)'" >/dev/stderr
return 1
;;
esac
return 0
}
_getCpuArch debian_dist >/dev/null || exit 1
# ----------------------------------------------------------
function printUsageAndExit() {
echo "Usage: $VAR_MYNAME <COMMAND> ..." >/dev/stderr
echo "Example: $VAR_MYNAME ./build.sh" >/dev/stderr
exit 1
}
cd "$VAR_MYDIR" || exit 1
if [ $# -eq 0 ]; then
printUsageAndExit
fi
# ----------------------------------------------------------
LVAR_REPO_PREFIX="tsle"
LVAR_IMAGE_NAME="app-go_native_compiler-$(_getCpuArch debian_dist)"
LVAR_GOLANG_VER="1.13.5"
LVAR_IMG_FULL="${LVAR_IMAGE_NAME}:${LVAR_GOLANG_VER}"
# ----------------------------------------------------------
# @param string $1 Docker Image name
# @param string $2 optional: Docker Image version
#
# @returns int If Docker Image exists 0, otherwise 1
function _getDoesDockerImageExist() {
local TMP_SEARCH="$1"
[ -n "$2" ] && TMP_SEARCH="$TMP_SEARCH:$2"
local TMP_AWK="$(echo -n "$1" | sed -e 's/\//\\\//g')"
#echo " checking '$TMP_SEARCH'"
local TMP_IMGID="$(docker image ls "$TMP_SEARCH" | awk '/^'$TMP_AWK' / { print $3 }')"
[ -n "$TMP_IMGID" ] && return 0 || return 1
}
_getDoesDockerImageExist "$LVAR_IMAGE_NAME" "$LVAR_GOLANG_VER"
if [ $? -ne 0 ]; then
LVAR_IMG_FULL="${LVAR_REPO_PREFIX}/$LVAR_IMG_FULL"
_getDoesDockerImageExist "${LVAR_REPO_PREFIX}/${LVAR_IMAGE_NAME}" "$LVAR_GOLANG_VER"
if [ $? -ne 0 ]; then
echo "$VAR_MYNAME: Trying to pull image from repository '${LVAR_REPO_PREFIX}/'..."
docker pull ${LVAR_IMG_FULL}
if [ $? -ne 0 ]; then
echo "$VAR_MYNAME: Error: could not pull image '${LVAR_IMG_FULL}'. Aborting." >/dev/stderr
exit 1
fi
elif [ "$LCFG_UPDATE_REMOTE_IMAGE" = "true" ]; then
echo "$VAR_MYNAME: Updating image from repository '${LVAR_REPO_PREFIX}/'..."
docker pull ${LVAR_IMG_FULL} || exit 1
fi
fi
# ----------------------------------------------------------
TMP_BIN=""
if [ $# -gt 0 ]; then
TMP_BIN="$(echo -n "$1" | sed -e "s;^./mpapp/;./;g" -e "s;^mpapp/;./;g")"
shift
fi
docker run \
--rm \
-v "$VAR_MYDIR/mpapp":"/root/app" \
$LVAR_IMG_FULL \
"$TMP_BIN" $@