-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathinstall-ambari-python.sh
More file actions
executable file
·284 lines (259 loc) · 8.65 KB
/
Copy pathinstall-ambari-python.sh
File metadata and controls
executable file
·284 lines (259 loc) · 8.65 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Requires a CPython 3.9.2+ runtime and a CPython 3.10+ build interpreter.
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
BUILD_VENV="$SCRIPT_DIR/target/ambari-python-build-venv"
BUILD_PYTHON="$BUILD_VENV/bin/python"
BUILD_PYTHON_LAUNCHER="$SCRIPT_DIR/dev-support/ambari-python-build"
RUNTIME_VENV="$SCRIPT_DIR/target/ambari-python-runtime-venv"
RUNTIME_PYTHON="$RUNTIME_VENV/bin/python"
WHEELHOUSE=""
function print_help() {
cat << EOF
Usage: ./install-ambari-python.sh [additional options]
-c, --clean clean generated python distribution directories
-d, --deploy deploy ambari-python artifact to maven remote repository
-v, --version <version> override ambari-python artifact versison
-i, --repository-id <id> repository id in settings.xml for remote repository
-r, --repository-url <url> repository url of remote repository
-w, --wheelhouse <path> install locked build tools from an offline wheelhouse
-b, --build-python <path> use this Python 3.10+ interpreter for package builds
-h, --help print help
EOF
}
function get_python_artifact_file() {
local artifacts=()
while IFS= read -r artifact; do
artifacts+=("$artifact")
done < <(find "$SCRIPT_DIR/dist" -maxdepth 1 -type f \
\( -name 'ambari-python-*.tar.gz' -o -name 'ambari_python-*.tar.gz' \) \
-print 2>/dev/null)
if [[ ${#artifacts[@]} -ne 1 ]]; then
echo "Expected exactly one ambari-python source distribution" >&2
return 1
fi
basename "${artifacts[0]}"
}
function get_python_wheel_file() {
local artifacts=()
while IFS= read -r artifact; do
artifacts+=("$artifact")
done < <(find "$SCRIPT_DIR/dist" -maxdepth 1 -type f \
-name 'ambari_python-*.whl' -print 2>/dev/null)
if [[ ${#artifacts[@]} -ne 1 ]]; then
echo "Expected exactly one ambari-python wheel" >&2
return 1
fi
basename "${artifacts[0]}"
}
function get_version() {
local artifact_file
artifact_file=$(get_python_artifact_file)
local artifact_version=${artifact_file#ambari-python-}
artifact_version=${artifact_version#ambari_python-}
echo "${artifact_version%.tar.gz}"
}
function clean() {
if [[ -d "$SCRIPT_DIR/dist" ]]; then
echo "Removing '$SCRIPT_DIR/dist' directoy ..."
rm -r "$SCRIPT_DIR/dist"
echo "Directory '$SCRIPT_DIR/dist' successfully deleted."
fi
if [[ -d "$SCRIPT_DIR/ambari_python.egg-info" ]]; then
echo "Removing '$SCRIPT_DIR/ambari_python.egg-info' directoy ..."
rm -r "$SCRIPT_DIR/ambari_python.egg-info"
echo "Directory '$SCRIPT_DIR/ambari_python.egg-info' successfully deleted."
fi
if [[ -d "$SCRIPT_DIR/target/ambari-python-dist" ]]; then
echo "Removing '$SCRIPT_DIR/target/ambari-python' directoy ..."
rm -r "$SCRIPT_DIR/target/ambari-python-dist"
echo "Directory '$SCRIPT_DIR/target/ambari-python' successfully deleted."
fi
if [[ -d "$BUILD_VENV" ]]; then
echo "Removing '$BUILD_VENV' directory ..."
rm -r "$BUILD_VENV"
fi
if [[ -d "$RUNTIME_VENV" ]]; then
echo "Removing '$RUNTIME_VENV' directory ..."
rm -r "$RUNTIME_VENV"
fi
}
function select_python() {
local candidates=()
if [[ -n ${AMBARI_PYTHON:-} ]]; then
candidates+=("$AMBARI_PYTHON")
fi
candidates+=(/usr/bin/python3.9 /usr/local/bin/python3 python3)
local candidate
for candidate in "${candidates[@]}"; do
if command -v "$candidate" >/dev/null 2>&1 \
&& "$candidate" -c 'import sys; raise SystemExit(sys.version_info < (3, 9, 2))'; then
AMBARI_PYTHON=$(command -v "$candidate")
return 0
fi
done
echo "A Python 3.9.2+ interpreter is required; set AMBARI_PYTHON explicitly" >&2
return 1
}
function prepare_build_environment() {
mkdir -p "$SCRIPT_DIR/target"
"$BUILD_PYTHON_LAUNCHER" -m venv "$BUILD_VENV"
local pip_args=(
install
--disable-pip-version-check
--only-binary=:all:
--require-hashes
--requirement "$SCRIPT_DIR/requirements-build.lock"
)
if [[ -n "$WHEELHOUSE" ]]; then
pip_args+=(--no-index --find-links "$WHEELHOUSE")
fi
"$BUILD_PYTHON" -m pip "${pip_args[@]}"
}
function prepare_runtime_environment() {
"$AMBARI_PYTHON" -m venv "$RUNTIME_VENV"
}
function generate_site_packages() {
local artifact="$1"
local destination="$SCRIPT_DIR/target/ambari-python-dist/site-packages"
mkdir -p "$destination"
"$RUNTIME_PYTHON" -m pip install \
"$SCRIPT_DIR/dist/$artifact" \
--disable-pip-version-check \
--no-build-isolation \
--no-compile \
--no-deps \
--target "$destination"
}
function archive_python_dist() {
local artifact="$1"
local base_dir="$SCRIPT_DIR/target/ambari-python-dist"
if [[ -f "$SCRIPT_DIR/target/$artifact" ]]; then
echo "Removing '$SCRIPT_DIR/target/$artifact' file ..."
echo "File '$SCRIPT_DIR/target/$artifact' successfully deleted."
fi
tar -zcf "$SCRIPT_DIR/target/$artifact" -C "$base_dir" site-packages
}
function install() {
local artifact_file="$1"
local version="$2"
mvn install:install-file -Dfile="$artifact_file" -DgeneratePom=true -Dversion="$version" -DartifactId=ambari-python -DgroupId=org.apache.ambari -Dpackaging=tar.gz
}
function deploy() {
local artifact_file="$1"
local version="$2"
local repo_id="$3"
local repo_url="$4"
mvn gpg:sign-and-deploy-file -Dfile="$artifact_file" -Dpackaging=tar.gz -DgeneratePom=true -Dversion="$version" -DartifactId=ambari-python -DgroupId=org.apache.ambari -Durl="$repo_url" -DrepositoryId="$repo_id"
}
function build_distributions() {
local version="$1"
if [[ -n "$version" ]]; then
(cd "$SCRIPT_DIR" && env AMBARI_VERSION="$version" "$BUILD_PYTHON" -m build --sdist --wheel --no-isolation)
else
(cd "$SCRIPT_DIR" && "$BUILD_PYTHON" -m build --sdist --wheel --no-isolation)
fi
}
function main() {
local DEPLOY="false"
local CLEAN="false"
local VERSION=""
local REPOSITORY_ID=""
local REPOSITORY_URL=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-d|--deploy)
DEPLOY="true"
shift 1
;;
-c|--clean)
CLEAN="true"
shift 1
;;
-v|--version)
VERSION="$2"
shift 2
;;
-i|--repository-id)
REPOSITORY_ID="$2"
shift 2
;;
-r|--repository-url)
REPOSITORY_URL="$2"
shift 2
;;
-w|--wheelhouse)
WHEELHOUSE="$2"
shift 2
;;
-b|--build-python)
AMBARI_BUILD_PYTHON="$2"
export AMBARI_BUILD_PYTHON
shift 2
;;
-h|--help)
shift 1
print_help
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
clean
if [[ "$CLEAN" == "true" ]]; then
return 0
fi
select_python
if [[ -n "$WHEELHOUSE" && ! -d "$WHEELHOUSE" ]]; then
echo "Wheelhouse does not exist: $WHEELHOUSE" >&2
return 1
fi
prepare_build_environment
prepare_runtime_environment
build_distributions "$VERSION"
local artifact_name
artifact_name=$(get_python_artifact_file)
local wheel_name
wheel_name=$(get_python_wheel_file)
local artifact_version
artifact_version=$(get_version)
generate_site_packages "$wheel_name"
archive_python_dist "$artifact_name"
install "$SCRIPT_DIR/target/$artifact_name" "$artifact_version"
if [[ "$DEPLOY" == "true" ]] ; then
if [[ -z "$REPOSITORY_ID" ]] ; then
echo "Repository id option is required for deploying ambari-python artifact (-i or --repository-id)"
exit 1
fi
if [[ -z "$REPOSITORY_URL" ]] ; then
echo "Repository url option is required for deploying ambari-python artifact (-r or --repository-url)"
exit 1
fi
deploy "$SCRIPT_DIR/target/$artifact_name" "$artifact_version" "$REPOSITORY_ID" "$REPOSITORY_URL"
else
echo "Skip deploying ambari-python artifact to remote repository."
fi
}
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
main "$@"
fi