Class: Harbor::CoolifyAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/coolify_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, config: nil) ⇒ CoolifyAdapter

Returns a new instance of CoolifyAdapter.

Raises:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/harbor/coolify_adapter.rb', line 11

def initialize(project, config: nil)
  @project = project
  @config = config
  @base_url = project_setting("coolify_url") || ENV["COOLIFY_URL"]
  @token = project_setting("coolify_token") || ENV["COOLIFY_TOKEN"]
  @app_uuid = project_setting("coolify_app_uuid")

  raise ConfigError, "coolify_url not configured for project '#{project.name}'" unless @base_url
  raise ConfigError, "coolify_token not configured" unless @token
  raise ConfigError, "coolify_app_uuid not configured for project '#{project.name}'" unless @app_uuid
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/harbor/coolify_adapter.rb', line 9

def project
  @project
end

Class Method Details

.create_application(coolify_url:, token:, server_uuid:, project_uuid:, environment_name:, git_repository:, git_branch: "main", build_pack: "nixpacks", ports_exposes: "3000", domains: nil, instant_deploy: true) ⇒ Object

Coolify-specific: create an application



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
# File 'lib/harbor/coolify_adapter.rb', line 206

def self.create_application(coolify_url:, token:, server_uuid:, project_uuid:,
                             environment_name:, git_repository:, git_branch: "main",
                             build_pack: "nixpacks", ports_exposes: "3000",
                             domains: nil, instant_deploy: true)
  uri = URI("#{coolify_url}/api/v1/applications/public")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  body = {
    server_uuid: server_uuid,
    project_uuid: project_uuid,
    environment_name: environment_name,
    git_repository: git_repository,
    git_branch: git_branch,
    build_pack: build_pack,
    ports_exposes: ports_exposes,
    instant_deploy: instant_deploy
  }
  body[:domains] = domains if domains

  request = Net::HTTP::Post.new(uri.path)
  request["Authorization"] = "Bearer #{token}"
  request["Content-Type"] = "application/json"
  request.body = JSON.generate(body)

  response = http.request(request)
  JSON.parse(response.body)
end

Instance Method Details

#app_version(destination: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/harbor/coolify_adapter.rb', line 64

def app_version(destination: nil)
  response = api_get("/api/v1/applications/#{@app_uuid}")
  data = JSON.parse(response.body)
  version = data["git_commit_sha"] || data["current_process_id"] || "unknown"
  Result.new(
    stdout: "Version: #{version}\nBranch: #{data['git_branch']}\nStatus: #{data['status']}",
    stderr: "", success: true, exit_code: 0, timed_out: false
  )
rescue => e
  error_result(e)
end

#audit_log(destination: nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/harbor/coolify_adapter.rb', line 190

def audit_log(destination: nil)
  response = api_get("/api/v1/deployments?application_uuid=#{@app_uuid}")
  if response.is_a?(Net::HTTPSuccess)
    data = JSON.parse(response.body)
    lines = data.map do |d|
      "#{d['created_at']} | #{d['status']} | #{d['commit_sha']&.slice(0, 8)} | #{d['commit_message']&.slice(0, 50)}"
    end
    Result.new(stdout: lines.join("\n"), stderr: "", success: true, exit_code: 0, timed_out: false)
  else
    error_result(StandardError.new(response.body))
  end
rescue => e
  error_result(e)
end

#deploy(destination: nil, skip_push: false, timeout_seconds: nil) ⇒ Object

Write operations



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/harbor/coolify_adapter.rb', line 78

def deploy(destination: nil, skip_push: false, timeout_seconds: nil)
  response = api_get("/api/v1/deploy?uuid=#{@app_uuid}&force=#{skip_push ? 'false' : 'true'}")
  if response.is_a?(Net::HTTPSuccess)
    data = JSON.parse(response.body)
    deployment_uuid = data.dig("deployments", 0, "deployment_uuid") || "unknown"
    Result.new(
      stdout: "Deploy triggered. Deployment UUID: #{deployment_uuid}\n" \
              "Coolify is building and deploying. Check status in the Coolify dashboard.",
      stderr: "", success: true, exit_code: 0, timed_out: false
    )
  else
    Result.new(
      stdout: "", stderr: "Deploy failed: #{response.body}",
      success: false, exit_code: 1, timed_out: false
    )
  end
rescue => e
  error_result(e)
end

#details(destination: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/harbor/coolify_adapter.rb', line 53

def details(destination: nil)
  response = api_get("/api/v1/applications/#{@app_uuid}")
  data = JSON.parse(response.body)
  Result.new(
    stdout: JSON.pretty_generate(data),
    stderr: "", success: true, exit_code: 0, timed_out: false
  )
rescue => e
  error_result(e)
end

#exec_command(command, role: nil, destination: nil, timeout_seconds: nil) ⇒ Object



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
# File 'lib/harbor/coolify_adapter.rb', line 152

