-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.sh
executable file
·223 lines (195 loc) · 7.31 KB
/
util.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
219
220
221
222
223
#!/bin/bash
SCRIPT_DIR=$(dirname $(readlink -f $0))
SCRIPT_NAME=$(basename "$0")
fn_exists() {
# appended double quote is an ugly trick to make sure we do get a string -- if $1 is not a known command, type does not output anything
[ `type -t $1`"" == 'function' ]
}
export -f fn_exists
pushd () {
command pushd "$@" > /dev/null
}
export -f pushd
popd () {
command popd "$@" > /dev/null
}
export -f popd
# Based on: https://stackoverflow.com/a/28926650
ingroup () {
[[ " `id -Gn ${2-}` " == *" $1 "* ]];
}
export -f ingroup
# Check presence of Java/JDK.
check_java () {
if ! command -v java &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - java could not be found. Please ensure Java 8 or greater is available. Exiting."
exit 1
fi
JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{print $1$2}')
if [ "$JAVA_VERSION" -ge 18 ] ; then
echo "[$SCRIPT_NAME][INFO] - Java version: $JAVA_VERSION (found in \$PATH)."
JAVA_HOME_VERSION=$($JAVA_HOME/bin/java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{print $1$2}')
printf "[$SCRIPT_NAME][INFO] - JAVA_HOME (used by Maven and Gradle in this repository):\n\t$JAVA_HOME\n\tJava version: $JAVA_HOME_VERSION\n"
else
echo "[$SCRIPT_NAME][ERROR] - Java version must be greater or equal to 8. Exiting."
exit 1
fi
}
export -f check_java
# Check presence of Maven.
check_maven () {
if ! command -v mvn &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - 'mvn' could not be found. Please install it. Exiting."
exit 1
else
MVN_VERSION=$(mvn -v | grep Apache | cut -d' ' -f3-4)
echo "[$SCRIPT_NAME][INFO] - Maven version: $MVN_VERSION."
fi
}
export -f check_maven
# Check presence of Gradle.
check_gradle() {
if [ "$#" -gt 2 ]; then
echo "[$SCRIPT_NAME][ERROR] - function 'check_gradle' takes at most 1 argument. Exiting."
exit 1
fi
#echo $1
if [ "$1" = "-g" ]; then
if ! command -v gradle &> /dev/null
then
echo "[$SCRIPT_NAME][INFO] - 'gradle' not found on path, defaulting to repository 'gradlew'."
if ! command -v ./phylodb/gradlew &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - 'gradlew' could not be found on the repository. There should be a 'phyloDB-official.git/phylodb/gradlew'. Exiting."
exit 1
else
GRADLE_VERSION=$(./phylodb/gradlew -v | grep Gradle | cut -d' ' -f2)
echo "[$SCRIPT_NAME][INFO] - Gradle version ('gradlew' from repository): $GRADLE_VERSION."
fi
else
GRADLE_VERSION=$(gradle -v | grep Gradle | cut -d' ' -f2)
echo "[$SCRIPT_NAME][INFO] - Gradle version (found \$PATH): $GRADLE_VERSION."
fi
else
GRADLE_VERSION=$(./phylodb/gradlew -v | grep Gradle | cut -d' ' -f2)
echo "[$SCRIPT_NAME][INFO] - Gradle version ('gradlew' from repository): $GRADLE_VERSION."
fi
}
export -f check_gradle
get_gradle_binary() {
if [ "$#" -gt 2 ]; then
echo "[$SCRIPT_NAME][ERROR] - function 'get_gradle_binary' takes at most 1 argument. Exiting."
exit 1
fi
if [ "$1" = "-g" ]; then
if command -v gradle &> /dev/null
then
echo "gradle"
else
echo "[$SCRIPT_NAME][INFO] - 'gradle' not found on path, defaulting to repository 'gradlew'."
if ! command -v ./phylodb/gradlew &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - 'gradlew' could not be found on the repository. There should be a 'phyloDB-official.git/phylodb/gradlew'. Exiting."
exit 1
else
REPO_GRADLEW_PATH="$(pwd)/gradlew"
echo "$REPO_GRADLEW_PATH"
fi
fi
else
REPO_GRADLEW_PATH="$(pwd)/gradlew"
echo "$REPO_GRADLEW_PATH"
fi
}
export -f get_gradle_binary
# Check presence of Docker.
check_docker() {
if ! command -v docker &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - 'docker' could not be found. Please install it. Exiting."
exit 1
fi
DOCKER_VERSION=$(docker -v | cut -d' ' -f3-5)
echo "[$SCRIPT_NAME][INFO] - Docker version: $DOCKER_VERSION."
# Check if the Docker daemon is running.
docker version > /dev/null 2>&1
ret_status=$?
if [ $ret_status -eq 0 ]; then
echo "[$SCRIPT_NAME][INFO] - Docker daemon is running."
else
echo "[$SCRIPT_NAME][ERROR] - Docker daemon is not running. Please launch it. Exiting."
exit 1
fi
# Check if current user is in 'docker' group.
if ingroup "docker"; then
echo "[$SCRIPT_NAME][INFO] - User '$USER' is in group 'docker'."
else
echo "[$SCRIPT_NAME][ERROR] - User '$USER' is not in group 'docker', please check with your administrator. Exiting."
exit 1
fi
if ! command -v docker-compose &> /dev/null
then
echo "[$SCRIPT_NAME][ERROR] - 'docker-compose' could not be found. Please install it. Exiting."
exit 1
fi
}
export -f check_docker
get_docker_build_command() {
DOCKER_CMD_INFO=$(docker buildx 2>&1 | head -n 1 | grep 'not a docker command')
if [[ $string == *"not a docker command"* ]]; then
echo "build"
else
echo "buildx"
fi
}
export -f get_docker_build_command
get_algorithms_name() {
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd "$SCRIPT_DIR/algorithms"
ALGO_NAME=$(cat pom.xml | grep "artifactId" | cut -d'>' -f2 | cut -d'<' -f1 | head -n 1)
popd
echo "$ALGO_NAME"
}
export -f get_algorithms_name
get_algorithms_neo4j_version() {
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd "$SCRIPT_DIR/algorithms"
ALGORITHMS_POM_NEO4J_VERSION=$(cat pom.xml | grep "neo4j.version" | head -n 1 | cut -d'>' -f2 | cut -d'<' -f1)
popd
echo "$ALGORITHMS_POM_NEO4J_VERSION"
}
export -f get_algorithms_neo4j_version
get_algorithms_version() {
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd "$SCRIPT_DIR/algorithms"
ALGO_VERSION=$(cat pom.xml | grep "version" | cut -d'>' -f2 | cut -d'<' -f1 | head -n 1)
popd
echo "$ALGO_VERSION"
}
export -f get_algorithms_version
get_phylodb_name() {
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd "$SCRIPT_DIR/phylodb"
PHYLODB_NAME=$(cat settings.gradle | cut -d' ' -f3 | cut -d "'" -f2)
popd
echo "$PHYLODB_NAME"
}
export -f get_phylodb_name
get_phylodb_version() {
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd "$SCRIPT_DIR/phylodb"
PHYLODB_VERSION=$(cat build.gradle | grep "version = " | cut -d'=' -f2 | cut -d"'" -f2)
popd
echo "$PHYLODB_VERSION"
}
export -f get_phylodb_version
get_latest_stable_neo4j_version() {
NEO4J_APOC_URL="https://github.com/neo4j/apoc/releases/latest"
# Need to go through URL redirect. Based on: https://stackoverflow.com/a/5300429
NEO4J_LATEST_APOC_URL=$(curl "$NEO4J_APOC_URL" -s -L -I -o /dev/null -w '%{url_effective}')
NEO4J_LATEST_VERSION=$(echo "$NEO4J_LATEST_APOC_URL" | awk -F/ '{print $NF}')
echo "$NEO4J_LATEST_VERSION"
}
export -f get_latest_stable_neo4j_version