Add initial docker-container support (#7)

* Initial docker-container support

* lint
This commit is contained in:
Merikei 2020-10-31 21:34:05 +00:00 committed by GitHub
parent 11a62d0023
commit c24fad522e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,20 @@
from scone.default.utensils.docker_utensils import DockerContainerRun
from scone.head.kitchen import Kitchen
from scone.head.recipe import Recipe, RecipeContext
from scone.head.utils import check_type
class DockerContainer(Recipe):
_NAME = "docker-container"
def __init__(self, recipe_context: RecipeContext, args: dict, head):
super().__init__(recipe_context, args, head)
self.image = check_type(args.get("image"), str)
self.command = check_type(args.get("command"), str)
async def cook(self, kitchen: Kitchen) -> None:
kitchen.get_dependency_tracker()
await kitchen.ut1areq(
DockerContainerRun(self.image, self.command), DockerContainerRun.Result
)

View File

@ -0,0 +1,42 @@
import attr
import docker.errors
from scone.common.chanpro import Channel
from scone.sous import Utensil
from scone.sous.utensils import Worktop
_docker_client_instance = None
def _docker_client():
global _docker_client_instance
if not _docker_client_instance:
_docker_client_instance = docker.from_env()
return _docker_client_instance
@attr.s(auto_attribs=True)
class DockerContainerRun(Utensil):
image: str
command: str
@attr.s(auto_attribs=True)
class Result:
name: str
async def execute(self, channel: Channel, worktop: Worktop):
try:
container = _docker_client().containers.run(
self.image, self.command, detach=True
)
except docker.errors.ImageNotFound:
# specified image does not exist (or requires login)
await channel.send(None)
return
except docker.errors.APIError:
# the docker server returned an error
await channel.send(None)
return
await channel.send(DockerContainerRun.Result(name=container.name))