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

56 lines
1.8 KiB
Python
Raw Normal View History

2019-09-20 10:13:41 +02:00
# -*- coding: utf-8 -*-
import os
import sys
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 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.force_build = False
self.scratch_build = 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
try:
builder = ImageBuilder(self)
builder.build()
2019-09-23 15:34:37 +02:00
# In case of successful build, packaging needs to happen in all cases to prevent outdated packages
self.force_build = True
2019-09-20 10:13:41 +02:00
except ImageExistsError as e:
print('Image {} already exists, skipping build tasks'.format(e))
except ImageNotFoundError as e:
print('Image {} not found, can\'t build {}'.format(e, self.name))
builder.clean()
sys.exit(1)
except:
if not self.scratch_build:
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.scratch_build:
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()