2019-09-20 10:13:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-10-05 22:26:54 +02:00
|
|
|
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:
|
2019-10-05 22:26:54 +02:00
|
|
|
def __init__(self):
|
2019-09-20 10:13:41 +02:00
|
|
|
self.name = None
|
|
|
|
self.conf = {}
|
2019-10-05 22:26:54 +02:00
|
|
|
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:
|
2019-10-05 22:26:54 +02:00
|
|
|
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
|
2019-10-05 22:26:54 +02:00
|
|
|
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:
|
2019-10-05 22:26:54 +02:00
|
|
|
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']
|
2019-10-05 22:26:54 +02:00
|
|
|
# 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()
|