Skip to content

VM Tool Runner

SSHConfig

Bases: BaseModel

Configuration model for SSH authentication.

Attributes:

Name Type Description
ssh_username str

SSH username.

ssh_hostname str

SSH host/IP.

ssh_password Optional[str]

SSH password (optional if identity file is provided).

ssh_identity_file Optional[str]

Path to SSH private key file (optional if password is provided).

Source code in vm_tool/runner.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class SSHConfig(BaseModel):
    """
    Configuration model for SSH authentication.

    Attributes:
        ssh_username (str): SSH username.
        ssh_hostname (str): SSH host/IP.
        ssh_password (Optional[str]): SSH password (optional if identity file is provided).
        ssh_identity_file (Optional[str]): Path to SSH private key file (optional if password is provided).
    """

    ssh_username: str = Field(..., description="SSH username")
    ssh_hostname: str = Field(..., description="SSH host/IP")
    ssh_password: Optional[str] = Field(
        default=None, description="SSH password (optional if identity file is provided)"
    )
    ssh_identity_file: Optional[str] = Field(
        default=None,
        description="Path to SSH private key file (optional if password is provided)",
    )

    @model_validator(mode="before")
    def validate_authentication(cls, values):
        """Ensures that either an SSH password or identity file is provided for authentication."""
        password = values.get("ssh_password")
        identity_file = values.get("ssh_identity_file")
        if not password and not identity_file:
            raise ValueError(
                "Either ssh_password or ssh_identity_file must be provided."
            )
        return values

validate_authentication(values)

Ensures that either an SSH password or identity file is provided for authentication.

Source code in vm_tool/runner.py
135
136
137
138
139
140
141
142
143
144
@model_validator(mode="before")
def validate_authentication(cls, values):
    """Ensures that either an SSH password or identity file is provided for authentication."""
    password = values.get("ssh_password")
    identity_file = values.get("ssh_identity_file")
    if not password and not identity_file:
        raise ValueError(
            "Either ssh_password or ssh_identity_file must be provided."
        )
    return values

SetupRunner

Main class to handle setup execution.

Attributes:

Name Type Description
github_username str

GitHub username.

github_token str

GitHub token.

github_project_url str

GitHub repository URL.

github_branch str

GitHub branch to use.

docker_compose_file_path str

Path to Docker Compose file.

dockerhub_username str

DockerHub username.

dockerhub_password str

DockerHub password.

