Skip to content

Commit

Permalink
Merge pull request #85 from TomasTomecek/0.5.0-release
Browse files Browse the repository at this point in the history
0.5.0 release
  • Loading branch information
TomasTomecek authored Mar 2, 2019
2 parents 1111531 + 3757a69 commit 0cdafe6
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 159 deletions.
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
# 0.5.0

## Breaking changes

* Option `--labels` was renamed to `--label` to match other container tooling.


## Features

* Ansible-bender can now be configured using Ansible variables. For more info,
please [read the documentation](/docs/configuration.md)
* Given this change, base image and target image name are now optional
arguments of the `build` command.
* Bender now uses more candidates when searching for python interpreter in the
base image, namely `/usr/bin/python3.7` and so on.
* You can now set annotations on the target image.
* When bender invokes a playbook against a container, it now changes hosts
variable (in a copy), so that it's not accidentally executed in localhost
environment.
* Json schema is now used to validate input.
* Before starting the build process, bender checks if the container backend
works.


## Bug fixes

* When ansible-playbook command uses python 2, bender refuses to continue since
the build will not work.
* Errors are now being properly logged when bender looks for python interpreter
in the base image.
* There was a need for a compatibility fix with buildah 1.7.
* A build will terminate if there was an exception thrown during the caching or
layering process.
* Bender will not try to load non-existent layers from cache.


# 0.4.0

Ansible-bender now uses Azure Pipelines as a CI system.

## Features

* There were updates to documentation in README:
* Info about vfs and overlay buildah storage backends.
* Rootless containers.
Expand Down
271 changes: 136 additions & 135 deletions README.md

Large diffs are not rendered by default.

40 changes: 23 additions & 17 deletions ansible_bender/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,36 @@ def build(self, build):
# we have to record as soon as possible
self.db.record_build(build)

builder = self.get_builder(build)
builder.sanity_check()
try:
builder = self.get_builder(build)
builder.sanity_check()

# before we start messing with the base image, we need to check for its presence first
if not builder.is_base_image_present():
builder.pull()
build.pulled = True
# before we start messing with the base image, we need to check for its presence first
if not builder.is_base_image_present():
builder.pull()
build.pulled = True

builder.check_container_creation()
builder.check_container_creation()

# let's record base image as a first layer
base_image_id = builder.get_image_id(build.base_image)
build.record_layer(None, base_image_id, None, cached=True)
# let's record base image as a first layer
base_image_id = builder.get_image_id(build.base_image)
build.record_layer(None, base_image_id, None, cached=True)

a_runner = AnsibleRunner(build.playbook_path, builder, build, debug=self.debug)
a_runner = AnsibleRunner(build.playbook_path, builder, build, debug=self.debug)

# we are about to perform the build
build.build_start_time = datetime.datetime.now()
self.db.record_build(build, build_state=BuildState.IN_PROGRESS)
# we are about to perform the build
build.build_start_time = datetime.datetime.now()
self.db.record_build(build, build_state=BuildState.IN_PROGRESS)

if not build.python_interpreter:
build.python_interpreter = builder.find_python_interpreter()
if not build.python_interpreter:
build.python_interpreter = builder.find_python_interpreter()

builder.create()
builder.create()
except Exception:
self.db.record_build(None, build_id=build.build_id,
build_state=BuildState.FAILED,
set_finish_time=True)
raise

