Skip to content

Commit

Permalink
state machine updates in docker actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fmirus committed Oct 11, 2024
1 parent 9d6c153 commit 910a785
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def update(self) -> py_trees.common.Status:
self.feedback_message = f"Docker container {self.container} not yet running {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.RUNNING

elif self.current_state == CopyStatus.FOUND_CONTAINER:
if self.current_state == CopyStatus.FOUND_CONTAINER:
try:
self.result_data, _ = self.container_object.get_archive(
path=self.file_path)
Expand All @@ -78,7 +78,8 @@ def update(self) -> py_trees.common.Status:
except docker.errors.APIError as e:
self.feedback_message = f"Copying of data from path {self.file_path} failed: {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE
elif self.current_state == CopyStatus.COPYING:

if self.current_state == CopyStatus.COPYING:
output_tar = tempfile.NamedTemporaryFile(suffix=".tar")
try:
with open(output_tar.name, 'wb') as f:
Expand All @@ -91,7 +92,7 @@ def update(self) -> py_trees.common.Status:
self.feedback_message = f"Copying of data from path {self.file_path} failed: {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE

elif self.current_state == CopyStatus.DONE:
if self.current_state == CopyStatus.DONE:
self.feedback_message = f"Finished copying of data from path {self.file_path} to {self.output_dir}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.SUCCESS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def update(self) -> py_trees.common.Status:
self.feedback_message = f"Docker container {self.container} not yet running {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.RUNNING

elif self.current_state == ExecutionStatus.FOUND_CONTAINER:
if self.current_state == ExecutionStatus.FOUND_CONTAINER:
try:
self.execution_instance = self.client.api.exec_create(
self.container_object.id,
Expand All @@ -83,13 +83,15 @@ def update(self) -> py_trees.common.Status:
except docker.errors.APIError as e:
self.feedback_message = f"Docker exec of command '{self.command}' failed: {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE
elif self.current_state == ExecutionStatus.EXECUTING:

if self.current_state == ExecutionStatus.EXECUTING:
try:
log = next(self.execution_output)
self.feedback_message = f"Executing '{self.command}' in container {self.container} with output: {log.decode()}" # pylint: disable= attribute-defined-outside-init
except StopIteration:
self.current_state = ExecutionStatus.DONE
elif self.current_state == ExecutionStatus.DONE:

if self.current_state == ExecutionStatus.DONE:
exit_metadata = self.client.api.exec_inspect(self.execution_instance['Id'])
assert not exit_metadata['Running']
exit_code = exit_metadata['ExitCode']
Expand All @@ -99,4 +101,5 @@ def update(self) -> py_trees.common.Status:
else:
self.feedback_message = f"Execution of '{self.command}' in container {self.container} failed" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE

return py_trees.common.Status.RUNNING
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def update(self) -> py_trees.common.Status:
self.feedback_message = f"Docker container {self.container} not yet running {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.RUNNING

elif self.current_state == CopyStatus.FOUND_CONTAINER:
if self.current_state == CopyStatus.FOUND_CONTAINER:
self.tar = tempfile.NamedTemporaryFile(suffix=".tar")
try:
with tarfile.open(self.tar.name, 'w:') as tar:
Expand All @@ -75,7 +75,8 @@ def update(self) -> py_trees.common.Status:
except tarfile.ReadError as e:
self.feedback_message = f"Compressing data to a tar file from path {self.source_path} failed: {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE
elif self.current_state == CopyStatus.COPYING:

if self.current_state == CopyStatus.COPYING:
success = self.container_object.put_archive(
path=self.target_path,
data=self.tar
Expand All @@ -87,7 +88,7 @@ def update(self) -> py_trees.common.Status:
self.feedback_message = f"Copying data from path {self.source_path} to {self.target_path} inside container {self.container} failed: {e}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.FAILURE

elif self.current_state == CopyStatus.DONE:
if self.current_state == CopyStatus.DONE:
self.feedback_message = f"Finished copying data from path {self.source_path} to {self.target_path} inside container {self.container}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.SUCCESS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def update(self) -> py_trees.common.Status:
self.current_state = ContainerStatus.RUNNING
self.feedback_message = f"Running docker container {self.image}" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.RUNNING
elif self.current_state == ContainerStatus.RUNNING:

if self.current_state == ContainerStatus.RUNNING:
if self.stream and not self.detach:
try:
log = next(self.container)
Expand All @@ -100,9 +101,11 @@ def update(self) -> py_trees.common.Status:
self.current_state = ContainerStatus.DONE
self.feedback_message = f"Docker container {self.image} finished cleanly" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.SUCCESS
elif self.current_state == ContainerStatus.DONE:

if self.current_state == ContainerStatus.DONE:
self.feedback_message = f"Docker container {self.image} finished cleanly" # pylint: disable= attribute-defined-outside-init
return py_trees.common.Status.SUCCESS

return py_trees.common.Status.RUNNING

def shutdown(self):
Expand Down

0 comments on commit 910a785

Please sign in to comment.