This commit is contained in:
Emmanuel Garette 2021-05-24 22:24:15 +02:00
parent 4853bb47f0
commit 2ea04e708d
1 changed files with 18 additions and 7 deletions

View File

@ -74,6 +74,8 @@ class Image:
if image_dir is None:
image_dir = IMAGES_DIRECTORY
self.image_dir = image_dir
if isdir(self.image_dir):
rmtree(self.image_dir)
if tmp_dir is None:
tmp_dir = PACKER_TMP_DIRECTORY
self.tmp_dir = tmp_dir
@ -227,7 +229,6 @@ class Image:
config = self.load_configuration(dependencies_path, packer_tmp_directory)
self.merge_funcs(config, dependencies_path, packer_tmp_directory)
packer_configuration = await self.get_packer_information(config, packer_tmp_directory)
# OS image needed ?
packer_dst_os_filename = join(self.image_dir,
'os',
packer_configuration['os_name'] + '_' + packer_configuration['os_version'] + '.img',
@ -247,33 +248,44 @@ class Image:
self.copy_files(packer_directory,
packer_tmp_img_directory,
)
if not isdir(join(self.image_dir, 'os')):
makedirs(join(self.image_dir, 'os'))
packer_configuration['tmp_directory'] = packer_tmp_os_directory
recipe = {'variables': packer_configuration}
if not isfile(packer_dst_os_filename):
self.build_image(packer_dst_os_filename,
packer_tmp_os_directory,
packer_configuration,
recipe,
)
recipe_checksum = self.do_recipe_checksum(packer_tmp_img_directory)
packer_dst_filename = join(self.image_dir,
f'{recipe_checksum}.img',
)
sha_file = f'{packer_dst_os_filename}.sha256'
with open(sha_file, 'r') as fh:
sha256 = fh.read().split(' ', 1)[0]
packer_configuration['tmp_directory'] = packer_tmp_img_directory
recipe = {'variables': packer_configuration}
recipe['variables']['iso_checksum'] = sha256
recipe['variables']['iso_url'] = packer_dst_os_filename
self.build_image(packer_dst_filename,
packer_tmp_img_directory,
packer_configuration,
recipe,
)
rmtree(packer_tmp_directory)
def build_image(self,
packer_dst_filename: str,
tmp_directory: str,
packer_configuration: dict,
recipe: dict,
) -> None:
packer_configuration['tmp_directory'] = tmp_directory
recipe = {'variables': packer_configuration}
packer_filename = join(tmp_directory, PACKER_FILE_NAME)
with open(packer_filename, 'r') as recipe_fd:
for key, value in jload(recipe_fd).items():
recipe[key] = value
with open(packer_filename, 'w') as recipe_fd:
jdump(recipe, recipe_fd, indent=2)
print(['packer', 'build', packer_filename])
proc = Popen(['packer', 'build', packer_filename],
#stdout=PIPE,
#stderr=PIPE,
@ -284,5 +296,4 @@ class Image:
raise Exception(_(f'cannot build {packer_dst_filename} with {packer_filename}'))
move(join(tmp_directory, 'image.img'), packer_dst_filename)
move(join(tmp_directory, 'image.sha256'), f'{packer_dst_filename}.sha256')
rmtree(tmp_directory)
print(_(f'Image {packer_dst_filename} created'))