(self)
| 39 | self.communication_port = config.communication_port |
| 40 | |
| 41 | def init_container(self): |
| 42 | container_check_command = ["docker", "ps", "-a", "--filter", f"name={self.container_name}", "--format", "{{.Names}}"] |
| 43 | existing_container = subprocess.run(container_check_command, capture_output=True, text=True) |
| 44 | os.makedirs(self.local_workplace, exist_ok=True) |
| 45 | |
| 46 | if self.setup_package is not None: |
| 47 | unzip_command = ["tar", "-xzvf", f"packages/{self.setup_package}.tar.gz", "-C", self.local_workplace] |
| 48 | subprocess.run(unzip_command) |
| 49 | if self.git_clone: |
| 50 | if not os.path.exists(os.path.join(self.local_workplace, 'metachain')): |
| 51 | git_command = ["cd", self.local_workplace, "&&", "git", "clone", "-b", self.test_pull_name, f"https://{AI_USER}:{GITHUB_AI_TOKEN}@github.com/tjb-tech/metachain.git"] |
| 52 | git_command = " ".join(git_command) |
| 53 | |
| 54 | result = subprocess.run(git_command, shell=True) |
| 55 | if result.returncode != 0: |
| 56 | raise Exception(f"Failed to clone the repository. Please check your internet connection and try again.") |
| 57 | # create a new branch |
| 58 | new_branch_name = f"{self.test_pull_name}_{self.task_name}" |
| 59 | create_branch_command = f"cd {self.local_workplace}/metachain && git checkout -b {new_branch_name}" |
| 60 | result = subprocess.run(create_branch_command, shell=True, capture_output=True, text=True) |
| 61 | if result.returncode != 0: |
| 62 | print(Exception(f"Failed to create and switch to new branch. Error: {result.stderr}")) |
| 63 | switch_branch_command = f"cd {self.local_workplace}/metachain && git checkout {new_branch_name}" |
| 64 | result = subprocess.run(switch_branch_command, shell=True, capture_output=True, text=True) |
| 65 | if result.returncode != 0: |
| 66 | raise Exception(f"Failed to switch to new branch. Error: {result.stderr}") |
| 67 | else: |
| 68 | print(f"Successfully switched to new branch: {new_branch_name}") |
| 69 | else: |
| 70 | print(f"Successfully created and switched to new branch: {new_branch_name}") |
| 71 | |
| 72 | if existing_container.stdout.strip() == self.container_name: |
| 73 | # check if the container is running |
| 74 | running_check_command = ["docker", "ps", "--filter", f"name={self.container_name}", "--format", "{{.Names}}"] |
| 75 | running_container = subprocess.run(running_check_command, capture_output=True, text=True) |
| 76 | |
| 77 | if running_container.stdout.strip() == self.container_name: |
| 78 | print(f"Container '{self.container_name}' is already running. Skipping creation.") |
| 79 | return # container is already running, skip creation |
| 80 | else: |
| 81 | # container exists but is not running, start it |
| 82 | start_command = ["docker", "start", self.container_name] |
| 83 | subprocess.run(start_command) |
| 84 | print(f"Container '{self.container_name}' has been started.") |
| 85 | return |
| 86 | |
| 87 | # if the container does not exist, create and start a new container |
| 88 | gpu_cmd = ["--gpus", GPUS] if GPUS else [] |
| 89 | docker_command = [ |
| 90 | "docker", "run", "-d", "--platform", PLATFORM, "--userns=host",] + gpu_cmd + ["--name", self.container_name, |
| 91 | "--user", "root", "-v", f"{self.local_workplace}:{self.docker_workplace}", |
| 92 | "-w", f"{self.docker_workplace}", "-p", f"{self.communication_port}:8000", |
| 93 | "--restart", "unless-stopped", BASE_IMAGES |
| 94 | ] |
| 95 | print(docker_command) |
| 96 | # execute the docker command |
| 97 | result = subprocess.run(docker_command, capture_output=True, text=True) |
| 98 | if result.returncode != 0: |
no test coverage detected