Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for buildah --entrypoint #155

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ansible_bender/builders/buildah_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def create_buildah_container(container_image, container_name, build_volumes=None

def configure_buildah_container(container_name, working_dir=None, env_vars=None,
labels=None, annotations=None,
user=None, cmd=None, ports=None, volumes=None,
user=None, cmd=None, entrypoint=None,
ports=None, volumes=None,
debug=False):
"""
apply metadata on the container so they get inherited in an image
Expand All @@ -95,6 +96,7 @@ def configure_buildah_container(container_name, working_dir=None, env_vars=None,
:param annotations: dict with annotations
:param env_vars: dict with env vars
:param cmd: str, command to run by default in the container
:param entrypoint: str, entrypoint script to configure for the container
:param user: str, username or uid; the container gets invoked with this user by default
:param ports: list of str, ports to expose from container by default
:param volumes: list of str; paths within the container which has data stored outside
Expand All @@ -117,6 +119,8 @@ def configure_buildah_container(container_name, working_dir=None, env_vars=None,
config_args += ["--user", user]
if cmd:
config_args += ["--cmd", cmd]
if entrypoint:
config_args += ["--entrypoint", entrypoint]
if ports:
for p in ports:
config_args += ["-p", p]
Expand Down Expand Up @@ -216,12 +220,14 @@ def commit(self, image_name: Optional[str] = None, print_output: bool = True, fi
else:
user = self.build.build_user

if self.build.metadata.user or self.build.metadata.cmd or self.build.metadata.volumes:
if (self.build.metadata.user or self.build.metadata.cmd or
self.build.metadata.entrypoint or self.build.metadata.volumes):
# change user if needed
configure_buildah_container(
self.ansible_host,
user=user,
cmd=self.build.metadata.cmd,
entrypoint=self.build.metadata.entrypoint,
volumes=self.build.metadata.volumes,
)

Expand Down
6 changes: 6 additions & 0 deletions ansible_bender/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def _do_build_interface(self):
"--cmd",
help="command to run by default in the container"
)
self.build_parser.add_argument(
"--entrypoint",
help="entrypoint script to configure for the container"
)
self.build_parser.add_argument(
"-u", "--user",
help="the container gets invoked with this user by default"
Expand Down Expand Up @@ -266,6 +270,8 @@ def _build(self):
metadata.env_vars[k] = v
if self.args.cmd:
metadata.cmd = self.args.cmd
if self.args.entrypoint:
metadata.entrypoint = self.args.entrypoint
if self.args.user:
metadata.user = self.args.user
if self.args.ports:
Expand Down
5 changes: 5 additions & 0 deletions ansible_bender/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ImageMetadata:
labels: dict with labels
env_vars: dict with env vars
cmd: str, command to run by default in the container
entrypoint: str, entrypoint script to configure for the container
user: str, username or uid; the container gets invoked with this user by default
ports: list of str, ports to expose from container by default
volumes: list of str; paths within the container which has data stored outside
Expand All @@ -28,6 +29,7 @@ def __init__(self):
self.annotations = {}
self.env_vars = {}
self.cmd = None
self.entrypoint = None
self.user = None
self.ports = []
self.volumes = []
Expand All @@ -39,6 +41,7 @@ def to_dict(self):
ANNOTATIONS_KEY: self.annotations,
"env_vars": self.env_vars,
"cmd": self.cmd,
"entrypoint": self.entrypoint,
"user": self.user,
"ports": self.ports,
"volumes": self.volumes
Expand All @@ -51,6 +54,7 @@ def update_from_configuration(self, data):
self.annotations.update(data.get(ANNOTATIONS_KEY, {}))
self.env_vars.update(data.get("environment", {}))
self.cmd = data.get("cmd", None)
self.entrypoint = data.get("entrypoint", None)
self.user = data.get("user", None)
self.ports += data.get("ports", [])
self.volumes += data.get("volumes", [])
Expand All @@ -64,6 +68,7 @@ def from_json(cls, j):
m.annotations = graceful_get(j, ANNOTATIONS_KEY, default={})
m.env_vars = j["env_vars"]
m.cmd = j["cmd"]
m.entrypoint = j["entrypoint"]
m.user = j["user"]
m.ports = j["ports"]
m.volumes = j["volumes"]
Expand Down
13 changes: 12 additions & 1 deletion ansible_bender/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"annotations",
"env_vars",
"cmd",
"entrypoint",
"user",
"ports",
"volumes"
Expand Down Expand Up @@ -272,6 +273,16 @@
],
"pattern": "^(.*)$"
},
"entrypoint": {
"$id": "#/properties/entrypoint",
"type": ["string", "null"],
"title": "An entrypoint script to configure for the container",
"default": "",
"examples": [
"entrypoint.sh"
],
"pattern": "^(.*)$"
},
"user": {
"$id": "#/properties/user",
"type": ["string", "null"],
Expand Down Expand Up @@ -313,4 +324,4 @@
}
}
}
}
}
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ only from the first play. All the plays will end up in a single container image.
| `annotations` | dict | key/value data to apply to the final image (buildah/runc specific) |
| `environment` | dict | implicit environment variables to set in a container |
| `cmd` | string | a default command to invoke the container |
| `entrypoint` | string | entrypoint script to configure for the container |
| `user` | string | UID or username used to invoke the container |
| `ports` | list of strings | a list of ports which are meant to be exposed on the host |
| `volumes` | list of strings | a list of paths which are meant to be hosted outside of the container|
Expand Down Expand Up @@ -95,6 +96,7 @@ usage: ansible-bender build [-h] [--builder {docker,buildah}] [--no-cache]
[-l [LABELS [LABELS ...]]]
[--annotation [ANNOTATIONS [ANNOTATIONS ...]]]
[-e [ENV_VARS [ENV_VARS ...]]] [--cmd CMD]
[--entrypoint ENTRYPOINT]
[-u USER] [-p [PORTS [PORTS ...]]]
[--runtime-volumes [RUNTIME_VOLUMES [RUNTIME_VOLUMES ...]]]
[--extra-buildah-from-args EXTRA_BUILDAH_FROM_ARGS]
Expand Down Expand Up @@ -132,6 +134,8 @@ optional arguments:
add an environment variable to the metadata of the
image, should be specified as 'KEY=VALUE'
--cmd CMD command to run by default in the container
--entrypoint ENTRYPOINT
entrypoint script to configure for the container
-u USER, --user USER the container gets invoked with this user by default
-p [PORTS [PORTS ...]], --ports [PORTS [PORTS ...]]
ports to expose from container by default
Expand Down
1 change: 1 addition & 0 deletions tests/data/full_conf_pb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
environment:
z: '{{ key }}'
cmd: command -x -y z
entrypoint: great-entry-point
user: leonardo

tasks: []
3 changes: 3 additions & 0 deletions tests/functional/test_buildah.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def test_build_basic_image_with_all_params(tmpdir, target_image):
e_x_y = "X=Y"
ann = "bohemian=rhapsody"
cmd, cmd_e = "ls -lha", ["ls", "-lha"]
entrypoint, entrypoint_e = "ls -lha", ["/bin/sh", "-c", "ls -lha"]
# FIXME: this doesn't work with user namespaces
# user = "1000"
p_80, p_443 = "80", "443"
Expand All @@ -117,6 +118,7 @@ def test_build_basic_image_with_all_params(tmpdir, target_image):
"--annotation", ann,
"-e", e_a_b, e_x_y,
"--cmd", cmd,
"--entrypoint", entrypoint,
# "-u", user,
"-p", p_80, p_443,
"--runtime-volumes", runtime_volume,
Expand All @@ -132,6 +134,7 @@ def test_build_basic_image_with_all_params(tmpdir, target_image):
assert e_a_b in co["Env"]
assert e_x_y in co["Env"]
assert co["Cmd"] == cmd_e
assert co["Entrypoint"] == entrypoint_e
# assert co["User"] == user
assert p_80 in co["ExposedPorts"]
assert p_443 in co["ExposedPorts"]
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_inspect_cmd(tmpdir, target_image):
e_a_b = "A=B"
e_x_y = "X=Y"
cmd, cmd_e = "ls -lha", ["ls", "-lha"]
entrypoint = "ls -lha"
# FIXME
# user = "1000"
p_80, p_443 = "80", "443"
Expand All @@ -18,6 +19,7 @@ def test_inspect_cmd(tmpdir, target_image):
"-l", l_a_b, l_x_y,
"-e", e_a_b, e_x_y,
"--cmd", cmd,
"--entrypoint", entrypoint,
# "-u", user,
"-p", p_80, p_443,
"--runtime-volumes", runtime_volume,
Expand All @@ -43,6 +45,7 @@ def test_inspect_cmd(tmpdir, target_image):
metadata:
annotations: {}
cmd: ls -lha
entrypoint: ls -lha
env_vars:
A: B
X: Y
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_set_all_params():
assert m.labels == {"x": "y"}
assert m.annotations == {"bohemian": "rhapsody"}
assert m.cmd == "command -x -y z"
assert m.entrypoint == "great-entry-point"
assert m.user == "leonardo"


Expand Down