try:
try:
Expand Down
3 changes: 2 additions & 1 deletion ansible_bender/builders/buildah_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def find_python_interpreter(self):
for i in self.python_interpr_prio:
cmd = ["ls", i]
try:
podman_run_cmd(self.build.base_image, cmd, log_stderr=True)
run_cmd(["podman", "run", "--rm", self.build.base_image] + cmd,
log_stderr=False, log_output=True)
except subprocess.CalledProcessError:
logger.info("python interpreter %s does not exist", i)
continue
Expand Down
2 changes: 1 addition & 1 deletion ansible_bender/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def from_json(cls, j):
m = cls()
m.working_dir = j["working_dir"]
m.labels = j["labels"]
m.annotations = j[ANNOTATIONS_KEY]
m.annotations = graceful_get(j, ANNOTATIONS_KEY, default={})
m.env_vars = j["env_vars"]
m.cmd = j["cmd"]
m.user = j["user"]
Expand Down
2 changes: 2 additions & 0 deletions ansible_bender/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def run_playbook(playbook_path, inventory_path, a_cfg_path, connection, extra_va
"""
ap = ap_command_exists()
if is_ansibles_python_2(ap):
# I just realized it could work, we would just had to disable the
# callback plugin: no caching and layering
raise RuntimeError(
"ansible-bender is written in python 3 and does not work in python 2,\n"
f"it seems that {ap} is using python 2 - ansible-bender will not"
Expand Down
31 changes: 26 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

## Target image metadata

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

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


### Via playbook vars

**This feature is present in git master only, it was not released yet.**

Configuration is done using a top-level Ansible variable `ansible_bender`. All
the values are nested under it. The values are processed before a build starts.
The changes to values are not reflected during a playbook run.
Expand Down Expand Up @@ -51,6 +49,7 @@ only from the first play. All the plays will end up in a single container image.
|----------------------|-----------------|----------------------------------------------------------------------|
| `name` | string | name of the image |
| `labels` | dict | key/value data to apply to the final 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 |
| `user` | string | UID or username used to invoke the container |
Expand Down Expand Up @@ -91,6 +90,24 @@ Please bear in mind that most of the facts won't be available.
Please check out `ansible-bender build --help` for up to date options:

```
$ ansible-bender build -h
usage: ansible-bender build [-h] [--builder {docker,buildah}] [--no-cache]
[--build-volumes [BUILD_VOLUMES [BUILD_VOLUMES ...]]]
[-w WORKDIR] [-l [LABELS [LABELS ...]]]
[--annotation [ANNOTATIONS [ANNOTATIONS ...]]]
[-e [ENV_VARS [ENV_VARS ...]]] [--cmd CMD]
[-u USER] [-p [PORTS [PORTS ...]]]
[--runtime-volumes [RUNTIME_VOLUMES [RUNTIME_VOLUMES ...]]]
[--extra-ansible-args EXTRA_ANSIBLE_ARGS]
[--python-interpreter PYTHON_INTERPRETER]
PLAYBOOK_PATH [BASE_IMAGE] [TARGET_IMAGE]
positional arguments:
PLAYBOOK_PATH path to Ansible playbook
BASE_IMAGE name of a container image to use as a base
TARGET_IMAGE name of the built container image
optional arguments:
-h, --help show this help message and exit
--builder {docker,buildah}
pick preferred builder backend
Expand All @@ -104,9 +121,11 @@ Please check out `ansible-bender build --help` for up to date options:
'/host/dir:/container/dir'
-w WORKDIR, --workdir WORKDIR
path to an implicit working directory in the container
-l [LABELS [LABELS ...]], --labels [LABELS [LABELS ...]]
-l [LABELS [LABELS ...]], --label [LABELS [LABELS ...]]
add a label to the metadata of the image, should be
specified as 'key=value'
--annotation [ANNOTATIONS [ANNOTATIONS ...]]
Add key=value annotation for the target image
-e [ENV_VARS [ENV_VARS ...]], --env-vars [ENV_VARS [ENV_VARS ...]]
add an environment variable to the metadata of the
image, should be specified as 'KEY=VALUE'
Expand All @@ -122,4 +141,6 @@ Please check out `ansible-bender build --help` for up to date options:
careful!)
--python-interpreter PYTHON_INTERPRETER
Path to a python interpreter inside the base image
Please use '--' to separate options and arguments.
```
25 changes: 25 additions & 0 deletions simple-playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
- name: Demonstration of ansible-bender functionality
hosts: all
vars:
ansible_bender:
base_image: python:3-alpine

working_container:
volumes:
- '{{ playbook_dir }}:/src'

target_image:
name: a-very-nice-image
working_dir: /src
labels:
built-by: '{{ ansible_user }}'
environment:
FILE_TO_PROCESS: README.md
tasks:
- name: Run a sample command
command: 'ls -lha /src'
- name: Stat a file
stat:
path: "{{ lookup('env','FILE_TO_PROCESS') }}"

0 comments on commit 0cdafe6

Please sign in to comment.