Add initial docker-network support (#8)
* Initial docker-container support * lint * Add initial docker-network support * Lint Co-authored-by: reivilibre <38398653+reivilibre@users.noreply.github.com>
This commit is contained in:
parent
c24fad522e
commit
6c5cd04d35
@ -1,7 +1,10 @@
|
|||||||
from scone.default.utensils.docker_utensils import DockerContainerRun
|
from scone.default.utensils.docker_utensils import (
|
||||||
|
DockerContainerRun,
|
||||||
|
DockerNetworkCreate,
|
||||||
|
)
|
||||||
from scone.head.kitchen import Kitchen
|
from scone.head.kitchen import Kitchen
|
||||||
from scone.head.recipe import Recipe, RecipeContext
|
from scone.head.recipe import Recipe, RecipeContext
|
||||||
from scone.head.utils import check_type
|
from scone.head.utils import check_type, check_type_opt
|
||||||
|
|
||||||
|
|
||||||
class DockerContainer(Recipe):
|
class DockerContainer(Recipe):
|
||||||
@ -18,3 +21,33 @@ class DockerContainer(Recipe):
|
|||||||
await kitchen.ut1areq(
|
await kitchen.ut1areq(
|
||||||
DockerContainerRun(self.image, self.command), DockerContainerRun.Result
|
DockerContainerRun(self.image, self.command), DockerContainerRun.Result
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DockerNetwork(Recipe):
|
||||||
|
_NAME = "docker-network"
|
||||||
|
|
||||||
|
def __init__(self, recipe_context: RecipeContext, args: dict, head):
|
||||||
|
super().__init__(recipe_context, args, head)
|
||||||
|
|
||||||
|
self.name = check_type(args.get("name"), str)
|
||||||
|
self.driver = check_type_opt(args.get("driver"), str)
|
||||||
|
self.check_duplicate = check_type_opt(args.get("check_duplicate"), bool)
|
||||||
|
self.internal = check_type_opt(args.get("internal"), bool)
|
||||||
|
self.enable_ipv6 = check_type_opt(args.get("enable_ipv6"), bool)
|
||||||
|
self.attachable = check_type_opt(args.get("attachable"), bool)
|
||||||
|
self.scope = check_type_opt(args.get("scope"), str)
|
||||||
|
self.ingress = check_type_opt(args.get("ingress"), bool)
|
||||||
|
|
||||||
|
async def cook(self, kitchen: Kitchen) -> None:
|
||||||
|
kitchen.get_dependency_tracker()
|
||||||
|
await kitchen.ut1areq(
|
||||||
|
DockerNetworkCreate(
|
||||||
|
self.name,
|
||||||
|
self.check_duplicate,
|
||||||
|
self.internal,
|
||||||
|
self.enable_ipv6,
|
||||||
|
self.attachable,
|
||||||
|
self.ingress,
|
||||||
|
),
|
||||||
|
DockerNetworkCreate.Result,
|
||||||
|
)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import docker.errors
|
import docker.errors
|
||||||
|
|
||||||
@ -40,3 +42,35 @@ class DockerContainerRun(Utensil):
|
|||||||
return
|
return
|
||||||
|
|
||||||
await channel.send(DockerContainerRun.Result(name=container.name))
|
await channel.send(DockerContainerRun.Result(name=container.name))
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(auto_attribs=True)
|
||||||
|
class DockerNetworkCreate(Utensil):
|
||||||
|
name: str
|
||||||
|
check_duplicate: Optional[bool]
|
||||||
|
internal: Optional[bool]
|
||||||
|
enable_ipv6: Optional[bool]
|
||||||
|
attachable: Optional[bool]
|
||||||
|
ingress: Optional[bool]
|
||||||
|
|
||||||
|
@attr.s(auto_attribs=True)
|
||||||
|
class Result:
|
||||||
|
name: str
|
||||||
|
|
||||||
|
async def execute(self, channel: Channel, worktop: Worktop):
|
||||||
|
try:
|
||||||
|
network = _docker_client().networks.create(
|
||||||
|
self.name,
|
||||||
|
check_duplicate=self.check_duplicate,
|
||||||
|
internal=self.internal,
|
||||||
|
enable_ipv6=self.enable_ipv6,
|
||||||
|
attachable=self.attachable,
|
||||||
|
ingress=self.ingress,
|
||||||
|
)
|
||||||
|
|
||||||
|
except docker.errors.APIError:
|
||||||
|
# the docker server returned an error
|
||||||
|
await channel.send(None)
|
||||||
|
return
|
||||||
|
|
||||||
|
await channel.send(DockerContainerRun.Result(name=network.name))
|
||||||
|
Loading…
Reference in New Issue
Block a user