database.go 4.6 KB

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