Rename State to ContainerState

This commit is contained in:
Disassembler 2020-03-26 21:32:16 +01:00
parent c0ce525004
commit c8b0d02e8c
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
5 changed files with 19 additions and 22 deletions

View File

@ -5,9 +5,7 @@ import argparse
import os
from pkg_resources import parse_version
from spoc import repo_local
from spoc import repo_online
from spoc import repo_publish
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

View File

@ -8,7 +8,7 @@ import sys
from spoc import repo_local
from spoc.config import VOLUMES_DIR
from spoc.container import Container, State
from spoc.container import Container, ContainerState
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() == State.RUNNING]
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == ContainerState.RUNNING]
elif state == 'stopped':
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == State.STOPPED]
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == ContainerState.STOPPED]
for container in containers:
print(container)

View File

@ -5,9 +5,7 @@ import argparse
import os
import sys
from spoc import repo_local
from spoc import repo_online
from spoc import repo_publish
from spoc import repo_local, repo_online, repo_publish
from spoc.cli import ActionQueue, print_lock, readable_size
from spoc.config import LOCK_FILE
from spoc.depsolver import DepSolver

View File

@ -17,7 +17,7 @@ from .config import CONTAINERS_DIR, LAYERS_DIR, LOG_DIR, HOSTS_FILE, VOLUMES_DIR
from .templates import LXC_CONTAINER_TEMPLATE
# States taken from https://github.com/lxc/lxc/blob/master/src/lxc/state.h
class State(enum.Enum):
class ContainerState(enum.Enum):
STOPPED = 'STOPPED'
STARTING = 'STARTING'
RUNNING = 'RUNNING'
@ -68,7 +68,7 @@ class Container:
def get_state(self):
# Get current state of the container, uses LXC monitor socket accessible only in ocntainer's namespace
state = subprocess.run(['lxc-info', '-sH', '-P', CONTAINERS_DIR, self.name], capture_output=True, check=True)
return State[state.stdout.strip().decode()]
return ContainerState[state.stdout.strip().decode()]
def await_state(self, awaited_state):
# Block execution until the container reaches the desired state or until timeout
@ -160,13 +160,13 @@ class Container:
depsolver = DepSolver()
self.get_start_dependencies(depsolver)
for dependency in depsolver.solve():
if dependency.get_state() != State.RUNNING:
if dependency.get_state() != ContainerState.RUNNING:
dependency.do_start()
def do_start(self):
# Start the current container, wait until it is reported as started and execute application readiness check
subprocess.Popen(['lxc-start', '-P', CONTAINERS_DIR, self.name])
self.await_state(State.RUNNING)
self.await_state(ContainerState.RUNNING)
# Launch the readiness check in a separate thread, so it can be reliably cancelled after timeout
with ThreadPoolExecutor(max_workers=1) as pool:
# Create anonymous object to pass the task cancellation information
@ -180,7 +180,7 @@ class Container:
ready_cmd = shlex.split(self.ready) if self.ready else ['/bin/true']
while not guard.cancel:
state = self.get_state()
if state != State.RUNNING:
if state != ContainerState.RUNNING:
raise InvalidContainerStateError(self.name, state)
check = subprocess.run(['lxc-attach', '-P', CONTAINERS_DIR, '--clear-env', self.name, '--']+ready_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30)
if check.returncode == 0:
@ -192,22 +192,22 @@ class Container:
depsolver = DepSolver()
self.get_stop_dependencies(depsolver)
for dependency in depsolver.solve():
if dependency.get_state() != State.STOPPED:
if dependency.get_state() != ContainerState.STOPPED:
dependency.do_stop()
def do_stop(self):
# Stop the current container and wait until it stops completely
subprocess.Popen(['lxc-stop', '-P', CONTAINERS_DIR, self.name])
self.await_state(State.STOPPED)
self.await_state(ContainerState.STOPPED)
def execute(self, cmd, uid=None, gid=None, **kwargs):
# If the container is starting or stopping, wait until the operation is finished
state = self.get_state()
if state == State.STARTING:
self.await_state(State.RUNNING)
if state == ContainerState.STARTING:
self.await_state(ContainerState.RUNNING)
state = self.get_state()
elif state == State.STOPPING:
self.await_state(State.STOPPED)
elif state == ContainerState.STOPPING:
self.await_state(ContainerState.STOPPED)
state = self.get_state()
# Resolve UID/GID, if they have been given
uidgid_param = []
@ -217,9 +217,9 @@ class Container:
if gid:
uidgid_param.extend(('-g', gid))
# If the container is stopped, use lxc-execute, otherwise use lxc-attach
if state == State.STOPPED:
if state == ContainerState.STOPPED:
return subprocess.run(['lxc-execute', '-P', CONTAINERS_DIR]+uidgid_param+[self.name, '--']+cmd, **kwargs)
elif state == State.RUNNING:
elif state == ContainerState.RUNNING:
return subprocess.run(['lxc-attach', '-P', CONTAINERS_DIR, '--clear-env']+uidgid_param+[self.name, '--']+cmd, **kwargs)
else:
raise InvalidContainerStateError(self.name, state)

View File

@ -41,6 +41,7 @@ def download_archive(archive_url, archive_path, expected_hash, observer):
observer.units_done = os.path.getsize(archive_path)
do_download = False
except InvalidSignature:
# If the signature is invalid, redownload the file
pass
if do_download:
# Download archive via http(s) and store in temporary directory