216 lines
7.6 KiB
Python
216 lines
7.6 KiB
Python
# Copyright (c) 2015 SUSE Linux GmbH. All rights reserved.
|
|
#
|
|
# This file is part of kiwi.
|
|
#
|
|
# kiwi is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# kiwi is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
|
|
#
|
|
"""
|
|
usage: kiwi result bundle -h | --help
|
|
kiwi result bundle --target-dir=<directory> --id=<bundle_id> --bundle-dir=<directory>
|
|
kiwi result bundle help
|
|
|
|
commands:
|
|
bundle
|
|
create result bundle from the image build results in the
|
|
specified target directory. Each result image will contain
|
|
the specified bundle identifier as part of its filename.
|
|
Uncompressed image files will also become xz compressed
|
|
and a sha sum will be created from every result image.
|
|
|
|
options:
|
|
--target-dir=<directory>
|
|
the target directory to expect image build results
|
|
|
|
--id=<bundle_id>
|
|
the bundle id. A free form text appended to the version
|
|
information of the result image filename
|
|
|
|
--bundle-dir=<directory>
|
|
directory to store the bundle results
|
|
"""
|
|
from collections import namedtuple
|
|
import hashlib
|
|
import os
|
|
|
|
# project
|
|
from cli_task import CliTask
|
|
from help import Help
|
|
from result import Result
|
|
from logger import log
|
|
from path import Path
|
|
from defaults import Defaults
|
|
from compress import Compress
|
|
from command import Command
|
|
|
|
from exceptions import (
|
|
KiwiRequestedTypeError,
|
|
KiwiBundleError
|
|
)
|
|
|
|
|
|
class ResultBundleTask(CliTask):
|
|
"""
|
|
Implements result bundler
|
|
"""
|
|
def process(self):
|
|
self.manual = Help()
|
|
if self.__help():
|
|
return
|
|
|
|
# load serialized result object from target directory
|
|
result_directory = os.path.normpath(self.command_args['--target-dir'])
|
|
bundle_directory = os.path.normpath(self.command_args['--bundle-dir'])
|
|
if result_directory == bundle_directory:
|
|
raise KiwiBundleError(
|
|
'Bundle directory must be different from target directory'
|
|
)
|
|
|
|
log.info(
|
|
'Bundle build results from %s', result_directory
|
|
)
|
|
result = Result.load(
|
|
result_directory + '/kiwi.result'
|
|
)
|
|
|
|
# read results
|
|
results = result.get_results()
|
|
image_type = result.xml_state.get_build_type_name()
|
|
image_version = result.xml_state.get_image_version()
|
|
result_files_for_bundle = []
|
|
|
|
# build result files and metadata for bundle
|
|
result_file_type = namedtuple(
|
|
'result_file_type', ['filename', 'compress']
|
|
)
|
|
if image_type in Defaults.get_filesystem_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['filesystem_image'], compress=True
|
|
)
|
|
)
|
|
elif image_type in Defaults.get_disk_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['disk_image'], compress=True
|
|
)
|
|
)
|
|
if 'installation_image' in results:
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['installation_image'], compress=False
|
|
)
|
|
)
|
|
if 'installation_pxe_archive' in results:
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['installation_pxe_archive'],
|
|
compress=False
|
|
)
|
|
)
|
|
if 'disk_format_image' in results:
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['disk_format_image'], compress=True
|
|
)
|
|
)
|
|
elif image_type in Defaults.get_live_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['live_image'], compress=False
|
|
)
|
|
)
|
|
elif image_type in Defaults.get_network_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['filesystem_image'], compress=False
|
|
)
|
|
)
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['filesystem_md5'], compress=False
|
|
)
|
|
)
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['kernel'], compress=False
|
|
)
|
|
)
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['initrd'], compress=False
|
|
)
|
|
)
|
|
if 'xen_hypervisor' in results:
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['xen_hypervisor'], compress=False
|
|
)
|
|
)
|
|
elif image_type in Defaults.get_archive_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['root_archive'], compress=False
|
|
)
|
|
)
|
|
elif image_type in Defaults.get_container_image_types():
|
|
result_files_for_bundle.append(
|
|
result_file_type(
|
|
filename=results['container'], compress=True
|
|
)
|
|
)
|
|
else:
|
|
raise KiwiRequestedTypeError(
|
|
'Image type %s unknown for bundle' % image_type
|
|
)
|
|
|
|
# hard link bundle files, compress and build checksum
|
|
if not os.path.exists(bundle_directory):
|
|
Path.create(bundle_directory)
|
|
for result_file in result_files_for_bundle:
|
|
bundle_file_basename = os.path.basename(result_file.filename)
|
|
# The bundle id is only taken into account for image results
|
|
# which contains the image version in its nane
|
|
bundle_file_basename = bundle_file_basename.replace(
|
|
image_version, image_version + '-' + self.command_args['--id']
|
|
)
|
|
log.info('Creating %s', bundle_file_basename)
|
|
bundle_file = ''.join(
|
|
[bundle_directory, '/', bundle_file_basename]
|
|
)
|
|
checksum_file = ''.join(
|
|
[bundle_directory, '/', bundle_file_basename, '.sha256']
|
|
)
|
|
Command.run(
|
|
[
|
|
'cp', '-l', result_file.filename, bundle_file
|
|
]
|
|
)
|
|
if result_file.compress:
|
|
log.info('--> XZ compressing')
|
|
compress = Compress(bundle_file)
|
|
compress.xz()
|
|
bundle_file = compress.compressed_filename
|
|
log.info('--> Creating SHA 256 sum')
|
|
with open(bundle_file, 'rb') as data:
|
|
checksum = hashlib.sha256(data.read()).hexdigest()
|
|
with open(checksum_file, 'w') as sha256:
|
|
sha256.write(checksum)
|
|
|
|
def __help(self):
|
|
if self.command_args['help']:
|
|
self.manual.show('kiwi::result::bundle')
|
|
else:
|
|
return False
|
|
return self.manual
|