Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdk/python/src/dagger/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from gql.client import AsyncClientSession, SyncClientSession
from gql.dsl import DSLField, DSLQuery, DSLSchema, DSLSelectable, DSLType, dsl_gql

from dagger.exceptions import DaggerException

_T = TypeVar("_T")


Expand Down Expand Up @@ -146,7 +148,7 @@ def _schema(self) -> graphql.GraphQLSchema:
return self._ctx.schema._schema


class ClientError(Exception):
class ClientError(DaggerException):
"""Base class for client errors."""


Expand Down
49 changes: 40 additions & 9 deletions sdk/python/src/dagger/connectors/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from attrs import Factory, define, field

from dagger import Client
from dagger.exceptions import DaggerException

from .base import Config, register_connector
from .http import HTTPConnector
Expand Down Expand Up @@ -74,6 +75,12 @@ def start(self) -> None:
"delete": False,
}
with tempfile.NamedTemporaryFile(**tempfile_args) as tmp_bin:

def cleanup():
"""Remove the tmp_bin on error."""
tmp_bin.close()
os.unlink(tmp_bin.name)

docker_run_args = [
"docker",
"run",
Expand All @@ -91,12 +98,21 @@ def start(self) -> None:
encoding="utf-8",
check=True,
)
except FileNotFoundError as e:
raise ProvisionError(
f"Command '{docker_run_args[0]}' not found."
) from e
except subprocess.CalledProcessError as e:
tmp_bin.close()
os.unlink(tmp_bin.name)
Comment thread
KGB33 marked this conversation as resolved.
Resolved
raise ProvisionError(
f"Failed to copy engine session binary: {e.stdout}"
)
f"Failed to copy engine session binary: {e.stderr}"
) from e
else:
# Flake8 Ignores
# F811 -- redefinition of (yet) unused function
# E731 -- assigning a lambda.
cleanup = lambda: None # noqa: F811,E731
finally:
cleanup()

tmp_bin_path = Path(tmp_bin.name)
tmp_bin_path.chmod(0o700)
Expand Down Expand Up @@ -124,15 +140,30 @@ def start(self) -> None:
engine_session_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=self.cfg.log_output or subprocess.DEVNULL,
stderr=self.cfg.log_output or subprocess.PIPE,
encoding="utf-8",
)

# read port number from first line of stdout
port = int(self._proc.stdout.readline())
try:
# read port number from first line of stdout
port = int(self._proc.stdout.readline())
except ValueError as e:
# Check if the subprocess exited with an error.
if not self._proc.poll():
raise e

# FIXME: Duplicate writes into a buffer until end of provisioning
# instead of reading directly from what the user may set in `log_output`
if self._proc.stderr is not None and self._proc.stderr.readable():
raise ProvisionError(
f"Dagger engine failed to start: {self._proc.stderr.readline()}"
) from e

raise ProvisionError(
"Dagger engine failed to start, is docker running?"
) from e

# TODO: verify port number is valid

self.cfg.host = f"http://localhost:{port}"

def is_running(self) -> bool:
Expand Down Expand Up @@ -189,5 +220,5 @@ def close_sync(self, exc_type) -> None:
self.engine.stop(exc_type)


class ProvisionError(Exception):
class ProvisionError(DaggerException):
"""Error while provisioning the Dagger engine."""
2 changes: 2 additions & 0 deletions sdk/python/src/dagger/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DaggerException(Exception):
"""Base exception for all Dagger exceptions."""