68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import subprocess
|
|
|
|
from lxcmgr.paths import LXC_STORAGE_DIR
|
|
from lxcmgr.pkgmgr import PkgMgr
|
|
|
|
from . import crypto
|
|
from .packer import PackageExistsError, Packer
|
|
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.pack:
|
|
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
|
|
print('Archiving image', self.image.name)
|
|
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
|
|
print('Registering image package', self.image.name)
|
|
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
|
|
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)
|