Compare commits

..

No commits in common. "f3c0b6787ab310160e463b0cea1212117bfde3bb" and "143eecbcf90ea28b4d85b4cb74dfd9953de79ad2" have entirely different histories.

4 changed files with 106 additions and 59 deletions

View File

@ -160,9 +160,9 @@ router.post("/new-backup", function (req, res, next) {
hassioApiTools.startAddons().catch(() => { hassioApiTools.startAddons().catch(() => {
}) })
}); });
}).catch(()=>{}); });
}).catch(()=>{}); });
}).catch(()=>{});; });
}) })
.catch(() => { .catch(() => {
hassioApiTools.startAddons().catch(() => { hassioApiTools.startAddons().catch(() => {

View File

@ -13,7 +13,7 @@ const pipeline = promisify(stream.pipeline);
// Default timout to 90min // Default timout to 90min
const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || (90 * 60 * 1000); const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || ( 90 * 60 * 1000 );
function getVersion() { function getVersion() {
@ -37,8 +37,12 @@ function getVersion() {
resolve(version); resolve(version);
}) })
.catch((error) => { .catch((error) => {
statusTools.setError(`Fail to fetch HA Version (${error.message})`, 1); status.status = "error";
reject(`Fail to fetch HA Version (${error.message})`); status.message = "Fail to fetch HA Version (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
}); });
}); });
} }
@ -75,8 +79,12 @@ function getAddonList() {
resolve(installed); resolve(installed);
}) })
.catch((error) => { .catch((error) => {
statusTools.setError(`Fail to fetch addons list (${error.message})`, 1); status.status = "error";
reject(`Fail to fetch addons list (${error.message})`); status.message = "Fail to fetch addons list (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
}); });
}); });
} }
@ -158,8 +166,12 @@ function getSnapshots() {
resolve(snaps); resolve(snaps);
}) })
.catch((error) => { .catch((error) => {
statusTools.setError(`Fail to fetch Hassio backups (${error.message})`, 1); status.status = "error";
reject(`Fail to fetch Hassio backups (${error.message})`); status.message = "Fail to fetch Hassio snapshots (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
}); });
}); });
} }
@ -201,12 +213,20 @@ function downloadSnapshot(id) {
}) })
.catch((err) => { .catch((err) => {
fs.unlinkSync(tmp_file); fs.unlinkSync(tmp_file);
statusTools.setError(`Fail to download Hassio backup (${error.message})`, 7); status.status = "error";
reject(`Fail to download Hassio backup (${error.message})`); status.message = "Fail to download Hassio snapshot (" + err.message + ")";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
reject(err.message);
}); });
}) })
.catch((err) => { .catch((err) => {
statusTools.setError("Fail to download Hassio backup. Not found ?", 7); status.status = "error";
status.message = "Fail to download Hassio snapshot. Not found ?";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
reject(); reject();
}); });
}); });
@ -285,8 +305,12 @@ function createNewBackup(name) {
resolve(result.body.data.slug); resolve(result.body.data.slug);
}) })
.catch((error) => { .catch((error) => {
statusTools.setError(`Can't create new snapshot (${error.message})`, 5); status.status = "error";
reject(`Can't create new snapshot (${error.message})`); status.message = "Can't create new snapshot (" + error.message + ")";
status.error_code = 5;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
}); });
}).catch(reject); }).catch(reject);
@ -316,9 +340,8 @@ function clean() {
logger.info("Local clean done."); logger.info("Local clean done.");
resolve(); resolve();
}) })
.catch((e) => { .catch(() => {
statusTools.setError(`Fail to clean backups (${e}) !`, 6); reject();
reject(`Fail to clean backups (${e}) !`);
}); });
}); });
} }
@ -379,15 +402,19 @@ function uploadSnapshot(path) {
}) })
.on("error", (err) => { .on("error", (err) => {
fs.unlinkSync(path); fs.unlinkSync(path);
statusTools.setError(`Fail to upload backup to home assistant (${err}) !`, 4); status.status = "error";
reject(`Fail to upload backup to home assistant (${err}) !`); status.error_code = 4;
status.message = `Fail to upload backup to home assistant (${err}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
}); });
}); });
} }
function stopAddons() { function stopAddons() {
return new Promise(((resolve, reject) => { return new Promise(((resolve, reject) => {
logger.info('Stopping addons...'); logger.info('Stopping addons...')
let status = statusTools.getStatus(); let status = statusTools.getStatus();
status.status = "stopping"; status.status = "stopping";
status.progress = -1; status.progress = -1;
@ -403,7 +430,7 @@ function stopAddons() {
let addons_slug = settingsTools.getSettings().auto_stop_addon let addons_slug = settingsTools.getSettings().auto_stop_addon
for (let addon of addons_slug) { for (let addon of addons_slug) {
if (addon !== "") { if (addon !== "") {
logger.debug(`... Stopping addon ${addon}`); logger.debug(`... Stopping addon ${addon}`)
promises.push(got.post(`http://hassio/addons/${addon}/stop`, option)); promises.push(got.post(`http://hassio/addons/${addon}/stop`, option));
} }
@ -415,11 +442,14 @@ function stopAddons() {
error = val.reason; error = val.reason;
if (error) { if (error) {
statusTools.setError(`Fail to stop addons(${error}) !`, 8); status.status = "error";
status.error_code = 8;
status.message = `Fail to stop addons(${error}) !`;
statusTools.setStatus(status);
logger.error(status.message); logger.error(status.message);
reject(status.message); reject(status.message);
} else { } else {
logger.info('... Ok'); logger.info('... Ok')
resolve(); resolve();
} }
}); });
@ -455,7 +485,12 @@ function startAddons() {
error = val.reason; error = val.reason;
if (error) { if (error) {
statusTools.setError(`Fail to start addons (${error}) !`, 9) 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); reject(status.message);
} else { } else {
logger.info('... Ok') logger.info('... Ok')
@ -470,7 +505,7 @@ function startAddons() {
})); }));
} }
function publish_state(state) { function publish_state(state){
// let data_error_sensor = { // let data_error_sensor = {
// state: state.status == "error" ? "on" : "off", // state: state.status == "error" ? "on" : "off",
@ -540,16 +575,16 @@ function publish_state(state) {
// }); // });
} }
export { export {
getVersion, getVersion,
getAddonList, getAddonList,
getFolderList, getFolderList,
getSnapshots, getSnapshots,
downloadSnapshot, downloadSnapshot,
createNewBackup, createNewBackup,
uploadSnapshot, uploadSnapshot,
stopAddons, stopAddons,
startAddons, startAddons,
clean, clean,
publish_state publish_state
} }

View File

@ -1,38 +1,45 @@
import fs from "fs"
import * as hassioApiTools from "./hassioApiTools.js"; import * as hassioApiTools from "./hassioApiTools.js";
import logger from "../config/winston.js"
const statusPath = "/data/status.json";
let status = { let baseStatus = {
status: "idle", status: "idle",
last_backup: null, last_backup: null,
next_backup: null, next_backup: null,
}; };
export function init() { export function init() {
if (status.status !== "idle") { if (!fs.existsSync(statusPath)) {
status.status = "idle"; fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
status.message = null; } else {
let content = getStatus();
if (content.status !== "idle") {
content.status = "idle";
content.message = null;
setStatus(content);
}
} }
} }
export function getStatus() { export function getStatus() {
return status; if (!fs.existsSync(statusPath)) {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
}
return JSON.parse(fs.readFileSync(statusPath).toString());
} }
export function setStatus(new_state) { export function setStatus(state) {
let old_state_str = JSON.stringify(status); if (fs.existsSync(statusPath)) {
if(old_state_str !== JSON.stringify(new_state)){ let old_state_str = fs.readFileSync(statusPath).toString();
status = new_state; if(old_state_str !== JSON.stringify(state)){
hassioApiTools.publish_state(status); fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
}
}else{
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
} }
}
export function setError(message, error_code){
// Check if we don't have another error stored
if (status.status != "error") {
status.status = "error"
status.message = message
status.error_code = error_code;
}
logger.error(message);
} }

View File

@ -70,7 +70,12 @@ class WebdavTools {
}); });
} }
__cant_connect_status(err){ __cant_connect_status(err){
statusTools.setError(`Can't connect to Nextcloud (${err})`, 3); let status = statusTools.getStatus();
status.status = "error";
status.error_code = 3;
status.message = "Can't connect to Nextcloud (" + err + ") !";
statusTools.setStatus(status);
logger.error("Can't connect to Nextcloud (" + err + ") !");
} }
async __createRoot() { async __createRoot() {