Rename State to ContainerState
This commit is contained in:
parent
c0ce525004
commit
c8b0d02e8c
@ -5,9 +5,7 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
from pkg_resources import parse_version
|
from pkg_resources import parse_version
|
||||||
|
|
||||||
from spoc import repo_local
|
from spoc import repo_local, repo_online, repo_publish
|
||||||
from spoc import repo_online
|
|
||||||
from spoc import 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
|
||||||
|
@ -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, State
|
from spoc.container import Container, ContainerState
|
||||||
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() == State.RUNNING]
|
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == ContainerState.RUNNING]
|
||||||
elif state == 'stopped':
|
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:
|
for container in containers:
|
||||||
print(container)
|
print(container)
|
||||||
|
|
||||||
|
@ -5,9 +5,7 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from spoc import repo_local
|
from spoc import repo_local, repo_online, repo_publish
|
||||||
from spoc import repo_online
|
|
||||||
from spoc import repo_publish
|
|
||||||
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.depsolver import DepSolver
|
from spoc.depsolver import DepSolver
|
||||||
|
@ -17,7 +17,7 @@ from .config import CONTAINERS_DIR, LAYERS_DIR, LOG_DIR, HOSTS_FILE, VOLUMES_DIR
|
|||||||
from .templates import LXC_CONTAINER_TEMPLATE
|
from .templates import LXC_CONTAINER_TEMPLATE
|
||||||
|
|
||||||
# States taken from https://github.com/lxc/lxc/blob/master/src/lxc/state.h
|
# States taken from https://github.com/lxc/lxc/blob/master/src/lxc/state.h
|
||||||
class State(enum.Enum):
|
class ContainerState(enum.Enum):
|
||||||
STOPPED = 'STOPPED'
|
STOPPED = 'STOPPED'
|
||||||
STARTING = 'STARTING'
|
STARTING = 'STARTING'
|
||||||
RUNNING = 'RUNNING'
|
RUNNING = 'RUNNING'
|
||||||
@ -68,7 +68,7 @@ class Container:
|
|||||||
def get_state(self):
|
def get_state(self):
|
||||||
# Get current state of the container, uses LXC monitor socket accessible only in ocntainer's namespace
|
# 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)
|
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):
|
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
|
||||||
@ -160,13 +160,13 @@ 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() != State.RUNNING:
|
if dependency.get_state() != ContainerState.RUNNING:
|
||||||
dependency.do_start()
|
dependency.do_start()
|
||||||
|
|
||||||
def do_start(self):
|
def do_start(self):
|
||||||
# Start the current container, wait until it is reported as started and execute application readiness check
|
# 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])
|
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
|
# Launch the readiness check in a separate thread, so it can be reliably cancelled after timeout
|
||||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||||
# Create anonymous object to pass the task cancellation information
|
# 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']
|
ready_cmd = shlex.split(self.ready) if self.ready else ['/bin/true']
|
||||||
while not guard.cancel:
|
while not guard.cancel:
|
||||||
state = self.get_state()
|
state = self.get_state()
|
||||||
if state != State.RUNNING:
|
if state != ContainerState.RUNNING:
|
||||||
raise InvalidContainerStateError(self.name, state)
|
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)
|
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:
|
if check.returncode == 0:
|
||||||
@ -192,22 +192,22 @@ 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() != State.STOPPED:
|
if dependency.get_state() != ContainerState.STOPPED:
|
||||||
dependency.do_stop()
|
dependency.do_stop()
|
||||||
|
|
||||||
def do_stop(self):
|
def do_stop(self):
|
||||||
# Stop the current container and wait until it stops completely
|
# Stop the current container and wait until it stops completely
|
||||||
subprocess.Popen(['lxc-stop', '-P', CONTAINERS_DIR, self.name])
|
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):
|
def execute(self, cmd, uid=None, gid=None, **kwargs):
|
||||||
# If the container is starting or stopping, wait until the operation is finished
|
# If the container is starting or stopping, wait until the operation is finished
|
||||||
state = self.get_state()
|
state = self.get_state()
|
||||||
if state == State.STARTING:
|
if state == ContainerState.STARTING:
|
||||||
self.await_state(State.RUNNING)
|
self.await_state(ContainerState.RUNNING)
|
||||||
state = self.get_state()
|
state = self.get_state()
|
||||||
elif state == State.STOPPING:
|
elif state == ContainerState.STOPPING:
|
||||||
self.await_state(State.STOPPED)
|
self.await_state(ContainerState.STOPPED)
|
||||||
state = self.get_state()
|
state = self.get_state()
|
||||||
# Resolve UID/GID, if they have been given
|
# Resolve UID/GID, if they have been given
|
||||||
uidgid_param = []
|
uidgid_param = []
|
||||||
@ -217,9 +217,9 @@ class Container:
|
|||||||
if gid:
|
if gid:
|
||||||
uidgid_param.extend(('-g', gid))
|
uidgid_param.extend(('-g', gid))
|
||||||
# If the container is stopped, use lxc-execute, otherwise use lxc-attach
|
# 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)
|
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)
|
return subprocess.run(['lxc-attach', '-P', CONTAINERS_DIR, '--clear-env']+uidgid_param+[self.name, '--']+cmd, **kwargs)
|
||||||
else:
|
else:
|
||||||
raise InvalidContainerStateError(self.name, state)
|
raise InvalidContainerStateError(self.name, state)
|
||||||
|
@ -41,6 +41,7 @@ def download_archive(archive_url, archive_path, expected_hash, observer):
|
|||||||
observer.units_done = os.path.getsize(archive_path)
|
observer.units_done = os.path.getsize(archive_path)
|
||||||
do_download = False
|
do_download = False
|
||||||
except InvalidSignature:
|
except InvalidSignature:
|
||||||
|
# If the signature is invalid, redownload the file
|
||||||
pass
|
pass
|
||||||
if do_download:
|
if do_download:
|
||||||
# Download archive via http(s) and store in temporary directory
|
# Download archive via http(s) and store in temporary directory
|
||||||
|
Loading…
Reference in New Issue
Block a user