Merge ~pappacena/launchpad-buildd:oci-build-arg into launchpad-buildd:master

Proposed by Thiago F. Pappacena
Status: Merged
Approved by: Thiago F. Pappacena
Approved revision: 3d24c4ccf555e8f20cc7fbb571e50e84803f2e9a
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~pappacena/launchpad-buildd:oci-build-arg
Merge into: launchpad-buildd:master
Diff against target: 161 lines (+71/-2)
5 files modified
README (+28/-0)
lpbuildd/oci.py (+6/-0)
lpbuildd/target/build_oci.py (+11/-0)
lpbuildd/target/tests/test_build_oci.py (+21/-0)
lpbuildd/tests/test_oci.py (+5/-2)
Reviewer Review Type Date Requested Status
Colin Watson Approve
Review via email: mp+389869@code.launchpad.net

Commit message

Adding support for --build-arg when building OCI images

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

Pushed the requested changes. This should be good to be landed now.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/README b/README
2index d2eb28e..05a74c8 100644
3--- a/README
4+++ b/README
5@@ -29,6 +29,34 @@ fakeroot debian/rules clean
6 rm launchpad-buildd*deb
7 rm ../launchpad-buildd*changes
8
9+
10+Developing
11+************
12+
13+First of all, it is recommended that you create a lxc container, since the
14+following steps will make changes in your system. And since some build types
15+will only work with virtualized containers, creating a lxc vm is the best way
16+to go. You can create a container with the following command:
17+
18+ lxc launch --vm ubuntu:18.04 lp-builddev
19+
20+Note that you may want to have a profile to share the source code with the
21+container before running the above command.
22+
23+Then, inside the container, install the necessary dependencies:
24+
25+ sudo apt-get update
26+ cat system-dependencies.txt | sudo xargs apt-get install -y
27+
28+This should be enough for you to be able to run `make check`, which runs the
29+test suite both in python2 and python3.
30+
31+More information on how to integrate it with Launchpad can be found here:
32+https://dev.launchpad.net/Soyuz/HowToDevelopWithBuildd
33+
34+Now, happy coding.
35+
36+
37 Production deployment notes
38 ***************************
39
40diff --git a/lpbuildd/oci.py b/lpbuildd/oci.py
41index 1fccada..480c5d6 100644
42--- a/lpbuildd/oci.py
43+++ b/lpbuildd/oci.py
44@@ -21,6 +21,7 @@ from lpbuildd.debian import (
45 DebianBuildState,
46 )
47 from lpbuildd.snap import SnapBuildProxyMixin
48+from lpbuildd.util import shell_escape
49
50
51 RETCODE_SUCCESS = 0
52@@ -49,6 +50,7 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
53 self.git_repository = extra_args.get("git_repository")
54 self.git_path = extra_args.get("git_path")
55 self.build_file = extra_args.get("build_file")
56+ self.build_args = extra_args.get("build_args", {})
57 self.build_path = extra_args.get("build_path")
58 self.proxy_url = extra_args.get("proxy_url")
59 self.revocation_endpoint = extra_args.get("revocation_endpoint")
60@@ -70,6 +72,10 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
61 args.extend(["--git-path", self.git_path])
62 if self.build_file is not None:
63 args.extend(["--build-file", self.build_file])
64+ if self.build_args:
65+ for k, v in self.build_args.items():
66+ args.extend([
67+ "--build-arg=%s=%s" % (shell_escape(k), shell_escape(v))])
68 if self.build_path is not None:
69 args.extend(["--build-path", self.build_path])
70 try:
71diff --git a/lpbuildd/target/build_oci.py b/lpbuildd/target/build_oci.py
72index 377fc68..af56671 100644
73--- a/lpbuildd/target/build_oci.py
74+++ b/lpbuildd/target/build_oci.py
75@@ -42,6 +42,11 @@ class BuildOCI(SnapBuildProxyOperationMixin, VCSOperationMixin,
76 parser.add_argument(
77 "--build-path", default=".",
78 help="context directory for docker build")
79+ parser.add_argument(
80+ "--build-arg", default=[], action='append',
81+ help="A docker build ARG in the format of key=value. "
82+ "This option can be repeated many times. For example: "
83+ "--build-arg VAR1=A --build-arg VAR2=B")
84 parser.add_argument("name", help="name of snap to build")
85
86 def __init__(self, args, parser):
87@@ -127,6 +132,12 @@ class BuildOCI(SnapBuildProxyOperationMixin, VCSOperationMixin,
88 self.args.build_path, self.args.build_file)
89 self._check_path_escape(build_file_path)
90 args.extend(["--file", build_file_path])
91+
92+ # Keep this at the end, so we give the user a chance to override any
93+ # build-arg we set automatically (like http_proxy).
94+ for arg in self.args.build_arg:
95+ args.extend(["--build-arg=%s" % arg])
96+
97 build_context_path = os.path.join(
98 self.buildd_path, self.args.build_path)
99 self._check_path_escape(build_context_path)
100diff --git a/lpbuildd/target/tests/test_build_oci.py b/lpbuildd/target/tests/test_build_oci.py
101index a2c67d7..e58344f 100644
102--- a/lpbuildd/target/tests/test_build_oci.py
103+++ b/lpbuildd/target/tests/test_build_oci.py
104@@ -360,6 +360,27 @@ class TestBuildOCI(TestCase):
105 cwd="/home/buildd/test-image"),
106 ]))
107
108+ def test_build_with_args(self):
109+ args = [
110+ "build-oci",
111+ "--backend=fake", "--series=xenial", "--arch=amd64", "1",
112+ "--branch", "lp:foo", "--build-file", "build-aux/Dockerfile",
113+ "--build-path", "test-build-path",
114+ "--build-arg=VAR1=xxx", "--build-arg=VAR2=yyy",
115+ "test-image",
116+ ]
117+ build_oci = parse_args(args=args).operation
118+ build_oci.backend.add_dir('/build/test-directory')
119+ build_oci.build()
120+ self.assertThat(build_oci.backend.run.calls, MatchesListwise([
121+ RanBuildCommand(
122+ ["docker", "build", "--no-cache", "--tag", "test-image",
123+ "--file", "test-build-path/build-aux/Dockerfile",
124+ "--build-arg=VAR1=xxx", "--build-arg=VAR2=yyy",
125+ "/home/buildd/test-image/test-build-path"],
126+ cwd="/home/buildd/test-image"),
127+ ]))
128+
129 def test_build_proxy(self):
130 args = [
131 "build-oci",
132diff --git a/lpbuildd/tests/test_oci.py b/lpbuildd/tests/test_oci.py
133index f2dd421..6079634 100644
134--- a/lpbuildd/tests/test_oci.py
135+++ b/lpbuildd/tests/test_oci.py
136@@ -183,7 +183,7 @@ class TestOCIBuildManagerIteration(TestCase):
137 self.assertFalse(self.builder.wasCalled("buildFail"))
138
139 @defer.inlineCallbacks
140- def test_iterate_with_file(self):
141+ def test_iterate_with_file_and_args(self):
142 # This sha would change as it includes file attributes in the
143 # tar file. Fix it so we can test against a known value.
144 sha_mock = self.useFixture(
145@@ -195,12 +195,15 @@ class TestOCIBuildManagerIteration(TestCase):
146 "git_repository": "https://git.launchpad.dev/~example/+git/snap",
147 "git_path": "master",
148 "build_file": "build-aux/Dockerfile",
149+ "build_args": {"VAR1": "xxx", "VAR2": "yyy"}
150 }
151 expected_options = [
152 "--git-repository", "https://git.launchpad.dev/~example/+git/snap",
153 "--git-path", "master",
154 "--build-file", "build-aux/Dockerfile",
155- ]
156+ "--build-arg=VAR1=xxx",
157+ "--build-arg=VAR2=yyy",
158+ ]
159 yield self.startBuild(args, expected_options)
160
161 log_path = os.path.join(self.buildmanager._cachepath, "buildlog")

Subscribers

People subscribed via source and target branches