Skip to content

Commit 0cdafe6

Browse files
authored
Merge pull request #85 from TomasTomecek/0.5.0-release
0.5.0 release
2 parents 1111531 + 3757a69 commit 0cdafe6

File tree

8 files changed

+253
-159
lines changed

8 files changed

+253
-159
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
1+
# 0.5.0
2+
3+
## Breaking changes
4+
5+
* Option `--labels` was renamed to `--label` to match other container tooling.
6+
7+
8+
## Features
9+
10+
* Ansible-bender can now be configured using Ansible variables. For more info,
11+
please [read the documentation](/docs/configuration.md)
12+
* Given this change, base image and target image name are now optional
13+
arguments of the `build` command.
14+
* Bender now uses more candidates when searching for python interpreter in the
15+
base image, namely `/usr/bin/python3.7` and so on.
16+
* You can now set annotations on the target image.
17+
* When bender invokes a playbook against a container, it now changes hosts
18+
variable (in a copy), so that it's not accidentally executed in localhost
19+
environment.
20+
* Json schema is now used to validate input.
21+
* Before starting the build process, bender checks if the container backend
22+
works.
23+
24+
25+
## Bug fixes
26+
27+
* When ansible-playbook command uses python 2, bender refuses to continue since
28+
the build will not work.
29+
* Errors are now being properly logged when bender looks for python interpreter
30+
in the base image.
31+
* There was a need for a compatibility fix with buildah 1.7.
32+
* A build will terminate if there was an exception thrown during the caching or
33+
layering process.
34+
* Bender will not try to load non-existent layers from cache.
35+
36+
137
# 0.4.0
38+
239
Ansible-bender now uses Azure Pipelines as a CI system.
340

441
## Features
42+
543
* There were updates to documentation in README:
644
* Info about vfs and overlay buildah storage backends.
745
* Rootless containers.

README.md

Lines changed: 136 additions & 135 deletions
Large diffs are not rendered by default.

ansible_bender/api.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,36 @@ def build(self, build):
6464
# we have to record as soon as possible
6565
self.db.record_build(build)
6666

67-
builder = self.get_builder(build)
68-
builder.sanity_check()
67+
try:
68+
builder = self.get_builder(build)
69+
builder.sanity_check()
6970

70-
# before we start messing with the base image, we need to check for its presence first
71-
if not builder.is_base_image_present():
72-
builder.pull()
73-
build.pulled = True
71+
# before we start messing with the base image, we need to check for its presence first
72+
if not builder.is_base_image_present():
73+
builder.pull()
74+
build.pulled = True
7475

75-
builder.check_container_creation()
76+
builder.check_container_creation()
7677

77-
# let's record base image as a first layer
78-
base_image_id = builder.get_image_id(build.base_image)
79-
build.record_layer(None, base_image_id, None, cached=True)
78+
# let's record base image as a first layer
79+
base_image_id = builder.get_image_id(build.base_image)
80+
build.record_layer(None, base_image_id, None, cached=True)
8081

81-
a_runner = AnsibleRunner(build.playbook_path, builder, build, debug=self.debug)
82+
a_runner = AnsibleRunner(build.playbook_path, builder, build, debug=self.debug)
8283

83-
# we are about to perform the build
84-
build.build_start_time = datetime.datetime.now()
85-
self.db.record_build(build, build_state=BuildState.IN_PROGRESS)
84+
# we are about to perform the build
85+
build.build_start_time = datetime.datetime.now()
86+
self.db.record_build(build, build_state=BuildState.IN_PROGRESS)
8687

87-
if not build.python_interpreter:
88-
build.python_interpreter = builder.find_python_interpreter()
88+
if not build.python_interpreter:
89+
build.python_interpreter = builder.find_python_interpreter()
8990

90-
builder.create()
91+
builder.create()
92+
except Exception:
93+
self.db.record_build(None, build_id=build.build_id,
94+
build_state=BuildState.FAILED,
95+
set_finish_time=True)
96+
raise
9197

9298
try:
9399
try:

ansible_bender/builders/buildah_builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ def find_python_interpreter(self):
256256
for i in self.python_interpr_prio:
257257
cmd = ["ls", i]
258258
try:
259-
podman_run_cmd(self.build.base_image, cmd, log_stderr=True)
259+
run_cmd(["podman", "run", "--rm", self.build.base_image] + cmd,
260+
log_stderr=False, log_output=True)
260261
except subprocess.CalledProcessError:
261262
logger.info("python interpreter %s does not exist", i)
262263
continue

ansible_bender/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def from_json(cls, j):
6161
m = cls()
6262
m.working_dir = j["working_dir"]
6363
m.labels = j["labels"]
64-
m.annotations = j[ANNOTATIONS_KEY]
64+
m.annotations = graceful_get(j, ANNOTATIONS_KEY, default={})
6565
m.env_vars = j["env_vars"]
6666
m.cmd = j["cmd"]
6767
m.user = j["user"]

