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
|
||||
|
||||
import attr
|
||||
|
||||
try:
|
||||
import docker.errors
|
||||
from docker.models.containers import Container
|
||||
except ImportError:
|
||||
docker = None
|
||||
Container = None
|
||||
|
||||
from scone.common.chanpro import Channel
|
||||
from scone.sous import Utensil
|
||||
@ -21,10 +24,46 @@ def _docker_client():
|
||||
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)
|
||||
class DockerContainerRun(Utensil):
|
||||
# Name of the image to use to create the container.
|
||||
image: str
|
||||
# Command to create the container with.
|
||||
command: str
|
||||
# Custom name to give the container.
|
||||
name: Optional[str] = None
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class Result:
|
||||
|
Loading…
Reference in New Issue
Block a user