Spotter-VM/build/usr/lib/python3.6/lxcbuild/image.py

68 lines
2.3 KiB
Python
Raw Normal View History

2019-09-20 10:13:41 +02:00
# -*- coding: utf-8 -*-
import os
import sys
from enum import Enum
from lxcmgr import lxcmgr
from .imagebuilder import ImageBuilder, ImageExistsError, ImageNotFoundError
from .imagepacker import ImagePacker
from .packer import PackageExistsError
2019-09-20 10:13:41 +02:00
class BuildType(Enum):
NORMAL = 1
FORCE = 2
SCRATCH = 3
METADATA = 4
2019-09-20 10:13:41 +02:00
class Image:
def __init__(self):
2019-09-20 10:13:41 +02:00
self.name = None
self.conf = {}
self.lxcfile = None
self.build_dir = None
self.build_type = BuildType.NORMAL
self.pack = False
def build_and_pack(self, lxcfile):
2019-09-20 10:13:41 +02:00
self.lxcfile = lxcfile
self.build_dir = os.path.dirname(lxcfile)
self.conf['build'] = True
builder = ImageBuilder(self)
2019-09-20 10:13:41 +02:00
try:
builder.build()
# Packaging needs to happen in any case after a successful build in order to prevent outdated packages
self.pack = True
2019-09-20 10:13:41 +02:00
except ImageExistsError as e:
# If container already exists and build hasn't been forced, rerun the build just for metadata which are still needed for packaging
2019-09-20 10:13:41 +02:00
print('Image {} already exists, skipping build tasks'.format(e))
self.build_type = BuildType.METADATA
builder.build()
2019-09-20 10:13:41 +02:00
except ImageNotFoundError as e:
# If one of the layers is missing, cleanup and die
2019-09-20 10:13:41 +02:00
print('Image {} not found, can\'t build {}'.format(e, self.name))
builder.clean()
sys.exit(1)
except:
# If build fails with another exception, cleanup (unless we were doing scratch build) and re-raise
if not self.build_type == BuildType.SCRATCH:
builder.clean()
2019-09-20 10:13:41 +02:00
raise
2019-09-23 15:34:37 +02:00
del self.conf['build']
# If we're doing a scratch build, regenerate the final LXC container configuration including ephemeral layer
if self.build_type == BuildType.SCRATCH:
lxcmgr.create_container(self.name, self.conf)
else:
try:
packer = ImagePacker(self)
packer.pack()
except PackageExistsError as e:
print('Package {} already exists, skipping packaging tasks'.format(e))
def remove(self):
builder = ImageBuilder(self)
builder.clean()
packer = ImagePacker(self)
packer.remove()