discover/sysinfo: Set IPv6 addresses
[petitboot] / docker / build-builder
1 #!/usr/bin/env bash
2
3 set -e
4
5 name="$(basename $0)"
6
7 : ${TOP_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"}
8
9 source ${TOP_DIR}/docker/builder-include
10
11 : ${DOCKER_FILE:="${TOP_DIR}/docker/Dockerfile.builder"}
12 : ${DOCKER_FROM:="$(docker_from)"}
13
14 usage () {
15         echo "${name} - Builds a docker image that contains tools for building petitboot." >&2
16         echo "Usage: ${name} [flags]" >&2
17         echo "Option flags:" >&2
18         echo "  -d --dry-run  - Do not run build commands." >&2
19         echo "  -h --help     - Show this help and exit." >&2
20         echo "  -p --purge    - Remove existing docker image and rebuild." >&2
21         echo "  -r --rebuild  - Rebuild existing docker image." >&2
22         echo "  -t --tag      - Print Docker tag to stdout and exit." >&2
23         echo "  -v --verbose  - Verbose execution." >&2
24         echo "Environment:" >&2
25         echo "  DOCKER_FILE   - Default: '${DOCKER_FILE}'" >&2
26         echo "  DOCKER_FROM   - Default: '${DOCKER_FROM}'" >&2
27         echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
28         echo "Examples:" >&2
29         echo "  ${name} -v"
30 }
31
32 short_opts="dhprtv"
33 long_opts="dry-run,help,purge,rebuild,tag,verbose"
34
35 opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
36
37 if [ $? != 0 ]; then
38         echo "${name}: ERROR: Internal getopt" >&2 
39         exit 1
40 fi
41
42 eval set -- "${opts}"
43
44 while true ; do
45         case "${1}" in
46         -d | --dry-run)
47                 dry_run=1
48                 shift
49                 ;;
50         -h | --help)
51                 usage=1
52                 shift
53                 ;;
54         -p | --purge)
55                 purge=1
56                 shift
57                 ;;
58         -r | --rebuild)
59                 rebuild=1
60                 shift
61                 ;;
62         -t | --tag)
63                 tag=1
64                 shift
65                 ;;
66         -v | --verbose)
67                 set -x
68                 verbose=1
69                 shift
70                 ;;
71         --)
72                 shift
73                 break
74                 ;;
75         *)
76                 echo "${name}: ERROR: Internal opts" >&2 
77                 exit 1
78                 ;;
79         esac
80 done
81
82 if [[ -n "${usage}" ]]; then
83         usage
84         exit 0
85 fi
86
87 if [[ -n "${tag}" ]]; then
88         show_tag
89         exit 0
90 fi
91
92 version () {
93         echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
94 }
95
96 # Support for docker versions older than 17.05.
97 # See https://github.com/moby/moby/issues/32457
98 if [[ $(version "$(docker version --format '{{.Server.Version}}')") < $(version "17.05") ]]; then
99         tmp_file="${DOCKER_FILE}.tmp"
100         trap "rm -f ${tmp_file}" EXIT
101
102         cp -f ${DOCKER_FILE} ${tmp_file}
103         DOCKER_FILE=${tmp_file}
104         sed --in-place "s|ARG DOCKER_FROM||" ${tmp_file}
105         sed --in-place "s|\${DOCKER_FROM}|${DOCKER_FROM}|" ${tmp_file}
106 fi
107
108 if [[ -n "${purge}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
109         echo "Removing docker image: ${DOCKER_TAG}" >&2
110         run_cmd "docker rmi --force ${DOCKER_TAG}"
111 elif [[ -z "${rebuild}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
112         echo "Docker image exists: ${DOCKER_TAG}" >&2
113         show_tag
114         exit 0
115 fi
116
117 echo "Building docker image: ${DOCKER_TAG}" >&2
118
119 cd ${TOP_DIR}
120 run_cmd "docker build \
121         --file "${DOCKER_FILE}" \
122         --build-arg DOCKER_FROM=${DOCKER_FROM} \
123         --tag ${DOCKER_TAG} \
124         ."
125
126 show_tag