refactor(SQL): Now has seperate config
This commit is contained in:
parent
c0f93788fb
commit
67af246a61
3 changed files with 132 additions and 66 deletions
|
|
@ -19,33 +19,39 @@ type MariaDBConnector struct {
|
||||||
DbPassword string
|
DbPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMariaDBInstance(config Tools.Config) MariaDBConnector {
|
func GetMariaDBInstance(sqlConfig Tools.SQLConfig) MariaDBConnector {
|
||||||
var mariadb MariaDBConnector
|
var mariadb MariaDBConnector
|
||||||
|
|
||||||
mariadb.Address = config.SQLConfig.SqlAddress
|
mariadb.Address = sqlConfig.SqlAddress
|
||||||
mariadb.Port = config.SQLConfig.SqlPort
|
mariadb.Port = sqlConfig.SqlPort
|
||||||
mariadb.Database = config.SQLConfig.Database
|
mariadb.Database = sqlConfig.Database
|
||||||
mariadb.DbUser = config.SQLConfig.DbUser
|
mariadb.DbUser = sqlConfig.DbUser
|
||||||
mariadb.DbPassword = config.SQLConfig.DbPassword
|
mariadb.DbPassword = sqlConfig.DbPassword
|
||||||
|
|
||||||
return mariadb
|
return mariadb
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfEventLogTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
func checkIfEventLogTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
||||||
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'EventLog';", mariadb.Database)
|
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'EventLog';", mariadb.Database)
|
||||||
if !rows.Next(){ return false }
|
if !rows.Next() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
||||||
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'Backups';", mariadb.Database)
|
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'Backups';", mariadb.Database)
|
||||||
if !rows.Next(){ return false }
|
if !rows.Next() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string, hostname string) bool {
|
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string, hostname string) bool {
|
||||||
rows, _ := db.Query("SELECT * FROM `"+mariadb.Database+"`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName)
|
rows, _ := db.Query("SELECT * FROM `"+mariadb.Database+"`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName)
|
||||||
if !rows.Next(){ return false; }
|
if !rows.Next() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,33 +13,38 @@ type SQLService interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDefaultTables(sqlService SQLService) {
|
func CreateDefaultTables(sqlService SQLService) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.createDefaultTables()
|
sqlService.createDefaultTables()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSQLInstance() SQLService {
|
func GetSQLInstance() SQLService {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
|
|
||||||
if !config.SQLConfig.EnableSQL { return nil }
|
if !sqlConfig.EnableSQL {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
switch config.SQLConfig.SqlType {
|
switch sqlConfig.SqlType {
|
||||||
case "mariadb": {return GetMariaDBInstance(config)}
|
case "mariadb":
|
||||||
|
{
|
||||||
|
return GetMariaDBInstance(sqlConfig)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ package Tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type SQLConfig struct {
|
||||||
SQLConfig struct {
|
|
||||||
EnableSQL bool `json:"enableSQL"`
|
EnableSQL bool `json:"enableSQL"`
|
||||||
SqlType string `json:"sqlType"`
|
SqlType string `json:"sqlType"`
|
||||||
SqlAddress string `json:"sql-address"`
|
SqlAddress string `json:"sql-address"`
|
||||||
|
|
@ -15,11 +15,15 @@ type Config struct {
|
||||||
Database string `json:"database"`
|
Database string `json:"database"`
|
||||||
DbUser string `json:"db-user"`
|
DbUser string `json:"db-user"`
|
||||||
DbPassword string `json:"db-password"`
|
DbPassword string `json:"db-password"`
|
||||||
} `json:"sqlConfig"`
|
}
|
||||||
|
|
||||||
type AzureConfig struct {
|
type AzureConfig struct {
|
||||||
FileshareName string `json:"fileshareName"`
|
FileshareName string `json:"fileshareName"`
|
||||||
StorageAccountName string `json:"storageAccountName"`
|
StorageAccountName string `json:"storageAccountName"`
|
||||||
StorageAccountKey string `json:"storageAccountKey"`
|
StorageAccountKey string `json:"storageAccountKey"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
FolderToBackup []struct {
|
FolderToBackup []struct {
|
||||||
BackupName string `json:"backupName"`
|
BackupName string `json:"backupName"`
|
||||||
FolderPath string `json:"folderPath"`
|
FolderPath string `json:"folderPath"`
|
||||||
|
|
@ -40,6 +44,16 @@ func readConfig() []byte {
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readSQLConfig() []byte {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "readSQLConfig")
|
||||||
|
|
||||||
|
file, err := os.ReadFile("config/sql-config.json")
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
func CheckIfConfigExists() {
|
func CheckIfConfigExists() {
|
||||||
logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists")
|
logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists")
|
||||||
|
|
||||||
|
|
@ -48,27 +62,68 @@ func CheckIfConfigExists() {
|
||||||
if fileErr != nil {
|
if fileErr != nil {
|
||||||
logger.Fatal(fileErr)
|
logger.Fatal(fileErr)
|
||||||
}
|
}
|
||||||
generateDefaultConfig()
|
fmt.Printf("No configs detected. Please use 'scabiosa generate-config'\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDefaultConfig() {
|
func GenerateBaseConfig() {
|
||||||
logger := Logging.DetailedLogger("ConfigHandler", "GenerateDefaultConfig")
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateBaseConfig")
|
||||||
|
var baseConfig Config
|
||||||
|
|
||||||
var config Config
|
conf, err := json.MarshalIndent(baseConfig, "", "\t")
|
||||||
var conf []byte
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
conf, err := json.MarshalIndent(config, "", "\t")
|
}
|
||||||
//conf, err := json.Marshal(config)
|
for _, s := range baseConfig.FolderToBackup {
|
||||||
|
s.BackupName = ""
|
||||||
|
}
|
||||||
|
err = os.WriteFile("config/config.json", conf, 0775)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile("config/config.json", conf, 0755)
|
}
|
||||||
|
|
||||||
|
func GenerateAzureConfig(azure AzureConfig) {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateAzureConfig")
|
||||||
|
|
||||||
|
conf, err := json.MarshalIndent(azure, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile("config/azure.json", conf, 0775)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateSQLConfig(sqlConfig SQLConfig) {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateSQLConfig")
|
||||||
|
|
||||||
|
conf, err := json.MarshalIndent(sqlConfig, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile("config/sql-config.json", conf, 0775)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSQLConfig() SQLConfig {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GetSQLConfig")
|
||||||
|
var sqlConfig SQLConfig
|
||||||
|
|
||||||
|
err := json.Unmarshal(readSQLConfig(), &sqlConfig)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig() Config {
|
func GetConfig() Config {
|
||||||
|
|
|
||||||
Reference in a new issue