discourse/app/assets/javascripts/admin/addon/routes/admin-backups.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
4.9 KiB
JavaScript
Raw Normal View History

import Backup from "admin/models/backup";
import BackupStatus from "admin/models/backup-status";
import DiscourseRoute from "discourse/routes/discourse";
import EmberObject from "@ember/object";
import I18n from "I18n";
import PreloadStore from "discourse/lib/preload-store";
import User from "discourse/models/user";
2016-06-30 13:55:44 -04:00
import { ajax } from "discourse/lib/ajax";
import { extractError } from "discourse/lib/ajax-error";
import getURL from "discourse-common/lib/get-url";
import showModal from "discourse/lib/show-modal";
import { inject as service } from "@ember/service";
const LOG_CHANNEL = "/admin/backups/logs";
2014-02-12 23:35:46 -05:00
export default DiscourseRoute.extend({
dialog: service(),
activate() {
2018-01-31 06:05:06 -05:00
this.messageBus.subscribe(LOG_CHANNEL, (log) => {
if (log.message === "[STARTED]") {
User.currentProp("hideReadOnlyAlert", true);
2018-01-31 06:05:06 -05:00
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(
2018-01-31 06:05:06 -05:00
I18n.t("admin.backups.operations.failed", {
operation: log.operation,
})
);
} else if (log.message === "[SUCCESS]") {
User.currentProp("hideReadOnlyAlert", false);
2018-01-31 06:05:06 -05:00
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("/");
2018-01-31 06:05:06 -05:00
}
} else {
this.controllerFor("adminBackupsLogs")
.get("logs")
.pushObject(EmberObject.create(log));
2014-02-12 23:35:46 -05:00
}
2018-01-31 06:05:06 -05:00
});
2014-02-12 23:35:46 -05:00
},
model() {
return PreloadStore.getAndRemove("operations_status", () =>
ajax("/admin/backups/status.json")
).then((status) =>
BackupStatus.create({
2014-02-12 23:35:46 -05:00
isOperationRunning: status.is_operation_running,
canRollback: status.can_rollback,
allowRestore: status.allow_restore,
})
);
2014-02-12 23:35:46 -05:00
},
deactivate() {
this.messageBus.unsubscribe(LOG_CHANNEL);
2014-02-12 23:35:46 -05:00
},
actions: {
showStartBackupModal() {
2016-11-15 17:09:55 -05:00
showModal("admin-start-backup", { admin: true });
2014-08-20 12:48:56 -04:00
this.controllerFor("modal").set("modalClass", "start-backup-modal");
},
startBackup(withUploads) {
2014-08-20 12:48:56 -04:00
this.transitionTo("admin.backups.logs");
Backup.start(withUploads).then((result) => {
if (!result.success) {
this.dialog.alert(result.message);
}
});
2014-02-12 23:35:46 -05:00
},
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-12 23:35:46 -05:00
},
startRestore(backup) {
this.dialog.yesNoConfirm({
message: I18n.t("admin.backups.operations.restore.confirm"),
didConfirm: () => {
this.transitionTo("admin.backups.logs");
backup.restore();
},
});
2014-02-12 23:35:46 -05:00
},
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-12 23:35:46 -05:00
},
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-12 23:35:46 -05:00
},
2014-02-21 19:41:01 -05:00
uploadSuccess(filename) {
this.dialog.alert(I18n.t("admin.backups.upload.success", { filename }));
2014-02-21 19:41:01 -05:00
},
uploadError(filename, message) {
this.dialog.alert(
I18n.t("admin.backups.upload.error", { filename, message })
2014-02-21 19:41:01 -05:00
);
},
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 [];
});
},
2014-02-12 23:35:46 -05:00
},
});