HBASE-21443 [hbase-connectors] Purge hbase-* modules from core now they've been moved...
[hbase.git] / dev-support / hbase_docker.sh
blobcc17cf7af012252c0caa63c6b728538cd7aab70c
1 #!/bin/bash
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 # hbase_docker.sh
21 # A driver script to build HBase master branch from source and start the HBase
22 # shell in a Docker container.
24 # Usage: This script automates the build process facilitated by the Dockerfile
25 # in the hbase_docker folder. In particular, it is assumed that an
26 # Oracle JDK .tar.gz and an Apache Maven .tar.gz file are both present in
27 # the same directory as the Dockerfile; this script can download and place
28 # those tarballs for you. Moreover, due to bugs in Docker, the docker build
29 # command occasionally fails, but then succeeds upon rerunning; this script
30 # will rerun the command and attempt to finish a build. Finally, this
31 # script allows you to specify a desired name for the Docker image being
32 # created, defaulting to "hbase_docker." For complete usage instructions,
33 # run this script with the -h or --help option.
35 # Example: To build HBase and start an HBase shell using Oracle JDK 7u67 and
36 # Apache Maven 3.2.3:
38 # # ./hbase_docker.sh -j http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.tar.gz \
39 # -m http://apache.claz.org/maven/maven-3/3.2.3/binaries/apache-maven-3.2.3-bin.tar.gz
42 # Before doing anything else, make sure Docker is installed.
43 if ! which docker &> /dev/null; then
44 cat >&2 << __EOF
45 Docker must be installed to run hbase_docker. Go to http://www.docker.io
46 for installation instructions. Exiting...
47 __EOF
48 exit 1
49 elif ! docker ps &> /dev/null; then
50 echo "Docker must be run as root or with sudo privileges. Exiting..." >&2
51 exit 1
54 # Usage message.
55 usage() {
56 SCRIPT=$(basename "${BASH_SOURCE}")
58 cat << __EOF
60 hbase_docker. A driver script for building HBase and starting the HBase shell
61 inside of a Docker container.
63 Usage: ${SCRIPT} [-j | --jdk <url>] [-m | --mvn <url>] [-n | --name <name>]
64 ${SCRIPT} -h | --help
66 -h | --help Show this screen.
67 -j | --jdk '<url>' A URL pointing to an x64 .tar.gz file of Oracle's JDK.
68 Using this argument implies acceptance of the Oracle
69 Binary Code License Agreement for Java SE. See www.oracle.com
70 for more details.
71 -m | --mvn '<url>' A URL pointing to an x64 .tar.gz file of Apache Maven.
72 -n | --name <name> The name to give to the Docker image created by this script.
73 If left blank, "hbase_docker" will be used.
74 __EOF
77 if ! [ ${#} -le 6 ]; then
78 usage >&2
79 exit 1
82 # Default Docker image name.
83 IMAGE_NAME=hbase_docker
85 while ((${#})); do
86 case "${1}" in
87 -h | --help )
88 usage; exit 0 ;;
89 -j | --jdk )
90 JDK_URL="${2}"; shift 2 ;;
91 -m | --mvn )
92 MAVEN_URL="${2}"; shift 2 ;;
93 -n | --name )
94 IMAGE_NAME="${2}"; shift 2 ;;
95 * )
96 usage >&2; exit 1 ;;
97 esac
98 done
100 # The relative file path to the directory containing this script. This allows the
101 # script to be run from any working directory and still have it place downloaded
102 # files in the right locations.
103 SCRIPT_DIRECTORY=$(dirname ${BASH_SOURCE})
105 # If JDK_URL is set, download the JDK into the hbase_docker folder.
106 if [ -n "${JDK_URL}" ]; then
107 echo "Downloading Oracle JDK..."
108 ORACLE_HEADER="Cookie: oraclelicense=accept-securebackup-cookie"
109 if ! wget -nv -N --header "${ORACLE_HEADER}" -P "${SCRIPT_DIRECTORY}/hbase_docker" \
110 "${JDK_URL}"; then
111 echo "Error downloading Oracle JDK. Exiting..." >&2
112 exit 1
116 # If MAVEN_URL is set, download Maven into the hbase_docker folder.
117 if [ -n "${MAVEN_URL}" ]; then
118 echo "Downloading Apache Maven..."
119 if ! wget -nv -N -P "${SCRIPT_DIRECTORY}/hbase_docker" "${MAVEN_URL}"; then
120 echo "Error downloading Apache Maven. Exiting..." >&2
121 exit 1
125 # Before running docker build, confirm that the hbase_docker folder contains
126 # one JDK .tar.gz and one Maven .tar.gz.
127 FILE_CHECK_EXIT_CODE=0
128 for file in jdk maven; do
129 NUM_FILES=$(ls -l "${SCRIPT_DIRECTORY}/hbase_docker/"*${file}*.tar.gz 2>/dev/null | \
130 wc -l)
131 if [ ${NUM_FILES} -eq 0 ]; then
132 echo "Could not detect tarball containing \"${file}\" in hbase_docker folder." >&2
133 FILE_CHECK_EXIT_CODE=1
134 elif [ ${NUM_FILES} -gt 1 ]; then
135 echo "There are too many files containing \"${file}\" in hbase_docker folder." >&2
136 FILE_CHECK_EXIT_CODE=1
138 done
139 if [ ${FILE_CHECK_EXIT_CODE} -ne 0 ]; then
140 echo "Required dependencies not satisfied. Exiting..." >&2
141 exit 1
144 # docker build can be buggy (e.g. see https://github.com/docker/docker/issues/4036).
145 # To get around this, this script will try doing up to ${MAX_BUILD_ATTEMPTS} builds.
146 BUILD_ATTEMPTS=0
147 MAX_BUILD_ATTEMPTS=10
148 echo "Beginning docker build..."
149 until docker build -t ${IMAGE_NAME} ${SCRIPT_DIRECTORY}/hbase_docker; do
150 ((++BUILD_ATTEMPTS))
151 if [ ${BUILD_ATTEMPTS} -ge ${MAX_BUILD_ATTEMPTS} ]; then
152 echo "Build of ${IMAGE_NAME} failed after ${BUILD_ATTEMPTS} attempts. Exiting..." >&2
153 exit 1
154 else
155 echo "Build attempt #${BUILD_ATTEMPTS} of ${IMAGE_NAME} was unsuccessful. Retrying..."
157 done
159 echo "Successfully built ${IMAGE_NAME}."
161 echo "Starting hbase shell..."
162 docker run -it ${IMAGE_NAME}