✏️ Some code clean...

This commit is contained in:
SebClem 2021-01-07 16:37:30 +01:00
parent f34154a4f1
commit d85c04755e
6 changed files with 329 additions and 333 deletions

View File

@ -1,8 +1,7 @@
const appRoot = require("app-root-path");
const winston = require("winston"); const winston = require("winston");
const logger = winston.createLogger({ const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info' , level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine( format: winston.format.combine(
winston.format.timestamp({ winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss", format: "YYYY-MM-DD HH:mm:ss",

View File

@ -1,16 +1,12 @@
const settingsTools = require("./settingsTools"); const CronJob = require("cron").CronJob;
const settingsTools = require("./settingsTools");
const WebdavTools = require("./webdavTools"); const WebdavTools = require("./webdavTools");
const webdav = new WebdavTools().getInstance(); const webdav = new WebdavTools().getInstance();
const hassioApiTools = require("./hassioApiTools"); const hassioApiTools = require("./hassioApiTools");
const statusTools = require("./status"); const statusTools = require("./status");
const pathTools = require("./pathTools"); const pathTools = require("./pathTools");
const CronJob = require("cron").CronJob;
const moment = require("moment");
const logger = require("../config/winston"); const logger = require("../config/winston");
function startCron() { function startCron() {
@ -100,21 +96,26 @@ class CronContainer {
.then(() => { .then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + ".tar"); webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + ".tar");
}) })
.catch(() => {}); .catch(() => {
});
}) })
.catch(() => {}); .catch(() => {
});
}) })
.catch(() => {}); .catch(() => {
});
} }
_clean() { _clean() {
let autoCleanlocal = settingsTools.getSettings().auto_clean_local; let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
if (autoCleanlocal != null && autoCleanlocal === "true") { if (autoCleanlocal != null && autoCleanlocal === "true") {
hassioApiTools.clean().catch(() => {}); hassioApiTools.clean().catch(() => {
});
} }
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup; let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
if (autoCleanCloud != null && autoCleanCloud === "true") { if (autoCleanCloud != null && autoCleanCloud === "true") {
webdav.clean().catch(() => {}); webdav.clean().catch(() => {
});
} }
} }
} }

View File

