diff --git a/usr/bin/spoc-app b/usr/bin/spoc-app index 120a7fe..17f70bd 100755 --- a/usr/bin/spoc-app +++ b/usr/bin/spoc-app @@ -9,7 +9,6 @@ from spoc import repo_local, repo_online, repo_publish from spoc.app import App from spoc.cli import ActionQueue, print_lock, readable_size from spoc.config import LOCK_FILE -from spoc.container import ContainerState from spoc.flock import locked from spoc.image import Image @@ -25,21 +24,9 @@ def listing(list_type): elif list_type == 'published': apps = repo_publish.get_apps() elif list_type == 'running': - apps = [] - for app in repo_local.get_apps(): - app = App(app) - for container in app.containers: - if container.get_state() == ContainerState.RUNNING: - apps.append(app.name) - break + apps = [app for app in repo_local.get_apps() if App(app).is_running()] elif list_type == 'stopped': - apps = list(repo_local.get_apps()) - for app in repo_local.get_apps(): - app = App(app) - for container in app.containers: - if container.get_state() == ContainerState.RUNNING: - apps.remove(app.name) - break + apps = [app for app in repo_local.get_apps() if App(app).is_stopped()] for app in apps: print(app) diff --git a/usr/bin/spoc-container b/usr/bin/spoc-container index 7317ad7..4e725d2 100755 --- a/usr/bin/spoc-container +++ b/usr/bin/spoc-container @@ -8,7 +8,7 @@ import sys from spoc import repo_local from spoc.config import VOLUMES_DIR -from spoc.container import Container, ContainerState +from spoc.container import Container from spoc.image import Image def listing(state): @@ -16,9 +16,9 @@ def listing(state): if state == 'all': containers = repo_local.get_containers().keys() elif state == 'running': - containers = [c for c in repo_local.get_containers() if Container(c).get_state() == ContainerState.RUNNING] + containers = [c for c in repo_local.get_containers() if Container(c).is_running()] elif state == 'stopped': - containers = [c for c in repo_local.get_containers() if Container(c).get_state() == ContainerState.STOPPED] + containers = [c for c in repo_local.get_containers() if Container(c).is_stopped()] for container in containers: print(container) diff --git a/usr/lib/python3.8/spoc/app.py b/usr/lib/python3.8/spoc/app.py index 95a6999..88563fe 100644 --- a/usr/lib/python3.8/spoc/app.py +++ b/usr/lib/python3.8/spoc/app.py @@ -157,9 +157,20 @@ class App: observer.units_done += 1 def status(self): - # Return status fo all application containers + # Return status for all application containers return {container.name:container.get_state() for container in sorted(self.containers)} + def is_running(self): + # Convenience method to determine if any of the application's containers are running + for container in self.containers: + if container.is_running(): + return True + return False + + def is_stopped(self): + # Convenience method to determine if all of the application's containers are stopped + return not self.is_running() + def set_autostart(self, autostart): # Configure if the application should be automatically started after boot self.autostart = autostart diff --git a/usr/lib/python3.8/spoc/container.py b/usr/lib/python3.8/spoc/container.py index d0dd482..a887ba3 100644 --- a/usr/lib/python3.8/spoc/container.py +++ b/usr/lib/python3.8/spoc/container.py @@ -68,6 +68,14 @@ class Container: state = subprocess.run(['lxc-info', '-sH', '-P', config.CONTAINERS_DIR, self.name], capture_output=True, check=True) return ContainerState[state.stdout.strip().decode()] + def is_running(self): + # Convenience method to determine if the container is running + return self.get_state() == ContainerState.RUNNING + + def is_stopped(self): + # Convenience method to determine if the container is stopped + return self.get_state() == ContainerState.STOPPED + def await_state(self, awaited_state): # Block execution until the container reaches the desired state or until timeout try: @@ -156,7 +164,8 @@ class Container: depsolver = DepSolver() self.get_start_dependencies(depsolver) for dependency in depsolver.solve(): - if dependency.get_state() != ContainerState.RUNNING: + if not dependency.is_running(): + # Pass start command only to the current container dependency.do_start(command if dependency.name == self.name else None) def do_start(self, command=None): @@ -189,7 +198,7 @@ class Container: depsolver = DepSolver() self.get_stop_dependencies(depsolver) for dependency in depsolver.solve(): - if dependency.get_state() != ContainerState.STOPPED: + if not dependency.is_stopped(): dependency.do_stop() def do_stop(self):