netmaker/database/database.go

190 lines
4.6 KiB
Go
Raw Normal View History

2021-07-21 05:18:45 +08:00
package database
import (
2021-07-26 04:42:07 +08:00
"encoding/json"
"errors"
2021-10-09 03:07:12 +08:00
"log"
"time"
2021-10-13 03:44:19 +08:00
"github.com/gravitl/netmaker/servercfg"
2021-07-21 05:18:45 +08:00
)
2021-10-13 03:44:19 +08:00
// NETWORKS_TABLE_NAME - networks table
2021-07-21 05:18:45 +08:00
const NETWORKS_TABLE_NAME = "networks"
2021-10-13 03:44:19 +08:00
// NODES_TABLE_NAME - nodes table
2021-07-21 05:18:45 +08:00
const NODES_TABLE_NAME = "nodes"
2021-10-13 03:44:19 +08:00
// DELETED_NODES_TABLE_NAME - deleted nodes table
2021-08-03 06:06:26 +08:00
const DELETED_NODES_TABLE_NAME = "deletednodes"
2021-10-13 03:44:19 +08:00
// USERS_TABLE_NAME - users table
2021-07-21 05:18:45 +08:00
const USERS_TABLE_NAME = "users"
2021-10-13 03:44:19 +08:00
// DNS_TABLE_NAME - dns table
2021-07-21 05:18:45 +08:00
const DNS_TABLE_NAME = "dns"
2021-10-13 03:44:19 +08:00
// EXT_CLIENT_TABLE_NAME - ext client table
2021-07-21 05:18:45 +08:00
const EXT_CLIENT_TABLE_NAME = "extclients"
2021-10-13 03:44:19 +08:00
// INT_CLIENTS_TABLE_NAME - int client table
2021-07-21 05:18:45 +08:00
const INT_CLIENTS_TABLE_NAME = "intclients"
2021-10-13 03:44:19 +08:00
// PEERS_TABLE_NAME - peers table
2021-07-29 10:14:06 +08:00
const PEERS_TABLE_NAME = "peers"
2021-10-13 03:44:19 +08:00
// SERVERCONF_TABLE_NAME
const SERVERCONF_TABLE_NAME = "serverconf"
// DATABASE_FILENAME - database file name
2021-07-21 05:18:45 +08:00
const DATABASE_FILENAME = "netmaker.db"
2021-10-22 08:32:23 +08:00
// GENERATED_TABLE_NAME - stores server generated k/v
const GENERATED_TABLE_NAME = "generated"
2021-08-03 06:06:26 +08:00
// == ERROR CONSTS ==
2021-10-13 03:44:19 +08:00
// NO_RECORD - no singular result found
2021-08-03 06:06:26 +08:00
const NO_RECORD = "no result found"
2021-10-13 03:44:19 +08:00
// NO_RECORDS - no results found
2021-08-03 06:06:26 +08:00
const NO_RECORDS = "could not find any records"
// == Constants ==
2021-10-13 03:44:19 +08:00
// INIT_DB - initialize db
const INIT_DB = "init"
2021-10-13 03:44:19 +08:00
// CREATE_TABLE - create table const
const CREATE_TABLE = "createtable"
2021-10-13 03:44:19 +08:00
// INSERT - insert into db const
const INSERT = "insert"
2021-10-13 03:44:19 +08:00
// INSERT_PEER - insert peer into db const
const INSERT_PEER = "insertpeer"
2021-10-13 03:44:19 +08:00
// DELETE - delete db record const
const DELETE = "delete"
2021-10-13 03:44:19 +08:00
// DELETE_ALL - delete a table const
const DELETE_ALL = "deleteall"
2021-10-13 03:44:19 +08:00
// FETCH_ALL - fetch table contents const
const FETCH_ALL = "fetchall"
2021-10-13 03:44:19 +08:00
// CLOSE_DB - graceful close of db const
const CLOSE_DB = "closedb"
func getCurrentDB() map[string]interface{} {
switch servercfg.GetDB() {
case "rqlite":
return RQLITE_FUNCTIONS
case "sqlite":
return SQLITE_FUNCTIONS
2021-10-09 01:11:31 +08:00
case "postgres":
return PG_FUNCTIONS
default:
return SQLITE_FUNCTIONS
}
}
2021-07-21 05:18:45 +08:00
func InitializeDatabase() error {
2021-10-22 03:28:58 +08:00
log.Println("[netmaker] connecting to", servercfg.GetDB())
tperiod := time.Now().Add(10 * time.Second)
for {
if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
2021-10-22 03:28:58 +08:00
log.Println("[netmaker] unable to connect to db, retrying . . .")
if time.Now().After(tperiod) {
return err
}
} else {
break
}
time.Sleep(2 * time.Second)
2021-07-21 05:18:45 +08:00
}
createTables()
return nil
}
func createTables() {
createTable(NETWORKS_TABLE_NAME)
createTable(NODES_TABLE_NAME)
2021-08-03 06:06:26 +08:00
createTable(DELETED_NODES_TABLE_NAME)
2021-07-21 05:18:45 +08:00
createTable(USERS_TABLE_NAME)
createTable(DNS_TABLE_NAME)
createTable(EXT_CLIENT_TABLE_NAME)
createTable(INT_CLIENTS_TABLE_NAME)
2021-07-29 10:14:06 +08:00
createTable(PEERS_TABLE_NAME)
2021-10-13 03:44:19 +08:00
createTable(SERVERCONF_TABLE_NAME)
2021-10-22 08:32:23 +08:00
createTable(GENERATED_TABLE_NAME)
2021-07-21 05:18:45 +08:00
}
func createTable(tableName string) error {
return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
2021-07-21 05:18:45 +08:00
}
2021-10-13 03:44:19 +08:00
// IsJSONString - checks if valid json
func IsJSONString(value string) bool {
2021-07-26 04:42:07 +08:00
var jsonInt interface{}
return json.Unmarshal([]byte(value), &jsonInt) == nil
}
2021-10-13 03:44:19 +08:00
// Insert - inserts object into db
2021-07-21 05:18:45 +08:00
func Insert(key string, value string, tableName string) error {
if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
2021-07-26 04:42:07 +08:00
} else {
return errors.New("invalid insert " + key + " : " + value)
2021-07-29 10:14:06 +08:00
}
}
2021-10-13 03:44:19 +08:00
// InsertPeer - inserts peer into db
2021-07-29 10:14:06 +08:00
func InsertPeer(key string, value string) error {
if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
2021-07-29 10:14:06 +08:00
} else {
return errors.New("invalid peer insert " + key + " : " + value)
2021-07-21 05:18:45 +08:00
}
}
2021-10-13 03:44:19 +08:00
// DeleteRecord - deletes a record from db
2021-07-21 05:18:45 +08:00
func DeleteRecord(tableName string, key string) error {
return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
2021-07-21 05:18:45 +08:00
}
2021-10-13 03:44:19 +08:00
// DeleteAllRecords - removes a table and remakes
2021-07-21 05:18:45 +08:00
func DeleteAllRecords(tableName string) error {
err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
2021-07-21 05:18:45 +08:00
if err != nil {
return err
}
err = createTable(tableName)
if err != nil {
return err
}
return nil
}
2021-10-13 03:44:19 +08:00
// FetchRecord - fetches a record
2021-07-21 05:18:45 +08:00
func FetchRecord(tableName string, key string) (string, error) {
results, err := FetchRecords(tableName)
if err != nil {
return "", err
}
2021-08-03 06:06:26 +08:00
if results[key] == "" {
return "", errors.New(NO_RECORD)
}
2021-07-21 05:18:45 +08:00
return results[key], nil
}
2021-10-13 03:44:19 +08:00
// FetchRecords - fetches all records in given table
2021-07-21 05:18:45 +08:00
func FetchRecords(tableName string) (map[string]string, error) {
return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
}
2021-10-13 03:44:19 +08:00
// CloseDB - closes a database gracefully
func CloseDB() {
getCurrentDB()[CLOSE_DB].(func())()
2021-07-21 05:18:45 +08:00
}