|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +# This script builds and pushes docker images when run from a release of Kyuubi |
| 20 | +# with Kubernetes support. |
| 21 | + |
| 22 | +function error { |
| 23 | + echo "$@" 1>&2 |
| 24 | + exit 1 |
| 25 | +} |
| 26 | + |
| 27 | +if [ -z "${KYUUBI_HOME}" ]; then |
| 28 | + KYUUBI_HOME="$(cd "`dirname "$0"`"/..; pwd)" |
| 29 | +fi |
| 30 | + |
| 31 | +CTX_DIR="$KYUUBI_HOME/target/tmp/docker" |
| 32 | + |
| 33 | +function is_dev_build { |
| 34 | + [ ! -f "$KYUUBI_HOME/RELEASE" ] |
| 35 | +} |
| 36 | + |
| 37 | +function cleanup_ctx_dir { |
| 38 | + if is_dev_build; then |
| 39 | + rm -rf "$CTX_DIR" |
| 40 | + fi |
| 41 | +} |
| 42 | +trap cleanup_ctx_dir EXIT |
| 43 | + |
| 44 | +function image_ref { |
| 45 | + local image="$1" |
| 46 | + local add_repo="${2:-1}" |
| 47 | + if [ $add_repo = 1 ] && [ -n "$REPO" ]; then |
| 48 | + image="$REPO/$image" |
| 49 | + fi |
| 50 | + if [ -n "$TAG" ]; then |
| 51 | + image="$image:$TAG" |
| 52 | + fi |
| 53 | + echo "$image:$KYUUBI_VERSION" |
| 54 | +} |
| 55 | + |
| 56 | +function docker_push { |
| 57 | + local image_name="$1" |
| 58 | + if [ ! -z $(docker images -q "$(image_ref ${image_name})") ]; then |
| 59 | + docker push "$(image_ref ${image_name})" |
| 60 | + if [ $? -ne 0 ]; then |
| 61 | + error "Failed to push $image_name Docker image." |
| 62 | + fi |
| 63 | + else |
| 64 | + echo "$(image_ref ${image_name}) image not found. Skipping push for this image." |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +function resolve_file { |
| 69 | + local FILE=$1 |
| 70 | + if [ -n "$FILE" ]; then |
| 71 | + local DIR=$(dirname $FILE) |
| 72 | + DIR=$(cd $DIR && pwd) |
| 73 | + FILE="${DIR}/$(basename $FILE)" |
| 74 | + fi |
| 75 | + echo $FILE |
| 76 | +} |
| 77 | + |
| 78 | +# Create a smaller build context for docker in dev builds to make the build faster. Docker |
| 79 | +# uploads all of the current directory to the daemon, and it can get pretty big with dev |
| 80 | +# builds that contain test log files and other artifacts. |
| 81 | +# |
| 82 | +# Note: docker does not support symlinks in the build context. |
| 83 | +function create_dev_build_context {( |
| 84 | + set -e |
| 85 | + local BASE_CTX="$CTX_DIR/base" |
| 86 | + mkdir -r "$BASE_CTX/docker" |
| 87 | + cp -r "docker/" "$BASE_CTX/docker" |
| 88 | + |
| 89 | + cp -r "kyuubi-assembly/target/scala-${KYUUBI_SCALA_VERSION}/jars" "$BASE_CTX/jars" |
| 90 | + |
| 91 | + mkdir -p "$BASE_CTX/externals/engines/spark" |
| 92 | + cp "$KYUUBI_HOME/externals/kyuubi-spark-sql-engine/target/kyuubi-spark-sql-engine_${KYUUBI_SCALA_VERSION}-${KYUUBI_VERSION}.jar" "$BASE_CTX/externals/engines/spark" |
| 93 | + |
| 94 | + for other in bin conf; do |
| 95 | + cp -r "$other" "$BASE_CTX/$other" |
| 96 | + done |
| 97 | +)} |
| 98 | + |
| 99 | +function img_ctx_dir { |
| 100 | + if is_dev_build; then |
| 101 | + echo "$CTX_DIR/$1" |
| 102 | + else |
| 103 | + echo "$KYUUBI_HOME" |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +function build { |
| 108 | + local BUILD_ARGS |
| 109 | + local KYUUBI_ROOT="$KYUUBI_HOME" |
| 110 | + |
| 111 | + if is_dev_build; then |
| 112 | + create_dev_build_context || error "Failed to create docker build context." |
| 113 | + KYUUBI_ROOT="$CTX_DIR/base" |
| 114 | + fi |
| 115 | + |
| 116 | + # cp spark for kyuubi as submit client |
| 117 | + # if user set -s(spark-provider), use if |
| 118 | + # else use builtin spark |
| 119 | + if [[ ! -d "$KYUUBI_ROOT/spark-binary" ]]; then |
| 120 | + mkdir "$KYUUBI_ROOT/spark-binary" |
| 121 | + fi |
| 122 | + cp -r "$SPARK_HOME/" "$KYUUBI_ROOT/spark-binary/" |
| 123 | + |
| 124 | + # Verify that the Docker image content directory is present |
| 125 | + if [ ! -d "$KYUUBI_ROOT/docker" ]; then |
| 126 | + error "Cannot find docker image. This script must be run from a runnable distribution of Apache Kyuubi." |
| 127 | + fi |
| 128 | + |
| 129 | + # Verify that Kyuubi has actually been built/is a runnable distribution |
| 130 | + # i.e. the Kyuubi JARs that the Docker files will place into the image are present |
| 131 | + local TOTAL_JARS=$(ls $KYUUBI_ROOT/jars/kyuubi-* | wc -l) |
| 132 | + TOTAL_JARS=$(( $TOTAL_JARS )) |
| 133 | + if [ "${TOTAL_JARS}" -eq 0 ]; then |
| 134 | + error "Cannot find Kyuubi JARs. This script assumes that Apache Kyuubi has first been built locally or this is a runnable distribution." |
| 135 | + fi |
| 136 | + |
| 137 | + local BUILD_ARGS=(${BUILD_PARAMS}) |
| 138 | + |
| 139 | + # If a custom Kyuubi_UID was set add it to build arguments |
| 140 | + if [ -n "$KYUUBI_UID" ]; then |
| 141 | + BUILD_ARGS+=(--build-arg kyuubi_uid=$KYUUBI_UID) |
| 142 | + fi |
| 143 | + |
| 144 | + local BINDING_BUILD_ARGS=( |
| 145 | + ${BUILD_ARGS[@]} |
| 146 | + --build-arg |
| 147 | + base_img=$(image_ref kyuubi) |
| 148 | + ) |
| 149 | + |
| 150 | + local BASEDOCKERFILE=${BASEDOCKERFILE:-"docker/Dockerfile"} |
| 151 | + local ARCHS=${ARCHS:-"--platform linux/amd64,linux/arm64"} |
| 152 | + |
| 153 | + (cd $(img_ctx_dir base) && docker build $NOCACHEARG "${BUILD_ARGS[@]}" \ |
| 154 | + -t $(image_ref kyuubi) \ |
| 155 | + -f "$BASEDOCKERFILE" .) |
| 156 | + if [ $? -ne 0 ]; then |
| 157 | + error "Failed to build Kyuubi JVM Docker image, please refer to Docker build output for details." |
| 158 | + fi |
| 159 | + if [ "${CROSS_BUILD}" != "false" ]; then |
| 160 | + (cd $(img_ctx_dir base) && docker buildx build $ARCHS $NOCACHEARG "${BUILD_ARGS[@]}" --push \ |
| 161 | + -t $(image_ref kyuubi) \ |
| 162 | + -f "$BASEDOCKERFILE" .) |
| 163 | + fi |
| 164 | +} |
| 165 | + |
| 166 | +function push { |
| 167 | + docker_push "kyuubi" |
| 168 | +} |
| 169 | + |
| 170 | +function usage { |
| 171 | + cat <<EOF |
| 172 | +Usage: $0 [options] [command] |
| 173 | +Builds or pushes the built-in Kyuubi Docker image. |
| 174 | +
|
| 175 | +Commands: |
| 176 | + build Build image. Requires a repository address to be provided if the image will be |
| 177 | + pushed to a different registry. |
| 178 | + push Push a pre-built image to a registry. Requires a repository address to be provided. |
| 179 | +
|
| 180 | +Options: |
| 181 | + -f Dockerfile to build for JVM based Jobs. By default builds the Dockerfile shipped with Kyuubi. |
| 182 | + -r Repository address. |
| 183 | + -t Tag to apply to the built image, or to identify the image to be pushed. |
| 184 | + -n Build docker image with --no-cache |
| 185 | + -u UID to use in the USER directive to set the user the main Kyuubi process runs as inside the |
| 186 | + resulting container |
| 187 | + -X Use docker buildx to cross build. Automatically pushes. |
| 188 | + See https://docs.docker.com/buildx/working-with-buildx/ for steps to setup buildx. |
| 189 | + -b Build arg to build or push the image. For multiple build args, this option needs to |
| 190 | + be used separately for each build arg. |
| 191 | + -s Put the specified Spark into the Kyuubi image to be used as the internal SPARK_HOME |
| 192 | + of the container. |
| 193 | +
|
| 194 | +Examples: |
| 195 | +
|
| 196 | + - Build and push image with tag "v1.4.0" to docker.io/myrepo |
| 197 | + $0 -r docker.io/myrepo -t v1.4.0 build |
| 198 | + $0 -r docker.io/myrepo -t v1.4.0 push |
| 199 | +
|
| 200 | + - Build and push with tag "v3.0.0" and Spark-3.1.2 as base image to docker.io/myrepo |
| 201 | + $0 -r docker.io/myrepo -t v3.0.0 -b BASE_IMAGE=repo/spark:3.1.2 build |
| 202 | + $0 -r docker.io/myrepo -t v3.0.0 push |
| 203 | +
|
| 204 | + - Build and push for multiple archs to docker.io/myrepo |
| 205 | + $0 -r docker.io/myrepo -t v3.0.0 -X build |
| 206 | +
|
| 207 | + # Note: buildx, which does cross building, needs to do the push during build |
| 208 | + # So there is no separate push step with -X |
| 209 | +
|
| 210 | + - Build with Spark placed "/path/spark" |
| 211 | + $0 -s /path/spark build |
| 212 | +
|
| 213 | +EOF |
| 214 | +} |
| 215 | + |
| 216 | +if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then |
| 217 | + usage |
| 218 | + exit 0 |
| 219 | +fi |
| 220 | + |
| 221 | +REPO= |
| 222 | +TAG= |
| 223 | +BASEDOCKERFILE= |
| 224 | +NOCACHEARG= |
| 225 | +BUILD_PARAMS= |
| 226 | +KYUUBI_UID= |
| 227 | +CROSS_BUILD="false" |
| 228 | +while getopts f:r:t:Xnb:u:s: option |
| 229 | +do |
| 230 | + case "${option}" |
| 231 | + in |
| 232 | + f) BASEDOCKERFILE=$(resolve_file ${OPTARG});; |
| 233 | + r) REPO=${OPTARG};; |
| 234 | + t) TAG=${OPTARG};; |
| 235 | + n) NOCACHEARG="--no-cache";; |
| 236 | + b) BUILD_PARAMS=${BUILD_PARAMS}" --build-arg "${OPTARG};; |
| 237 | + X) CROSS_BUILD=1;; |
| 238 | + u) KYUUBI_UID=${OPTARG};; |
| 239 | + s) SPARK_HOME=${OPTARG};; |
| 240 | + esac |
| 241 | +done |
| 242 | + |
| 243 | +. "${KYUUBI_HOME}/bin/load-kyuubi-env.sh" |
| 244 | +case "${@: -1}" in |
| 245 | + build) |
| 246 | + build |
| 247 | + ;; |
| 248 | + push) |
| 249 | + if [ -z "$REPO" ]; then |
| 250 | + usage |
| 251 | + exit 1 |
| 252 | + fi |
| 253 | + push |
| 254 | + ;; |
| 255 | + *) |
| 256 | + usage |
| 257 | + exit 1 |
| 258 | + ;; |
| 259 | +esac |
0 commit comments