ulysses-you commented on a change in pull request #1435:
URL: https://github.com/apache/incubator-kyuubi/pull/1435#discussion_r757191290



##########
File path: bin/docker-image-tool.sh
##########
@@ -0,0 +1,263 @@
+#!/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:$KYUUBI_VERSION"
+}
+
+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
+
+  # cp spark for kyuubi as submit client
+  # if user set -s(spark-provider), use if
+  # else use builtin spark
+  if [[ ! -d "$KYUUBI_ROOT/spark-binary" ]]; then
+    mkdir "$KYUUBI_ROOT/spark-binary"
+  fi
+  cp -r "$SPARK_HOME/" "$KYUUBI_ROOT/spark-binary/"
+
+  # 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"
+}
+
+function usage {
+  cat <<EOF
+Usage: $0 [options] [command]
+Builds or pushes the built-in Kyuubi Docker image.
+
+Commands:
+  build       Build image. Requires a repository address to be provided if the 
image will be
+              pushed to a different registry.
+  push        Push a pre-built image to a registry. Requires a repository 
address to be provided.
+
+Options:
+  -f                    Dockerfile to build for JVM based Jobs. By default 
builds the Dockerfile shipped with Kyuubi.
+  -r                    Repository address.
+  -t                    Tag to apply to the built image, or to identify the 
image to be pushed.
+  -n                    Build docker image with --no-cache
+  -u                    UID to use in the USER directive to set the user the 
main Kyuubi process runs as inside the
+                        resulting container
+  -X                    Use docker buildx to cross build. Automatically pushes.
+                        See 
https://docs.docker.com/buildx/working-with-buildx/ for steps to setup buildx.
+  -b                    Build arg to build or push the image. For multiple 
build args, this option needs to
+                        be used separately for each build arg.
+  -s                    Put the specified Spark into the Kyuubi image to be 
used as the internal SPARK_HOME
+                        of the container.
+
+Examples:
+  - Build image in minikube with tag "testing"
+    $0 -m -t testing build
+
+  - Build and push image with tag "v1.4.0" to docker.io/myrepo
+    $0 -r docker.io/myrepo -t v1.4.0 build
+    $0 -r docker.io/myrepo -t v1.4.0 push
+
+  - Build and push with tag "v3.0.0" and Spark-3.1.2 as base image to 
docker.io/myrepo
+    $0 -r docker.io/myrepo -t v3.0.0 -b BASE_IMAGE=repo/spark:3.1.2 build

Review comment:
       ah,  I see




-- 
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]


Reply via email to