database.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package database
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "time"
  7. "github.com/gravitl/netmaker/servercfg"
  8. )
  9. // NETWORKS_TABLE_NAME - networks table
  10. const NETWORKS_TABLE_NAME = "networks"
  11. // NODES_TABLE_NAME - nodes table
  12. const NODES_TABLE_NAME = "nodes"
  13. // DELETED_NODES_TABLE_NAME - deleted nodes table
  14. const DELETED_NODES_TABLE_NAME = "deletednodes"
  15. // USERS_TABLE_NAME - users table
  16. const USERS_TABLE_NAME = "users"
  17. // DNS_TABLE_NAME - dns table
  18. const DNS_TABLE_NAME = "dns"
  19. // EXT_CLIENT_TABLE_NAME - ext client table
  20. const EXT_CLIENT_TABLE_NAME = "extclients"
  21. // INT_CLIENTS_TABLE_NAME - int client table
  22. const INT_CLIENTS_TABLE_NAME = "intclients"
  23. // PEERS_TABLE_NAME - peers table
  24. const PEERS_TABLE_NAME = "peers"
  25. // SERVERCONF_TABLE_NAME
  26. const SERVERCONF_TABLE_NAME = "serverconf"
  27. // DATABASE_FILENAME - database file name
  28. const DATABASE_FILENAME = "netmaker.db"
  29. // == ERROR CONSTS ==
  30. // NO_RECORD - no singular result found
  31. const NO_RECORD = "no result found"
  32. // NO_RECORDS - no results found
  33. const NO_RECORDS = "could not find any records"
  34. // == Constants ==
  35. // INIT_DB - initialize db
  36. const INIT_DB = "init"
  37. // CREATE_TABLE - create table const
  38. const CREATE_TABLE = "createtable"
  39. // INSERT - insert into db const
  40. const INSERT = "insert"
  41. // INSERT_PEER - insert peer into db const
  42. const INSERT_PEER = "insertpeer"
  43. // DELETE - delete db record const
  44. const DELETE = "delete"
  45. // DELETE_ALL - delete a table const
  46. const DELETE_ALL = "deleteall"
  47. // FETCH_ALL - fetch table contents const
  48. const FETCH_ALL = "fetchall"
  49. // CLOSE_DB - graceful close of db const
  50. const CLOSE_DB = "closedb"
  51. func getCurrentDB() map[string]interface{} {
  52. switch servercfg.GetDB() {
  53. case "rqlite":
  54. return RQLITE_FUNCTIONS
  55. case "sqlite":
  56. return SQLITE_FUNCTIONS
  57. case "postgres":
  58. return PG_FUNCTIONS
  59. default:
  60. return SQLITE_FUNCTIONS
  61. }
  62. }
  63. func InitializeDatabase() error {
  64. log.Println("connecting to", servercfg.GetDB())
  65. tperiod := time.Now().Add(10 * time.Second)
  66. for {
  67. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  68. log.Println("unable to connect to db, retrying . . .")
  69. if time.Now().After(tperiod) {
  70. return err
  71. }
  72. } else {
  73. break
  74. }
  75. time.Sleep(2 * time.Second)
  76. }
  77. createTables()
  78. return nil
  79. }
  80. func createTables() {
  81. createTable(NETWORKS_TABLE_NAME)
  82. createTable(NODES_TABLE_NAME)
  83. createTable(DELETED_NODES_TABLE_NAME)
  84. createTable(USERS_TABLE_NAME)
  85. createTable(DNS_TABLE_NAME)
  86. createTable(EXT_CLIENT_TABLE_NAME)
  87. createTable(INT_CLIENTS_TABLE_NAME)
  88. createTable(PEERS_TABLE_NAME)
  89. createTable(SERVERCONF_TABLE_NAME)
  90. }
  91. func createTable(tableName string) error {
  92. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  93. }
  94. // IsJSONString - checks if valid json
  95. func IsJSONString(value string) bool {
  96. var jsonInt interface{}
  97. return json.Unmarshal([]byte(value), &jsonInt) == nil
  98. }
  99. // Insert - inserts object into db
  100. func Insert(key string, value string, tableName string) error {
  101. if key != "" && value != "" && IsJSONString(value) {
  102. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  103. } else {
  104. return errors.New("invalid insert " + key + " : " + value)
  105. }
  106. }
  107. // InsertPeer - inserts peer into db
  108. func InsertPeer(key string, value string) error {
  109. if key != "" && value != "" && IsJSONString(value) {
  110. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  111. } else {
  112. return errors.New("invalid peer insert " + key + " : " + value)
  113. }
  114. }
  115. // DeleteRecord - deletes a record from db
  116. func DeleteRecord(tableName string, key string) error {
  117. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  118. }
  119. // DeleteAllRecords - removes a table and remakes
  120. func DeleteAllRecords(tableName string) error {
  121. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  122. if err != nil {
  123. return err
  124. }
  125. err = createTable(tableName)
  126. if err != nil {
  127. return err
  128. }
  129. return nil
  130. }
  131. // FetchRecord - fetches a record
  132. func FetchRecord(tableName string, key string) (string, error) {
  133. results, err := FetchRecords(tableName)
  134. if err != nil {
  135. return "", err
  136. }
  137. if results[key] == "" {
  138. return "", errors.New(NO_RECORD)
  139. }
  140. return results[key], nil
  141. }
  142. // FetchRecords - fetches all records in given table
  143. func FetchRecords(tableName string) (map[string]string, error) {
  144. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  145. }
  146. // CloseDB - closes a database gracefully
  147. func CloseDB() {
  148. getCurrentDB()[CLOSE_DB].(func())()
  149. }