Introduce is_running()/is_stopped() Container/App convenience methods

This commit is contained in:
Disassembler 2020-04-26 16:46:09 +02:00
parent 7a07d563e9
commit 9269c4384c
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
4 changed files with 28 additions and 21 deletions

View File

@ -9,7 +9,6 @@ from spoc import repo_local, repo_online, repo_publish
from spoc.app import App from spoc.app import App
from spoc.cli import ActionQueue, print_lock, readable_size from spoc.cli import ActionQueue, print_lock, readable_size
from spoc.config import LOCK_FILE from spoc.config import LOCK_FILE
from spoc.container import ContainerState
from spoc.flock import locked from spoc.flock import locked
from spoc.image import Image from spoc.image import Image
@ -25,21 +24,9 @@ def listing(list_type):
elif list_type == 'published': elif list_type == 'published':
apps = repo_publish.get_apps() apps = repo_publish.get_apps()
elif list_type == 'running': elif list_type == 'running':
apps = [] apps = [app for app in repo_local.get_apps() if App(app).is_running()]
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
elif list_type == 'stopped': elif list_type == 'stopped':
apps = list(repo_local.get_apps()) apps = [app for app in repo_local.get_apps() if App(app).is_stopped()]
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
for app in apps: for app in apps:
print(app) print(app)

View File

@ -8,7 +8,7 @@ import sys
from spoc import repo_local from spoc import repo_local
from spoc.config import VOLUMES_DIR from spoc.config import VOLUMES_DIR
from spoc.container import Container, ContainerState from spoc.container import Container
from spoc.image import Image from spoc.image import Image
def listing(state): def listing(state):
@ -16,9 +16,9 @@ def listing(state):
if state == 'all': if state == 'all':
containers = repo_local.get_containers().keys() containers = repo_local.get_containers().keys()
elif state == 'running': 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': 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: for container in containers:
print(container) print(container)

View File

@ -157,9 +157,20 @@ class App:
observer.units_done += 1 observer.units_done += 1
def status(self): 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)} 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): def set_autostart(self, autostart):
# Configure if the application should be automatically started after boot # Configure if the application should be automatically started after boot
self.autostart = autostart self.autostart = autostart

View File

@ -68,6 +68,14 @@ class Container:
state = subprocess.run(['lxc-info', '-sH', '-P', config.CONTAINERS_DIR, self.name], capture_output=True, check=True) state = subprocess.run(['lxc-info', '-sH', '-P', config.CONTAINERS_DIR, self.name], capture_output=True, check=True)
return ContainerState[state.stdout.strip().decode()] 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): def await_state(self, awaited_state):
# Block execution until the container reaches the desired state or until timeout # Block execution until the container reaches the desired state or until timeout
try: try:
@ -156,7 +164,8 @@ class Container:
depsolver = DepSolver() depsolver = DepSolver()
self.get_start_dependencies(depsolver) self.get_start_dependencies(depsolver)
for dependency in depsolver.solve(): 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) dependency.do_start(command if dependency.name == self.name else None)
def do_start(self, command=None): def do_start(self, command=None):
@ -189,7 +198,7 @@ class Container:
depsolver = DepSolver() depsolver = DepSolver()
self.get_stop_dependencies(depsolver) self.get_stop_dependencies(depsolver)
for dependency in depsolver.solve(): for dependency in depsolver.solve():
if dependency.get_state() != ContainerState.STOPPED: if not dependency.is_stopped():
dependency.do_stop() dependency.do_stop()
def do_stop(self): def do_stop(self):