def exec_command(command, role: nil, destination: nil, timeout_seconds: nil)
  # Coolify executes commands via the server API
  # We need the server UUID and container name
  app_response = api_get("/api/v1/applications/#{@app_uuid}")
  app_data = JSON.parse(app_response.body)
  server_uuid = app_data["server_uuid"] || app_data.dig("destination", "server", "uuid")

  response = api_post("/api/v1/servers/#{server_uuid}/command", {
    command: "docker exec $(docker ps --filter label=coolify.applicationId=#{@app_uuid} -q | head -1) #{command}"
  })

  if response.is_a?(Net::HTTPSuccess)
    Result.new(
      stdout: response.body, stderr: "",
      success: true, exit_code: 0, timed_out: false
    )
  else
    Result.new(
      stdout: "", stderr: "Exec failed: #{response.body}",
      success: false, exit_code: 1, timed_out: false
    )
  end
rescue => e
  error_result(e)
end

#logs(role: nil, lines: 100, grep: nil, destination: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/harbor/coolify_adapter.rb', line 36

def logs(role: nil, lines: 100, grep: nil, destination: nil)
  response = api_get("/api/v1/applications/#{@app_uuid}/logs")
  output = response.body
  if grep && !output.empty?
    output = output.lines.select { |l| l.include?(grep) }.join
  end
  if lines
    output = output.lines.last(lines).join
  end
  Result.new(
    stdout: output, stderr: "",
    success: response.is_a?(Net::HTTPSuccess), exit_code: 0, timed_out: false
  )
rescue => e
  error_result(e)
end

#maintenance(enabled:, destination: nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/harbor/coolify_adapter.rb', line 178

def maintenance(enabled:, destination: nil)
  # Coolify: stop or start the application
  endpoint = enabled ? "stop" : "start"
  response = api_get("/api/v1/applications/#{@app_uuid}/#{endpoint}")
  Result.new(
    stdout: "Maintenance mode #{enabled ? 'enabled' : 'disabled'} (app #{endpoint}ped)",
    stderr: "", success: response.is_a?(Net::HTTPSuccess), exit_code: 0, timed_out: false
  )
rescue => e
  error_result(e)
end

#rollback(version:, destination: nil, timeout_seconds: nil) ⇒ Object



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
# File 'lib/harbor/coolify_adapter.rb', line 98

def rollback(version:, destination: nil, timeout_seconds: nil)
  # Coolify rollback strategy:
  # 1. Restart the app (fast — uses existing container/image, no rebuild)
  # 2. If a specific version is requested, set git_commit_sha and redeploy
  #    (slow — triggers full rebuild, but guarantees the exact version)

  if version == "restart" || version == "previous"
    # Fast restart — Coolify restarts the current container
    response = api_get("/api/v1/applications/#{@app_uuid}/restart")
    if response.is_a?(Net::HTTPSuccess)
      Result.new(
        stdout: "App restarted via Coolify. This restarts the current container (no rebuild).\n" \
                "If you need a specific version, use: harbor_rollback(version: \"<git-sha>\")",
        stderr: "", success: true, exit_code: 0, timed_out: false
      )
    else
      Result.new(
        stdout: "", stderr: "Restart failed: #{response.body}",
        success: false, exit_code: 1, timed_out: false
      )
    end
  else
    # Full version rollback — set commit SHA and redeploy
    patch_response = api_patch("/api/v1/applications/#{@app_uuid}", {
      git_commit_sha: version
    })
    if patch_response.is_a?(Net::HTTPSuccess)
      deploy_response = api_get("/api/v1/deploy?uuid=#{@app_uuid}&force=true")
      if deploy_response.is_a?(Net::HTTPSuccess)
        data = JSON.parse(deploy_response.body)
        deployment_uuid = data.dig("deployments", 0, "deployment_uuid") || "unknown"
        Result.new(
          stdout: "Rolling back to #{version}. Coolify is rebuilding from that commit.\n" \
                  "Deployment UUID: #{deployment_uuid}\n" \
                  "This is a full rebuild — takes longer than a restart.",
          stderr: "", success: true, exit_code: 0, timed_out: false
        )
      else
        Result.new(
          stdout: "", stderr: "Rollback deploy failed: #{deploy_response.body}",
          success: false, exit_code: 1, timed_out: false
        )
      end
    else
      Result.new(
        stdout: "", stderr: "Failed to set rollback version: #{patch_response.body}",
        success: false, exit_code: 1, timed_out: false
      )
    end
  end
rescue => e
  error_result(e)
end

#status(destination: nil) ⇒ Object

Read operations



25
26
27
28
29
30
31
32
33
34
# File 'lib/harbor/coolify_adapter.rb', line 25

def status(destination: nil)
  response = api_get("/api/v1/applications/#{@app_uuid}")
  data = JSON.parse(response.body)
  Result.new(
    stdout: format_app_status(data),
    stderr: "", success: true, exit_code: 0, timed_out: false
  )
rescue => e
  error_result(e)
end