Source code in vm_tool/runner.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
class SetupRunner:
    """
    Main class to handle setup execution.

    Attributes:
        github_username (str): GitHub username.
        github_token (str): GitHub token.
        github_project_url (str): GitHub repository URL.
        github_branch (str): GitHub branch to use.
        docker_compose_file_path (str): Path to Docker Compose file.
        dockerhub_username (str): DockerHub username.
        dockerhub_password (str): DockerHub password.
    """

    def __init__(self, config: SetupRunnerConfig):
        """Initializes the setup runner with the given configuration."""
        self.config = config
        self.github_username = config.github_username
        self.github_token = config.github_token
        self.github_project_url = config.github_project_url
        self.github_branch = config.github_branch
        self.docker_compose_file_path = config.docker_compose_file_path
        self.dockerhub_username = config.dockerhub_username
        self.dockerhub_password = config.dockerhub_password
        self.env_path = config.env_path
        self.env_data = config.env_data

    def _get_git_commit(self) -> Optional[str]:
        """Get current git commit hash if in a git repository."""
        import subprocess

        try:
            result = subprocess.run(
                ["git", "rev-parse", "HEAD"],
                capture_output=True,
                text=True,
                timeout=5,
            )
            if result.returncode == 0:
                return result.stdout.strip()
        except Exception:
            pass
        return None

    def _run_ansible_playbook(
        self, extravars: dict, inventory_file: str = "inventory.yml"
    ):
        """Executes an Ansible playbook with the given variables and inventory."""
        current_dir = os.path.dirname(os.path.abspath(__file__))
        venv_dir = os.path.join(sys.prefix, "ansible_runner_data")
        os.makedirs(venv_dir, exist_ok=True)

        playbook_path = os.path.join(current_dir, "vm_setup", "main.yml")
        inventory_path = os.path.join(current_dir, "vm_setup", inventory_file)

        try:
            r = ansible_runner.run(
                private_data_dir=venv_dir,
                playbook=playbook_path,
                inventory=inventory_path,
                extravars=extravars,
            )

            if r.rc != 0:
                logger.error(
                    f"Ansible playbook execution failed with return code {r.rc}: {r.stdout}"
                )
                raise RuntimeError(
                    f"Ansible playbook execution failed with return code {r.rc}: {r.stdout}"
                )

            logger.info("Ansible playbook executed successfully.")

        except Exception as e:
            logger.error(
                f"An error occurred while running the Ansible playbook: {str(e)}"
            )
            raise RuntimeError(
                f"An error occurred while running the Ansible playbook: {str(e)}"
            )

    def run_setup(self):
        """Runs the setup process using Ansible."""
        extravars = {
            "GITHUB_USERNAME": self.github_username,
            "GITHUB_TOKEN": self.github_token,
            "GITHUB_PROJECT_URL": self.github_project_url,
            "GITHUB_BRANCH": self.github_branch,
            "DOCKERHUB_USERNAME": self.dockerhub_username,
            "DOCKERHUB_PASSWORD": self.dockerhub_password,
            "EXECUTION_TYPE": "normal",
        }

        if self.docker_compose_file_path:
            extravars["DOCKER_COMPOSE_FILE_PATH"] = self.docker_compose_file_path
        if self.env_path and self.env_data:
            extravars["ENV_PATH"] = self.env_path
            extravars["ENV_DATA"] = self.env_data

        self._run_ansible_playbook(extravars, "inventory.yml")

    def run_cloud_setup(self, ssh_configs: List[SSHConfig]):
        """Runs the cloud setup using Ansible with dynamic inventory generation."""
        inventory_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "vm_setup",
            "dynamic_inventory.yml",
        )

        inventory_content = {
            "all": {
                "hosts": {},
                "vars": {"ansible_python_interpreter": "/usr/bin/python3"},
            }
        }

        for i, ssh_config in enumerate(ssh_configs):
            host_key = f"cloud_host_{i}"
            host_entry = {
                "ansible_host": ssh_config.ssh_hostname,
                "ansible_user": ssh_config.ssh_username,
            }
            if ssh_config.ssh_identity_file:
                host_entry["ansible_ssh_private_key_file"] = (
                    ssh_config.ssh_identity_file
                )
            elif ssh_config.ssh_password:
                host_entry["ansible_ssh_pass"] = ssh_config.ssh_password

            inventory_content["all"]["hosts"][host_key] = host_entry

        with open(inventory_file_path, "w") as inventory_file:
            yaml.dump(inventory_content, inventory_file)

        extravars = {
            "GITHUB_USERNAME": self.github_username,
            "GITHUB_TOKEN": self.github_token,
            "GITHUB_PROJECT_URL": self.github_project_url,
            "GITHUB_BRANCH": self.github_branch,
            "DOCKERHUB_USERNAME": self.dockerhub_username,
            "DOCKERHUB_PASSWORD": self.dockerhub_password,
            "EXECUTION_TYPE": "cloud",
        }

        if self.docker_compose_file_path:
            extravars["DOCKER_COMPOSE_FILE_PATH"] = self.docker_compose_file_path
        if self.env_path and self.env_data:
            extravars["ENV_PATH"] = self.env_path
            extravars["ENV_DATA"] = self.env_data

        self._run_ansible_playbook(extravars, "dynamic_inventory.yml")

    def run_k8s_setup(self, inventory_file="inventory.yml"):
        """Runs the K8s setup playbook."""
        logger.info("Starting K8s setup...")
        # Reuse existing variables or allow bare execution
        extravars = {"ansible_python_interpreter": "/usr/bin/python3"}
        self._run_ansible_playbook(extravars, "k8s.yml")
        logger.info("K8s setup completed.")

    def run_monitoring_setup(self, inventory_file="inventory.yml"):
        """Runs the Monitoring setup playbook."""
        logger.info("Starting Monitoring setup...")
        extravars = {"ansible_python_interpreter": "/usr/bin/python3"}
        self._run_ansible_playbook(extravars, "monitoring.yml")
        logger.info("Monitoring setup completed.")

    def run_docker_deploy(
        self,
        compose_file="docker-compose.yml",
        inventory_file="inventory.yml",
        host: str = None,
        user: str = None,
        env_file: str = None,
        deploy_command: str = None,
        force: bool = False,
    ):
        """Runs the Docker Compose deployment with idempotency."""
        from vm_tool.state import DeploymentState

        # Initialize state tracker
        state = DeploymentState()

        # Compute hash of compose file for change detection
        compose_hash = state.compute_hash(compose_file)

        # Check if deployment is needed (unless force is True)
        service_name = "docker-compose"
        if host and not force:
            if not state.needs_update(host, compose_hash, service_name):
                logger.info(
                    f"✅ Deployment is up-to-date for {host}. "
                    f"Use --force to redeploy anyway."
                )
                print(
                    f"✅ No changes detected. Deployment is up-to-date.\n"
                    f"   Use --force flag to redeploy anyway."
                )
                return

        target_inventory = inventory_file

        # Generate dynamic inventory if host is provided
        if host:
            logger.info(f"Generating dynamic inventory for host: {host}")
            inventory_content = {
                "all": {
                    "hosts": {
                        "target_host": {
                            "ansible_host": host,
                            "ansible_connection": "ssh",
                            "ansible_ssh_common_args": "-o StrictHostKeyChecking=no",
                        }
                    },
                    "vars": {"ansible_python_interpreter": "/usr/bin/python3"},
                }
            }
            if user:
                inventory_content["all"]["hosts"]["target_host"]["ansible_user"] = user

            # Use a temporary file or override inventory.yml locally
            current_dir = os.path.dirname(os.path.abspath(__file__))
            generated_inventory_path = os.path.join(
                current_dir, "vm_setup", "generated_inventory.yml"
            )

            with open(generated_inventory_path, "w") as f:
                yaml.dump(inventory_content, f)

            target_inventory = "generated_inventory.yml"

        logger.info(
            f"Starting Docker deployment using {compose_file} on {target_inventory}..."
        )

        extravars = {
            "ansible_python_interpreter": "/usr/bin/python3",
            "DOCKER_COMPOSE_FILE_PATH": compose_file,
            "GITHUB_USERNAME": self.github_username,
            "GITHUB_TOKEN": self.github_token,
            "GITHUB_PROJECT_URL": self.github_project_url,
            "DEPLOY_MODE": "push",
            "SOURCE_PATH": os.getcwd(),  # Current working directory where vm_tool is run
        }

        if env_file:
            extravars["ENV_FILE_PATH"] = env_file

        if deploy_command:
            extravars["DEPLOY_COMMAND"] = deploy_command

        playbook_path = os.path.join(
            os.path.dirname(__file__), "vm_setup", "push_code.yml"
        )

        try:
            r = ansible_runner.run(
                private_data_dir=os.path.dirname(__file__),
                playbook=playbook_path,
                inventory=target_inventory,
                extravars=extravars,
            )

            if r.status == "successful":
                logger.info("Docker deployment completed successfully.")
                # Record successful deployment
                if host:
                    state.record_deployment(
                        host, compose_file, compose_hash, service_name
                    )
                    logger.info(f"✅ Deployment state recorded for {host}")

                    # Record in history
                    from vm_tool.history import DeploymentHistory

                    history = DeploymentHistory()
                    git_commit = self._get_git_commit()
                    deployment_id = history.record_deployment(
                        host=host,
                        compose_file=compose_file,
                        compose_hash=compose_hash,
                        git_commit=git_commit,
                        service_name=service_name,
                        status="success",
                    )
                    logger.info(f"📝 Deployment recorded in history: {deployment_id}")
            else:
                error_msg = f"Deployment failed with status: {r.status}"
                logger.error(error_msg)
                if host:
                    state.mark_failed(host, service_name, error_msg)
                raise RuntimeError(error_msg)

        except Exception as e:
            if host:
                state.mark_failed(host, service_name, str(e))
            raise

