Add ability to give containers names & check containers by name
This commit is contained in:
parent
4806196012
commit
b36226573b
@ -1,11 +1,14 @@
|
|||||||
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import docker.errors
|
import docker.errors
|
||||||
|
from docker.models.containers import Container
|
||||||
except ImportError:
|
except ImportError:
|
||||||
docker = None
|
docker = None
|
||||||
|
Container = None
|
||||||
|
|
||||||
from scone.common.chanpro import Channel
|
from scone.common.chanpro import Channel
|
||||||
from scone.sous import Utensil
|
from scone.sous import Utensil
|
||||||
@ -21,10 +24,46 @@ def _docker_client():
|
|||||||
return _docker_client_instance
|
return _docker_client_instance
|
||||||
|
|
||||||
|
|
||||||
|
class ContainerState(Enum):
|
||||||
|
NOTFOUND = 0
|
||||||
|
RUNNING = 1
|
||||||
|
EXITED = 2
|
||||||
|
RESTARTING = 3
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(auto_attribs=True)
|
||||||
|
class DockerContainerState(Utensil):
|
||||||
|
# Name of the container to check the existence of.
|
||||||
|
name: str
|
||||||
|
|
||||||
|
async def execute(self, channel: Channel, worktop: Worktop):
|
||||||
|
client = _docker_client()
|
||||||
|
# this is essentially `docker ps -a`
|
||||||
|
# TODO(perf) run this in a threaded executor since docker can be slow.
|
||||||
|
for container in client.containers.list(all=True):
|
||||||
|
container: Container
|
||||||
|
if self.name == container.name:
|
||||||
|
if container.status == 'running':
|
||||||
|
await channel.send(ContainerState.RUNNING)
|
||||||
|
elif container.status == 'exited':
|
||||||
|
await channel.send(ContainerState.EXITED)
|
||||||
|
elif container.status == 'restarting':
|
||||||
|
await channel.send(ContainerState.RESTARTING)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown container status: {container.status}")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
await channel.send(ContainerState.NOTFOUND)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(auto_attribs=True)
|
@attr.s(auto_attribs=True)
|
||||||
class DockerContainerRun(Utensil):
|
class DockerContainerRun(Utensil):
|
||||||
|
# Name of the image to use to create the container.
|
||||||
image: str
|
image: str
|
||||||
|
# Command to create the container with.
|
||||||
command: str
|
command: str
|
||||||
|
# Custom name to give the container.
|
||||||
|
name: Optional[str] = None
|
||||||
|
|
||||||
@attr.s(auto_attribs=True)
|
@attr.s(auto_attribs=True)
|
||||||
class Result:
|
class Result:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user