diff --git a/usr/bin/spoc-app b/usr/bin/spoc-app index adb5624..27051dc 100755 --- a/usr/bin/spoc-app +++ b/usr/bin/spoc-app @@ -73,6 +73,10 @@ def publish(filename, force): def unpublish(app_name): App(app_name, False).unpublish() +def autostart(value): + value = value.lower() in ('1', 'on', 'enable', 'true') + App(app_name, False).set_autostart(value) + parser = argparse.ArgumentParser(description='SPOC application manager') parser.set_defaults(action=None) subparsers = parser.add_subparsers() @@ -114,6 +118,10 @@ parser_unpublish = subparsers.add_parser('unpublish') parser_unpublish.set_defaults(action=unpublish) parser_unpublish.add_argument('app') +parser_autostart = subparsers.add_parser('autostart') +parser_autostart.set_defaults(action=autostart) +parser_autostart.add_argument('value', choices=('1', 'on', 'enable', 'true', '0', 'off', 'disable', 'false'), help='Sets the applications to be automatically started after the host boots up') + args = parser.parse_args() if args.action is listing: @@ -134,5 +142,7 @@ elif args.action is publish: publish(args.filename, args.force) elif args.action is unpublish: unpublish(args.app) +elif args.action is autostart: + autostart(args.value) else: parser.print_usage() diff --git a/usr/bin/spoc-container b/usr/bin/spoc-container index b2153e1..affc1f2 100755 --- a/usr/bin/spoc-container +++ b/usr/bin/spoc-container @@ -52,31 +52,30 @@ def modify_env(container, env): except KeyError: pass -def modify_container(container, depends, mounts, envs, uid, gid, cmd, cwd, ready, halt, autostart): +def modify_container(container, depends, mounts, envs, uid, gid, cmd, cwd, ready, halt): for depend in depends: modify_depend(container, depend) for mount in mounts: modify_mount(container, mount) for env in envs: modify_env(container, env) - autostart = autostart == 'on' args = locals() - for member in ('uid', 'gid', 'cmd', 'cwd', 'ready', 'halt', 'autostart'): + for member in ('uid', 'gid', 'cmd', 'cwd', 'ready', 'halt'): value = args[member] if 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): # Create container based on image definition and extrea fields container = Container(container_name, False) container.set_definition(Image(image_name).get_definition()) - 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) 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): # Change configuration of an existing container 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) container.create() def destroy(container_name): @@ -120,7 +119,6 @@ parser_create.add_argument('-c', '--cmd', help='Sets the container init command' parser_create.add_argument('-w', '--workdir', help='Sets the container init working directory') parser_create.add_argument('-r', '--ready', help='Sets the container ready command') parser_create.add_argument('-s', '--stopsig', help='Sets the signal to be sent to init on container shutdown') -parser_create.add_argument('-a', '--autostart', choices=('on', 'off'), help='Sets the container to be automatically started after the host boots up') parser_create.add_argument('container') parser_create.add_argument('image') @@ -135,7 +133,6 @@ parser_modify.add_argument('-c', '--cmd', help='Sets the container init command' parser_modify.add_argument('-w', '--workdir', help='Sets the container init working directory') parser_modify.add_argument('-r', '--ready', help='Sets the container ready command') parser_modify.add_argument('-s', '--stopsig', help='Sets the signal to be sent to init on container shutdown') -parser_modify.add_argument('-a', '--autostart', choices=('on', 'off'), help='Sets the container to be automatically started after the host boots up') parser_modify.add_argument('container') parser_destroy = subparsers.add_parser('destroy') @@ -166,9 +163,9 @@ args = parser.parse_args() if args.action is listing: listing(args.type) elif args.action is create: - create(args.container, args.image, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart) + create(args.container, args.image, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig) elif args.action is modify: - modify(args.container, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart) + modify(args.container, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig) elif args.action is destroy: destroy(args.container) elif args.action is start: diff --git a/usr/lib/python3.8/spoc/app.py b/usr/lib/python3.8/spoc/app.py index 476162d..3c8cbfa 100644 --- a/usr/lib/python3.8/spoc/app.py +++ b/usr/lib/python3.8/spoc/app.py @@ -20,6 +20,7 @@ class App: self.version = None self.app_dir = os.path.join(APPS_DIR, name) self.meta = {} + self.autostart = False self.containers = [] if load_from_repo: self.set_definition(repo_local.get_app(name)) @@ -27,12 +28,14 @@ class App: def set_definition(self, definition): self.version = definition['version'] self.meta = definition['meta'] + self.autostart = definition['autostart'] self.containers = [Container(container) for container in definition['containers']] def get_definition(self): return { 'version': self.version, 'meta': self.meta.copy(), + 'autostart': self.autostart, 'containers': [container.name for container in self.containers] } @@ -158,3 +161,7 @@ class App: os.unlink(archive_path) except FileNotFoundError: pass + + def set_autostart(self, autostart): + self.autostart = autostart + repo_local.register_app(self.name, self.get_definition()) diff --git a/usr/lib/python3.8/spoc/container.py b/usr/lib/python3.8/spoc/container.py index d63e1e8..62e1a87 100644 --- a/usr/lib/python3.8/spoc/container.py +++ b/usr/lib/python3.8/spoc/container.py @@ -27,7 +27,7 @@ class State(enum.Enum): FROZEN = 'FROZEN' 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'} class Container: def __init__(self, name, load_from_repo=True): @@ -43,7 +43,6 @@ class Container: self.cwd = None self.ready = None self.halt = None - self.autostart = False self.container_path = os.path.join(CONTAINERS_DIR, name) self.config_path = os.path.join(self.container_path, 'config') self.rootfs_path = os.path.join(self.container_path, 'rootfs')