@ -14,7 +14,6 @@ const logger = require("../config/winston");
const create_snap_timeout = 90 * 60 * 1000; const create_snap_timeout = 90 * 60 * 1000;
function getVersion() { function getVersion() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN; let token = process.env.HASSIO_TOKEN;
@ -64,19 +63,19 @@ function getAddonList() {
statusTools.setStatus(status); statusTools.setStatus(status);
} }
let addons = result.body.data.addons; let addons = result.body.data.addons;
let instaled = []; let installed = [];
for(let index in addons){ for (let index in addons) {
let current = addons[index]; let current = addons[index];
if(current.installed == true){ if (current.installed === true) {
instaled.push({slug:current.slug, name: current.name}) installed.push({ slug: current.slug, name: current.name })
} }
} }
instaled.sort((a,b)=>{ installed.sort((a, b) => {
var textA = a.name.toUpperCase(); let textA = a.name.toUpperCase();
var textB = b.name.toUpperCase(); let textB = b.name.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
}); });
resolve(instaled); resolve(installed);
}) })
.catch((error) => { .catch((error) => {
status.status = "error"; status.status = "error";
@ -89,21 +88,23 @@ function getAddonList() {
}); });
} }
function getAddonToBackup(){ function getAddonToBackup() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let excluded_addon = settingsTools.getSettings().exclude_addon; let excluded_addon = settingsTools.getSettings().exclude_addon;
getAddonList().then((all_addon)=>{ getAddonList()
.then((all_addon) => {
let slugs = []; let slugs = [];
for(let i in all_addon){ for (let i in all_addon) {
if(!excluded_addon.includes(all_addon[i].slug)) if (!excluded_addon.includes(all_addon[i].slug))
slugs.push(all_addon[i].slug) slugs.push(all_addon[i].slug)
} }
resolve(slugs) resolve(slugs)
}).catch(()=>reject()); })
.catch(() => reject());
}); });
} }
function getFolderList(){ function getFolderList() {
return [ return [
{ {
name: "Homme Assistant configuration", name: "Homme Assistant configuration",
@ -129,12 +130,12 @@ function getFolderList(){
] ]
} }
function getFolderToBackup(){ function getFolderToBackup() {
let excluded_folder = settingsTools.getSettings().exclude_folder; let excluded_folder = settingsTools.getSettings().exclude_folder;
let all_folder = getFolderList() let all_folder = getFolderList()
let slugs = []; let slugs = [];
for(let i in all_folder){ for (let i in all_folder) {
if(!excluded_folder.includes(all_folder[i].slug)) if (!excluded_folder.includes(all_folder[i].slug))
slugs.push(all_folder[i].slug) slugs.push(all_folder[i].slug)
} }
return slugs; return slugs;
@ -189,9 +190,10 @@ function downloadSnapshot(id) {
}; };
pipeline( pipeline(
got.stream.get(`http://hassio/snapshots/${id}/download`, option).on("downloadProgress", (e) => { got.stream.get(`http://hassio/snapshots/${id}/download`, option)
.on("downloadProgress", (e) => {
let percent = Math.round(e.percent * 100) / 100; let percent = Math.round(e.percent * 100) / 100;
if (status.progress != percent) { if (status.progress !== percent) {
status.progress = percent; status.progress = percent;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
@ -238,12 +240,8 @@ function dellSnap(id) {
}; };
got.post(`http://hassio/snapshots/${id}/remove`, option) got.post(`http://hassio/snapshots/${id}/remove`, option)
.then((result) => { .then(() => resolve())
resolve(); .catch(() => reject());
})
.catch((error) => {
reject();
});
}) })
.catch(() => { .catch(() => {
reject(); reject();
@ -264,9 +262,7 @@ function checkSnap(id) {
logger.debug(`Snapshot size: ${result.body.data.size}`); logger.debug(`Snapshot size: ${result.body.data.size}`);
resolve(); resolve();
}) })
.catch((error) => { .catch(() => reject());
reject();
});
}); });
} }
@ -278,7 +274,7 @@ function createNewBackup(name) {
statusTools.setStatus(status); statusTools.setStatus(status);
logger.info("Creating new snapshot..."); logger.info("Creating new snapshot...");
let token = process.env.HASSIO_TOKEN; let token = process.env.HASSIO_TOKEN;
getAddonToBackup().then((addons)=>{ getAddonToBackup().then((addons) => {
let folders = getFolderToBackup(); let folders = getFolderToBackup();
let option = { let option = {
headers: { "X-HASSIO-KEY": token }, headers: { "X-HASSIO-KEY": token },
@ -322,7 +318,8 @@ function clean() {
} }
snaps.sort((a, b) => { snaps.sort((a, b) => {
if (moment(a.date).isBefore(moment(b.date))) return 1; if (moment(a.date).isBefore(moment(b.date))) return 1;
else return -1; else
return -1;
}); });
let toDel = snaps.slice(limit); let toDel = snaps.slice(limit);
for (let i in toDel) { for (let i in toDel) {
@ -363,7 +360,7 @@ function uploadSnapshot(path) {
.post(`http://hassio/snapshots/new/upload`, options) .post(`http://hassio/snapshots/new/upload`, options)
.on("uploadProgress", (e) => { .on("uploadProgress", (e) => {
let percent = e.percent; let percent = e.percent;
if (status.progress != percent) { if (status.progress !== percent) {
status.progress = percent; status.progress = percent;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
@ -372,7 +369,7 @@ function uploadSnapshot(path) {
} }
}) })
.on("response", (res) => { .on("response", (res) => {
if (res.statusCode != 200) { if (res.statusCode !== 200) {
status.status = "error"; status.status = "error";
status.error_code = 4; status.error_code = 4;
status.message = `Fail to upload backup to home assitant (Status code: ${res.statusCode})!`; status.message = `Fail to upload backup to home assitant (Status code: ${res.statusCode})!`;

View File

@ -1,4 +1,4 @@
let default_root = "/Hassio Backup/"; let default_root = "/Hassio Backup/";
exports.default_root = default_root; exports.default_root = default_root;
exports.manual = "Manual/"; exports.manual = "Manual/";
exports.auto = "Auto/"; exports.auto = "Auto/";

View File

@ -4,7 +4,7 @@ const moment = require('moment')
const settingsPath = "/data/backup_conf.json"; const settingsPath = "/data/backup_conf.json";
function check_cron(conf){ function check_cron(conf) {
if (conf.cron_base != null) { if (conf.cron_base != null) {
if (conf.cron_base === "1" || conf.cron_base === "2" || conf.cron_base === "3") { if (conf.cron_base === "1" || conf.cron_base === "2" || conf.cron_base === "3") {
if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) { if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) {
@ -27,112 +27,104 @@ function check_cron(conf){
} }
function check(conf, fallback = false){ function check(conf, fallback = false) {
let needSave = false; let needSave = false;
if(!check_cron(conf)){ if (!check_cron(conf)) {
if(fallback){ if (fallback) {
logger.warn("Bad value for cron settings, fallback to default ") logger.warn("Bad value for cron settings, fallback to default ")
conf.cron_base = "0"; conf.cron_base = "0";
conf.cron_hour = "00:00", conf.cron_hour = "00:00",
conf.cron_weekday = "0", conf.cron_weekday = "0",
conf.cron_month_day = "1" conf.cron_month_day = "1"
} } else {
else {
logger.error("Bad value for cron settings") logger.error("Bad value for cron settings")
return false; return false;
} }
} }
if(conf.name_template == null){ if (conf.name_template == null) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'name_template', fallback to default ") logger.warn("Bad value for 'name_template', fallback to default ")
conf.name_template = "{type}-{ha_version}-{date}_{hour}" conf.name_template = "{type}-{ha_version}-{date}_{hour}"
} } else {
else {
logger.error("Bad value for 'name_template'") logger.error("Bad value for 'name_template'")
return false; return false;
} }
} }
if(conf.auto_clean_local_keep == null || !/^\d+$/.test(conf.auto_clean_local_keep)){ if (conf.auto_clean_local_keep == null || !/^\d+$/.test(conf.auto_clean_local_keep)) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'auto_clean_local_keep', fallback to 5 ") logger.warn("Bad value for 'auto_clean_local_keep', fallback to 5 ")
conf.auto_clean_local_keep = "5" conf.auto_clean_local_keep = "5"
} } else {
else {
logger.error("Bad value for 'auto_clean_local_keep'") logger.error("Bad value for 'auto_clean_local_keep'")
return false; return false;
} }
} }
if(conf.auto_clean_backup_keep == null || !/^\d+$/.test(conf.auto_clean_backup_keep)){ if (conf.auto_clean_backup_keep == null || !/^\d+$/.test(conf.auto_clean_backup_keep)) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'auto_clean_backup_keep', fallback to 5 ") logger.warn("Bad value for 'auto_clean_backup_keep', fallback to 5 ")
conf.auto_clean_backup_keep = "5" conf.auto_clean_backup_keep = "5"
} } else {
else {
logger.error("Bad value for 'auto_clean_backup_keep'") logger.error("Bad value for 'auto_clean_backup_keep'")
return false; return false;
} }
} }
if(conf.auto_clean_local == null){ if (conf.auto_clean_local == null) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'auto_clean_local', fallback to false ") logger.warn("Bad value for 'auto_clean_local', fallback to false ")
conf.auto_clean_local = "false" conf.auto_clean_local = "false"
} } else {
else {
logger.error("Bad value for 'auto_clean_local'") logger.error("Bad value for 'auto_clean_local'")
return false; return false;
} }
} }
if(conf.auto_clean_backup == null){ if (conf.auto_clean_backup == null) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'auto_clean_backup', fallback to false ") logger.warn("Bad value for 'auto_clean_backup', fallback to false ")
conf.auto_clean_backup = "false" conf.auto_clean_backup = "false"
} } else {
else {
logger.error("Bad value for 'auto_clean_backup'") logger.error("Bad value for 'auto_clean_backup'")
return false; return false;
} }
} }
if(conf.exclude_addon == null){ if (conf.exclude_addon == null) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'exclude_addon', fallback to [] ") logger.warn("Bad value for 'exclude_addon', fallback to [] ")
conf.exclude_addon = [] conf.exclude_addon = []
} } else {
else {
logger.error("Bad value for 'exclude_addon'") logger.error("Bad value for 'exclude_addon'")
return false; return false;
} }
} }
if(conf.exclude_folder == null){ if (conf.exclude_folder == null) {
if(fallback){ if (fallback) {
logger.warn("Bad value for 'exclude_folder', fallback to [] ") logger.warn("Bad value for 'exclude_folder', fallback to [] ")
conf.exclude_folder = [] conf.exclude_folder = []
} } else {
else {
logger.error("Bad value for 'exclude_folder'") logger.error("Bad value for 'exclude_folder'")
return false; return false;
} }
} }
if(!Array.isArray(conf.exclude_folder)){ if (!Array.isArray(conf.exclude_folder)) {
logger.debug("exclude_folder is not array (Empty value), reset..."); logger.debug("exclude_folder is not array (Empty value), reset...");
conf.exclude_folder = [] conf.exclude_folder = []
needSave = true; needSave = true;
} }
if(!Array.isArray(conf.exclude_addon)){ if (!Array.isArray(conf.exclude_addon)) {
logger.debug("exclude_addon is not array (Empty value), reset..."); logger.debug("exclude_addon is not array (Empty value), reset...");
conf.exclude_addon = [] conf.exclude_addon = []
needSave = true; needSave = true;
} }
if(fallback || needSave){ if (fallback || needSave) {
setSettings(conf); setSettings(conf);
} }
return true return true
} }
function getFormatedName(is_manual, ha_version){ function getFormatedName(is_manual, ha_version) {
let setting = getSettings(); let setting = getSettings();
let template = setting.name_template; let template = setting.name_template;
template = template.replace('{type_low}', is_manual ? 'manual' : 'auto'); template = template.replace('{type_low}', is_manual ? 'manual' : 'auto');

View File

@ -36,7 +36,11 @@ class WebdavTools {
this.password = password; this.password = password;
let agent_option = ssl === "true" ? { rejectUnauthorized: accept_selfsigned_cert === "false" } : {}; let agent_option = ssl === "true" ? { rejectUnauthorized: accept_selfsigned_cert === "false" } : {};
try { try {
this.client = createClient(this.baseUrl, { username: username, password: password, httpsAgent: new https.Agent(agent_option) }); this.client = createClient(this.baseUrl, {
username: username,
password: password,
httpsAgent: new https.Agent(agent_option)
});
this.client this.client
.getDirectoryContents("/") .getDirectoryContents("/")
@ -72,6 +76,7 @@ class WebdavTools {
} }
}); });
} }
async __createRoot() { async __createRoot() {
let root_splited = this.getConf().back_dir.split("/").splice(1); let root_splited = this.getConf().back_dir.split("/").splice(1);
let path = "/"; let path = "/";
@ -91,18 +96,17 @@ class WebdavTools {
initFolder() { initFolder() {
return new Promise((resolve) => { return new Promise((resolve) => {
this.__createRoot() this.__createRoot().catch((err) => {
.catch((err) => {
logger.error(err); logger.error(err);
}).then(() => {
this.client.createDirectory(this.getConf().back_dir + pathTools.auto)
.catch(() => {
}) })
.then(() => {
this.client
.createDirectory(this.getConf().back_dir + pathTools.auto)
.catch(() => {})
.then(() => { .then(() => {
this.client this.client
.createDirectory(this.getConf().back_dir + pathTools.manual) .createDirectory(this.getConf().back_dir + pathTools.manual)
.catch(() => {}) .catch(() => {
})
.then(() => { .then(() => {
resolve(); resolve();
}); });
@ -110,6 +114,7 @@ class WebdavTools {
}); });
}); });
} }
/** /**
* Check if theh webdav config is valid, if yes, start init of webdav client * Check if theh webdav config is valid, if yes, start init of webdav client
*/ */
@ -177,7 +182,8 @@ class WebdavTools {
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {
let content = JSON.parse(fs.readFileSync(configPath)); let content = JSON.parse(fs.readFileSync(configPath));
return content; return content;
} else return null; } else
return null;
} }
setConf(conf) { setConf(conf) {
@ -194,7 +200,8 @@ class WebdavTools {
.catch((err) => { .catch((err) => {
reject(err); reject(err);
}); });
} else this._startUpload(id, path); } else
this._startUpload(id, path);
}); });
} }
@ -237,7 +244,7 @@ class WebdavTools {
} }
}) })
.on("response", (res) => { .on("response", (res) => {
if (res.statusCode != 201 && res.statusCode != 204) { if (res.statusCode !== 201 && res.statusCode !== 204) {
status.status = "error"; status.status = "error";
status.error_code = 4; status.error_code = 4;
status.message = `Fail to upload snapshot to nextcloud (Status code: ${res.statusCode})!`; status.message = `Fail to upload snapshot to nextcloud (Status code: ${res.statusCode})!`;
@ -255,11 +262,11 @@ class WebdavTools {
statusTools.setStatus(status); statusTools.setStatus(status);
cleanTempFolder(); cleanTempFolder();
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup; let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
if (autoCleanCloud != null && autoCleanCloud == "true") { if (autoCleanCloud != null && autoCleanCloud === "true") {
this.clean().catch(); this.clean().catch();
} }
let autoCleanlocal = settingsTools.getSettings().auto_clean_local; let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
if (autoCleanlocal != null && autoCleanlocal == "true") { if (autoCleanlocal != null && autoCleanlocal === "true") {
hassioApiTools.clean(); hassioApiTools.clean();
} }
resolve(); resolve();
@ -296,6 +303,7 @@ class WebdavTools {
.catch(() => reject()); .catch(() => reject());
}); });
} }
_startDownload(path) { _startDownload(path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
@ -321,23 +329,22 @@ class WebdavTools {
logger.debug(`...URI: ${encodeURI(this.baseUrl.replace(this.host, 'host.hiden') + path)}`); logger.debug(`...URI: ${encodeURI(this.baseUrl.replace(this.host, 'host.hiden') + path)}`);
logger.debug(`...rejectUnauthorized: ${options["https"]["rejectUnauthorized"]}`); logger.debug(`...rejectUnauthorized: ${options["https"]["rejectUnauthorized"]}`);
pipeline( pipeline(
got.stream.get(encodeURI(this.baseUrl + path), options).on("downloadProgress", (e) => { got.stream.get(encodeURI(this.baseUrl + path), options)
.on("downloadProgress", (e) => {
let percent = Math.round(e.percent * 100) / 100; let percent = Math.round(e.percent * 100) / 100;
if (status.progress != percent) { if (status.progress !== percent) {
status.progress = percent; status.progress = percent;
statusTools.setStatus(status); statusTools.setStatus(status);
} }
}), }),
stream stream
) ).then((res) => {
.then((res) => {
logger.info("Download success !"); logger.info("Download success !");
status.progress = 1; status.progress = 1;
statusTools.setStatus(status); statusTools.setStatus(status);
logger.debug("Backup dl size : " + fs.statSync(tmpFile).size / 1024 / 1024); logger.debug("Backup dl size : " + fs.statSync(tmpFile).size / 1024 / 1024);
resolve(tmpFile); resolve(tmpFile);
}) }).catch((err) => {
.catch((err) => {
if (fs.existsSync(tmpFile)) fs.unlinkSync(tmpFile); if (fs.existsSync(tmpFile)) fs.unlinkSync(tmpFile);
status.status = "error"; status.status = "error";
status.message = "Fail to download Hassio snapshot (" + err.message + ")"; status.message = "Fail to download Hassio snapshot (" + err.message + ")";
@ -358,12 +365,8 @@ class WebdavTools {
} }
this.client this.client
.getDirectoryContents(path) .getDirectoryContents(path)
.then((contents) => { .then((contents) => resolve(contents))
resolve(contents); .catch((error) => reject(error));
})
.catch((error) => {
reject(error);
});
}); });
} }
@ -378,8 +381,10 @@ class WebdavTools {
return; return;
} }
contents.sort((a, b) => { contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod))) return 1; if (moment(a.lastmod).isBefore(moment(b.lastmod)))
else return -1; return 1;
else
return -1;
}); });
let toDel = contents.slice(limit); let toDel = contents.slice(limit);
@ -403,11 +408,13 @@ class WebdavTools {
function cleanTempFolder() { function cleanTempFolder() {
fs.readdir("./temp/", (err, files) => { fs.readdir("./temp/", (err, files) => {
if (err) throw err; if (err)
throw err;
for (const file of files) { for (const file of files) {
fs.unlink(path.join("./temp/", file), (err) => { fs.unlink(path.join("./temp/", file), (err) => {
if (err) throw err; if (err)
throw err;
}); });
} }
}); });