__init__(config)

Initializes the setup runner with the given configuration.

Source code in vm_tool/runner.py
161
162
163
164
165
166
167
168
169
170
171
172
def __init__(self, config: SetupRunnerConfig):
    """Initializes the setup runner with the given configuration."""
    self.config = config
    self.github_username = config.github_username
    self.github_token = config.github_token
    self.github_project_url = config.github_project_url
    self.github_branch = config.github_branch
    self.docker_compose_file_path = config.docker_compose_file_path
    self.dockerhub_username = config.dockerhub_username
    self.dockerhub_password = config.dockerhub_password
    self.env_path = config.env_path
    self.env_data = config.env_data

run_cloud_setup(ssh_configs)

Runs the cloud setup using Ansible with dynamic inventory generation.

Source code in vm_tool/runner.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def run_cloud_setup(self, ssh_configs: List[SSHConfig]):
    """Runs the cloud setup using Ansible with dynamic inventory generation."""
    inventory_file_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        "vm_setup",
        "dynamic_inventory.yml",
    )

    inventory_content = {
        "all": {
            "hosts": {},
            "vars": {"ansible_python_interpreter": "/usr/bin/python3"},
        }
    }

    for i, ssh_config in enumerate(ssh_configs):
        host_key = f"cloud_host_{i}"
        host_entry = {
            "ansible_host": ssh_config.ssh_hostname,
            "ansible_user": ssh_config.ssh_username,
        }
        if ssh_config.ssh_identity_file:
            host_entry["ansible_ssh_private_key_file"] = (
                ssh_config.ssh_identity_file
            )
        elif ssh_config.ssh_password:
            host_entry["ansible_ssh_pass"] = ssh_config.ssh_password

        inventory_content["all"]["hosts"][host_key] = host_entry

    with open(inventory_file_path, "w") as inventory_file:
        yaml.dump(inventory_content, inventory_file)

    extravars = {
        "GITHUB_USERNAME": self.github_username,
        "GITHUB_TOKEN": self.github_token,
        "GITHUB_PROJECT_URL": self.github_project_url,
        "GITHUB_BRANCH": self.github_branch,
        "DOCKERHUB_USERNAME": self.dockerhub_username,
        "DOCKERHUB_PASSWORD": self.dockerhub_password,
        "EXECUTION_TYPE": "cloud",
    }

    if self.docker_compose_file_path:
        extravars["DOCKER_COMPOSE_FILE_PATH"] = self.docker_compose_file_path
    if self.env_path and self.env_data:
        extravars["ENV_PATH"] = self.env_path
        extravars["ENV_DATA"] = self.env_data

    self._run_ansible_playbook(extravars, "dynamic_inventory.yml")

