8a5015c80e3f406871f0e8fa6eb24f0dc1d12dc8
[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.$(arch)"}
12
13 usage () {
14         echo "${name} - Builds a docker image that contains tools for building petitboot." >&2
15         echo "Usage: ${name} [flags]" >&2
16         echo "Option flags:" >&2
17         echo "  -d --dry-run  - Do not run build commands." >&2
18         echo "  -h --help     - Show this help and exit." >&2
19         echo "  -p --purge    - Remove existing docker image and rebuild." >&2
20         echo "  -r --rebuild  - Rebuild existing docker image." >&2
21         echo "  -t --tag      - Print Docker tag to stdout and exit." >&2
22         echo "  -v --verbose  - Verbose execution." >&2
23         echo "Environment:" >&2
24         echo "  DOCKER_FILE   - Default: '${DOCKER_FILE}'" >&2
25         echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
26         echo "Examples:" >&2
27         echo "  ${name} -v"
28 }
29
30 short_opts="dhprtv"
31 long_opts="dry-run,help,purge,rebuild,tag,verbose"
32
33 opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
34
35 if [ $? != 0 ]; then
36         echo "${name}: ERROR: Internal getopt" >&2 
37         exit 1
38 fi
39
40 eval set -- "${opts}"
41
42 while true ; do
43         case "${1}" in
44         -d | --dry-run)
45                 dry_run=1
46                 shift
47                 ;;
48         -h | --help)
49                 usage=1
50                 shift
51                 ;;
52         -p | --purge)
53                 purge=1
54                 shift
55                 ;;
56         -r | --rebuild)
57                 rebuild=1
58                 shift
59                 ;;
60         -t | --tag)
61                 tag=1
62                 shift
63                 ;;
64         -v | --verbose)
65                 set -x
66                 verbose=1
67                 shift
68                 ;;
69         --)
70                 shift
71                 break
72                 ;;
73         *)
74                 echo "${name}: ERROR: Internal opts" >&2 
75                 exit 1
76                 ;;
77         esac
78 done
79
80 if [[ -n "${usage}" ]]; then
81         usage
82         exit 0
83 fi
84
85 if [[ -n "${tag}" ]]; then
86         show_tag
87         exit 0
88 fi
89
90 if [[ -n "${purge}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
91         echo "Removing docker image: ${DOCKER_TAG}" >&2
92         run_cmd "docker rmi --force ${DOCKER_TAG}"
93 elif [[ -z "${rebuild}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
94         echo "Docker image exists: ${DOCKER_TAG}" >&2
95         show_tag
96         exit 0
97 fi
98
99 echo "Building docker image: ${DOCKER_TAG}" >&2
100
101 cd ${TOP_DIR}
102 run_cmd "docker build --file ${DOCKER_FILE} --tag ${DOCKER_TAG} ."
103
104 show_tag