2
2
3
3
# This library holds utility functions for building container images.
4
4
5
- # os::build::image builds an image from a directory, to a tag, with an optional dockerfile to
6
- # use as the third argument. The environment variable OS_BUILD_IMAGE_ARGS adds additional
7
- # options to the command. The default is to use the imagebuilder binary if it is available
8
- # on the path with fallback to docker build if it is not available.
5
+ # os::build::image builds an image from a directory, to a tag or tags The default
6
+ # behavior is to use the imagebuilder binary if it is available on the path with
7
+ # fallback to docker build if it is not available.
8
+ #
9
+ # Globals:
10
+ # - OS_BUILD_IMAGE_ARGS
11
+ # - OS_BUILD_IMAGE_NUM_RETRIES
12
+ # Arguments:
13
+ # - 1: the directory in which to build
14
+ # - 2: the tag to apply to the image
15
+ # Returns:
16
+ # None
9
17
function os::build::image() {
10
- local directory=$1
11
- local tag=$2
12
- local dockerfile=" ${3-} "
13
- local extra_tag=" ${4-} "
14
- local options=" ${OS_BUILD_IMAGE_ARGS-} "
15
- local mode=" ${OS_BUILD_IMAGE_TYPE:- imagebuilder} "
16
-
17
- if [[ " ${mode} " == " imagebuilder" ]]; then
18
- if os::util::find::system_binary ' imagebuilder' ; then
19
- if [[ -n " ${extra_tag} " ]]; then
20
- extra_tag=" -t '${extra_tag} '"
21
- fi
22
- if [[ -n " ${dockerfile} " ]]; then
23
- eval " imagebuilder -f '${dockerfile} ' -t '${tag} ' ${extra_tag} ${options} '${directory} '"
24
- return $?
25
- fi
26
- eval " imagebuilder -t '${tag} ' ${extra_tag} ${options} '${directory} '"
27
- return $?
28
- fi
29
-
30
- os::log::warning " Unable to locate 'imagebuilder' on PATH, falling back to Docker build"
31
- # clear options since we were unable to select imagebuilder
32
- options=" "
33
- fi
34
-
35
- if [[ -n " ${dockerfile} " ]]; then
36
- eval " docker build -f '${dockerfile} ' -t '${tag} ' ${options} '${directory} '"
37
- if [[ -n " ${extra_tag} " ]]; then
38
- docker tag " ${tag} " " ${extra_tag} "
39
- fi
40
- return $?
41
- fi
42
- eval " docker build -t '${tag} ' ${options} '${directory} '"
43
- if [[ -n " ${extra_tag} " ]]; then
44
- docker tag " ${tag} " " ${extra_tag} "
45
- fi
46
- return $?
18
+ local tag=$1
19
+ local directory=$2
20
+ local extra_tag
21
+
22
+ if [[ ! " ${tag} " == * " :" * ]]; then
23
+ # if no tag was specified in the image name,
24
+ # tag with :latest and the release commit, if
25
+ # available, falling back to the last commit
26
+ # if no release commit is recorded
27
+ local release_commit
28
+ release_commit=" ${OS_RELEASE_COMMIT:- " $( git log -1 --pretty=%h ) " } "
29
+ extra_tag=" ${tag} :${release_commit} "
30
+
31
+ tag=" ${tag} :latest"
32
+ fi
33
+
34
+ local result=' 1'
35
+ local image_build_log
36
+ image_build_log=" $( mktemp " ${BASETMPDIR} /imagelogs.XXXXX" ) "
37
+ for (( i = 0 ; i < "${OS_BUILD_IMAGE_NUM_RETRIES:- 2} "; i++ )) ; do
38
+ if [[ " ${i} " -gt 0 ]]; then
39
+ os::log::warning " Retrying image build for ${tag} , attempt ${i} ..."
40
+ fi
41
+
42
+ if os::build::image::internal::generic " ${tag} " " ${directory} " " ${extra_tag:- } " >> " ${image_build_log} " 2>&1 ; then
43
+ result=' 0'
44
+ break
45
+ fi
46
+ done
47
+
48
+ os::log::internal::prefix_lines " [${tag%:* } ]" " $( cat " ${image_build_log} " ) "
49
+ return " ${result} "
50
+ }
51
+ readonly -f os::build::image
52
+
53
+ # os::build::image::internal::generic builds a container image using either imagebuilder
54
+ # or docker, defaulting to imagebuilder if present
55
+ #
56
+ # Globals:
57
+ # - OS_BUILD_IMAGE_ARGS
58
+ # Arguments:
59
+ # - 1: the directory in which to build
60
+ # - 2: the tag to apply to the image
61
+ # - 3: optionally, extra tags to add
62
+ # Returns:
63
+ # None
64
+ function os::build::image::internal::generic() {
65
+ local directory=$2
66
+
67
+ if os::util::find::system_binary ' imagebuilder' > /dev/null; then
68
+ os::build::image::internal::imagebuilder " $@ "
69
+ else
70
+ os::log::warning " Unable to locate 'imagebuilder' on PATH, falling back to Docker build"
71
+ os::build::image::internal::docker " $@ "
72
+ fi
73
+
74
+ # ensure the temporary contents are cleaned up
75
+ git clean -fdx " ${directory} "
76
+ }
77
+ readonly -f os::build::image::internal::generic
78
+
79
+ # os::build::image::internal::imagebuilder builds a container image using imagebuilder
80
+ #
81
+ # Globals:
82
+ # - OS_BUILD_IMAGE_ARGS
83
+ # Arguments:
84
+ # - 1: the directory in which to build
85
+ # - 2: the tag to apply to the image
86
+ # - 3: optionally, extra tags to add
87
+ # Returns:
88
+ # None
89
+ function os::build::image::internal::imagebuilder() {
90
+ local tag=$1
91
+ local directory=$2
92
+ local extra_tag=" ${3-} "
93
+ local options=()
94
+
95
+ if [[ -n " ${OS_BUILD_IMAGE_ARGS:- } " ]]; then
96
+ options=( ${OS_BUILD_IMAGE_ARGS} )
97
+ fi
98
+
99
+ if [[ -n " ${extra_tag} " ]]; then
100
+ options+=( -t " ${extra_tag} " )
101
+ fi
102
+
103
+ imagebuilder " ${options[@]:- } " -t " ${tag} " " ${directory} "
104
+ }
105
+ readonly -f os::build::image::internal::imagebuilder
106
+
107
+ # os::build::image::internal::docker builds a container image using docker
108
+ #
109
+ # Globals:
110
+ # - OS_BUILD_IMAGE_ARGS
111
+ # Arguments:
112
+ # - 1: the directory in which to build
113
+ # - 2: the tag to apply to the image
114
+ # - 3: optionally, extra tags to add
115
+ # Returns:
116
+ # None
117
+ function os::build::image::internal::docker() {
118
+ local tag=$1
119
+ local directory=$2
120
+ local extra_tag=" ${3-} "
121
+ local options=()
122
+
123
+ docker build ${OS_BUILD_IMAGE_ARGS:- } -t " ${tag} " " ${directory} "
124
+
125
+ if [[ -n " ${extra_tag} " ]]; then
126
+ docker tag " ${tag} " " ${extra_tag} "
127
+ fi
47
128
}
48
- readonly -f os::build::image
129
+ readonly -f os::build::image::internal::docker
0 commit comments