Added basic docker action
This commit is contained in:
parent
9f7cbb0b57
commit
a8a889a00f
|
@ -1 +1,4 @@
|
||||||
PROVIDED_ACTIONS = ["jeeves.core.actions.shell:ScriptAction"]
|
PROVIDED_ACTIONS = [
|
||||||
|
"jeeves.core.actions.shell:ScriptAction",
|
||||||
|
"jeeves.core.actions.docker:DockerAction",
|
||||||
|
]
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
from typing import Text
|
||||||
|
|
||||||
|
import docker
|
||||||
|
import pydantic
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from jeeves.core.objects import Result
|
||||||
|
from .base import Action
|
||||||
|
|
||||||
|
|
||||||
|
class DockerAction(Action):
|
||||||
|
"""
|
||||||
|
.. automethod:: _run_container
|
||||||
|
"""
|
||||||
|
|
||||||
|
id = "contrib/docker"
|
||||||
|
verbose_name = "Execute docker container"
|
||||||
|
|
||||||
|
class Parameters(pydantic.BaseModel):
|
||||||
|
"""
|
||||||
|
+----------------+------+-----------+----------------------------------------------+
|
||||||
|
| Parameter name | Type | Mandatory | Description |
|
||||||
|
+================+======+===========+==============================================+
|
||||||
|
| ``image`` | text | no | Image to run (defaults to ``DEFAULT_IMAGE``) |
|
||||||
|
| ``command`` | text | yes | The command to be executed |
|
||||||
|
+----------------+------+-----------+----------------------------------------------+
|
||||||
|
"""
|
||||||
|
|
||||||
|
image: Text = "alpine:latest"
|
||||||
|
command: Text
|
||||||
|
remove_container: bool = True
|
||||||
|
|
||||||
|
def _run_container(self):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def execute(self, **kwargs):
|
||||||
|
workspace = kwargs.get("workspace")
|
||||||
|
image = self.parameters.image
|
||||||
|
command = self.parameters.command
|
||||||
|
environment = {"WORKSPACE_PATH": "/workspace"}
|
||||||
|
|
||||||
|
client = docker.from_env()
|
||||||
|
|
||||||
|
self.logger.info("Pulling image...")
|
||||||
|
try:
|
||||||
|
client.images.get(image)
|
||||||
|
except docker.errors.ImageNotFound:
|
||||||
|
self.logger.error("Image does not exist")
|
||||||
|
return Result(success=False)
|
||||||
|
|
||||||
|
self.logger.info("Execute command in container...")
|
||||||
|
container = client.containers.run(
|
||||||
|
image=image,
|
||||||
|
command=command,
|
||||||
|
detach=True,
|
||||||
|
environment=environment,
|
||||||
|
volumes={"/workspace": {"bind": str(workspace.path), "mode": "rw"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = container.wait(timeout=30, condition="not-running")
|
||||||
|
logs = container.logs()
|
||||||
|
success = result["StatusCode"] == 0
|
||||||
|
except requests.exceptions.ReadTimeout:
|
||||||
|
success = False
|
||||||
|
logs = container.logs()
|
||||||
|
|
||||||
|
if self.parameters.remove_container:
|
||||||
|
self.logger.info("Removing container")
|
||||||
|
container.remove()
|
||||||
|
|
||||||
|
return Result(success=success, output=logs)
|
|
@ -13,6 +13,7 @@ jinja2 = "^2.10"
|
||||||
pydantic = "^0.32.2"
|
pydantic = "^0.32.2"
|
||||||
click = "^7.0"
|
click = "^7.0"
|
||||||
toml = "^0.10.0"
|
toml = "^0.10.0"
|
||||||
|
docker = "^4.1.0"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
black = {version = "^18.3-alpha.0", allow-prereleases = true}
|
black = {version = "^18.3-alpha.0", allow-prereleases = true}
|
||||||
|
|
|
@ -11,6 +11,6 @@ include_trailing_comma = True
|
||||||
length_sort = 1
|
length_sort = 1
|
||||||
lines_between_types = 0
|
lines_between_types = 0
|
||||||
line_length = 88
|
line_length = 88
|
||||||
known_third_party = click,django,factory,pydantic,pytest,toml
|
known_third_party = click,django,docker,factory,pydantic,pytest,requests,toml
|
||||||
sections = FUTURE, STDLIB, DJANGO, THIRDPARTY, FIRSTPARTY, LOCALFOLDER
|
sections = FUTURE, STDLIB, DJANGO, THIRDPARTY, FIRSTPARTY, LOCALFOLDER
|
||||||
no_lines_before = LOCALFOLDER
|
no_lines_before = LOCALFOLDER
|
||||||
|
|
Reference in New Issue