martinzink commented on code in PR #2044:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2044#discussion_r2466586508


##########
behave_framework/src/minifi_test_framework/containers/container.py:
##########
@@ -0,0 +1,266 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+#
+
+import logging
+import os
+import shlex
+import tempfile
+from docker.models.networks import Network
+from minifi_test_framework.containers.directory import Directory
+from minifi_test_framework.containers.file import File
+from minifi_test_framework.containers.host_file import HostFile
+from typing import Dict, Any, List
+
+import docker
+
+
+class Container:
+    def __init__(self, image_name: str, container_name: str, network: Network):
+        self.image_name: str = image_name
+        self.container_name: str = container_name
+        self.network: Network = network
+        self.user: str = "0:0"
+        self.client = docker.from_env()
+        self.container = None
+        self.files: List[File] = []
+        self.dirs: List[Directory] = []
+        self.host_files: List[HostFile] = []
+        self.volumes = {}
+        self.command = None
+        self._temp_dir = None
+        self.ports = None
+        self.environment: List[str] = []
+
+    def deploy(self) -> bool:
+        self._temp_dir = tempfile.TemporaryDirectory()
+
+        if len(self.files) != 0 or len(self.dirs) != 0 or len(self.host_files) 
!= 0:
+            for file in self.files:
+                temp_path = self._temp_dir.name + "/" + file.host_filename
+                with open(temp_path, "w") as temp_file:
+                    temp_file.write(file.content)
+                self.volumes[temp_path] = {
+                    "bind": file.path + "/" + file.host_filename,
+                    "mode": file.mode
+                }
+            for directory in self.dirs:
+                temp_path = self._temp_dir.name + directory.path
+                for file_name, content in directory.files.items():
+                    file_path = temp_path + "/" + file_name
+                    os.makedirs(temp_path, exist_ok=True)
+                    with open(file_path, "w") as temp_file:
+                        temp_file.write(content)
+                self.volumes[temp_path] = {
+                    "bind": directory.path,
+                    "mode": directory.mode
+                }
+            for host_file in self.host_files:
+                self.volumes[host_file.container_path] = {
+                    "bind": host_file.host_path,
+                    "mode": host_file.mode
+                }
+
+        try:
+            existing_container = 
self.client.containers.get(self.container_name)
+            logging.warning(f"Found existing container 
'{self.container_name}'. Removing it first.")
+            existing_container.remove(force=True)
+        except docker.errors.NotFound:
+            pass
+        try:
+            print(f"Creating and starting container 
'{self.container_name}'...")
+            self.container = self.client.containers.run(
+                image=self.image_name,
+                name=self.container_name,
+                ports=self.ports,
+                environment=self.environment,
+                volumes=self.volumes,
+                network=self.network.name,
+                command=self.command,
+                user=self.user,
+                detach=True  # Always run in the background
+            )
+        except Exception as e:
+            logging.error(f"Error starting container: {e}")
+            raise
+        return True
+
+    def clean_up(self):
+        if self.container is not None:
+            self.container.remove(force=True)
+
+    def exec_run(self, command):

Review Comment:
   
https://github.com/apache/nifi-minifi-cpp/pull/2044/commits/d58939bf209e4ed1cf9428404b1b1843a970bf69#diff-b9acc60a55b3f931c457c238886505df8e3401fcb942eae49005cac3f96c397bR104



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to