Swap the load_from_repo logic and improve definition members iteration
This commit is contained in:
parent
88b8520ff8
commit
a131948826
@ -66,33 +66,33 @@ def modify_container(container, depends, mounts, envs, uid, gid, cmd, cwd, ready
|
||||
setattr(container, member, value)
|
||||
|
||||
def create(container_name, image_name, depends, mounts, env, uid, gid, cmd, cwd, ready, halt, autostart):
|
||||
container = Image(image_name, True).get_container(container_name)
|
||||
container = Image(image_name).get_container(container_name)
|
||||
modify_container(container, depends, mounts, env, uid, gid, cmd, cwd, ready, halt, autostart)
|
||||
container.create()
|
||||
|
||||
def modify(container_name, depends, mounts, env, uid, gid, cmd, cwd, ready, halt, autostart):
|
||||
container = Container(container_name, True)
|
||||
container = Container(container_name)
|
||||
modify_container(container, depends, mounts, env, uid, gid, cmd, cwd, ready, halt, autostart)
|
||||
container.create()
|
||||
|
||||
def destroy(container_name):
|
||||
container = Container(container_name)
|
||||
container = Container(container_name, False)
|
||||
container.destroy()
|
||||
|
||||
def start(container_name):
|
||||
container = Container(container_name, True)
|
||||
container = Container(container_name)
|
||||
container.start()
|
||||
|
||||
def stop(container_name):
|
||||
container = Container(container_name, True)
|
||||
container = Container(container_name)
|
||||
container.stop()
|
||||
|
||||
def status(container_name):
|
||||
container = Container(container_name, True)
|
||||
container = Container(container_name)
|
||||
print(container.get_state().value)
|
||||
|
||||
def execute(container_name, command):
|
||||
container = Container(container_name, True)
|
||||
container = Container(container_name)
|
||||
container.execute(command)
|
||||
|
||||
parser = argparse.ArgumentParser(description='SPOC container manager')
|
||||
|
@ -7,7 +7,7 @@ from spoc.container import Container
|
||||
|
||||
if __name__ == '__main__':
|
||||
hook_type = os.environ['LXC_HOOK_TYPE']
|
||||
container = Container(os.environ['LXC_NAME'], True)
|
||||
container = Container(os.environ['LXC_NAME'])
|
||||
if hook_type == 'pre-start':
|
||||
container.clean_ephemeral_layer()
|
||||
container.mount_rootfs()
|
||||
|
@ -38,13 +38,13 @@ def download(image_name):
|
||||
raise NotImplementedException() # TODO
|
||||
|
||||
def delete(image_name):
|
||||
image = Image(image_name)
|
||||
image = Image(image_name, False)
|
||||
image.delete()
|
||||
|
||||
def build(filename, force, do_publish):
|
||||
# Check if a build is needed and attempt to build the image from image file
|
||||
image_name = get_image_name(filename)
|
||||
image = Image(image_name)
|
||||
image = Image(image_name, False)
|
||||
if force or image.name not in repo_local.get_images():
|
||||
image.delete()
|
||||
print(f'Building image {image_name} from file {filename}')
|
||||
@ -59,7 +59,7 @@ def build(filename, force, do_publish):
|
||||
|
||||
def publish(image_name, force):
|
||||
# Check if publishing is needed and attempt to publish the image
|
||||
image = Image(image_name, True)
|
||||
image = Image(image_name)
|
||||
if force or image.name not in repo_publish.get_images():
|
||||
image.unpublish()
|
||||
print(f'Publishing image {image_name}')
|
||||
@ -69,7 +69,7 @@ def publish(image_name, force):
|
||||
print(f'Image {image_name} already published, skipping publish task')
|
||||
|
||||
def unpublish(image_name):
|
||||
image = Image(image_name)
|
||||
image = Image(image_name, False)
|
||||
image.unpublish()
|
||||
|
||||
def extract(image_name, source, destination):
|
||||
|
@ -21,10 +21,10 @@ STATE_FREEZING = 'FREEZING'
|
||||
STATE_FROZEN = 'FROZEN'
|
||||
STATE_THAWED = 'THAWED'
|
||||
|
||||
DEFINITION_MEMBERS = ('build', 'depends', 'layers', 'mounts', 'env', 'uid', 'gid', 'cmd', 'cwd', 'ready', 'halt', 'autostart')
|
||||
DEFINITION_MEMBERS = {'build', 'depends', 'layers', 'mounts', 'env', 'uid', 'gid', 'cmd', 'cwd', 'ready', 'halt', 'autostart'}
|
||||
|
||||
class Container:
|
||||
def __init__(self, name, load_from_repo=False):
|
||||
def __init__(self, name, load_from_repo=True):
|
||||
self.name = name
|
||||
self.build = False
|
||||
self.depends = []
|
||||
@ -48,9 +48,8 @@ class Container:
|
||||
self.set_definition(repo_local.get_container(name))
|
||||
|
||||
def set_definition(self, definition):
|
||||
for key in DEFINITION_MEMBERS:
|
||||
if key in definition:
|
||||
setattr(self, key, definition[key])
|
||||
for key in DEFINITION_MEMBERS.intersection(definition):
|
||||
setattr(self, key, definition[key])
|
||||
|
||||
def get_definition(self):
|
||||
definition = {}
|
||||
|
@ -12,10 +12,10 @@ from .container import Container
|
||||
from .imagebuilder import ImageBuilder
|
||||
from .paths import LAYERS_DIR, PUB_LAYERS_DIR
|
||||
|
||||
DEFINITION_MEMBERS = ('parent', 'env', 'uid', 'gid', 'cmd', 'cwd', 'ready', 'halt', 'size', 'dlsize', 'hash')
|
||||
DEFINITION_MEMBERS = {'parent', 'env', 'uid', 'gid', 'cmd', 'cwd', 'ready', 'halt', 'size', 'dlsize', 'hash'}
|
||||
|
||||
class Image:
|
||||
def __init__(self, name, load_from_repo=False):
|
||||
def __init__(self, name, load_from_repo=True):
|
||||
self.name = name
|
||||
self.layer_path = os.path.join(LAYERS_DIR, name)
|
||||
self.archive_path = os.path.join(PUB_LAYERS_DIR, f'{name}.tar.xz')
|
||||
@ -34,9 +34,8 @@ class Image:
|
||||
self.set_definition(repo_local.get_image(name))
|
||||
|
||||
def set_definition(self, definition):
|
||||
for key in DEFINITION_MEMBERS:
|
||||
if key in definition:
|
||||
setattr(self, key, definition[key])
|
||||
for key in DEFINITION_MEMBERS.intersection(definition):
|
||||
setattr(self, key, definition[key])
|
||||
|
||||
def get_definition(self):
|
||||
definition = {}
|
||||
@ -47,10 +46,9 @@ class Image:
|
||||
return definition
|
||||
|
||||
def get_container(self, container_name):
|
||||
container = Image(self.parent, True).get_container(container_name) if self.parent else Container(container_name)
|
||||
container = Image(self.parent).get_container(container_name) if self.parent else Container(container_name, False)
|
||||
container.layers.append(self.name)
|
||||
for key,value in self.env.items():
|
||||
container.env[key] = value
|
||||
container.env.update(self.env)
|
||||
for member in ('uid', 'gid', 'cmd', 'cwd', 'ready', 'halt'):
|
||||
value = getattr(self, member)
|
||||
if value:
|
||||
|
@ -72,11 +72,11 @@ class ImageBuilder:
|
||||
script.write(f'#!/bin/sh\nset -ev\n\n{script_lines}\n')
|
||||
os.chmod(script_path, 0o700)
|
||||
os.chown(script_path, 100000, 100000)
|
||||
# Get the current image container definition and add the script file as a mount
|
||||
# Create a temporary container from the current image definition
|
||||
container = self.image.get_container(self.image.name)
|
||||
container.is_build = True
|
||||
container.build = True
|
||||
# Add the script file as a mount and execute it withing the container
|
||||
container.mounts[script_name] = (os.path.basename(script_path), False)
|
||||
# Create a temporary container and run the script in it
|
||||
container.create()
|
||||
container.execute(['/bin/sh', '-lc', os.path.join('/', script_name)], True)
|
||||
container.destroy()
|
||||
|
Loading…
Reference in New Issue
Block a user