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