run_docker_deploy(compose_file='docker-compose.yml', inventory_file='inventory.yml', host=None, user=None, env_file=None, deploy_command=None, force=False)

Runs the Docker Compose deployment with idempotency.

Source code in vm_tool/runner.py
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def run_docker_deploy(
    self,
    compose_file="docker-compose.yml",
    inventory_file="inventory.yml",
    host: str = None,
    user: str = None,
    env_file: str = None,
    deploy_command: str = None,
    force: bool = False,
):
    """Runs the Docker Compose deployment with idempotency."""
    from vm_tool.state import DeploymentState

    # Initialize state tracker
    state = DeploymentState()

    # Compute hash of compose file for change detection
    compose_hash = state.compute_hash(compose_file)

    # Check if deployment is needed (unless force is True)
    service_name = "docker-compose"
    if host and not force:
        if not state.needs_update(host, compose_hash, service_name):
            logger.info(
                f"✅ Deployment is up-to-date for {host}. "
                f"Use --force to redeploy anyway."
            )
            print(
                f"✅ No changes detected. Deployment is up-to-date.\n"
                f"   Use --force flag to redeploy anyway."
            )
            return

    target_inventory = inventory_file

    # Generate dynamic inventory if host is provided
    if host:
        logger.info(f"Generating dynamic inventory for host: {host}")
        inventory_content = {
            "all": {
                "hosts": {
                    "target_host": {
                        "ansible_host": host,
                        "ansible_connection": "ssh",
                        "ansible_ssh_common_args": "-o StrictHostKeyChecking=no",
                    }
                },
                "vars": {"ansible_python_interpreter": "/usr/bin/python3"},
            }
        }
        if user:
            inventory_content["all"]["hosts"]["target_host"]["ansible_user"] = user

        # Use a temporary file or override inventory.yml locally
        current_dir = os.path.dirname(os.path.abspath(__file__))
        generated_inventory_path = os.path.join(
            current_dir, "vm_setup", "generated_inventory.yml"
        )

        with open(generated_inventory_path, "w") as f:
            yaml.dump(inventory_content, f)

        target_inventory = "generated_inventory.yml"

    logger.info(
        f"Starting Docker deployment using {compose_file} on {target_inventory}..."
    )

    extravars = {
        "ansible_python_interpreter": "/usr/bin/python3",
        "DOCKER_COMPOSE_FILE_PATH": compose_file,
        "GITHUB_USERNAME": self.github_username,
        "GITHUB_TOKEN": self.github_token,
        "GITHUB_PROJECT_URL": self.github_project_url,
        "DEPLOY_MODE": "push",
        "SOURCE_PATH": os.getcwd(),  # Current working directory where vm_tool is run
    }

    if env_file:
        extravars["ENV_FILE_PATH"] = env_file

    if deploy_command:
        extravars["DEPLOY_COMMAND"] = deploy_command

    playbook_path = os.path.join(
        os.path.dirname(__file__), "vm_setup", "push_code.yml"
    )

    try:
        r = ansible_runner.run(
            private_data_dir=os.path.dirname(__file__),
            playbook=playbook_path,
            inventory=target_inventory,
            extravars=extravars,
        )

        if r.status == "successful":
            logger.info("Docker deployment completed successfully.")
            # Record successful deployment
            if host:
                state.record_deployment(
                    host, compose_file, compose_hash, service_name
                )
                logger.info(f"✅ Deployment state recorded for {host}")

                # Record in history
                from vm_tool.history import DeploymentHistory

                history = DeploymentHistory()
                git_commit = self._get_git_commit()
                deployment_id = history.record_deployment(
                    host=host,
                    compose_file=compose_file,
                    compose_hash=compose_hash,
                    git_commit=git_commit,
                    service_name=service_name,
                    status="success",
                )
                logger.info(f"📝 Deployment recorded in history: {deployment_id}")
        else:
            error_msg = f"Deployment failed with status: {r.status}"
            logger.error(error_msg)
            if host:
                state.mark_failed(host, service_name, error_msg)
            raise RuntimeError(error_msg)

    except Exception as e:
        if host:
            state.mark_failed(host, service_name, str(e))
        raise

