OSDN Git Service

[update] : Added comments for depend packages
[alterlinux/alterlinux.git] / tools / docker-build.sh
1 #!/usr/bin/env bash
2 set -e
3
4 if ! type docker >/dev/null 2>&1; then
5     echo "You have to install docker in your system before." 1>&2
6     exit 1
7 fi
8
9 if ! docker info >/dev/null 2>&1; then
10     cat - 1>&2 <<EOF
11 Cannot connect to docker socket.
12 Please make sure that you have the right permission to use docker and the docker daemon has been running correctly.
13 EOF
14     exit 1
15 fi
16
17 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
18
19 _usage () {
20     echo "usage ${0} [options]"
21     echo
22     echo " General options:"
23     echo "    -o | --build-opiton \"[options]\"     Set build options passed to build.sh"
24     echo "    -d | --dist-out-dir \"[path]\"        Set distributions' output directory"
25     echo "    -w | --work-dir \"[path]\"            Set build working dir for debug"
26     echo "    -n | --noninteractive                 The process will not ask you any questions"
27     echo "    -c | --clean                          Enable --no-cache option when building docker image"
28     echo "    -s | --no-share-pkgfile               Disable pacman pkgcache"
29     echo "    -p | --pkg-cache-dir \"[path]\"       Set pacman pkg cache directory"
30     echo "    -h | --help                           This help message and exit"
31     echo
32 }
33
34 # Start define var
35 if [[ -d "/var/cache/pacman/pkg" ]] && [[ -d "/var/lib/pacman/sync" ]]; then
36     SHARE_PKG_DIR="/var/cache/pacman/pkg"
37     SHARE_DB_DIR="/var/lib/pacman/sync"
38 else
39     SHARE_PKG_DIR="${script_path}/cache/pkg"
40     SHARE_DB_DIR="${script_path}/cache/sync"
41 fi
42 # End define var
43
44 # Start parse options
45 DIST_DIR="${script_path}/out"
46 DOCKER_BUILD_OPTS=()
47 BUILD_SCRIPT_OPTS=()
48 while (( ${#} > 0 )); do
49     case "${1}" in
50         -o | --build-opiton)
51             # -o: dists output dir option in build.sh
52             if [[ "${2}" == *"-o"* ]]; then
53                 echo "The -o option cannot be set with docker build. Please use -d option instead." 1>&2
54                 exit 1
55             fi
56             #
57             #BUILD_SCRIPT_OPTS+=(${2})
58             IFS=" " read -r -a BUILD_SCRIPT_OPTS <<< "${2}"
59             
60             shift 2
61             ;;
62         -d | --dist-out-dir)
63             mkdir -p "${2}" || {
64                 echo "Error: failed creating output directory: ${2}" 1>&2
65                 exit 1
66             }
67             DIST_DIR=$(cd -P ${2} && pwd)
68             shift 2
69             ;;
70         -w | --work-dir)
71             mkdir -p "${2}" || {
72                 echo "Error: failed creating working directory: ${2}" 1>&2
73                 exit 1
74             }
75             EXTERNAL_WORK_DIR=$(cd -P "${2}" && pwd)
76             shift 2
77             ;;
78         -n | --noninteractive)
79             BUILD_SCRIPT_OPTS+=(--noconfirm)
80             shift 1
81             ;;
82         -c | --clean)
83             # Enable docker's --no-cache option
84             DOCKER_BUILD_OPTS+=(--no-cache)
85             shift 1
86             ;;
87         -s | --no-share-pkgfile)
88             NO_SHARE_PKG=True
89             shift 1
90             ;;
91         -p | --pkg-cache-dir)
92             if [[ -d "${2}" ]] && \
93                 mkdir -p "${2}/pkg" && \
94                 mkdir -p "${2}/sync"
95             then
96                 SHARE_PKG_DIR="${2}/pkg"
97                 SHARE_DB_DIR="${2}/sync"
98             else
99                 echo "Error: The directory is not found or cannot make directory." 1>&2
100                 exit 1
101             fi
102             ;;
103         -h | --help)
104             _usage
105             exit 0
106             ;;
107         --)
108             shift
109             break
110             ;;
111         *)
112             msg_error "Invalid argument '${1}'"
113             _usage 1
114             ;;
115     esac
116 done
117 # End parse options
118
119 if [[ -d "/usr/lib/modules/$(uname -r)" ]]; then
120     KMODS_DIR="/usr/lib/modules"
121 else
122     KMODS_DIR="/lib/modules"
123 fi
124
125 DOCKER_RUN_OPTS=()
126 DOCKER_RUN_OPTS+=(-v "${DIST_DIR}:/alterlinux/out")
127 DOCKER_RUN_OPTS+=(-v "${KMODS_DIR}:/usr/lib/modules:ro")
128 [[ "x${EXTERNAL_WORK_DIR}" != "x" ]] && \
129     DOCKER_RUN_OPTS+=(-v "${EXTERNAL_WORK_DIR}:/alterlinux/work")
130 [[ "x${NO_SHARE_PKG}" != "xTrue" ]] && {
131     DOCKER_RUN_OPTS+=(-v "${SHARE_PKG_DIR}:/var/cache/pacman/pkg")
132     DOCKER_RUN_OPTS+=(-v "${SHARE_DB_DIR}:/var/lib/pacman/sync")
133 }
134
135 tty >/dev/null 2>&1 && OPT_TTY="-it" || OPT_TTY=""
136
137 docker build "${DOCKER_BUILD_OPTS[@]}" -t alterlinux-build:latest "${script_path}"
138 exec docker run --rm ${OPT_TTY} --privileged -e _DOCKER=true "${DOCKER_RUN_OPTS[@]}" alterlinux-build "${BUILD_SCRIPT_OPTS[@]}"