vmsnap
Version:
A Node based backup and backup rotation tool for KVM domains.
2 lines (1 loc) • 1.64 kB
JavaScript
import{access as f}from"fs/promises";import d from"command-exists";import{ERR_DOMAINS as l,ERR_INVALID_SCRUB_TYPE as m,ERR_REQS as u,ERR_SCRUB as E,ERR_TOO_MANY_COMMANDS as p,logger as i}from"../vmsnap.js";import{cleanupCheckpoints as s,fetchAllDomains as w,VIRSH as R}from"./virsh.js";import{cleanupBitmaps as c,QEMU_IMG as _}from"./qemu-img.js";import{BACKUP as h}from"./libnbdbackup.js";const C="checkpoint",g="bitmap",y="both",D="*",B=async()=>{const e=[R,_,h],t=[];for(const r of e)try{await d(r)}catch{t.push(r)}if(t.length>0)throw new Error(`Missing dependencies (${t.join(", ")})`,u)},S=({status:e,scrub:t,backup:r})=>{let o=0;if(e&&++o,t&&++o,r&&++o,o>1)throw new Error("Only one command can be run at a time",p)},a=async(e,t=async()=>[])=>{let r=[];if(e===void 0||typeof e!="string")return r;if(e.indexOf(",")>-1)r=e.split(",");else if(e==="*")r=await t();else if(typeof e=="string")r.push(e);else throw new Error(`Invalid parameter: ${e}`);return r},P=async e=>{try{return await f(e),!0}catch{return!1}},A=async({domains:e,checkpointName:t,scrubType:r})=>{if(!e)throw new Error("No domains specified",{code:l});i.info("Scrubbing checkpoints and bitmaps");let o=!1;try{for(const n of await a(e,w))i.info(`Scrubbing domain: ${n}`),r===C?await s(n,t):r===g?await c(n,t):r===y?(await s(n,t),await c(n,t)):r==="*"?(await s(n),await c(n)):i.error("No scrub type specified",{code:m});o=!0}catch(n){i.error(n.message,{code:E}),o=!1}return o},b=(e,t)=>{for(const[r,o]of e.entries())if(o===t)return r};export{B as checkDependencies,S as checkCommand,P as fileExists,b as findKeyByValue,a as parseArrayParam,A as scrubCheckpointsAndBitmaps};