run_k8s_setup(inventory_file='inventory.yml')

Runs the K8s setup playbook.

Source code in vm_tool/runner.py
299
300
301
302
303
304
305
def run_k8s_setup(self, inventory_file="inventory.yml"):
    """Runs the K8s setup playbook."""
    logger.info("Starting K8s setup...")
    # Reuse existing variables or allow bare execution
    extravars = {"ansible_python_interpreter": "/usr/bin/python3"}
    self._run_ansible_playbook(extravars, "k8s.yml")
    logger.info("K8s setup completed.")

run_monitoring_setup(inventory_file='inventory.yml')

Runs the Monitoring setup playbook.

Source code in vm_tool/runner.py
307
308
309
310
311
312
def run_monitoring_setup(self, inventory_file="inventory.yml"):
    """Runs the Monitoring setup playbook."""
    logger.info("Starting Monitoring setup...")
    extravars = {"ansible_python_interpreter": "/usr/bin/python3"}
    self._run_ansible_playbook(extravars, "monitoring.yml")
    logger.info("Monitoring setup completed.")

run_setup()

Runs the setup process using Ansible.

Source code in vm_tool/runner.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def run_setup(self):
    """Runs the setup process using Ansible."""
    extravars = {
        "GITHUB_USERNAME": self.github_username,
        "GITHUB_TOKEN": self.github_token,
        "GITHUB_PROJECT_URL": self.github_project_url,
        "GITHUB_BRANCH": self.github_branch,
        "DOCKERHUB_USERNAME": self.dockerhub_username,
        "DOCKERHUB_PASSWORD": self.dockerhub_password,
        "EXECUTION_TYPE": "normal",
    }

    if self.docker_compose_file_path:
        extravars["DOCKER_COMPOSE_FILE_PATH"] = self.docker_compose_file_path
    if self.env_path and self.env_data:
        extravars["ENV_PATH"] = self.env_path
        extravars["ENV_DATA"] = self.env_data

    self._run_ansible_playbook(extravars, "inventory.yml")

