2024-10-19 05:25:26 -06:00
|
|
|
import { SSHConfig, SSHSocketClient } from "./SSHSocketClient"
|
2024-07-21 14:18:14 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Interface for the response structure from Dokku commands
|
2024-07-21 14:18:14 -04:00
|
|
|
export interface DokkuResponse {
|
2024-10-19 05:25:26 -06:00
|
|
|
ok: boolean
|
|
|
|
output: string
|
2024-07-21 14:18:14 -04:00
|
|
|
}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// DokkuClient class extends SSHSocketClient to interact with Dokku via SSH
|
2024-07-21 14:18:14 -04:00
|
|
|
export class DokkuClient extends SSHSocketClient {
|
|
|
|
constructor(config: SSHConfig) {
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize with Dokku daemon socket path
|
2024-10-19 05:25:26 -06:00
|
|
|
super(config, "/var/run/dokku-daemon/dokku-daemon.sock")
|
2024-07-21 14:18:14 -04:00
|
|
|
}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Send a command to Dokku and parse the response
|
2024-07-21 14:18:14 -04:00
|
|
|
async sendCommand(command: string): Promise<DokkuResponse> {
|
|
|
|
try {
|
2024-10-19 05:25:26 -06:00
|
|
|
const response = await this.sendData(command)
|
2024-07-21 14:18:14 -04:00
|
|
|
|
|
|
|
if (typeof response !== "string") {
|
2024-10-19 05:25:26 -06:00
|
|
|
throw new Error("Received data is not a string")
|
2024-07-21 14:18:14 -04:00
|
|
|
}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Parse the JSON response from Dokku
|
2024-10-19 05:25:26 -06:00
|
|
|
return JSON.parse(response)
|
2024-07-21 14:18:14 -04:00
|
|
|
} catch (error: any) {
|
2024-10-19 05:25:26 -06:00
|
|
|
throw new Error(`Failed to send command: ${error.message}`)
|
2024-07-21 14:18:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// List all deployed Dokku apps
|
2024-07-21 14:18:14 -04:00
|
|
|
async listApps(): Promise<string[]> {
|
2024-10-19 05:25:26 -06:00
|
|
|
const response = await this.sendCommand("apps:list")
|
2024-10-19 15:16:24 -06:00
|
|
|
// Split the output by newline and remove the header
|
|
|
|
return response.output.split("\n").slice(1)
|
2024-07-21 14:18:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 05:25:26 -06:00
|
|
|
export { SSHConfig }
|