database.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. case "postgres":
  37. return PG_FUNCTIONS
  38. default:
  39. return SQLITE_FUNCTIONS
  40. }
  41. }
  42. func InitializeDatabase() error {
  43. log.Println("connecting to",servercfg.GetDB())
  44. tperiod := time.Now().Add(10 * time.Second)
  45. for {
  46. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  47. log.Println("unable to connect to db, retrying . . .")
  48. if time.Now().After(tperiod) {
  49. return err
  50. }
  51. } else {
  52. break
  53. }
  54. time.Sleep(2 * time.Second)
  55. }
  56. createTables()
  57. return nil
  58. }
  59. func createTables() {
  60. createTable(NETWORKS_TABLE_NAME)
  61. createTable(NODES_TABLE_NAME)
  62. createTable(DELETED_NODES_TABLE_NAME)
  63. createTable(USERS_TABLE_NAME)
  64. createTable(DNS_TABLE_NAME)
  65. createTable(EXT_CLIENT_TABLE_NAME)
  66. createTable(INT_CLIENTS_TABLE_NAME)
  67. createTable(PEERS_TABLE_NAME)
  68. }
  69. func createTable(tableName string) error {
  70. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  71. }
  72. func IsJSONString(value string) bool {
  73. var jsonInt interface{}
  74. return json.Unmarshal([]byte(value), &jsonInt) == nil
  75. }
  76. func Insert(key string, value string, tableName string) error {
  77. if key != "" && value != "" && IsJSONString(value) {
  78. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  79. } else {
  80. return errors.New("invalid insert " + key + " : " + value)
  81. }
  82. }
  83. func InsertPeer(key string, value string) error {
  84. if key != "" && value != "" && IsJSONString(value) {
  85. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  86. } else {
  87. return errors.New("invalid peer insert " + key + " : " + value)
  88. }
  89. }
  90. func DeleteRecord(tableName string, key string) error {
  91. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  92. }
  93. func DeleteAllRecords(tableName string) error {
  94. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  95. if err != nil {
  96. return err
  97. }
  98. err = createTable(tableName)
  99. if err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func FetchRecord(tableName string, key string) (string, error) {
  105. results, err := FetchRecords(tableName)
  106. if err != nil {
  107. return "", err
  108. }
  109. if results[key] == "" {
  110. return "", errors.New(NO_RECORD)
  111. }
  112. return results[key], nil
  113. }
  114. func FetchRecords(tableName string) (map[string]string, error) {
  115. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  116. }
  117. func CloseDB() {
  118. getCurrentDB()[CLOSE_DB].(func())()
  119. }