| 1 | import type { nodes } from "@struxa/db"; |
| 2 | import { safeDecrypt } |
| 3 | |
| 4 | type NodeRecord |
| 5 | |
| 6 | interface CreateServerPayload { |
| 7 | uuid: string; |
| 8 | start_on_completion: boolean; |
| 9 | environment: Record |
| 10 | limits: { |
| 11 | cpu: number |
| 12 | memory: number |
| 13 | disk: number |
| 14 | swap: number |
| 15 | io: number |
| 16 | threads: string |
| 17 | oom_disabled: boolean |
| 18 | }; |
| 19 | container: { |
| 20 | image: string |
| 21 | oom_killer: boolean |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | interface BackupPayload { |
| 26 | uuid: string; |
| 27 | ignore: string; |
| 28 | adapter: string; |
| 29 | } |
| 30 | |
| 31 | class WingsError extends |
| 32 | constructor( |
| 33 | public status: |
| 34 | message: string, |
| 35 | ) { |
| 36 | super(message); |
| 37 | this.name = |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | export class WingsClient |
| 42 | private readonly baseUrl |
| 43 | private readonly authHeader |
| 44 | |
| 45 | constructor(node: |
| 46 | this.baseUrl = |
| 47 | this.authHeader = |
| 48 | } |
| 49 | |
| 50 | private async request |
| 51 | method: string, |
| 52 | path: string, |
| 53 | body?: unknown |
| 54 | ): Promise<Response |
| 55 | const res = |
| 56 | method, |
| 57 | headers: { |
| 58 | Authorization: this.authHeader |
| 59 | "Content-Type": "application/json", |
| 60 | Accept: "application/json", |
| 61 | "User-Agent": "Struxa/1.0", |
| 62 | }, |
| 63 | ...(body !== |
| 64 | }); |
| 65 | if (!res.ok |
| 66 | const body = |
| 67 | throw new WingsError( |
| 68 | } |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | async createServer(payload |
| 73 | await this.request |
| 74 | } |
| 75 | |
| 76 | async deleteServer(uuid |
| 77 | await this.request |
| 78 | } |
| 79 | |
| 80 | async sendPowerAction( |
| 81 | uuid: string, |
| 82 | action: "start |
| 83 | ): Promise<void |
| 84 | await this.request |
| 85 | } |
| 86 | |
| 87 | async reinstallServer(uuid |
| 88 | await this.request |
| 89 | } |
| 90 | |
| 91 | async createBackup(uuid |
| 92 | await this.request |
| 93 | } |
| 94 | |
| 95 | async deleteBackup(uuid |
| 96 | await this.request |
| 97 | } |
| 98 | |
| 99 | async restoreBackup( |
| 100 | uuid: string, |
| 101 | backupUuid: string, |
| 102 | options?: { |
| 103 | adapter?: string |
| 104 | download_url?: string |
| 105 | truncate_directory?: boolean |
| 106 | }, |
| 107 | ): Promise<void |
| 108 | await this.request |
| 109 | "POST", |
| 110 | `/api/servers/${uuid}/backup/${ |
| 111 | { |
| 112 | adapter: options?.adapter |
| 113 | ...(options?.download_url |
| 114 | truncate_directory: options?.truncate_directory |
| 115 | }, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | async sendCommands(uuid |
| 120 | await this.request |
| 121 | } |
| 122 | |
| 123 | async getUtilization(): |
| 124 | const res = |
| 125 | return res.json |
| 126 | } |
| 127 | |
| 128 | async getSystemInfo(): |
| 129 | const res = |
| 130 | return res.json |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | export function createWingsClient(node |
| 135 | return new WingsClient |
| 136 | } |