SetupRunnerConfig

Bases: BaseModel

Configuration model for setting up the runner.

Attributes:

Name Type Description
github_username Optional[str]

GitHub username for authentication.

github_token Optional[str]

GitHub token for authentication.

github_project_url str

URL of the GitHub repository.

github_branch str

GitHub branch to use (default: 'main').

docker_compose_file_path str

Path to the Docker Compose file (default: 'docker-compose.yml').

dockerhub_username Optional[str]

DockerHub username (optional).

dockerhub_password Optional[str]

DockerHub password (required if username is provided).

env_path Optional[str]

Path where the environment file should be created (optional).

env_data Optional[dict]

Environment data to dump into the file (optional, should be a dict).

Source code in vm_tool/runner.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class SetupRunnerConfig(BaseModel):
    """
    Configuration model for setting up the runner.

    Attributes:
        github_username (Optional[str]): GitHub username for authentication.
        github_token (Optional[str]): GitHub token for authentication.
        github_project_url (str): URL of the GitHub repository.
        github_branch (str): GitHub branch to use (default: 'main').
        docker_compose_file_path (str): Path to the Docker Compose file (default: 'docker-compose.yml').
        dockerhub_username (Optional[str]): DockerHub username (optional).
        dockerhub_password (Optional[str]): DockerHub password (required if username is provided).
        env_path (Optional[str]): Path where the environment file should be created (optional).
        env_data (Optional[dict]): Environment data to dump into the file (optional, should be a dict).
    """

    github_username: Optional[str] = Field(
        default=None, description="GitHub username for authentication (optional)"
    )
    github_token: Optional[str] = Field(
        default=None, description="GitHub token for authentication (optional)"
    )
    github_project_url: str = Field(..., description="URL of the GitHub repository")
    github_branch: str = Field(
        default="main", description="GitHub branch to use (default: 'main')"
    )
    docker_compose_file_path: str = Field(
        default="docker-compose.yml",
        description="Path to the Docker Compose file (default: 'docker-compose.yml')",
    )
    dockerhub_username: Optional[str] = Field(
        default=None, description="DockerHub username (optional)"
    )
    dockerhub_password: Optional[str] = Field(
        default=None,
        description="DockerHub password (required if username is provided)",
    )
    env_path: Optional[str] = Field(
        default=None,
        description="Path where the environment file should be created (optional)",
    )
    env_data: Optional[dict] = Field(
        default=None,
        description="Environment data to dump into the file (optional, should be a dict)",
    )

    @validator("env_path", always=True)
    def check_env_path_with_env_data(cls, v, values):
        """If env_data is provided, env_path must also be provided."""
        if values.get("env_data") is not None and not v:
            raise ValueError("env_path must be provided if env_data is specified")
        return v

    @validator("env_data", always=True)
    def check_env_data_with_env_path(cls, v, values):
        """If env_path is provided, env_data must also be provided."""
        if values.get("env_path") is not None and v is None:
            raise ValueError("env_data must be provided if env_path is specified")
        return v

    @validator("dockerhub_password", always=True)
    def check_dockerhub_password(cls, v, values):
        """Ensures that a password is provided if a DockerHub username is set."""
        if values.get("dockerhub_username") and not v:
            raise ValueError(
                "DockerHub password must be provided if DockerHub username is specified"
            )
        return v

    @validator("dockerhub_username", always=True)
    def check_dockerhub_username(cls, v, values):
        """Ensures that a username is provided if a DockerHub password is set."""
        if values.get("dockerhub_password") and not v:
            raise ValueError(
                "DockerHub username must be provided if DockerHub password is specified"
            )
        return v

    @validator("github_token", always=True)
    def check_github_token(cls, v, values):
        """Ensures that a GitHub token is provided if a GitHub username is set."""
        if values.get("github_username") and not v:
            raise ValueError(
                "GitHub token must be provided if GitHub username is specified"
            )
        return v

    @validator("github_username", always=True)
    def check_github_username(cls, v, values):
        """Ensures that a GitHub username is provided if a GitHub token is set."""
        if values.get("github_token") and not v:
            raise ValueError(
                "GitHub username must be provided if GitHub token is specified"
            )
        return v

