2021-08-21 16:11:09 +03:00
import * as fs from "fs" ;
2021-08-21 01:39:29 +03:00
/ * *
* A Collection of settings
* /
2021-08-21 02:26:59 +03:00
export interface ConfigOptions {
2021-08-21 01:39:29 +03:00
/ * *
* The number of nodes that will be ran in parallel ,
* if the number of nodes is smaller than the parallels ,
* it will simply divide them evenly and ignore extra parallels
* /
parallel_nodes : number ,
/ * *
* The amount of tasks that a node parallel can run at once ,
* these basically are mysql_dump processes .
* /
parallel_node_tasks : number ,
/ * *
* This option should be set to ON if you would like the dumps
* to only export tables .
* /
separate_into_tables : boolean ,
2021-08-21 02:26:59 +03:00
/ * *
* A string of CLI options for the mysql dump command
* /
mysql_dump_settings : string ,
2021-08-21 01:39:29 +03:00
}
/ * *
* Basic configuration interface for the mailer
* /
2021-08-21 02:26:59 +03:00
export interface SMTPConfig {
2021-08-21 01:39:29 +03:00
/ * *
* The email address to globally use for sending
* out email from .
* /
email_from : string ,
/ * *
* Email username login details
* /
username : string ,
/ * *
* Email password login details
* /
password : string ,
/ * *
* The SMTP host of the email
* /
host : string ,
/ * *
* The port to send to , common ports are 465 ( ssl ) , 587 for TLS
* /
2021-08-21 02:26:59 +03:00
port : number ,
2021-08-21 01:39:29 +03:00
/ * *
* Secure Connection
* /
ssl : boolean ,
}
2021-08-21 02:26:59 +03:00
export interface NodeConnectionMail {
2021-08-21 01:39:29 +03:00
/ * *
* Enable or disable email sending
* /
enabled : boolean ,
/ * *
* The main user to send out the email to
* /
email_to : string ,
/ * *
* A list of email addresses to CC into the email
* /
email_cc : Array < string > ,
}
2021-08-21 02:26:59 +03:00
export interface DatabaseFilter {
2021-08-21 01:39:29 +03:00
/ * *
* A filter where anything inside of it is going to be ignored
* /
blacklist? : null | Array < string > ,
/ * *
* A filter where anything out of it will be ignored
* /
whitelist? : null | Array < string > ,
}
/ * *
* A Node Connection for the database backup
* /
2021-08-21 02:26:59 +03:00
export interface NodeConnection {
2021-08-21 01:39:29 +03:00
/ * *
* The name for the connection to be identified by
* /
name : string ,
/ * *
* Send out console debug messages
* /
debug : string ,
/ * *
* Keep a log file for this connection independently
* /
log : boolean ,
/ * *
* Mail configuration details
* /
mail? : NodeConnectionMail | null ,
/ * *
* Ip Address of the database
* /
hostname : string ,
/ * *
* The username for the database connection
* /
username : string ,
/ * *
* The password to connect to the database with
* /
password : string ,
2021-08-21 16:11:09 +03:00
/ * *
* The port for the database connection
* /
port : number ,
2021-08-21 01:39:29 +03:00
/ * *
* The database filter , this can contain a blacklist or a whitelist .
* /
databases? : DatabaseFilter
}
2021-08-21 02:26:59 +03:00
export interface ConfigObject {
2021-08-21 01:39:29 +03:00
/ * *
* A list of global settings for the behaviour
* of the exporter
* /
options : ConfigOptions ,
/ * *
* SMTP details for sending out email notifications
* for real - time updates on erros , health , and backups .
* /
smtp : SMTPConfig ,
/ * *
* An array of Node connections that we should backup
* /
nodes : Array < NodeConnection >
}
2021-08-21 16:11:09 +03:00
/ * *
* Get the name of the config file
* /
export function getConfigFileName ( ) : string {
return "config.json" ;
}
/ * *
* The config file can really be in 2 places , in the project or above it , so we ' re going to take a peak in both cases .
* /
function findConfigFile ( ) : string {
const fileName : string = getConfigFileName ( ) ;
// Look in the same folder
if ( fs . existsSync ( fileName ) ) {
return fileName ;
}
// Check outside
if ( fs . existsSync ( fileName ) ) {
return ` ../ ${ fileName } ` ;
}
// Throw an error because the file does not exist.
throw new Error ( "Failed to find the config file, please create a `config.json` file and complete it similar to the provided example file `config.example.json`" ) ;
}
2021-08-21 15:46:21 +03:00
/ * *
* Read the config file from the files of the project
* @returns The configuration object of the project
* /
2021-08-21 16:11:09 +03:00
export function getConfig ( ) : ConfigObject {
2021-08-21 01:39:29 +03:00
2021-08-21 16:11:09 +03:00
// Check for the location of the config file
const fileName : string = findConfigFile ( ) ;
// Initialize the config data object
let configData : ConfigObject ;
// Load in the configuration data from the json file
const rawFileData : string = fs . readFileSync ( fileName , ` utf-8 ` ) ;
// Parse the data
configData = JSON . parse ( rawFileData ) ;
if ( ! ! configData ) {
return configData ;
}
// Throw an error because it has been a failure
throw new Error ( "Failed to load the configuration file." ) ;
2021-08-21 01:39:29 +03:00
}