| 1 |
# Copyright (c) 2018 SUSE Linux GmbH. All rights reserved. |
| 2 |
# |
| 3 |
# This file is part of kiwi. |
| 4 |
# |
| 5 |
# kiwi is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# kiwi is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with kiwi. If not, see <http://www.gnu.org/licenses/> |
| 17 |
# |
| 18 |
import os |
| 19 |
|
| 20 |
# project |
| 21 |
from kiwi.utils.temporary import Temporary |
| 22 |
from kiwi.oci_tools.base import OCIBase |
| 23 |
from kiwi.command import Command |
| 24 |
from kiwi.path import Path |
| 25 |
from kiwi.utils.command_capabilities import CommandCapabilities |
| 26 |
from kiwi.defaults import Defaults |
| 27 |
|
| 28 |
|
| 29 |
class OCIUmoci(OCIBase): |
| 30 |
""" |
| 31 |
**Open Container Operations using umoci** |
| 32 |
""" |
| 33 |
def post_init(self): |
| 34 |
""" |
| 35 |
Initializes some umoci parameters and options |
| 36 |
""" |
| 37 |
self.oci_dir_tempfile = Temporary(prefix='kiwi_oci_dir.').new_dir() |
| 38 |
self.oci_dir = self.oci_dir_tempfile.name |
| 39 |
self.container_dir = os.sep.join( |
| 40 |
[self.oci_dir, 'oci_layout'] |
| 41 |
) |
| 42 |
self.working_image = '{0}:{1}'.format( |
| 43 |
self.container_dir, Defaults.get_container_base_image_tag() |
| 44 |
) |
| 45 |
if CommandCapabilities.has_option_in_help( |
| 46 |
'umoci', '--no-history', ['config', '--help'], |
| 47 |
raise_on_error=False |
| 48 |
): |
| 49 |
self.no_history_flag = ['--no-history'] |
| 50 |
else: |
| 51 |
self.no_history_flag = [] |
| 52 |
|
| 53 |
def import_container_image(self, container_image_ref): |
| 54 |
""" |
| 55 |
Imports container image reference to an OCI layout |
| 56 |
|
| 57 |
:param str container_image_ref: container image reference |
| 58 |
""" |
| 59 |
Command.run( |
| 60 |
[ |
| 61 |
'skopeo', 'copy', container_image_ref, 'oci:{0}:{1}'.format( |
| 62 |
self.container_dir, Defaults.get_container_base_image_tag() |
| 63 |
) |
| 64 |
] + ( |
| 65 |
[ |
| 66 |
'--tmpdir', Defaults.get_temp_location() |
| 67 |
] if self._skopeo_provides_tmpdir_option() else [] |
| 68 |
) |
| 69 |
) |
| 70 |
|
| 71 |
def export_container_image( |
| 72 |
self, filename, transport, image_ref, additional_names=None |
| 73 |
): |
| 74 |
""" |
| 75 |
Exports the working container to a container image archive |
| 76 |
|
| 77 |
:param str filename: The resulting filename |
| 78 |
:param str transport: The archive format |
| 79 |
:param str image_name: Name of the exported image |
| 80 |
:param str image_tag: Tag of the exported image |
| 81 |
:param list additional_names: List of additional references |
| 82 |
""" |
| 83 |
extra_tags_opt = [] |
| 84 |
if additional_names: |
| 85 |
for ref in additional_names: |
| 86 |
extra_tags_opt.extend(['--additional-tag', ref]) |
| 87 |
|
| 88 |
# make sure the target tar file does not exist |
| 89 |
# skopeo doesn't support force overwrite |
| 90 |
Path.wipe(filename) |
| 91 |
Command.run( |
| 92 |
[ |
| 93 |
'skopeo', 'copy', 'oci:{0}'.format(self.working_image), |
| 94 |
'{0}:{1}:{2}'.format(transport, filename, image_ref) |
| 95 |
] + extra_tags_opt + ( |
| 96 |
[ |
| 97 |
'--tmpdir', Defaults.get_temp_location() |
| 98 |
] if self._skopeo_provides_tmpdir_option() else [] |
| 99 |
) |
| 100 |
) |
| 101 |
|
| 102 |
def init_container(self): |
| 103 |
""" |
| 104 |
Initialize a new container layout |
| 105 |
|
| 106 |
:param bool base_image: True|False |
| 107 |
""" |
| 108 |
Command.run( |
| 109 |
['umoci', 'init', '--layout', self.container_dir] |
| 110 |
) |
| 111 |
Command.run( |
| 112 |
['umoci', 'new', '--image', self.working_image] |
| 113 |
) |
| 114 |
|
| 115 |
def unpack(self): |
| 116 |
""" |
| 117 |
Unpack current container root data |
| 118 |
""" |
| 119 |
self.oci_root_dir_tempdir = Temporary( |
| 120 |
prefix='kiwi_oci_root_dir.' |
| 121 |
).new_dir() |
| 122 |
self.oci_root_dir = self.oci_root_dir_tempdir.name |
| 123 |
Command.run([ |
| 124 |
'umoci', 'unpack', '--image', |
| 125 |
self.working_image, self.oci_root_dir |
| 126 |
]) |
| 127 |
|
| 128 |
def sync_rootfs(self, root_dir, exclude_list=None): |
| 129 |
""" |
| 130 |
Synchronizes the image root with the rootfs of the container |
| 131 |
|
| 132 |
:param string root_dir: root directory of the prepare step |
| 133 |
:param list exclude_list: list of paths to exclude |
| 134 |
""" |
| 135 |
self._sync_data( |
| 136 |
''.join([root_dir, os.sep]), |
| 137 |
os.sep.join([self.oci_root_dir, 'rootfs']), |
| 138 |
exclude_list=exclude_list, |
| 139 |
options=Defaults.get_sync_options() + [ |
| 140 |
'--delete' |
| 141 |
] |
| 142 |
) |
| 143 |
|
| 144 |
def import_rootfs(self, root_dir, exclude_list=None): |
| 145 |
""" |
| 146 |
Synchronizes the container rootfs with the root tree of the build |
| 147 |
|
| 148 |
:param string root_dir: root directory used in prepare step |
| 149 |
:param list exclude_list: list of paths to exclude |
| 150 |
""" |
| 151 |
self._sync_data( |
| 152 |
os.sep.join([self.oci_root_dir, 'rootfs', '']), |
| 153 |
root_dir, exclude_list=exclude_list, |
| 154 |
options=Defaults.get_sync_options() |
| 155 |
) |
| 156 |
|
| 157 |
def repack(self, oci_config): |
| 158 |
""" |
| 159 |
Pack root data directory into container image |
| 160 |
|
| 161 |
:param list oci_config: meta data list |
| 162 |
""" |
| 163 |
history_flags = self._process_oci_history_to_arguments(oci_config) |
| 164 |
history_flags.extend(['--history.created', self.creation_date]) |
| 165 |
Command.run( |
| 166 |
['umoci', 'repack'] + history_flags + [ |
| 167 |
'--image', self.working_image, self.oci_root_dir |
| 168 |
] |
| 169 |
) |
| 170 |
|
| 171 |
def set_config(self, oci_config): |
| 172 |
""" |
| 173 |
Set list of meta data information such as entry_point, |
| 174 |
maintainer, etc... to the container. |
| 175 |
|
| 176 |
:param list oci_config: meta data list |
| 177 |
""" |
| 178 |
config_args = self._process_oci_config_to_arguments(oci_config) |
| 179 |
Command.run( |
| 180 |
[ |
| 181 |
'umoci', 'config' |
| 182 |
] + config_args + self.no_history_flag + [ |
| 183 |
'--image', self.working_image, |
| 184 |
'--tag', oci_config['container_tag'], |
| 185 |
'--created', self.creation_date |
| 186 |
] |
| 187 |
) |
| 188 |
self.working_image = '{0}:{1}'.format( |
| 189 |
self.container_dir, oci_config['container_tag'] |
| 190 |
) |
| 191 |
|
| 192 |
@staticmethod |
| 193 |
def _process_oci_config_to_arguments(oci_config): |
| 194 |
""" |
| 195 |
Process the oci configuration dictionary into a list of arguments |
| 196 |
for the 'umoci config' command |
| 197 |
|
| 198 |
:param list oci_config: meta data list |
| 199 |
|
| 200 |
:return: List of umoci config arguments |
| 201 |
|
| 202 |
:rtype: list |
| 203 |
""" |
| 204 |
arguments = [] |
| 205 |
if 'maintainer' in oci_config: |
| 206 |
arguments.append( |
| 207 |
'--author={0}'.format(oci_config['maintainer']) |
| 208 |
) |
| 209 |
|
| 210 |
if 'user' in oci_config: |
| 211 |
arguments.append( |
| 212 |
'--config.user={0}'.format(oci_config['user']) |
| 213 |
) |
| 214 |
|
| 215 |
if 'workingdir' in oci_config: |
| 216 |
arguments.append( |
| 217 |
'--config.workingdir={0}'.format(oci_config['workingdir']) |
| 218 |
) |
| 219 |
|
| 220 |
if 'entry_command' in oci_config: |
| 221 |
if len(oci_config['entry_command']) == 0: |
| 222 |
arguments.append('--clear=config.entrypoint') |
| 223 |
else: |
| 224 |
for arg in oci_config['entry_command']: |
| 225 |
arguments.append('--config.entrypoint={0}'.format(arg)) |
| 226 |
|
| 227 |
if 'entry_subcommand' in oci_config: |
| 228 |
if len(oci_config['entry_subcommand']) == 0: |
| 229 |
arguments.append('--clear=config.cmd') |
| 230 |
else: |
| 231 |
for arg in oci_config['entry_subcommand']: |
| 232 |
arguments.append('--config.cmd={0}'.format(arg)) |
| 233 |
|
| 234 |
if 'volumes' in oci_config: |
| 235 |
for vol in oci_config['volumes']: |
| 236 |
arguments.append('--config.volume={0}'.format(vol)) |
| 237 |
|
| 238 |
if 'stopsignal' in oci_config: |
| 239 |
arguments.append( |
| 240 |
'--config.stopsignal={0}'.format(oci_config['stopsignal']) |
| 241 |
) |
| 242 |
|
| 243 |
if 'expose_ports' in oci_config: |
| 244 |
for port in oci_config['expose_ports']: |
| 245 |
arguments.append('--config.exposedports={0}'.format(port)) |
| 246 |
|
| 247 |
if 'environment' in oci_config: |
| 248 |
for name in sorted(oci_config['environment']): |
| 249 |
arguments.append('--config.env={0}={1}'.format( |
| 250 |
name, oci_config['environment'][name] |
| 251 |
)) |
| 252 |
|
| 253 |
if 'labels' in oci_config: |
| 254 |
for name in sorted(oci_config['labels']): |
| 255 |
arguments.append('--config.label={0}={1}'.format( |
| 256 |
name, oci_config['labels'][name] |
| 257 |
)) |
| 258 |
|
| 259 |
return arguments |
| 260 |
|
| 261 |
@staticmethod |
| 262 |
def _process_oci_history_to_arguments(oci_config): |
| 263 |
history_args = [] |
| 264 |
if 'history' in oci_config: |
| 265 |
if 'comment' in oci_config['history']: |
| 266 |
history_args.append('--history.comment={0}'.format( |
| 267 |
oci_config['history']['comment'] |
| 268 |
)) |
| 269 |
if 'created_by' in oci_config['history']: |
| 270 |
history_args.append('--history.created_by={0}'.format( |
| 271 |
oci_config['history']['created_by'] |
| 272 |
)) |
| 273 |
if 'author' in oci_config['history']: |
| 274 |
history_args.append('--history.author={0}'.format( |
| 275 |
oci_config['history']['author'] |
| 276 |
)) |
| 277 |
return history_args |
| 278 |
|
| 279 |
def post_process(self): |
| 280 |
""" |
| 281 |
Cleanup unused data from operations |
| 282 |
""" |
| 283 |
Command.run( |
| 284 |
['umoci', 'gc', '--layout', self.container_dir] |
| 285 |
) |