Class: Harbor::Tools
- Inherits:
-
Object
- Object
- Harbor::Tools
- Defined in:
- lib/harbor/tools.rb
Overview
Harbor::Tools is the single kernel for domain operations. All surfaces (Thor CLI, stdio MCP, HTTP MCP, web controllers) invoke domain operations through this class. Web-app-local concerns (user management, pagination, UI filtering, session auth) are normal Rails controllers and do NOT go through Harbor::Tools. Kernel boundary: anything that touches a host or the fleet.
Constant Summary collapse
- VALID_ORIGINS =
Valid origins for tool invocations. Each surface passes its own origin when calling the kernel so the audit log records who/what triggered the operation (human CLI user vs. AI agent via stdio MCP vs. web UI click, etc.). See design doc premise P2.
%i[cli mcp_stdio mcp_http web_ui webhook host_agent system].freeze
Instance Method Summary collapse
-
#call(tool_name, arguments, actor: nil, origin: :mcp_stdio) ⇒ Object
Every surface calls through here.
- #definitions ⇒ Object
-
#initialize(config) ⇒ Tools
constructor
A new instance of Tools.
Constructor Details
#initialize(config) ⇒ Tools
Returns a new instance of Tools.
20 21 22 23 24 |
# File 'lib/harbor/tools.rb', line 20 def initialize(config) @config = config @actor = nil @origin = :mcp_stdio end |
Instance Method Details
#call(tool_name, arguments, actor: nil, origin: :mcp_stdio) ⇒ Object
Every surface calls through here. actor identifies the principal (user id for web/http_mcp, process owner for cli, nil for system events). origin identifies which surface invoked the call.
Emits start + completed events for observability. Emission is fire-and-forget: if the emitter fails, we log and continue — tool calls never fail because audit plumbing is down.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/harbor/tools.rb', line 33 def call(tool_name, arguments, actor: nil, origin: :mcp_stdio) unless VALID_ORIGINS.include?(origin) raise Error, "Invalid origin: #{origin.inspect}. Must be one of #{VALID_ORIGINS.inspect}" end @actor = actor @origin = origin app = arguments.is_a?(Hash) ? (arguments["project"] || arguments[:project]) : nil destination = arguments.is_a?(Hash) ? (arguments["destination"] || arguments[:destination]) : nil event_id = safe_emit_start(tool_name, arguments, app, destination) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) begin result = dispatch(tool_name, arguments) duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).to_i safe_emit_complete(event_id, result: "success", duration_ms: duration_ms) result rescue => e duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).to_i safe_emit_complete(event_id, result: "failure", error: e., duration_ms: duration_ms) raise end end |
#definitions ⇒ Object
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 112 113 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/harbor/tools.rb', line 57 def definitions [ tool_def("harbor_add_project", "Register a KAMAL project with Harbor. The project must have a config/deploy.yml.", { name: { type: "string", description: "Short name for the project (e.g. 'myapp', 'api')" }, path: { type: "string", description: "Absolute path to the project directory" }, destinations: { type: "string", description: "Comma-separated destinations (e.g. 'production,staging'). Auto-detected if omitted." }, description: { type: "string", description: "Short description of the project" } }, required: %w[name path]), tool_def("harbor_remove_project", "Unregister a project from Harbor (does not delete the project files)", { name: { type: "string", description: "Project name to remove" } }, required: ["name"]), tool_def("harbor_setup_kamal", "Initialize KAMAL in a Rails project directory. Creates config/deploy.yml and .kamal/secrets. Project must already exist on disk.", { path: { type: "string", description: "Absolute path to the Rails project" }, service: { type: "string", description: "Service name (used as Docker container prefix)" }, image: { type: "string", description: "Docker image name (e.g. 'youruser/myapp')" }, host: { type: "string", description: "Server IP address or hostname" }, domain: { type: "string", description: "Domain for the app (e.g. 'myapp.example.com')" }, registry: { type: "string", description: "Registry type: 'local' (default, no Docker Hub needed), 'dockerhub', or a custom server URL" }, registry_username: { type: "string", description: "Docker registry username (not needed for local registry)" } }, required: %w[path service image host]), tool_def("harbor_list_projects", "List all registered projects", {}), tool_def("harbor_project_status", "Get container status for a project", { project: { type: "string", description: "Project name" }, destination: { type: "string", description: "Destination (production, staging, etc.)" } }, required: ["project"]), tool_def("harbor_deploy", "Deploy a project", { project: { type: "string", description: "Project name" }, destination: { type: "string", description: "Destination" }, skip_push: { type: "boolean", description: "Skip image push (pull existing)" } }, required: ["project"]), tool_def("harbor_rollback", "Rollback a project to a specific version", { project: { type: "string", description: "Project name" }, version: { type: "string", description: "Version (git SHA) to rollback to" }, destination: { type: "string", description: "Destination" } }, required: %w[project version]), tool_def("harbor_get_logs", "Get recent app logs", { project: { type: "string", description: "Project name" }, role: { type: "string", description: "Role (web, workers, etc.)" }, lines: { type: "integer", description: "Number of lines (default 100)" }, grep: { type: "string", description: "Filter logs by pattern" } }, required: ["project"]), tool_def("harbor_exec", "Execute a command in a project's container", { project: { type: "string", description: "Project name" }, command: { type: "string", description: "Command to execute (no shell metacharacters)" }, role: { type: "string", description: "Role to exec on" } }, required: %w[project command]), tool_def("harbor_details", "Get container and proxy details for a project", { project: { type: "string", description: "Project name" }, destination: { type: "string", description: "Destination" } }, required: ["project"]), tool_def("harbor_audit", "View deployment audit log", { project: { type: "string", description: "Project name (omit for all)" }, limit: { type: "integer", description: "Number of entries (default 20)" } }), tool_def("harbor_list_servers", "List all servers across projects", { project: { type: "string", description: "Filter by project name" } }), tool_def("harbor_maintenance", "Toggle maintenance mode", { project: { type: "string", description: "Project name" }, enabled: { type: "boolean", description: "true to enable, false to disable" } }, required: %w[project enabled]), tool_def("harbor_app_version", "Get current deployed version", { project: { type: "string", description: "Project name" }, destination: { type: "string", description: "Destination" } }, required: ["project"]), tool_def("harbor_runner", "Execute Ruby code in the Rails environment via 'rails runner'. Use for queries, one-off tasks, inspecting state.", { project: { type: "string", description: "Project name" }, script: { type: "string", description: "Ruby code to execute (e.g. 'puts User.count', 'pp Order.last(5).pluck(:id, :status)')" }, destination: { type: "string", description: "Destination" } }, required: %w[project script]), tool_def("harbor_rails", "Run a Rails command (migrate, routes, runner, seed, db:migrate:status, etc.)", { project: { type: "string", description: "Project name" }, command: { type: "string", description: "Rails subcommand: migrate, migrate:status, rollback, seed, routes, runner, or any rails command" }, args: { type: "string", description: "Extra arguments (e.g. script for runner, grep pattern for routes)" }, destination: { type: "string", description: "Destination" } }, required: %w[project command]), tool_def("harbor_ci", "Run the local test suite for a project. Returns pass/fail with output. Deploy is gated on CI passing via KAMAL pre-build hook.", { project: { type: "string", description: "Project name" }, test_command: { type: "string", description: "Custom test command (default: auto-detect from project — bin/rails test, bundle exec rspec, etc.)" } }, required: ["project"]), tool_def("harbor_ship", "Full ship pipeline: run CI locally, push branch, create PR, wait for review, merge. If deploy_on_merge is true (default), GitHub Actions deploys automatically after merge. Otherwise deploys directly.", { project: { type: "string", description: "Project name" }, branch: { type: "string", description: "Branch name (default: current branch)" }, base: { type: "string", description: "Base branch to merge into (default: main)" }, title: { type: "string", description: "PR title" }, body: { type: "string", description: "PR body/description" }, skip_review_wait: { type: "boolean", description: "Skip waiting for review approval (default: false)" }, review_timeout: { type: "integer", description: "Seconds to wait for review approval (default: 300 = 5 min)" }, deploy_on_merge: { type: "boolean", description: "If true (default), skip direct deploy — GitHub Actions deploys on merge to main. Set false to deploy directly after merge." }, destination: { type: "string", description: "KAMAL deploy destination" } }, required: %w[project title]), tool_def("harbor_setup_autodeploy", "Set up automatic deploy-on-merge for a project. SSHes into the server, clones the repo, installs the webhook service, and configures the GitHub webhook. Like Coolify auto-deploy but with KAMAL.", { project: { type: "string", description: "Project name (must be registered with Harbor)" }, repo: { type: "string", description: "GitHub repo (e.g. 'rebulk/edgedeploy'). Auto-detected from git remote if omitted." }, branch: { type: "string", description: "Branch to deploy on push (default: main)" }, server_app_dir: { type: "string", description: "Where to clone the repo on the server (default: /opt/<project-name>)" } }, required: ["project"]), tool_def("harbor_diagnose", "Diagnose a failed deploy or unhealthy app. Gathers deploy output, recent error logs, health check results, and recent exceptions into a structured report the agent can act on.", { project: { type: "string", description: "Project name" }, deployment_output: { type: "string", description: "Paste the deploy error output here (optional, pulls fresh logs if omitted)" }, destination: { type: "string", description: "Destination" } }, required: ["project"]), tool_def("harbor_fix_and_redeploy", "After fixing code based on harbor_diagnose output: commit, push, and redeploy. Tracks retry count. Gives up after max_retries.", { project: { type: "string", description: "Project name" }, fix_description: { type: "string", description: "What was fixed (for commit message and audit trail)" }, max_retries: { type: "integer", description: "Max deploy attempts (default: 3)" }, attempt: { type: "integer", description: "Current attempt number (default: 1, auto-incremented)" }, destination: { type: "string", description: "Destination" } }, required: %w[project fix_description]) ] end |