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 6, 2018
1 parent 2fa147d commit 9bf38b7
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 80 deletions.
7 changes: 6 additions & 1 deletion 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
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
5 changes: 4 additions & 1 deletion 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 Down
36 changes: 31 additions & 5 deletions kiwi/oci_tools/umoci.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# 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
from kiwi.command import Command
from kiwi.defaults import Defaults


class OCIUmoci(OCIBase):
Expand Down Expand Up @@ -70,8 +70,16 @@ def repack(self, oci_root_dir):
:param string oci_root_dir: root data directory
"""
history_args = [
'--history.created', self.creation_date,
'--history.created_by',
Defaults.get_default_container_created_by(),
'--history.comment', 'umoci repack'
]
Command.run(
['umoci', 'repack', '--image', self.container_name, oci_root_dir]
['umoci', 'repack', ] + history_args + [
'--image', self.container_name, oci_root_dir
]
)

def add_tag(self, tag_name):
Expand Down Expand Up @@ -99,10 +107,9 @@ def set_config(self, oci_config):
[
'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'
)
'--created', self.creation_date
]
)

Expand Down Expand Up @@ -168,8 +175,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 9bf38b7

Please sign in to comment.