zwangsheng commented on a change in pull request #1435: URL: https://github.com/apache/incubator-kyuubi/pull/1435#discussion_r754996347
########## File path: bin/docker-image-tool.sh ########## @@ -0,0 +1,267 @@ +#!/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. +# + +# This script builds and pushes docker images when run from a release of Kyuubi +# with Kubernetes support. + +function error { + echo "$@" 1>&2 + exit 1 +} + +if [ -z "${KYUUBI_HOME}" ]; then + KYUUBI_HOME="$(cd "`dirname "$0"`"/..; pwd)" +fi + +CTX_DIR="$KYUUBI_HOME/target/tmp/docker" + +function is_dev_build { + [ ! -f "$KYUUBI_HOME/RELEASE" ] +} + +function cleanup_ctx_dir { + if is_dev_build; then + rm -rf "$CTX_DIR" + fi +} +trap cleanup_ctx_dir EXIT + +# trap cleanup_ctx_dir EXIT + +function image_ref { + local image="$1" + local add_repo="${2:-1}" + if [ $add_repo = 1 ] && [ -n "$REPO" ]; then + image="$REPO/$image" + fi + if [ -n "$TAG" ]; then + image="$image:$TAG" + fi + echo "$image" +} + +function docker_push { + local image_name="$1" + if [ ! -z $(docker images -q "$(image_ref ${image_name})") ]; then + docker push "$(image_ref ${image_name})" + if [ $? -ne 0 ]; then + error "Failed to push $image_name Docker image." + fi + else + echo "$(image_ref ${image_name}) image not found. Skipping push for this image." + fi +} + +function resolve_file { + local FILE=$1 + if [ -n "$FILE" ]; then + local DIR=$(dirname $FILE) + DIR=$(cd $DIR && pwd) + FILE="${DIR}/$(basename $FILE)" + fi + echo $FILE +} + +# Create a smaller build context for docker in dev builds to make the build faster. Docker +# uploads all of the current directory to the daemon, and it can get pretty big with dev +# builds that contain test log files and other artifacts. +# +# Note: docker does not support symlinks in the build context. +function create_dev_build_context {( + set -e + local BASE_CTX="$CTX_DIR/base" + mkdir -r "$BASE_CTX/docker" + cp -r "docker/" "$BASE_CTX/docker" + + cp -r "kyuubi-assembly/target/scala-${KYUUBI_SCALA_VERSION}/jars" "$BASE_CTX/jars" + + mkdir -p "$BASE_CTX/externals/engines/spark" + cp "$KYUUBI_HOME/externals/kyuubi-spark-sql-engine/target/kyuubi-spark-sql-engine_${KYUUBI_SCALA_VERSION}-${KYUUBI_VERSION}.jar" "$BASE_CTX/externals/engines/spark" + + for other in bin conf; do + cp -r "$other" "$BASE_CTX/$other" + done +)} + +function img_ctx_dir { + if is_dev_build; then + echo "$CTX_DIR/$1" + else + echo "$KYUUBI_HOME" + fi +} + +function build { + local BUILD_ARGS + local KYUUBI_ROOT="$KYUUBI_HOME" + + if is_dev_build; then + create_dev_build_context || error "Failed to create docker build context." + KYUUBI_ROOT="$CTX_DIR/base" + fi + + # Verify that the Docker image content directory is present + if [ ! -d "$KYUUBI_ROOT/docker" ]; then + error "Cannot find docker image. This script must be run from a runnable distribution of Apache Kyuubi." + fi + + # Verify that Kyuubi has actually been built/is a runnable distribution + # i.e. the Kyuubi JARs that the Docker files will place into the image are present + local TOTAL_JARS=$(ls $KYUUBI_ROOT/jars/kyuubi-* | wc -l) + TOTAL_JARS=$(( $TOTAL_JARS )) + if [ "${TOTAL_JARS}" -eq 0 ]; then + error "Cannot find Kyuubi JARs. This script assumes that Apache Kyuubi has first been built locally or this is a runnable distribution." + fi + + local BUILD_ARGS=(${BUILD_PARAMS}) + + # If a custom Kyuubi_UID was set add it to build arguments + if [ -n "$KYUUBI_UID" ]; then + BUILD_ARGS+=(--build-arg kyuubi_uid=$KYUUBI_UID) + fi + + local BINDING_BUILD_ARGS=( + ${BUILD_ARGS[@]} + --build-arg + base_img=$(image_ref kyuubi) + ) + + local BASEDOCKERFILE=${BASEDOCKERFILE:-"docker/Dockerfile"} + local ARCHS=${ARCHS:-"--platform linux/amd64,linux/arm64"} + + (cd $(img_ctx_dir base) && docker build $NOCACHEARG "${BUILD_ARGS[@]}" \ + -t $(image_ref kyuubi) \ + -f "$BASEDOCKERFILE" .) + if [ $? -ne 0 ]; then + error "Failed to build Kyuubi JVM Docker image, please refer to Docker build output for details." + fi + if [ "${CROSS_BUILD}" != "false" ]; then + (cd $(img_ctx_dir base) && docker buildx build $ARCHS $NOCACHEARG "${BUILD_ARGS[@]}" --push \ + -t $(image_ref kyuubi) \ + -f "$BASEDOCKERFILE" .) + fi +} + +function push { + docker_push "kyuubi" Review comment: We can use the information in this `load-kyuubi-env.sh` script to make a default value. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
