#!/usr/bin/env bash
set -e
-if [[ $UID != 0 ]]; then
- echo "You have to run this as root." 1>&2
+if ! type docker >/dev/null 2>&1; then
+ echo "You have to install docker in your system before." 1>&2
exit 1
fi
-if ! type docker >/dev/null 2>&1; then
- echo "You have to install docker." 1>&2
+if ! docker info >/dev/null 2>&1; then
+ cat - 1>&2 <<EOF
+Cannot connect to docker socket.
+Please make sure that you have the right permission to use docker and the docker daemon has been running correctly.
+EOF
exit 1
fi
-script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
+script_path=$(cd -P "$(dirname "$(readlink -f "${0}")")" && cd .. && pwd)
_usage () {
echo "usage ${0} [options]"
echo
echo " General options:"
- echo " -o | --build-opiton \"[options]\" Send the build option to build.sh"
- echo " -c | --clean Enable --no-cache option when build docker image"
+ echo " -o | --build-opiton \"[options]\" Set build options passed to build.sh"
+ echo " -d | --dist-out-dir \"[path]\" Set distributions' output directory"
+ echo " -c | --clean Enable --no-cache option when building docker image"
echo " -s | --no-share-pkgfile Disable pacman pkgcache"
- echo " -p | --pkg-cache-dir \"[path]\" Select pacman pkg cache directory"
+ echo " -p | --pkg-cache-dir \"[path]\" Set pacman pkg cache directory"
echo " -h | --help This help message and exit"
echo
}
# Start define var
-[[ -d /var/cache/pacman/pkg ]] && [[ -d /var/lib/pacman/sync ]] && SHARE_PKG_DIR='/var/cache/pacman/pkg' && SHARE_DB_DIR='/var/lib/pacman/sync' || SHARE_PKG_DIR=${script_path}/cache/pkg && SHARE_DB_DIR=${script_path}/cache/sync
+if [[ -d /var/cache/pacman/pkg ]] && [[ -d /var/lib/pacman/sync ]]; then
+ SHARE_PKG_DIR=/var/cache/pacman/pkg
+ SHARE_DB_DIR=/var/lib/pacman/sync
+else
+ SHARE_PKG_DIR=${script_path}/cache/pkg
+ SHARE_DB_DIR=${script_path}/cache/sync
+fi
# End define var
-
# Start parse options
-ARGUMENT="${@}"
+DIST_DIR=${script_path}/out
+DOCKER_BUILD_OPTS=()
+DOCKER_RUN_OPTS=()
+BUILD_SCRIPT_OPTS=()
while (( $# > 0 )); do
case ${1} in
-o | --build-opiton)
- BUILD_OPT="${2}"
+ # -o: dists output dir option in build.sh
+ if [[ ${2} == *"-o"* ]]; then
+ echo "The -o option cannot be set with docker build. Please use -d option instead." 1>&2
+ exit 1
+ fi
+ #
+ BUILD_SCRIPT_OPTS+=("${2}")
+ shift 2
+ ;;
+ -d | --dist-out-dir)
+ mkdir -p "${2}" || {
+ echo "Error: failed creating output directory: ${2}" 1>&2
+ exit 1
+ }
+ DIST_DIR=$(cd -P "${2}" && pwd)
shift 2
;;
-c | --clean)
- NO_CACHE="--no-cache" # Enable --no-cache option
+ # Enable docker's --no-cache option
+ DOCKER_BUILD_OPTS+=(--no-cache)
shift 1
;;
-s | --no-share-pkgfile)
shift 1
;;
-p | --pkg-cache-dir)
- [[ -d ${2} ]] && mkdir -p ${2}/pkg && mkdir -p ${2}/sync && SHARE_PKG_DIR=${2}/pkg && SHARE_DB_DIR=${2}/sync || echo "Error: The directory is not found or cannot make directory." 1>&2 && exit 1
+ if [[ -d ${2} ]] && \
+ mkdir -p "${2}/pkg" && \
+ mkdir -p "${2}/sync"
+ then
+ SHARE_PKG_DIR=${2}/pkg
+ SHARE_DB_DIR=${2}/sync
+ else
+ echo "Error: The directory is not found or cannot make directory." 1>&2
+ exit 1
+ fi
;;
-h | --help)
_usage
done
# End parse options
-cd $script_path
-docker build ${NO_CACHE} -t alterlinux-build:latest .
-[[ "${NO_SHARE_PKG}" == "True" ]] && SHARE_PACMAN_DIR="" || SHARE_PACMAN_DIR="-v ${SHARE_PKG_DIR}:/var/cache/pacman/pkg -v ${SHARE_DB_DIR}:/var/lib/pacman/sync"
-docker run -e _DOCKER=true -t -i --privileged -v $script_path/out:/alterlinux/out -v /usr/lib/modules:/usr/lib/modules:ro ${SHARE_PACMAN_DIR} alterlinux-build "${BUILD_OPT}"
+DOCKER_RUN_OPTS+=("-v ${DIST_DIR}:/alterlinux/out")
+DOCKER_RUN_OPTS+=("-v /usr/lib/modules:/usr/lib/modules:ro")
+[[ "${NO_SHARE_PKG}" != "True" ]] && {
+ DOCKER_RUN_OPTS+=("-v ${SHARE_PKG_DIR}:/var/cache/pacman/pkg")
+ DOCKER_RUN_OPTS+=("-v ${SHARE_DB_DIR}:/var/lib/pacman/sync")
+}
+
+# cd ${script_path}
+docker build "${DOCKER_BUILD_OPTS[@]}" -t alterlinux-build:latest "${script_path}"
+docker run --rm -t -i --privileged -e _DOCKER=true "${DOCKER_RUN_OPTS[@]}" alterlinux-build "${BUILD_SCRIPT_OPTS[@]}"