forked from nodejs/docker-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·218 lines (183 loc) · 4.7 KB
/
functions.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
# Utlity functions
info() {
printf "%s\\n" "$@"
}
fatal() {
printf "**********\\n"
printf "Fatal Error: %s\\n" "$@"
printf "**********\\n"
exit 1
}
# Get system architecture
#
# This is used to get the target architecture for docker image.
# For crossing building, we need a way to specify the target
# architecutre manually.
function get_arch() {
local arch
case $(uname -m) in
x86_64)
arch="amd64"
;;
ppc64le)
arch="ppc64le"
;;
s390x)
arch="s390x"
;;
aarch64)
arch="arm64"
;;
armv7l)
arch="arm32v7"
;;
*)
echo "$0 does not support architecture $arch ... aborting"
exit 1
;;
esac
echo "$arch"
}
# Get corresponding variants based on the architecture.
# All supported variants of each supported architecutre are listed in a
# file - 'architectures'. Its format is:
# <architecutre 1> <supported variant 1 >,<supported variant 2>...
# <architecutre 2> <supported variant 1 >,<supported variant 2>...
function get_variants() {
local dir
dir=${1:-.}
shift
local arch
arch=$(get_arch)
local variants
variants=$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')
echo "$variants"
}
# Get supported architectures for a specific version and variant
#
# Get default supported architectures from 'architectures'. Then go to the version folder
# to see if there is a local architectures file. The local architectures will override the
# default architectures. This will give us some benefits:
# - a specific version may or may not support some architectures
# - if there is no specialization for a version, just don't provide local architectures
function get_supported_arches () {
local version
local variant
local arches
local lines
local line
version="$1"; shift
variant="$1"; shift
# Get default supported arches
lines=$( grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1 )
# Get version specific supported architectures if there is specialized information
if [ -a "$version"/architectures ]; then
lines=$( grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1 )
fi
while IFS='' read -r line; do
arches+=( "$line" )
done <<< "$lines"
echo "${arches[@]}"
}
# Get configuration values from the config file
#
# The configuration entries are simple key/value pairs which are whitespace separated.
function get_config () {
local dir
dir=${1:-.}
shift
local name
name=$1
shift
local value
value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//')
echo "$value"
}
# Get available versions for a given path
#
# If full or partial versions are provided then they are processed and
# validated. e.g. "4 chakracore" returns "4 chakracore/8" since it processed the
# chakracore entry and found it to be a fork rather than a complete version.
#
# The result is a list of valid versions.
function get_versions () {
local prefix
prefix=${1:-.}
shift
local versions
local dirs=( "$@" )
if [ ${#dirs[@]} -eq 0 ]; then
IFS=' ' read -ra dirs <<< "$(echo "${prefix%/}/"*/)"
fi
for dir in "${dirs[@]}"; do
if [ -a "$dir/config" ]; then
local subdirs
IFS=' ' read -ra subdirs <<< "$(get_versions "${dir#./}")"
for subdir in "${subdirs[@]}"; do
versions+=( "$subdir" )
done
elif [ -a "$dir/Dockerfile" ]; then
versions+=( "${dir#./}" )
fi
done
if [ ${#versions[@]} -gt 0 ]; then
echo "${versions[@]%/}"
fi
}
function get_fork_name () {
local version
version=$1
shift
IFS='/' read -ra versionparts <<< "$version"
if [ ${#versionparts[@]} -gt 1 ]; then
echo "${versionparts[0]}"
fi
}
function get_full_version () {
local version
version=$1
shift
grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3
}
function get_major_minor_version () {
local version
version=$1
shift
local fullversion
fullversion=$(get_full_version "$version")
echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)"
}
function get_tag () {
local version
version=$1
shift
local versiontype
versiontype=${1:-full}
shift
local tagversion
if [ "$versiontype" = full ]; then
tagversion=$(get_full_version "$version")
elif [ "$versiontype" = majorminor ]; then
tagversion=$(get_major_minor_version "$version")
fi
local tagparts
IFS=' ' read -ra tagparts <<< "$(get_fork_name "$version") $tagversion"
IFS='-'; echo "${tagparts[*]}"; unset IFS
}
function sort_versions () {
local versions=( "$@" )
local sorted
local lines
local line
IFS=$'\n'
lines="${versions[*]}"
unset IFS
while IFS='' read -r line; do
sorted+=( "$line" )
done <<< "$(echo "$lines" | grep "^[0-9]" | sort -r)"
while IFS='' read -r line; do
sorted+=( "$line" )
done <<< "$(echo "$lines" | grep -v "^[0-9]" | sort -r)"
echo "${sorted[@]}"
}