database.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package database
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. "github.com/gravitl/netmaker/logger"
  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. // InitializeDatabase - initializes database
  66. func InitializeDatabase() error {
  67. logger.Log(0, "connecting to", servercfg.GetDB())
  68. tperiod := time.Now().Add(10 * time.Second)
  69. for {
  70. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  71. logger.Log(0, "unable to connect to db, retrying . . .")
  72. if time.Now().After(tperiod) {
  73. return err
  74. }
  75. } else {
  76. break
  77. }
  78. time.Sleep(2 * time.Second)
  79. }
  80. createTables()
  81. return nil
  82. }
  83. func createTables() {
  84. createTable(NETWORKS_TABLE_NAME)
  85. createTable(NODES_TABLE_NAME)
  86. createTable(DELETED_NODES_TABLE_NAME)
  87. createTable(USERS_TABLE_NAME)
  88. createTable(DNS_TABLE_NAME)
  89. createTable(EXT_CLIENT_TABLE_NAME)
  90. createTable(INT_CLIENTS_TABLE_NAME)
  91. createTable(PEERS_TABLE_NAME)
  92. createTable(SERVERCONF_TABLE_NAME)
  93. createTable(GENERATED_TABLE_NAME)
  94. }
  95. func createTable(tableName string) error {
  96. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  97. }
  98. // IsJSONString - checks if valid json
  99. func IsJSONString(value string) bool {
  100. var jsonInt interface{}
  101. return json.Unmarshal([]byte(value), &jsonInt) == nil
  102. }
  103. // Insert - inserts object into db
  104. func Insert(key string, value string, tableName string) error {
  105. if key != "" && value != "" && IsJSONString(value) {
  106. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  107. } else {
  108. return errors.New("invalid insert " + key + " : " + value)
  109. }
  110. }
  111. // InsertPeer - inserts peer into db
  112. func InsertPeer(key string, value string) error {
  113. if key != "" && value != "" && IsJSONString(value) {
  114. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  115. } else {
  116. return errors.New("invalid peer insert " + key + " : " + value)
  117. }
  118. }
  119. // DeleteRecord - deletes a record from db
  120. func DeleteRecord(tableName string, key string) error {
  121. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  122. }
  123. // DeleteAllRecords - removes a table and remakes
  124. func DeleteAllRecords(tableName string) error {
  125. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  126. if err != nil {
  127. return err
  128. }
  129. err = createTable(tableName)
  130. if err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. // FetchRecord - fetches a record
  136. func FetchRecord(tableName string, key string) (string, error) {
  137. results, err := FetchRecords(tableName)
  138. if err != nil {
  139. return "", err
  140. }
  141. if results[key] == "" {
  142. return "", errors.New(NO_RECORD)
  143. }
  144. return results[key], nil
  145. }
  146. // FetchRecords - fetches all records in given table
  147. func FetchRecords(tableName string) (map[string]string, error) {
  148. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  149. }
  150. // CloseDB - closes a database gracefully
  151. func CloseDB() {
  152. getCurrentDB()[CLOSE_DB].(func())()
  153. }