| import logging |
| from typing import List, Dict, Any |
| from src.adware_manager import DeploymentMethod, Payload |
|
|
| class DeploymentManager: |
| def __init__(self, logger: logging.Logger): |
| """ |
| Initializes the DeploymentManager with a logger. |
| |
| Args: |
| logger (logging.Logger): The logger instance to use. |
| """ |
| self.logger = logger |
|
|
| def add_deployment_method(self, name: str, description: str, config_schema: Dict[str, Any]) -> DeploymentMethod: |
| """ |
| Adds a new deployment method to the database. |
| |
| Args: |
| name (str): The name of the deployment method. |
| description (str): A description of the deployment method. |
| config_schema (Dict[str, Any]): A schema for the configuration parameters. |
| |
| Returns: |
| DeploymentMethod: The created deployment method object. |
| """ |
| deployment_method = DeploymentMethod(name=name, description=description, config_schema=config_schema) |
| deployment_method.save() |
| self.logger.info(f"Deployment method '{name}' added successfully.") |
| return deployment_method |
|
|
| def get_deployment_method(self, deployment_method_id: int) -> DeploymentMethod: |
| """ |
| Retrieves a deployment method by its ID. |
| |
| Args: |
| deployment_method_id (int): The ID of the deployment method to retrieve. |
| |
| Returns: |
| DeploymentMethod: The deployment method object, or None if not found. |
| """ |
| deployment_method = DeploymentMethod.get_or_none(DeploymentMethod.id == deployment_method_id) |
| if not deployment_method: |
| self.logger.warning(f"Deployment method with ID {deployment_method_id} not found.") |
| return deployment_method |
|
|
| def update_deployment_method(self, deployment_method_id: int, name: str = None, description: str = None, config_schema: Dict[str, Any] = None) -> DeploymentMethod: |
| """ |
| Updates an existing deployment method. |
| |
| Args: |
| deployment_method_id (int): The ID of the deployment method to update. |
| name (str, optional): The new name of the deployment method. |
| description (str, optional): The new description of the deployment method. |
| config_schema (Dict[str, Any], optional): The new schema for the configuration parameters. |
| |
| Returns: |
| DeploymentMethod: The updated deployment method object, or None if not found. |
| """ |
| deployment_method = self.get_deployment_method(deployment_method_id) |
| if not deployment_method: |
| return None |
|
|
| if name: |
| deployment_method.name = name |
| if description: |
| deployment_method.description = description |
| if config_schema: |
| deployment_method.config_schema = config_schema |
|
|
| deployment_method.save() |
| self.logger.info(f"Deployment method '{deployment_method.name}' updated successfully.") |
| return deployment_method |
|
|
| def delete_deployment_method(self, deployment_method_id: int) -> bool: |
| """ |
| Deletes a deployment method by its ID. |
| |
| Args: |
| deployment_method_id (int): The ID of the deployment method to delete. |
| |
| Returns: |
| bool: True if the deployment method was deleted, False otherwise. |
| """ |
| deployment_method = self.get_deployment_method(deployment_method_id) |
| if not deployment_method: |
| return False |
|
|
| deployment_method.delete_instance() |
| self.logger.info(f"Deployment method '{deployment_method.name}' deleted successfully.") |
| return True |
|
|
| def list_deployment_methods(self) -> List[DeploymentMethod]: |
| """ |
| Lists all available deployment methods. |
| |
| Returns: |
| List[DeploymentMethod]: A list of all deployment method objects. |
| """ |
| deployment_method_list = list(DeploymentMethod.select()) |
| return deployment_method_list |
|
|
| def deploy(self, deployment_method: DeploymentMethod, payload: Payload, config: Dict[str, Any]) -> bool: |
| """ |
| Deploys a payload using a specific deployment method. |
| |
| Args: |
| deployment_method (DeploymentMethod): The deployment method to use. |
| payload (Payload): The payload to deploy. |
| config (Dict[str, Any]): The configuration parameters for the deployment. |
| |
| Returns: |
| bool: True if the deployment was successful, False otherwise. |
| """ |
| try: |
| if deployment_method.name == "SSH": |
| self._deploy_via_ssh(payload, config) |
| elif deployment_method.name == "HTTP": |
| self._deploy_via_http(payload, config) |
| elif deployment_method.name == "FTP": |
| self._deploy_via_ftp(payload, config) |
| elif deployment_method.name == "SMB": |
| self._deploy_via_smb(payload, config) |
| else: |
| self.logger.error(f"Unsupported deployment method: {deployment_method.name}") |
| return False |
|
|
| self.logger.info(f"Payload '{payload.name}' deployed using method '{deployment_method.name}' with config: {config}") |
| return True |
| except Exception as e: |
| self.logger.error(f"Error deploying payload '{payload.name}' using method '{deployment_method.name}': {str(e)}") |
| return False |
|
|
| def _deploy_via_ssh(self, payload: Payload, config: Dict[str, Any]): |
| """ |
| Deploys a payload via SSH. |
| |
| Args: |
| payload (Payload): The payload to deploy. |
| config (Dict[str, Any]): The configuration parameters for the deployment. |
| """ |
| self.logger.info(f"Deploying payload '{payload.name}' via SSH with config: {config}") |
| |
|
|
| def _deploy_via_http(self, payload: Payload, config: Dict[str, Any]): |
| """ |
| Deploys a payload via HTTP. |
| |
| Args: |
| payload (Payload): The payload to deploy. |
| config (Dict[str, Any]): The configuration parameters for the deployment. |
| """ |
| self.logger.info(f"Deploying payload '{payload.name}' via HTTP with config: {config}") |
| |
|
|
| def _deploy_via_ftp(self, payload: Payload, config: Dict[str, Any]): |
| """ |
| Deploys a payload via FTP. |
| |
| Args: |
| payload (Payload): The payload to deploy. |
| config (Dict[str, Any]): The configuration parameters for the deployment. |
| """ |
| self.logger.info(f"Deploying payload '{payload.name}' via FTP with config: {config}") |
| |
|
|
| def _deploy_via_smb(self, payload: Payload, config: Dict[str, Any]): |
| """ |
| Deploys a payload via SMB. |
| |
| Args: |
| payload (Payload): The payload to deploy. |
| config (Dict[str, Any]): The configuration parameters for the deployment. |
| """ |
| self.logger.info(f"Deploying payload '{payload.name}' via SMB with config: {config}") |
| |
|
|