feat: expose backup endpoint notification
This commit is contained in:
parent
93d3c75035
commit
59841a609a
1 changed files with 28 additions and 2 deletions
|
|
@ -16,6 +16,7 @@ import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import { randomUUID } from "crypto";
|
import { randomUUID } from "crypto";
|
||||||
import { getFile } from "../services/minio";
|
import { getFile } from "../services/minio";
|
||||||
|
import { sendWebSocket } from "../services/socket";
|
||||||
|
|
||||||
function getEnvVar(environmentName: string) {
|
function getEnvVar(environmentName: string) {
|
||||||
const environmentValue = process.env[environmentName];
|
const environmentValue = process.env[environmentName];
|
||||||
|
|
@ -55,9 +56,9 @@ function jsonParseOrPlainText(str: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route("/api/v1/backup")
|
@Route("/api/v1/backup")
|
||||||
@Security("keycloak")
|
|
||||||
export class BackupController extends Controller {
|
export class BackupController extends Controller {
|
||||||
@Get()
|
@Get()
|
||||||
|
@Security("keycloak")
|
||||||
async listBackup() {
|
async listBackup() {
|
||||||
const data = await fetch(
|
const data = await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/run_wait_result/p/${WINDMILL_BACKUP_LIST_SCRIPT_PATH}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/run_wait_result/p/${WINDMILL_BACKUP_LIST_SCRIPT_PATH}`,
|
||||||
|
|
@ -99,6 +100,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("backup-running-list")
|
@Get("backup-running-list")
|
||||||
|
@Security("keycloak")
|
||||||
async runningBackupStatus() {
|
async runningBackupStatus() {
|
||||||
return await fetch(
|
return await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/list?running=true&script_path_exact=${WINDMILL_BACKUP_FLOW_PATH}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/list?running=true&script_path_exact=${WINDMILL_BACKUP_FLOW_PATH}`,
|
||||||
|
|
@ -116,6 +118,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("restore-running-list")
|
@Get("restore-running-list")
|
||||||
|
@Security("keycloak")
|
||||||
async runningRestoreStatus() {
|
async runningRestoreStatus() {
|
||||||
return await fetch(
|
return await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/list?running=true&script_path_exact=${WINDMILL_RESTORE_FLOW_PATH}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/list?running=true&script_path_exact=${WINDMILL_RESTORE_FLOW_PATH}`,
|
||||||
|
|
@ -126,7 +129,11 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("create")
|
@Post("create")
|
||||||
async runBackup(@Body() body?: { name?: string }) {
|
@Security("keycloak")
|
||||||
|
async runBackup(
|
||||||
|
@Request() req: Request & { user: { sub: string } },
|
||||||
|
@Body() body?: { name?: string },
|
||||||
|
) {
|
||||||
const timestamp = Math.round(Date.now() / 1000);
|
const timestamp = Math.round(Date.now() / 1000);
|
||||||
const name =
|
const name =
|
||||||
body?.name && body.name !== "auto-backup"
|
body?.name && body.name !== "auto-backup"
|
||||||
|
|
@ -148,6 +155,7 @@ export class BackupController extends Controller {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
triggerUserId: req.user.sub,
|
||||||
backup_name: name,
|
backup_name: name,
|
||||||
storage: {
|
storage: {
|
||||||
s3_source_endpoint: s3TargetUrl,
|
s3_source_endpoint: s3TargetUrl,
|
||||||
|
|
@ -175,6 +183,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("download/{name}")
|
@Get("download/{name}")
|
||||||
|
@Security("keycloak")
|
||||||
async downloadBackup(
|
async downloadBackup(
|
||||||
@Request() req: express.Request,
|
@Request() req: express.Request,
|
||||||
@Path() name: string,
|
@Path() name: string,
|
||||||
|
|
@ -188,6 +197,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("restore")
|
@Post("restore")
|
||||||
|
@Security("keycloak")
|
||||||
async restoreBackup(@Body() body: { name: string }) {
|
async restoreBackup(@Body() body: { name: string }) {
|
||||||
const listRunning = await this.runningRestoreStatus();
|
const listRunning = await this.runningRestoreStatus();
|
||||||
|
|
||||||
|
|
@ -230,6 +240,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("delete")
|
@Delete("delete")
|
||||||
|
@Security("keycloak")
|
||||||
async deleteBackup(@Body() body: { name: string }) {
|
async deleteBackup(@Body() body: { name: string }) {
|
||||||
await fetch(
|
await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/run/p/${WINDMILL_BACKUP_DELETE_SCRIPT_PATH}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/jobs/run/p/${WINDMILL_BACKUP_DELETE_SCRIPT_PATH}`,
|
||||||
|
|
@ -253,6 +264,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("schedule")
|
@Get("schedule")
|
||||||
|
@Security("keycloak")
|
||||||
async listSchedule() {
|
async listSchedule() {
|
||||||
const result = await fetch(
|
const result = await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/list?path=${WINDMILL_BACKUP_FLOW_PATH}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/list?path=${WINDMILL_BACKUP_FLOW_PATH}`,
|
||||||
|
|
@ -280,6 +292,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("schedule")
|
@Post("schedule")
|
||||||
|
@Security("keycloak")
|
||||||
async createSchedule(
|
async createSchedule(
|
||||||
@Body() body: { name: string; schedule: string; timezone?: string; startAt?: Date },
|
@Body() body: { name: string; schedule: string; timezone?: string; startAt?: Date },
|
||||||
) {
|
) {
|
||||||
|
|
@ -331,6 +344,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("schedule/{id}")
|
@Put("schedule/{id}")
|
||||||
|
@Security("keycloak")
|
||||||
async updateSchedule(
|
async updateSchedule(
|
||||||
@Path() id: string,
|
@Path() id: string,
|
||||||
@Body()
|
@Body()
|
||||||
|
|
@ -401,6 +415,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("schedule/{id}/toggle")
|
@Post("schedule/{id}/toggle")
|
||||||
|
@Security("keycloak")
|
||||||
async toggleSchedule(@Path() id: string) {
|
async toggleSchedule(@Path() id: string) {
|
||||||
return await fetch(
|
return await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/get/f/backup_schedule/${id}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/get/f/backup_schedule/${id}`,
|
||||||
|
|
@ -431,6 +446,7 @@ export class BackupController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("schedule/{id}")
|
@Delete("schedule/{id}")
|
||||||
|
@Security("keycloak")
|
||||||
async deleteSchedule(@Path() id: string) {
|
async deleteSchedule(@Path() id: string) {
|
||||||
return await fetch(
|
return await fetch(
|
||||||
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/delete/f/backup_schedule/${id}`,
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/delete/f/backup_schedule/${id}`,
|
||||||
|
|
@ -443,4 +459,14 @@ export class BackupController extends Controller {
|
||||||
},
|
},
|
||||||
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post("notify-backup")
|
||||||
|
async notifyBackup(
|
||||||
|
@Body() payload: { message: string; userId?: string | string[]; roles?: string | string[] },
|
||||||
|
) {
|
||||||
|
sendWebSocket("backup-notification", payload.message, {
|
||||||
|
roles: payload.roles || [],
|
||||||
|
userId: payload.userId || [],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue