2019-10-05 22:26:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from lxcmgr.paths import LXC_STORAGE_DIR
|
|
|
|
from lxcmgr.pkgmgr import PkgMgr
|
|
|
|
|
|
|
|
from . import crypto
|
2019-10-05 23:39:00 +02:00
|
|
|
from .packer import PackageExistsError, Packer
|
2019-10-05 22:26:54 +02:00
|
|
|
from .paths import REPO_IMAGES_DIR
|
|
|
|
|
|
|
|
class ImagePacker(Packer):
|
|
|
|
def __init__(self, image):
|
|
|
|
super().__init__()
|
|
|
|
self.image = image
|
|
|
|
# Prepare package file names
|
|
|
|
self.tar_path = os.path.join(REPO_IMAGES_DIR, '{}.tar'.format(self.image.name))
|
|
|
|
self.xz_path = '{}.xz'.format(self.tar_path)
|
|
|
|
|
|
|
|
def pack(self):
|
|
|
|
if self.image.force_build:
|
|
|
|
self.unregister()
|
|
|
|
try:
|
|
|
|
os.unlink(self.xz_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
elif os.path.exists(self.xz_path):
|
|
|
|
raise PackageExistsError(self.xz_path)
|
|
|
|
self.create_archive()
|
|
|
|
self.register()
|
|
|
|
self.sign_packages()
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
self.unregister()
|
|
|
|
try:
|
|
|
|
os.unlink(self.xz_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def create_archive(self):
|
|
|
|
# Create archive
|
2019-10-05 23:09:30 +02:00
|
|
|
print('Archiving image', self.image.name)
|
2019-10-05 22:26:54 +02:00
|
|
|
subprocess.run(['tar', '--xattrs', '-cpf', self.tar_path, self.image.name], cwd=LXC_STORAGE_DIR)
|
|
|
|
self.compress_archive()
|
|
|
|
|
|
|
|
def register(self):
|
|
|
|
# Register image in global repository metadata file
|
2019-10-05 23:09:30 +02:00
|
|
|
print('Registering image package', self.image.name)
|
2019-11-30 09:59:11 +01:00
|
|
|
image_conf = self.image.conf.copy()
|
|
|
|
image_conf['size'] = self.tar_size
|
|
|
|
image_conf['pkgsize'] = self.xz_size
|
|
|
|
image_conf['sha512'] = crypto.hash_file(self.xz_path)
|
|
|
|
self.packages['images'][self.image.name] = image_conf
|
2019-10-05 22:26:54 +02:00
|
|
|
self.save_repo_meta()
|
|
|
|
# Register the image also to locally installed images for package manager
|
|
|
|
pm = PkgMgr()
|
|
|
|
pm.register_image(self.image.name, self.packages['images'][self.image.name])
|
|
|
|
|
|
|
|
def unregister(self):
|
|
|
|
# Removes package from global repository metadata file
|
|
|
|
if self.image.name in self.packages['images']:
|
|
|
|
del self.packages['images'][self.image.name]
|
|
|
|
self.save_repo_meta()
|
|
|
|
# Unregister the image also from locally installed images for package manager
|
|
|
|
pm = PkgMgr()
|
|
|
|
pm.unregister_image(self.image.name)
|