database.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package database
  2. import (
  3. "encoding/json"
  4. "time"
  5. "errors"
  6. "log"
  7. "github.com/gravitl/netmaker/servercfg"
  8. )
  9. const NETWORKS_TABLE_NAME = "networks"
  10. const NODES_TABLE_NAME = "nodes"
  11. const DELETED_NODES_TABLE_NAME = "deletednodes"
  12. const USERS_TABLE_NAME = "users"
  13. const DNS_TABLE_NAME = "dns"
  14. const EXT_CLIENT_TABLE_NAME = "extclients"
  15. const INT_CLIENTS_TABLE_NAME = "intclients"
  16. const PEERS_TABLE_NAME = "peers"
  17. const DATABASE_FILENAME = "netmaker.db"
  18. // == ERROR CONSTS ==
  19. const NO_RECORD = "no result found"
  20. const NO_RECORDS = "could not find any records"
  21. // == Constants ==
  22. const INIT_DB = "init"
  23. const CREATE_TABLE = "createtable"
  24. const INSERT = "insert"
  25. const INSERT_PEER = "insertpeer"
  26. const DELETE = "delete"
  27. const DELETE_ALL = "deleteall"
  28. const FETCH_ALL = "fetchall"
  29. const CLOSE_DB = "closedb"
  30. func getCurrentDB() map[string]interface{} {
  31. switch servercfg.GetDB() {
  32. case "rqlite":
  33. return RQLITE_FUNCTIONS
  34. case "sqlite":
  35. return SQLITE_FUNCTIONS
  36. default:
  37. return SQLITE_FUNCTIONS
  38. }
  39. }
  40. func InitializeDatabase() error {
  41. log.Println("connecting to",servercfg.GetDB())
  42. tperiod := time.Now().Add(10 * time.Second)
  43. for {
  44. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  45. log.Println("unable to connect to db, retrying . . .")
  46. if time.Now().After(tperiod) {
  47. return err
  48. }
  49. } else {
  50. break
  51. }
  52. time.Sleep(2 * time.Second)
  53. }
  54. createTables()
  55. return nil
  56. }
  57. func createTables() {
  58. createTable(NETWORKS_TABLE_NAME)
  59. createTable(NODES_TABLE_NAME)
  60. createTable(DELETED_NODES_TABLE_NAME)
  61. createTable(USERS_TABLE_NAME)
  62. createTable(DNS_TABLE_NAME)
  63. createTable(EXT_CLIENT_TABLE_NAME)
  64. createTable(INT_CLIENTS_TABLE_NAME)
  65. createTable(PEERS_TABLE_NAME)
  66. }
  67. func createTable(tableName string) error {
  68. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  69. }
  70. func IsJSONString(value string) bool {
  71. var jsonInt interface{}
  72. return json.Unmarshal([]byte(value), &jsonInt) == nil
  73. }
  74. func Insert(key string, value string, tableName string) error {
  75. if key != "" && value != "" && IsJSONString(value) {
  76. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  77. } else {
  78. return errors.New("invalid insert " + key + " : " + value)
  79. }
  80. }
  81. func InsertPeer(key string, value string) error {
  82. if key != "" && value != "" && IsJSONString(value) {
  83. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  84. } else {
  85. return errors.New("invalid peer insert " + key + " : " + value)
  86. }
  87. }
  88. func DeleteRecord(tableName string, key string) error {
  89. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  90. }
  91. func DeleteAllRecords(tableName string) error {
  92. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  93. if err != nil {
  94. return err
  95. }
  96. err = createTable(tableName)
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func FetchRecord(tableName string, key string) (string, error) {
  103. results, err := FetchRecords(tableName)
  104. if err != nil {
  105. return "", err
  106. }
  107. if results[key] == "" {
  108. return "", errors.New(NO_RECORD)
  109. }
  110. return results[key], nil
  111. }
  112. func FetchRecords(tableName string) (map[string]string, error) {
  113. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  114. }
  115. func CloseDB() {
  116. getCurrentDB()[CLOSE_DB].(func())()
  117. }