database.go 4.8 KB

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