check_dockerhub_password(v, values)

Ensures that a password is provided if a DockerHub username is set.

Source code in vm_tool/runner.py
77
78
79
80
81
82
83
84
@validator("dockerhub_password", always=True)
def check_dockerhub_password(cls, v, values):
    """Ensures that a password is provided if a DockerHub username is set."""
    if values.get("dockerhub_username") and not v:
        raise ValueError(
            "DockerHub password must be provided if DockerHub username is specified"
        )
    return v

check_dockerhub_username(v, values)

Ensures that a username is provided if a DockerHub password is set.

Source code in vm_tool/runner.py
86
87
88
89
90
91
92
93
@validator("dockerhub_username", always=True)
def check_dockerhub_username(cls, v, values):
    """Ensures that a username is provided if a DockerHub password is set."""
    if values.get("dockerhub_password") and not v:
        raise ValueError(
            "DockerHub username must be provided if DockerHub password is specified"
        )
    return v

check_env_data_with_env_path(v, values)

If env_path is provided, env_data must also be provided.

Source code in vm_tool/runner.py
70
71
72
73
74
75
@validator("env_data", always=True)
def check_env_data_with_env_path(cls, v, values):
    """If env_path is provided, env_data must also be provided."""
    if values.get("env_path") is not None and v is None:
        raise ValueError("env_data must be provided if env_path is specified")
    return v

check_env_path_with_env_data(v, values)

If env_data is provided, env_path must also be provided.

Source code in vm_tool/runner.py
63
64
65
66
67
68
@validator("env_path", always=True)
def check_env_path_with_env_data(cls, v, values):
    """If env_data is provided, env_path must also be provided."""
    if values.get("env_data") is not None and not v:
        raise ValueError("env_path must be provided if env_data is specified")
    return v

check_github_token(v, values)

Ensures that a GitHub token is provided if a GitHub username is set.

Source code in vm_tool/runner.py
 95
 96
 97
 98
 99
100
101
102
@validator("github_token", always=True)
def check_github_token(cls, v, values):
    """Ensures that a GitHub token is provided if a GitHub username is set."""
    if values.get("github_username") and not v:
        raise ValueError(
            "GitHub token must be provided if GitHub username is specified"
        )
    return v

check_github_username(v, values)

Ensures that a GitHub username is provided if a GitHub token is set.

Source code in vm_tool/runner.py
104
105
106
107
108
109
110
111
@validator("github_username", always=True)
def check_github_username(cls, v, values):
    """Ensures that a GitHub username is provided if a GitHub token is set."""
    if values.get("github_token") and not v:
        raise ValueError(
            "GitHub username must be provided if GitHub token is specified"
        )
    return v