Skip to main content
Glama
washyu
by washyu

destroy_terraform_service

Remove a Terraform-managed service and delete all associated resources from your homelab infrastructure.

Instructions

Destroy a Terraform-managed service and clean up all resources

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
service_nameYesName of the service to destroy
hostnameYesHostname or IP address of the device
usernameNoSSH username (use 'mcp_admin' for passwordless access after setup)mcp_admin
passwordNoSSH password (not needed for mcp_admin after setup)
portNoSSH port (default: 22)

Implementation Reference

  • MCP tool handler function that wraps the destroy_terraform_service method. Creates a ServiceInstaller instance and delegates to the destroy_terraform_service method with the provided arguments, returning the result as JSON-formatted text.
    async def handle_destroy_terraform_service(arguments: dict[str, Any]) -> dict[str, Any]:
        """Handle destroy_terraform_service tool."""
        installer = ServiceInstaller()
        destroy_result = await installer.destroy_terraform_service(**arguments)
        return {"content": [{"type": "text", "text": json.dumps(destroy_result, indent=2)}]}
  • Core implementation of destroy_terraform_service method. Checks if the Terraform directory exists at /opt/terraform/{service_name}, runs 'terraform destroy -auto-approve', and cleans up the directory if destroy succeeds. Returns status, service name, action, and output.
    async def destroy_terraform_service(
        self,
        service_name: str,
        hostname: str,
        username: str = "mcp_admin",
        password: str | None = None,
    ) -> dict[str, Any]:
        """Destroy a Terraform-managed service."""
        tf_dir = f"/opt/terraform/{service_name}"
    
        # Check if Terraform directory exists
        dir_check = await ssh_execute_command(
            hostname=hostname,
            username=username,
            password=password,
            command=f"test -d {tf_dir}",
        )
    
        dir_data = json.loads(dir_check)
        if dir_data.get("exit_code") != 0:
            return {
                "status": "error",
                "error": f"Terraform directory not found: {tf_dir}",
            }
    
        # Run terraform destroy
        destroy_result = await ssh_execute_command(
            hostname=hostname,
            username=username,
            password=password,
            command=f"cd {tf_dir} && terraform destroy -auto-approve",
        )
    
        destroy_data = json.loads(destroy_result)
    
        # Clean up Terraform directory if destroy succeeded
        if destroy_data.get("exit_code") == 0:
            await ssh_execute_command(
                hostname=hostname,
                username=username,
                password=password,
                command=f"sudo rm -rf {tf_dir}",
                sudo=True,
            )
    
        return {
            "status": "success" if destroy_data.get("exit_code") == 0 else "error",
            "service": service_name,
            "action": "destroy",
            "output": destroy_data.get("output", ""),
        }
  • Input schema definition for destroy_terraform_service tool. Defines required parameters (service_name, hostname) and optional parameters (username with default 'mcp_admin', password, port with default 22). Describes the tool's purpose as destroying a Terraform-managed service and cleaning up all resources.
    "destroy_terraform_service": {
        "description": "Destroy a Terraform-managed service and clean up all resources",
        "inputSchema": {
            "type": "object",
            "properties": {
                "service_name": {
                    "type": "string",
                    "description": "Name of the service to destroy",
                },
                "hostname": {
                    "type": "string",
                    "description": "Hostname or IP address of the device",
                },
                "username": {
                    "type": "string",
                    "description": "SSH username (use 'mcp_admin' for passwordless access after setup)",
                    "default": "mcp_admin",
                },
                "password": {
                    "type": "string",
                    "description": "SSH password (not needed for mcp_admin after setup)",
                },
                "port": {
                    "type": "integer",
                    "description": "SSH port (default: 22)",
                    "default": 22,
                },
            },
            "required": ["service_name", "hostname"],
        },
    },
  • Import statement for handle_destroy_terraform_service handler function from service_handlers module.
    handle_destroy_terraform_service,
  • Tool registration mapping the tool name 'destroy_terraform_service' to its handler function in the TOOL_HANDLERS dictionary.
    "destroy_terraform_service": handle_destroy_terraform_service,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/washyu/mcp_python_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server