Skip to content

Commit c203514

Browse files
authored
Merge pull request #122 from 11mariom/master
Add option to specify build user
2 parents eb2ef80 + 4cf3887 commit c203514

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

ansible_bender/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def build(self, build):
121121
set_finish_time=True)
122122
b.log_lines = output
123123
# commit the final image and apply all metadata
124-
b.final_layer_id = builder.commit(build.target_image)
124+
b.final_layer_id = builder.commit(build.target_image, final_image=True)
125125

126126
if not b.is_layering_on():
127127
self.record_progress(b, None, b.final_layer_id)

ansible_bender/builders/buildah_builder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def create(self):
166166
# let's apply configuration before execing the playbook, except for user
167167
configure_buildah_container(
168168
self.ansible_host, working_dir=self.build.metadata.working_dir,
169+
user=self.build.build_user,
169170
env_vars=self.build.metadata.env_vars,
170171
ports=self.build.metadata.ports,
171172
labels=self.build.metadata.labels, # labels are not applied when they are configured
@@ -192,11 +193,16 @@ def swap_working_container(self):
192193
self.clean()
193194
self.create()
194195

195-
def commit(self, image_name, print_output=True):
196+
def commit(self, image_name, print_output=True, final_image=False):
197+
if final_image:
198+
user=self.build.metadata.user
199+
else:
200+
user=self.build.build_user
201+
196202
if self.build.metadata.user or self.build.metadata.cmd or self.build.metadata.volumes:
197203
# change user if needed
198204
configure_buildah_container(
199-
self.ansible_host, user=self.build.metadata.user,
205+
self.ansible_host, user=user,
200206
cmd=self.build.metadata.cmd,
201207
volumes=self.build.metadata.volumes,
202208
)

ansible_bender/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def _do_build_interface(self):
105105
"should be specified as '/host/dir:/container/dir'",
106106
nargs="*"
107107
)
108+
self.build_parser.add_argument(
109+
"--build-user",
110+
help="the container gets invoked with this user during build"
111+
)
108112
self.build_parser.add_argument(
109113
"-w", "--workdir",
110114
help="path to an implicit working directory in the container"

ansible_bender/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __init__(self):
118118
self.build_id = None # PK, should be set by database
119119
self.playbook_path = None
120120
self.build_volumes = [] # volumes for the build container
121+
self.build_user = None
121122
self.metadata = None # Image metadata
122123
self.state = BuildState.NEW
123124
self.build_start_time = None
@@ -144,6 +145,7 @@ def to_dict(self):
144145
"build_id": self.build_id,
145146
"playbook_path": self.playbook_path,
146147
"build_volumes": self.build_volumes,
148+
"build_user": self.build_user,
147149
"metadata": self.metadata.to_dict(),
148150
"state": self.state.value,
149151
"build_start_time": self.build_start_time.strftime(TIMESTAMP_FORMAT)
@@ -171,6 +173,7 @@ def to_dict(self):
171173
def update_from_configuration(self, data):
172174
""" update current object with data provided from Ansible vars """
173175
self.build_volumes += graceful_get(data, "working_container", "volumes", default=[])
176+
self.build_user = graceful_get(data, "working_container", "user")
174177
self.base_image = graceful_get(data, "base_image")
175178
self.target_image = graceful_get(data, "target_image", "name")
176179
# self.builder_name = None
@@ -187,6 +190,7 @@ def from_json(cls, j):
187190
b.build_id = j["build_id"]
188191
b.playbook_path = j.get("playbook_path", None)
189192
b.build_volumes = j["build_volumes"]
193+
b.build_user = j["build_user"]
190194
b.metadata = ImageMetadata.from_json(j["metadata"])
191195
b.state = BuildState(j["state"])
192196
b.build_start_time = None

docs/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ only from the first play. All the plays will end up in a single container image.
3636
| Key name | type | description |
3737
|----------------------|-----------------|----------------------------------------------------------------------|
3838
| `volumes` | list of strings | volumes mappings for the working container (`HOST:CONTAINER:PARAMS`) |
39+
| `user` | string | UID or username to invoke the container during build (run ansible) |
3940

4041
#### `target_image`
4142

@@ -113,6 +114,7 @@ optional arguments:
113114
mount selected directory inside the container during
114115
build, should be specified as
115116
'/host/dir:/container/dir'
117+
--build-user USER the container gets invoked with this user during build
116118
-w WORKDIR, --workdir WORKDIR
117119
path to an implicit working directory in the container
118120
-l [LABELS [LABELS ...]], --label [LABELS [LABELS ...]]

0 commit comments

Comments
 (0)