buildimage 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.3
2
+ Name: buildimage
3
+ Version: 1.0.0
4
+ Summary: Build Docker images for use in Kubernetes deployments
5
+ Author: Lars Berntzon
6
+ Author-email: Lars Berntzon <lars.berntzon@cecilia-data.se>
7
+ License: GNU LESSER GENERAL PUBLIC LICENSE
8
+ Version 3, 29 June 2007
9
+
10
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
11
+ Everyone is permitted to copy and distribute verbatim copies
12
+ of this license document, but changing it is not allowed.
13
+
14
+
15
+ This version of the GNU Lesser General Public License incorporates
16
+ the terms and conditions of version 3 of the GNU General Public
17
+ License, supplemented by the additional permissions listed below.
18
+
19
+ 0. Additional Definitions.
20
+
21
+ As used herein, "this License" refers to version 3 of the GNU Lesser
22
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
23
+ General Public License.
24
+
25
+ "The Library" refers to a covered work governed by this License,
26
+ other than an Application or a Combined Work as defined below.
27
+
28
+ An "Application" is any work that makes use of an interface provided
29
+ by the Library, but which is not otherwise based on the Library.
30
+ Defining a subclass of a class defined by the Library is deemed a mode
31
+ of using an interface provided by the Library.
32
+
33
+ A "Combined Work" is a work produced by combining or linking an
34
+ Application with the Library. The particular version of the Library
35
+ with which the Combined Work was made is also called the "Linked
36
+ Version".
37
+
38
+ The "Minimal Corresponding Source" for a Combined Work means the
39
+ Corresponding Source for the Combined Work, excluding any source code
40
+ for portions of the Combined Work that, considered in isolation, are
41
+ based on the Application, and not on the Linked Version.
42
+
43
+ The "Corresponding Application Code" for a Combined Work means the
44
+ object code and/or source code for the Application, including any data
45
+ and utility programs needed for reproducing the Combined Work from the
46
+ Application, but excluding the System Libraries of the Combined Work.
47
+
48
+ 1. Exception to Section 3 of the GNU GPL.
49
+
50
+ You may convey a covered work under sections 3 and 4 of this License
51
+ without being bound by section 3 of the GNU GPL.
52
+
53
+ 2. Conveying Modified Versions.
54
+
55
+ If you modify a copy of the Library, and, in your modifications, a
56
+ facility refers to a function or data to be supplied by an Application
57
+ that uses the facility (other than as an argument passed when the
58
+ facility is invoked), then you may convey a copy of the modified
59
+ version:
60
+
61
+ a) under this License, provided that you make a good faith effort to
62
+ ensure that, in the event an Application does not supply the
63
+ function or data, the facility still operates, and performs
64
+ whatever part of its purpose remains meaningful, or
65
+
66
+ b) under the GNU GPL, with none of the additional permissions of
67
+ this License applicable to that copy.
68
+
69
+ 3. Object Code Incorporating Material from Library Header Files.
70
+
71
+ The object code form of an Application may incorporate material from
72
+ a header file that is part of the Library. You may convey such object
73
+ code under terms of your choice, provided that, if the incorporated
74
+ material is not limited to numerical parameters, data structure
75
+ layouts and accessors, or small macros, inline functions and templates
76
+ (ten or fewer lines in length), you do both of the following:
77
+
78
+ a) Give prominent notice with each copy of the object code that the
79
+ Library is used in it and that the Library and its use are
80
+ covered by this License.
81
+
82
+ b) Accompany the object code with a copy of the GNU GPL and this license
83
+ document.
84
+
85
+ 4. Combined Works.
86
+
87
+ You may convey a Combined Work under terms of your choice that,
88
+ taken together, effectively do not restrict modification of the
89
+ portions of the Library contained in the Combined Work and reverse
90
+ engineering for debugging such modifications, if you also do each of
91
+ the following:
92
+
93
+ a) Give prominent notice with each copy of the Combined Work that
94
+ the Library is used in it and that the Library and its use are
95
+ covered by this License.
96
+
97
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
98
+ document.
99
+
100
+ c) For a Combined Work that displays copyright notices during
101
+ execution, include the copyright notice for the Library among
102
+ these notices, as well as a reference directing the user to the
103
+ copies of the GNU GPL and this license document.
104
+
105
+ d) Do one of the following:
106
+
107
+ 0) Convey the Minimal Corresponding Source under the terms of this
108
+ License, and the Corresponding Application Code in a form
109
+ suitable for, and under terms that permit, the user to
110
+ recombine or relink the Application with a modified version of
111
+ the Linked Version to produce a modified Combined Work, in the
112
+ manner specified by section 6 of the GNU GPL for conveying
113
+ Corresponding Source.
114
+
115
+ 1) Use a suitable shared library mechanism for linking with the
116
+ Library. A suitable mechanism is one that (a) uses at run time
117
+ a copy of the Library already present on the user's computer
118
+ system, and (b) will operate properly with a modified version
119
+ of the Library that is interface-compatible with the Linked
120
+ Version.
121
+
122
+ e) Provide Installation Information, but only if you would otherwise
123
+ be required to provide such information under section 6 of the
124
+ GNU GPL, and only to the extent that such information is
125
+ necessary to install and execute a modified version of the
126
+ Combined Work produced by recombining or relinking the
127
+ Application with a modified version of the Linked Version. (If
128
+ you use option 4d0, the Installation Information must accompany
129
+ the Minimal Corresponding Source and Corresponding Application
130
+ Code. If you use option 4d1, you must provide the Installation
131
+ Information in the manner specified by section 6 of the GNU GPL
132
+ for conveying Corresponding Source.)
133
+
134
+ 5. Combined Libraries.
135
+
136
+ You may place library facilities that are a work based on the
137
+ Library side by side in a single library together with other library
138
+ facilities that are not Applications and are not covered by this
139
+ License, and convey such a combined library under terms of your
140
+ choice, if you do both of the following:
141
+
142
+ a) Accompany the combined library with a copy of the same work based
143
+ on the Library, uncombined with any other library facilities,
144
+ conveyed under the terms of this License.
145
+
146
+ b) Give prominent notice with the combined library that part of it
147
+ is a work based on the Library, and explaining where to find the
148
+ accompanying uncombined form of the same work.
149
+
150
+ 6. Revised Versions of the GNU Lesser General Public License.
151
+
152
+ The Free Software Foundation may publish revised and/or new versions
153
+ of the GNU Lesser General Public License from time to time. Such new
154
+ versions will be similar in spirit to the present version, but may
155
+ differ in detail to address new problems or concerns.
156
+
157
+ Each version is given a distinguishing version number. If the
158
+ Library as you received it specifies that a certain numbered version
159
+ of the GNU Lesser General Public License "or any later version"
160
+ applies to it, you have the option of following the terms and
161
+ conditions either of that published version or of any later version
162
+ published by the Free Software Foundation. If the Library as you
163
+ received it does not specify a version number of the GNU Lesser
164
+ General Public License, you may choose any version of the GNU Lesser
165
+ General Public License ever published by the Free Software Foundation.
166
+
167
+ If the Library as you received it specifies that a proxy can decide
168
+ whether future versions of the GNU Lesser General Public License shall
169
+ apply, that proxy's public statement of acceptance of any version is
170
+ permanent authorization for you to choose that version for the
171
+ Library.
172
+ Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
173
+ Classifier: Programming Language :: Python :: 3
174
+ Classifier: Development Status :: 4 - Beta
175
+ Classifier: Operating System :: POSIX :: Linux
176
+ Classifier: Topic :: Software Development :: Build Tools
177
+ Requires-Dist: jinja2>=3.1.6
178
+ Requires-Dist: jsonschema>=4.26.0
179
+ Requires-Dist: pyyaml>=6.0.3
180
+ Requires-Python: >=3.10
181
+ Project-URL: homepage, https://github.com/martinpitt/python-dbusmock
182
+ Description-Content-Type: text/markdown
183
+
184
+ # buildimage
185
+
186
+ Build Docker images for use in Kubernetes deployments. The build images can be tagged with configurable values. The recomended way is to tag it with the tree hash from the docker folder. This is the way the example below use.
187
+
188
+ The script uses a file named `images.yaml` to describe what images to build and what deployments to patch with new tags. Built images will also have some labels with metadata defined. The YAML file can use facts using the Jinja2 template format `{{ name }}`.
189
+
190
+ ## Installation
191
+
192
+ Install from PyPI:
193
+
194
+ ```bash
195
+ pip install buildimage
196
+ ```
197
+
198
+ ## Usage
199
+
200
+ Run the `buildimage` command in a directory containing an `images.yaml` file or specify the directory as an argument:
201
+
202
+ ```bash
203
+ buildimage [OPTIONS] DIRECTORY
204
+ ```
205
+
206
+ ### Options
207
+
208
+ - `--nopush`: Skip pushing built images
209
+ - `--image IMAGE`: Only build named images (can be specified multiple times)
210
+ - `DIRECTORY`: Path to directory containing the `images.yaml` file (default: current directory)
211
+
212
+ ## Configuration
213
+
214
+ Create an `images.yaml` file in your project root with the following format:
215
+
216
+ ```yaml
217
+ images:
218
+ - directory: "build-directory"
219
+ name: "image name"
220
+ tags:
221
+ - "tree-{{ treeHash }}"
222
+ labels:
223
+ - name: com.mydomain.repository
224
+ value: "{{ repository }}"
225
+ buildArgs:
226
+ - name: "build-arg-name"
227
+ value: "build-arg-value"
228
+ deployments:
229
+ - path: "path/to/deployment.yaml"
230
+ match: "regex-to-match"
231
+ replace: "replacement-value"
232
+ ```
233
+
234
+ ### Available Facts
235
+
236
+ The following facts are available for templating in `images.yaml`:
237
+
238
+ - `{{ branch }}`: Current Git branch
239
+ - `{{ remote }}`: Git remote name
240
+ - `{{ repositoryFull }}`: Full repository URL
241
+ - `{{ repository }}`: Repository URL without user info
242
+ - `{{ commit }}`: Hash for current HEAD commit
243
+ - `{{ topTreeHash }}`: Git tree hash of the top directory
244
+ - `{{ treeHash }}`: Git tree hash of the image directory (per image)
245
+ - `{{ top }}`: Top directory path
246
+
247
+ ## Example
248
+
249
+ See the `tests/docker/` directory for a complete example with test images and deployment updates.
250
+
251
+ ## License
252
+
253
+ GNU Lesser General Public License v3.0
@@ -0,0 +1,70 @@
1
+ # buildimage
2
+
3
+ Build Docker images for use in Kubernetes deployments. The build images can be tagged with configurable values. The recomended way is to tag it with the tree hash from the docker folder. This is the way the example below use.
4
+
5
+ The script uses a file named `images.yaml` to describe what images to build and what deployments to patch with new tags. Built images will also have some labels with metadata defined. The YAML file can use facts using the Jinja2 template format `{{ name }}`.
6
+
7
+ ## Installation
8
+
9
+ Install from PyPI:
10
+
11
+ ```bash
12
+ pip install buildimage
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Run the `buildimage` command in a directory containing an `images.yaml` file or specify the directory as an argument:
18
+
19
+ ```bash
20
+ buildimage [OPTIONS] DIRECTORY
21
+ ```
22
+
23
+ ### Options
24
+
25
+ - `--nopush`: Skip pushing built images
26
+ - `--image IMAGE`: Only build named images (can be specified multiple times)
27
+ - `DIRECTORY`: Path to directory containing the `images.yaml` file (default: current directory)
28
+
29
+ ## Configuration
30
+
31
+ Create an `images.yaml` file in your project root with the following format:
32
+
33
+ ```yaml
34
+ images:
35
+ - directory: "build-directory"
36
+ name: "image name"
37
+ tags:
38
+ - "tree-{{ treeHash }}"
39
+ labels:
40
+ - name: com.mydomain.repository
41
+ value: "{{ repository }}"
42
+ buildArgs:
43
+ - name: "build-arg-name"
44
+ value: "build-arg-value"
45
+ deployments:
46
+ - path: "path/to/deployment.yaml"
47
+ match: "regex-to-match"
48
+ replace: "replacement-value"
49
+ ```
50
+
51
+ ### Available Facts
52
+
53
+ The following facts are available for templating in `images.yaml`:
54
+
55
+ - `{{ branch }}`: Current Git branch
56
+ - `{{ remote }}`: Git remote name
57
+ - `{{ repositoryFull }}`: Full repository URL
58
+ - `{{ repository }}`: Repository URL without user info
59
+ - `{{ commit }}`: Hash for current HEAD commit
60
+ - `{{ topTreeHash }}`: Git tree hash of the top directory
61
+ - `{{ treeHash }}`: Git tree hash of the image directory (per image)
62
+ - `{{ top }}`: Top directory path
63
+
64
+ ## Example
65
+
66
+ See the `tests/docker/` directory for a complete example with test images and deployment updates.
67
+
68
+ ## License
69
+
70
+ GNU Lesser General Public License v3.0
@@ -0,0 +1,65 @@
1
+ [project]
2
+ name = "buildimage"
3
+ version = "1.0.0"
4
+ description = "Build Docker images for use in Kubernetes deployments"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Lars Berntzon", email = "lars.berntzon@cecilia-data.se" }
8
+ ]
9
+
10
+ classifiers = [
11
+ "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
12
+ "Programming Language :: Python :: 3",
13
+ "Development Status :: 4 - Beta",
14
+ "Operating System :: POSIX :: Linux",
15
+ "Topic :: Software Development :: Build Tools",
16
+ ]
17
+
18
+ requires-python = ">=3.10"
19
+
20
+ dependencies = [
21
+ "jinja2>=3.1.6",
22
+ "jsonschema>=4.26.0",
23
+ "pyyaml>=6.0.3",
24
+ ]
25
+ license = { file = "LICENSE" }
26
+
27
+ [project.urls]
28
+ homepage = "https://github.com/martinpitt/python-dbusmock"
29
+
30
+ [project.scripts]
31
+ buildimage = "buildimage.__main__:main"
32
+
33
+ [build-system]
34
+ requires = ["uv_build>=0.8.22,<0.9.0"]
35
+ build-backend = "uv_build"
36
+
37
+ [dependency-groups]
38
+ dev = [
39
+ "bumpver>=2025.1131",
40
+ "pytest>=9.0.2",
41
+ "pytest-cov>=7.0.0",
42
+ ]
43
+
44
+
45
+ [tool.bumpver]
46
+ current_version = "1.0.0"
47
+ version_pattern = "MAJOR.MINOR.PATCH"
48
+ commit_message = "Bump version {old_version} -> {new_version}"
49
+ commit = true
50
+ tag = true
51
+ push = false
52
+ #tag_message = "{new_version}"
53
+ #tag_scope = "default"
54
+ #pre_commit_hook = ""
55
+ #post_commit_hook = ""
56
+ # commit = true
57
+
58
+ [tool.bumpver.file_patterns]
59
+ "pyproject.toml" = [
60
+ 'current_version = "{version}"',
61
+ 'version = "{version}"'
62
+ ]
63
+ "src/buildimage/__init__.py" = [
64
+ '^__version__ = "{version}"$',
65
+ ]
@@ -0,0 +1,253 @@
1
+ #!/usr/bin/env python
2
+ from dataclasses import dataclass
3
+ import io
4
+ from jinja2 import Environment, FileSystemLoader
5
+ import logging
6
+ import os
7
+ from pathlib import Path
8
+ import re
9
+ import subprocess
10
+ import sys
11
+ import yaml
12
+ from jsonschema import validate
13
+ from dataclasses import dataclass, field
14
+
15
+ __version__ = "1.0.0"
16
+
17
+ """
18
+ Build docker images for use in kubernetes deployments.
19
+
20
+ The script uses a file named images.yaml file to describe what images to build and what deployments to patch
21
+ with new tags. Built image will also have some labels with metadata defined.
22
+ The yaml-file can use facts using the ninja2 template forms {{ name }}.
23
+ Format of the file is:
24
+ images:
25
+ - directory: "build-directory"
26
+ name: "image name"
27
+ tags:
28
+ - "tree-{{ treeHash }}"
29
+ labels:
30
+ - name: com.mydomain.repository
31
+ value: "{{ repository }}"
32
+ buildArgs:
33
+ - name: "build-arg-name"
34
+ value: "build-arg-value"
35
+ . . .
36
+ """
37
+
38
+ # FIXME: Add targets and Dockerfile name
39
+
40
+ IMAGE_YAML_FILE = "images.yaml"
41
+
42
+ SCHEMA = {
43
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
44
+ "$id": "https://github.com/lboclboc/buildimage.git", # FIXME: correct to proper url.
45
+ "title": "buildimage",
46
+ "description": "Instructions for how to build images in kubernetes",
47
+ "type": "object",
48
+ "properties": {
49
+ "images": {
50
+ "description": "List of images to build",
51
+ "type": "array",
52
+ "items": {
53
+ "type": "object",
54
+ "properties": {
55
+ "directory": {
56
+ "description": "sub-director below this file where image Dockerfile is located",
57
+ "type": "string",
58
+ },
59
+ "name": {
60
+ "description": "name of image (not including the :tag)",
61
+ "type": "string",
62
+ },
63
+ "tags": {
64
+ "description": "list of tags to use. Example {{ treeHash }}",
65
+ "type": "array",
66
+ "items": {
67
+ "type": "string",
68
+ },
69
+ },
70
+ "labels": {
71
+ "description": "docker image labels",
72
+ "type": "array",
73
+ "items": {
74
+ "type": "object",
75
+ "properties": {
76
+ "name": {
77
+ "description": "name of label, example com.mycompany.repository",
78
+ "type": "string",
79
+ },
80
+ "value": {
81
+ "description": "value of label, example {{ repository }}",
82
+ "type": "string",
83
+ },
84
+ },
85
+ "required": ["name", "value"],
86
+ },
87
+ },
88
+ "buildArgs": {
89
+ "description": "Build arguments for image",
90
+ "type": "array",
91
+ "items": {
92
+ "type": "object",
93
+ "properties": {
94
+ "name": {
95
+ "description": "build arg. Example BASE_IMAGE",
96
+ "type": "string",
97
+ },
98
+ "value": {
99
+ "description": "value of build arg, example 'busybox'",
100
+ "type": "string",
101
+ },
102
+ },
103
+ "required": ["name", "value"],
104
+ },
105
+ },
106
+ "deployments": {
107
+ "description": "Deployments to update",
108
+ "type": "array",
109
+ "items": {
110
+ "type": "object",
111
+ "properties": {
112
+ "path": {
113
+ "description": "path to file to modify",
114
+ "type": "string",
115
+ },
116
+ "match": {
117
+ "description": "regular expression to match",
118
+ "type": "string",
119
+ },
120
+ "replace": {
121
+ "description": "recplavement value",
122
+ "type": "string",
123
+ },
124
+
125
+ },
126
+ "required": ["path", "match", "replace"],
127
+ },
128
+ },
129
+ },
130
+ "required": ["directory", "name", "tags"],
131
+ },
132
+ "minItems": 1
133
+ },
134
+ },
135
+ "required": ["images"],
136
+ }
137
+
138
+ @dataclass
139
+ class Image:
140
+ name: str # Basename of image, including host, port, namespace and repo
141
+ tag: str
142
+ image_facts: dict[str, str]
143
+ fullname: str = field(init=False)
144
+ def __post_init__(self):
145
+ self.fullname = self.name + ":" + self.tag
146
+
147
+ class ImageBuilder:
148
+ """Holds the context during the whole build for all images and specs."""
149
+ def __init__(self, top: str) -> None:
150
+ self._top = Path(top)
151
+ self._spec: dict[str, object] | None = None
152
+ self._facts: dict[str, str] | None = dict()
153
+ self.get_facts()
154
+
155
+ @staticmethod
156
+ def command(cmd: list[str]) -> str:
157
+ return subprocess.run(cmd, stdout=subprocess.PIPE, check=True).stdout.decode().strip()
158
+
159
+ @staticmethod
160
+ def get_tree_hash(dir: Path) -> str:
161
+ dir = f"./{dir}" if not dir.is_absolute() else str(dir) # Needed since Path strips "./"
162
+ tree_hash = ImageBuilder.command(["git", "rev-parse", f"HEAD:{dir}/."])
163
+ dirty = subprocess.run(["git", "diff", "--quiet", "HEAD", f"{dir}/"])
164
+ return (tree_hash + "-dirty" if dirty else tree_hash, dirty)
165
+
166
+ def get_facts(self) -> None:
167
+ """calculate various facts that can be used in the images.yaml file"""
168
+ f = self._facts
169
+ f["branch"] = self.command(["git", "branch", "--show-current"])
170
+ f["remote"] = self.command(["git", "config", f"branch.{f['branch']}.remote"])
171
+ f["commit"] = self.command(["git", "rev-parse", "HEAD"])
172
+ f["repositoryFull"] = self.command(["git", "remote", "get-url", f["remote"]])
173
+ f["repository"] = re.sub(r"//.*@", "//", f["repositoryFull"]) # Drop user info
174
+ (f["topTreeHash"], _) = self.get_tree_hash(self._top)
175
+ f["treeHash"] = "{treeHash}" # This actually needs to be expanded per image rather that globally.
176
+ f["top"] = str(self._top)
177
+
178
+ def load_spec(self) -> None:
179
+ env = Environment(loader=FileSystemLoader("."))
180
+ template = env.get_template(str(self._top / IMAGE_YAML_FILE))
181
+ rendered = template.render(**self._facts)
182
+
183
+ with io.StringIO(rendered) as fin:
184
+ self._spec: dict = yaml.safe_load(fin)
185
+
186
+ validate(instance=self._spec, schema=SCHEMA)
187
+
188
+ def build_images(self, images_to_build: list[str] = None) -> None:
189
+ """Builds all images and returns a list of built Image objects."""
190
+ build_result: dict[str, list[Image]] = dict()
191
+ for img in self._spec["images"]:
192
+ if images_to_build and img["name"] not in images_to_build:
193
+ continue
194
+ image_dir = Path(img["directory"])
195
+ if not image_dir.is_absolute():
196
+ image_dir = self._facts["top"] / image_dir
197
+
198
+ image_facts: dict[str, str] = dict()
199
+ (image_facts["treeHash"], image_facts["dirty"]) = self.get_tree_hash(image_dir)
200
+
201
+ labels = [
202
+ # This will make the images.yaml file usage traceable from the image.
203
+ "--label", f"com.github.lboclboc.buildimage.topTreeHash={self._facts['topTreeHash']}",
204
+ ]
205
+ for b in img.get("labels") or []:
206
+ labels.extend(["--label", f"{b['name']}={b['value'].format(**image_facts)}"])
207
+
208
+ buildargs = []
209
+ for b in img.get("buildArgs") or []:
210
+ buildargs.extend(["--build-arg", f"{b['name']}={b['value'].format(**image_facts)}"])
211
+
212
+ image_list: list[Image] = []
213
+ for tag in img.get("tags") or []:
214
+ tag = tag.format(**image_facts)
215
+ i = Image(img["name"], tag, image_facts)
216
+ if len(image_list) == 0:
217
+ cmd = ["docker", "build", "-t", i.fullname] + buildargs + labels + [str(image_dir)]
218
+ logging.info(f"Building {i.fullname} in {image_dir}...")
219
+ subprocess.run(cmd, check=True)
220
+ else:
221
+ cmd = ["docker", "tag", image_list[0].fullname, i.fullname]
222
+ logging.info(f"Tagging {i.fullname}...")
223
+ subprocess.run(cmd, check=True)
224
+
225
+ image_list.append(i)
226
+ if len(image_list) == 0:
227
+ raise RuntimeError(f"No tags specified in {IMAGE_YAML_FILE} for image {img['name']}")
228
+
229
+ build_result[img["name"]] = image_list
230
+
231
+ if len(build_result) == 0:
232
+ raise RuntimeError("No images found to build.")
233
+
234
+ return build_result
235
+
236
+ def update_deployments(self, build_result: dict[str, list[Image]]) -> None:
237
+ for img in self._spec["images"]:
238
+ if img["name"] not in build_result:
239
+ continue
240
+ i = build_result[img["name"]][0] # Use first built image data only.
241
+ for deploy in (img.get("deployments") or []):
242
+ path = self._top / deploy["path"].format(**i.image_facts)
243
+ match = deploy["match"].format(**i.image_facts)
244
+ replace = deploy["replace"].format(**i.image_facts)
245
+ lines = []
246
+ with path.open() as fin:
247
+ for l in fin:
248
+ lines.append(re.sub(match, replace, l))
249
+
250
+ with path.open("w") as fout:
251
+ for l in lines:
252
+ fout.write(l)
253
+
@@ -0,0 +1,34 @@
1
+ #/usr/bin/env python3
2
+ import argparse
3
+ from buildimage import ImageBuilder
4
+ import subprocess
5
+ from jsonschema import ValidationError
6
+
7
+
8
+ def get_arguments():
9
+ parser = argparse.ArgumentParser("buildimage")
10
+ parser.add_argument("--nopush", action="store_true", help="Skip pushing built images")
11
+ parser.add_argument("--image", action='append', default=None, help="Only build named images, can be specified multiple times")
12
+ parser.add_argument("directory", nargs="?", help="Path to directory for the images.yaml file", default=".")
13
+ return parser.parse_args()
14
+
15
+
16
+ def main() -> None:
17
+ args = get_arguments()
18
+
19
+ builder = ImageBuilder(args.directory)
20
+
21
+ try:
22
+ builder.load_spec()
23
+ except ValidationError as e:
24
+ logging.error(f"Error in yaml-file: {e}")
25
+ sys.exit(1)
26
+
27
+ build_result: dict[str, list[Image]] = builder.build_images(args.image)
28
+
29
+ if not args.nopush:
30
+ for name in build_result:
31
+ for image in build_result[name]:
32
+ subprocess.run(["docker", "push", image.fullname], check=True)
33
+
34
+ builder.update_deployments(build_result)