UNPKG

vmsnap

Version:

A Node based backup and backup rotation tool for KVM domains.

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