Skip to content

Commit

Permalink
Add history metadata for container builds
Browse files Browse the repository at this point in the history
This commit adds the history section in contianerconfig. With it
'author', 'created_by' and 'comment' can be customized. In addition
'created' is always included with the image creation date time.
'created_by' entry is set to 'KIWI __version__' by default if nothing
is provided.

Fixes #852
  • Loading branch information
davidcassany committed Nov 8, 2018
1 parent 2fa147d commit f067549
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 105 deletions.
17 changes: 14 additions & 3 deletions kiwi/container/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ class ContainerImageOCI(object):
'expose_ports': ['80', '42'],
'volumes': ['/var/log', '/tmp'],
'environment': {'PATH': '/bin'},
'labels': {'name': 'value'}
'labels': {'name': 'value'},
'history': {
'created_by': 'some explanation here',
'comment': 'some comment here',
'author': 'tux'
}
}
"""
def __init__(self, root_dir, custom_args=None):
Expand Down Expand Up @@ -95,6 +100,12 @@ def __init__(self, root_dir, custom_args=None):
self.oci_config['entry_subcommand'] = \
Defaults.get_default_container_subcommand()

if 'history' not in self.oci_config:
self.oci_config['history'] = {}
if 'created_by' not in self.oci_config['history']:
self.oci_config['history']['created_by'] = \
Defaults.get_default_container_created_by()

self.oci = OCI(self.oci_config['container_tag'])

def create(self, filename, base_image):
Expand All @@ -117,7 +128,7 @@ def create(self, filename, base_image):
image_tar = ArchiveTar(base_image)
image_tar.extract(self.oci.container_dir)

self.oci.init_layout(base_image)
self.oci.init_layout(True if base_image else False)

self.oci.unpack(self.oci_root_dir)
oci_root = DataSync(
Expand All @@ -133,7 +144,7 @@ def create(self, filename, base_image):
for tag in self.oci_config['additional_tags']:
self.oci.add_tag(tag)

self.oci.set_config(self.oci_config)
self.oci.set_config(self.oci_config, True if base_image else False)

self.oci.garbage_collect()

Expand Down
16 changes: 15 additions & 1 deletion kiwi/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

# project
from .path import Path
from .version import __githash__
from .version import (
__githash__,
__version__
)


class Defaults(object):
Expand Down Expand Up @@ -1173,6 +1176,17 @@ def get_default_container_subcommand(self):
"""
return ['/bin/bash']

@classmethod
def get_default_container_created_by(self):
"""
Provides the default 'created by' history entry for containers.
:return: the specific kiwi version used for the build
:rtype: str
"""
return 'KIWI {0}'.format(__version__)

@classmethod
def set_python_default_encoding_to_utf8(self):
"""
Expand Down
7 changes: 5 additions & 2 deletions kiwi/oci_tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
import os
from tempfile import mkdtemp
from datetime import datetime

# project
from kiwi.path import Path
Expand Down Expand Up @@ -51,6 +52,9 @@ def __init__(self, container_tag, container_dir=None):
self.container_name = ':'.join(
[self.container_dir, self.container_tag]
)
self.creation_date = datetime.utcnow().strftime(
'%Y-%m-%dT%H:%M:%S+00:00'
)

def init_layout(self, base_image=False):
"""
Expand Down Expand Up @@ -81,7 +85,6 @@ def repack(self, oci_root_dir):
Implementation in specialized tool class
:param string oci_root_dir: root data directory
:param string container_name: custom container_dir:tag specifier
"""
raise NotImplementedError

Expand All @@ -96,7 +99,7 @@ def add_tag(self, tag_name):
"""
raise NotImplementedError

def set_config(self, oci_config):
def set_config(self, oci_config, base_image=False):
"""
Set list of meta data information such as entry_point,
maintainer, etc... to the container. The validation of
Expand Down
44 changes: 31 additions & 13 deletions kiwi/oci_tools/umoci.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# You should have received a copy of the GNU General Public License
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
#
from datetime import datetime

# project
from kiwi.oci_tools.base import OCIBase
Expand All @@ -36,16 +35,10 @@ def init_layout(self, base_image=False):
The import and unpack of the base image is not a
responsibility of this class and done beforehead
:param string base_image: True|False
:param bool base_image: True|False
"""
if base_image:
Command.run(
[
'umoci', 'config', '--image',
'{0}:base_layer'.format(self.container_dir),
'--tag', self.container_tag
]
)
self.container_name = '{0}:base_layer'.format(self.container_dir)
else:
Command.run(
['umoci', 'init', '--layout', self.container_dir]
Expand Down Expand Up @@ -87,24 +80,30 @@ def add_tag(self, tag_name):
]
)

def set_config(self, oci_config):
def set_config(self, oci_config, base_image=False):
"""
Set list of meta data information such as entry_point,
maintainer, etc... to the container.
:param list oci_config: meta data list
:param bool base_image: True|False
"""
config_args = self._process_oci_config_to_arguments(oci_config)
Command.run(
[
'umoci', 'config'
] + config_args + [
'--history.created', self.creation_date,
'--image', self.container_name,
'--created', datetime.utcnow().strftime(
'%Y-%m-%dT%H:%M:%S+00:00'
)
'--tag', self.container_tag,
'--created', self.creation_date
]
)
if base_image:
Command.run(['umoci', 'rm', '--image', self.container_name])
self.container_name = self.container_name = ':'.join(
[self.container_dir, self.container_tag]
)

@classmethod # noqa:C091
def _process_oci_config_to_arguments(self, oci_config):
Expand Down Expand Up @@ -168,8 +167,27 @@ def _process_oci_config_to_arguments(self, oci_config):
name, oci_config['labels'][name]
))

arguments.extend(self._process_oci_history_to_arguments(oci_config))
return arguments

@classmethod
def _process_oci_history_to_arguments(self, oci_config):
history_args = []
if 'history' in oci_config:
if 'comment' in oci_config['history']:
history_args.append('--history.comment={0}'.format(
oci_config['history']['comment']
))
if 'created_by' in oci_config['history']:
history_args.append('--history.created_by={0}'.format(
oci_config['history']['created_by']
))
if 'author' in oci_config['history']:
history_args.append('--history.author={0}'.format(
oci_config['history']['author']
))
return history_args

def garbage_collect(self):
"""
Cleanup unused data from operations
Expand Down
Loading

0 comments on commit f067549

Please sign in to comment.