2023-03-15 21:17:51 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2015-11-21 09:27:06 +08:00
|
|
|
import Backup from "admin/models/backup";
|
|
|
|
import BackupStatus from "admin/models/backup-status";
|
2019-10-22 21:46:10 +08:00
|
|
|
import DiscourseRoute from "discourse/routes/discourse";
|
2023-03-15 21:17:51 +08:00
|
|
|
import EmberObject, { action } from "@ember/object";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2020-05-07 03:28:06 +08:00
|
|
|
import PreloadStore from "discourse/lib/preload-store";
|
2019-11-13 00:38:51 +08:00
|
|
|
import User from "discourse/models/user";
|
2016-07-01 01:55:44 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2021-01-20 13:04:21 +08:00
|
|
|
import { extractError } from "discourse/lib/ajax-error";
|
2020-06-04 00:45:26 +08:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2015-03-11 03:01:15 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2022-12-12 23:32:25 +08:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
|
|
|
|
2015-03-13 00:12:23 +08:00
|
|
|
const LOG_CHANNEL = "/admin/backups/logs";
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
export default class AdminBackupsRoute extends DiscourseRoute {
|
|
|
|
@service dialog;
|
2023-07-19 03:52:55 +08:00
|
|
|
@service router;
|
2022-09-14 23:06:56 +08:00
|
|
|
|
2015-03-11 03:01:15 +08:00
|
|
|
activate() {
|
2022-12-12 23:32:25 +08:00
|
|
|
this.messageBus.subscribe(LOG_CHANNEL, this.onMessage);
|
2023-03-15 21:17:51 +08:00
|
|
|
}
|
2022-12-12 23:32:25 +08:00
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.messageBus.unsubscribe(LOG_CHANNEL, this.onMessage);
|
2023-03-15 21:17:51 +08:00
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2015-03-11 03:01:15 +08:00
|
|
|
model() {
|
2019-01-24 00:40:24 +08:00
|
|
|
return PreloadStore.getAndRemove("operations_status", () =>
|
|
|
|
ajax("/admin/backups/status.json")
|
|
|
|
).then((status) =>
|
|
|
|
BackupStatus.create({
|
2014-02-13 12:35:46 +08:00
|
|
|
isOperationRunning: status.is_operation_running,
|
2014-08-29 05:02:26 +08:00
|
|
|
canRollback: status.can_rollback,
|
|
|
|
allowRestore: status.allow_restore,
|
2019-01-24 00:40:24 +08:00
|
|
|
})
|
|
|
|
);
|
2023-03-15 21:17:51 +08:00
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2022-12-12 23:32:25 +08:00
|
|
|
@bind
|
|
|
|
onMessage(log) {
|
|
|
|
if (log.message === "[STARTED]") {
|
|
|
|
User.currentProp("hideReadOnlyAlert", true);
|
|
|
|
this.controllerFor("adminBackups").set("model.isOperationRunning", true);
|
|
|
|
this.controllerFor("adminBackupsLogs").get("logs").clear();
|
|
|
|
} else if (log.message === "[FAILED]") {
|
|
|
|
this.controllerFor("adminBackups").set("model.isOperationRunning", false);
|
|
|
|
this.dialog.alert(
|
|
|
|
I18n.t("admin.backups.operations.failed", {
|
|
|
|
operation: log.operation,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (log.message === "[SUCCESS]") {
|
|
|
|
User.currentProp("hideReadOnlyAlert", false);
|
|
|
|
this.controllerFor("adminBackups").set("model.isOperationRunning", false);
|
|
|
|
if (log.operation === "restore") {
|
|
|
|
// redirect to homepage when the restore is done (session might be lost)
|
|
|
|
window.location = getURL("/");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.controllerFor("adminBackupsLogs")
|
|
|
|
.get("logs")
|
|
|
|
.pushObject(EmberObject.create(log));
|
|
|
|
}
|
2023-03-15 21:17:51 +08:00
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
showStartBackupModal() {
|
|
|
|
showModal("admin-start-backup", { admin: true });
|
|
|
|
}
|
2014-08-21 00:48:56 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
startBackup(withUploads) {
|
2023-07-19 03:52:55 +08:00
|
|
|
this.router.transitionTo("admin.backups.logs");
|
2023-03-15 21:17:51 +08:00
|
|
|
Backup.start(withUploads).then((result) => {
|
|
|
|
if (!result.success) {
|
|
|
|
this.dialog.alert(result.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
destroyBackup(backup) {
|
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.backups.operations.destroy.confirm"),
|
|
|
|
didConfirm: () => {
|
|
|
|
backup
|
|
|
|
.destroy()
|
|
|
|
.then(() =>
|
|
|
|
this.controllerFor("adminBackupsIndex")
|
|
|
|
.get("model")
|
|
|
|
.removeObject(backup)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
startRestore(backup) {
|
|
|
|
this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.backups.operations.restore.confirm"),
|
|
|
|
didConfirm: () => {
|
2023-07-19 03:52:55 +08:00
|
|
|
this.router.transitionTo("admin.backups.logs");
|
2023-03-15 21:17:51 +08:00
|
|
|
backup.restore();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
cancelOperation() {
|
|
|
|
this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.backups.operations.cancel.confirm"),
|
|
|
|
didConfirm: () => {
|
|
|
|
Backup.cancel().then(() => {
|
|
|
|
this.controllerFor("adminBackups").set(
|
|
|
|
"model.isOperationRunning",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
rollback() {
|
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.backups.operations.rollback.confirm"),
|
|
|
|
didConfirm: () => {
|
|
|
|
Backup.rollback().then((result) => {
|
|
|
|
if (!result.success) {
|
|
|
|
this.dialog.alert(result.message);
|
|
|
|
} else {
|
|
|
|
// redirect to homepage (session might be lost)
|
|
|
|
window.location = getURL("/");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2014-02-22 08:41:01 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
uploadSuccess(filename) {
|
|
|
|
this.dialog.alert(I18n.t("admin.backups.upload.success", { filename }));
|
|
|
|
}
|
2014-02-22 08:41:01 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
uploadError(filename, message) {
|
|
|
|
this.dialog.alert(
|
|
|
|
I18n.t("admin.backups.upload.error", { filename, message })
|
|
|
|
);
|
|
|
|
}
|
2018-10-15 09:43:31 +08:00
|
|
|
|
2023-03-15 21:17:51 +08:00
|
|
|
@action
|
|
|
|
remoteUploadSuccess() {
|
|
|
|
Backup.find()
|
|
|
|
.then((backups) => backups.map((backup) => Backup.create(backup)))
|
|
|
|
.then((backups) => {
|
|
|
|
this.controllerFor("adminBackupsIndex").set(
|
|
|
|
"model",
|
|
|
|
backups.map((backup) => Backup.create(backup))
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.dialog.alert(
|
|
|
|
I18n.t("admin.backups.backup_storage_error", {
|
|
|
|
error_message: extractError(error),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|