ansible_bender/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def run_playbook(playbook_path, inventory_path, a_cfg_path, connection, extra_va
8080
"""
8181
ap = ap_command_exists()
8282
if is_ansibles_python_2(ap):
83+
# I just realized it could work, we would just had to disable the
84+
# callback plugin: no caching and layering
8385
raise RuntimeError(
8486
"ansible-bender is written in python 3 and does not work in python 2,\n"
8587
f"it seems that {ap} is using python 2 - ansible-bender will not"

docs/configuration.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
## Target image metadata
44

5-
With dockerfiles, this is being usally done with instructions such as `LABEL`,
6-
`ENV` or `EXPOSE`. Bender supports two ways of configuring the metadata:
5+
With dockerfiles, this is being usually done with instructions such as `LABEL`,
6+
`ENV` or `EXPOSE`. Bender supports two ways of configuring the metadata:
77

88
* Setting specific Ansible variables inside your playbook.
99
* CLI options of `ansible-bender build`.
1010

1111

1212
### Via playbook vars
1313

14-
**This feature is present in git master only, it was not released yet.**
15-
1614
Configuration is done using a top-level Ansible variable `ansible_bender`. All
1715
the values are nested under it. The values are processed before a build starts.
1816
The changes to values are not reflected during a playbook run.
@@ -51,6 +49,7 @@ only from the first play. All the plays will end up in a single container image.
5149
|----------------------|-----------------|----------------------------------------------------------------------|
5250
| `name` | string | name of the image |
5351
| `labels` | dict | key/value data to apply to the final image |
52+
| `annotations` | dict | key/value data to apply to the final image (buildah/runc specific) |
5453
| `environment` | dict | implicit environment variables to set in a container |
5554
| `cmd` | string | a default command to invoke the container |
5655
| `user` | string | UID or username used to invoke the container |
@@ -91,6 +90,24 @@ Please bear in mind that most of the facts won't be available.
9190
Please check out `ansible-bender build --help` for up to date options:
9291

9392
```
93+
$ ansible-bender build -h
94+
usage: ansible-bender build [-h] [--builder {docker,buildah}] [--no-cache]
95+
[--build-volumes [BUILD_VOLUMES [BUILD_VOLUMES ...]]]
96+
[-w WORKDIR] [-l [LABELS [LABELS ...]]]
97+
[--annotation [ANNOTATIONS [ANNOTATIONS ...]]]
98+
[-e [ENV_VARS [ENV_VARS ...]]] [--cmd CMD]
99+
[-u USER] [-p [PORTS [PORTS ...]]]
100+
[--runtime-volumes [RUNTIME_VOLUMES [RUNTIME_VOLUMES ...]]]
101+
[--extra-ansible-args EXTRA_ANSIBLE_ARGS]
102+
[--python-interpreter PYTHON_INTERPRETER]
103+
PLAYBOOK_PATH [BASE_IMAGE] [TARGET_IMAGE]
104+
105+
positional arguments:
106+
PLAYBOOK_PATH path to Ansible playbook
107+
BASE_IMAGE name of a container image to use as a base
108+
TARGET_IMAGE name of the built container image
109+
110+
optional arguments:
94111
-h, --help show this help message and exit
95112
--builder {docker,buildah}
96113
pick preferred builder backend
@@ -104,9 +121,11 @@ Please check out `ansible-bender build --help` for up to date options:
104121
'/host/dir:/container/dir'
105122
-w WORKDIR, --workdir WORKDIR
106123
path to an implicit working directory in the container
107-
-l [LABELS [LABELS ...]], --labels [LABELS [LABELS ...]]
124+
-l [LABELS [LABELS ...]], --label [LABELS [LABELS ...]]
108125
add a label to the metadata of the image, should be
109126
specified as 'key=value'
127+
--annotation [ANNOTATIONS [ANNOTATIONS ...]]
128+
Add key=value annotation for the target image
110129
-e [ENV_VARS [ENV_VARS ...]], --env-vars [ENV_VARS [ENV_VARS ...]]
111130
add an environment variable to the metadata of the
112131
image, should be specified as 'KEY=VALUE'
@@ -122,4 +141,6 @@ Please check out `ansible-bender build --help` for up to date options:
122141
careful!)
123142
--python-interpreter PYTHON_INTERPRETER
124143
Path to a python interpreter inside the base image
144+
145+
Please use '--' to separate options and arguments.
125146
```

simple-playbook.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- name: Demonstration of ansible-bender functionality
3+
hosts: all
4+
vars:
5+
ansible_bender:
6+
base_image: python:3-alpine
7+
8+
working_container:
9+
volumes:
10+
- '{{ playbook_dir }}:/src'
11+
12+
target_image:
13+
name: a-very-nice-image
14+
working_dir: /src
15+
labels:
16+
built-by: '{{ ansible_user }}'
17+
environment:
18+
FILE_TO_PROCESS: README.md
19+
tasks:
20+
- name: Run a sample command
21+
command: 'ls -lha /src'
22+
- name: Stat a file
23+
stat:
24+
path: "{{ lookup('env','FILE_TO_PROCESS') }}"
25+

0 commit comments

Comments
 (0)