🔨 Add auto start/stop addons before/after backup

This commit is contained in:
Sebastien Clement 2021-02-10 15:22:30 +01:00
parent aa3805abf2
commit eaf926f42e
6 changed files with 161 additions and 38 deletions

View File

@ -1,9 +1,11 @@
# Error Codes
- `1` => Fail to downlaod snap
- `1` => Fail to download snap
- `2` => Nextcloud config invalid
- `3` => Can't connect to nextcloud
- `3` => Can't connect to Nextcloud
- `4` => Upload snap fail
- `5` => Fail create new snap
- `6` => Fail to clean
- `7` => Fail to Download snap
- `7` => Fail to download snap
- `8` => Fail to stop addon
- `9` => Fail to start addon

View File

@ -126,6 +126,14 @@ function update_status() {
printStatusWithBar('Creating Snapshot', data.progress);
buttons.addClass("disabled");
break;
case "stopping":
printStatusWithBar('Stopping addons', data.progress);
buttons.addClass("disabled");
break;
case "starting":
printStatusWithBar('Starting addons', data.progress);
buttons.addClass("disabled");
break;
}
if (data.last_backup != null) {
let last_back_status = $('#last_back_status');

View File

@ -142,32 +142,36 @@ router.post("/manual-backup", function (req, res, next) {
router.post("/new-backup", function (req, res, next) {
let status = statusTools.getStatus();
if (status.status === "creating" && status.status === "upload" && status.status === "download") {
if (status.status === "creating" || status.status === "upload" || status.status === "download" || status.status === "stopping" || status.status === "starting") {
res.status(503);
res.send();
return;
}
hassioApiTools
.getVersion()
.then((version) => {
let name = settingsTools.getFormatedName(true, version);
hassioApiTools
.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar").catch();
})
.catch(() => {
hassioApiTools.stopAddons()
.then(() => {
hassioApiTools.getVersion()
.then((version) => {
let name = settingsTools.getFormatedName(true, version);
hassioApiTools.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar")
.then(() => {
hassioApiTools.startAddons().catch(() => {
})
});
});
});
})
.catch(() => {
});
})
.catch(() => {
hassioApiTools.startAddons().catch(() => {
});
});
res.status(201);
res.send();
});

View File

@ -83,21 +83,31 @@ class CronContainer {
_createBackup() {
logger.debug("Cron triggered !");
let status = statusTools.getStatus();
if (status.status === "creating" || status.status === "upload" || status.status === "download") return;
hassioApiTools
.getVersion()
.then((version) => {
let name = settingsTools.getFormatedName(false, version)
hassioApiTools
.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + ".tar").catch();
}).catch();
}).catch();
}).catch();
if (status.status === "creating" || status.status === "upload" || status.status === "download" || status.status === "stopping" || status.status === "starting")
return;
hassioApiTools.stopAddons()
.then(() => {
hassioApiTools.getVersion()
.then((version) => {
let name = settingsTools.getFormatedName(false, version);
hassioApiTools.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + ".tar")
.then(() => {
hassioApiTools.startAddons().catch(() => {
});
});
});
});
});
})
.catch(() => {
hassioApiTools.startAddons().catch(() => {
});
});
}
_clean() {

View File

@ -284,7 +284,7 @@ function createNewBackup(name) {
folders: folders
},
};
if(settingsTools.getSettings().password_protected === "true"){
if (settingsTools.getSettings().password_protected === "true") {
option.json.password = settingsTools.getSettings().password_protect_value
}
@ -393,7 +393,7 @@ function uploadSnapshot(path) {
fs.unlinkSync(path);
status.status = "error";
status.error_code = 4;
status.message = `Fail to upload backup to home assitant (${err}) !`;
status.message = `Fail to upload backup to home assistant (${err}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
@ -401,6 +401,99 @@ function uploadSnapshot(path) {
});
}
function stopAddons() {
return new Promise(((resolve, reject) => {
logger.info('Stopping addons...')
let status = statusTools.getStatus();
status.status = "stopping";
status.progress = -1;
status.message = null;
status.error_code = null;
statusTools.setStatus(status);
let promises = [];
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "X-HASSIO-KEY": token },
responseType: "json",
};
let addons_slug = settingsTools.getSettings().auto_stop_addon
for (let addon of addons_slug) {
if (addon !== "") {
logger.debug(`... Stopping addon ${addon}`)
promises.push(got.post(`http://hassio/addons/${addon}/stop`, option));
}
}
Promise.allSettled(promises).then(values => {
let error = null;
for (let val of values)
if (val.status === "rejected")
error = val.reason;
if (error) {
status.status = "error";
status.error_code = 8;
status.message = `Fail to stop addons(${error}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
} else {
logger.info('... Ok')
resolve();
}
});
}));
}
function startAddons() {
return new Promise(((resolve, reject) => {
logger.info('Starting addons...');
let status = statusTools.getStatus();
status.status = "starting";
status.progress = -1;
status.message = null;
status.error_code = null;
statusTools.setStatus(status);
let promises = [];
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "X-HASSIO-KEY": token },
responseType: "json",
};
let addons_slug = settingsTools.getSettings().auto_stop_addon
for (let addon of addons_slug) {
if (addon !== "") {
logger.debug(`... Starting addon ${addon}`)
promises.push(got.post(`http://hassio/addons/${addon}/start`, option));
}
}
Promise.allSettled(promises).then(values => {
let error = null;
for (let val of values)
if (val.status === "rejected")
error = val.reason;
if (error) {
let status = statusTools.getStatus();
status.status = "error";
status.error_code = 9;
status.message = `Fail to start addons (${error}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
} else {
logger.info('... Ok')
status.status = "idle";
status.progress = -1;
status.message = null;
status.error_code = null;
statusTools.setStatus(status);
resolve();
}
});
}));
}
exports.getVersion = getVersion;
exports.getAddonList = getAddonList;
exports.getFolderList = getFolderList;
@ -408,4 +501,6 @@ exports.getSnapshots = getSnapshots;
exports.downloadSnapshot = downloadSnapshot;
exports.createNewBackup = createNewBackup;
exports.uploadSnapshot = uploadSnapshot;
exports.stopAddons = stopAddons;
exports.startAddons = startAddons;
exports.clean = clean;

View File

@ -194,13 +194,17 @@ class WebdavTools {
if (this.client == null) {
this.confIsValid()
.then(() => {
this._startUpload(id, path).catch((err) => reject(err));
this._startUpload(id, path)
.then(()=> resolve())
.catch((err) => reject(err));
})
.catch((err) => {
reject(err);
});
} else
this._startUpload(id, path).catch((err) => reject(err));
this._startUpload(id, path)
.then(()=> resolve())
.catch((err) => reject(err));
});
}