Rework actions to be loaded dynamically
This commit is contained in:
parent
e49f89d4ee
commit
08ab7b4383
22 changed files with 934 additions and 81 deletions
83
utils/wsclient/client/calls.ts
Normal file
83
utils/wsclient/client/calls.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import type { WebSocketCall } from ".";
|
||||
|
||||
/** Backup backups everything */
|
||||
export function Backup(): WebSocketCall {
|
||||
return {
|
||||
'call': 'backup',
|
||||
'params': [],
|
||||
}
|
||||
}
|
||||
|
||||
type ProvisionParams = {
|
||||
Slug: string;
|
||||
Flavor?: "Drupal 10" | "Drupal 9",
|
||||
System: SystemParams
|
||||
}
|
||||
|
||||
type SystemParams = {
|
||||
PHP: "Default (8.1)" | "8.0" | "8.1" | "8.2",
|
||||
OpCacheDevelopment: boolean,
|
||||
ContentSecurityPolicy: string,
|
||||
}
|
||||
|
||||
/** Provision provisions a new instance */
|
||||
export function Provision(params: ProvisionParams): WebSocketCall {
|
||||
return {
|
||||
'call': 'provision',
|
||||
'params': [
|
||||
JSON.stringify(params)
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/** Snapshot makes a snapshot of an instance */
|
||||
export function Snapshot(Slug: string): WebSocketCall {
|
||||
return {
|
||||
'call': 'snapshot',
|
||||
'params': [Slug],
|
||||
}
|
||||
}
|
||||
|
||||
/** Rebuild rebuilds an instance */
|
||||
export function Rebuild(Slug: string, params: SystemParams): WebSocketCall {
|
||||
return {
|
||||
'call': 'rebuild',
|
||||
'params': [
|
||||
Slug,
|
||||
JSON.stringify(params)
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/** Update updates a specific instance */
|
||||
export function Update(Slug: string): WebSocketCall {
|
||||
return {
|
||||
'call': 'update',
|
||||
'params': [Slug],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Start starts a specific instance */
|
||||
export function Start(Slug: string): WebSocketCall {
|
||||
return {
|
||||
'call': 'start',
|
||||
'params': [Slug],
|
||||
}
|
||||
}
|
||||
|
||||
/** Stop stops a specific instance */
|
||||
export function Stop(Slug: string): WebSocketCall {
|
||||
return {
|
||||
'call': 'stop',
|
||||
'params': [Slug],
|
||||
}
|
||||
}
|
||||
|
||||
/** Purge purges a specific instance */
|
||||
export function Purge(Slug: string): WebSocketCall {
|
||||
return {
|
||||
'call': 'purge',
|
||||
'params': [Slug],
|
||||
}
|
||||
}
|
||||
71
utils/wsclient/client/index.ts
Normal file
71
utils/wsclient/client/index.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/** @file implements the websocket protocol used by the distillery */
|
||||
|
||||
import WebSocket from "ws";
|
||||
|
||||
/** A call to the websocket endpoint */
|
||||
export interface WebSocketCall {
|
||||
call: string;
|
||||
params: string[];
|
||||
}
|
||||
|
||||
/** the result of a websocket call */
|
||||
export interface WebSocketResult {
|
||||
success: boolean,
|
||||
message: string,
|
||||
}
|
||||
|
||||
/** optional hooks to call when something happens */
|
||||
export interface Hooks {
|
||||
beforeCall: (call: WebSocketCall) => void; // called right before sending the request
|
||||
afterCall: (call: WebSocketCall, result: WebSocketResult) => void; // called when the socket is closed
|
||||
onError: (call: WebSocketCall, error: any) => void; // called when an error occurs before rejecting the promise
|
||||
onLogLine: (call: WebSocketCall, line: string) => void; // called when a log line is received
|
||||
}
|
||||
|
||||
/** specifies a remote endpoint */
|
||||
export interface Remote {
|
||||
url: string; // the remote websocket url to talk to
|
||||
token?: string; // optional token
|
||||
}
|
||||
|
||||
/** run a websocket remote call */
|
||||
export default async function Call(remote: Remote, call: WebSocketCall, hooks?: Partial<Hooks>): Promise<WebSocketResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let options = { headers: {} };
|
||||
if (remote.token) {
|
||||
options.headers = { 'Authorization': 'Bearer ' + remote.token };
|
||||
}
|
||||
const ws = new WebSocket(remote.url, options);
|
||||
|
||||
let result = {'success': false, 'message': 'Unknown error'};
|
||||
ws.on('error', (err) => {
|
||||
if (hooks && hooks.onError) {
|
||||
hooks.onError(call, err);
|
||||
}
|
||||
reject(err)
|
||||
});
|
||||
ws.on('open', () => {
|
||||
if (hooks && hooks.beforeCall) {
|
||||
hooks.beforeCall(call);
|
||||
}
|
||||
ws.send(Buffer.from(JSON.stringify(call), 'utf8'));
|
||||
});
|
||||
|
||||
ws.on('message', async (msg, isBinary) => {
|
||||
if (!isBinary) {
|
||||
if (hooks && hooks.onLogLine) {
|
||||
hooks.onLogLine(call, msg.toString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
result = JSON.parse(msg.toString());
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
if (hooks && hooks.afterCall) {
|
||||
hooks.afterCall(call, result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue