hosts.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package logic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gravitl/netmaker/database"
  6. "github.com/gravitl/netmaker/models"
  7. )
  8. // GetAllHosts - returns all hosts in flat list or error
  9. func GetAllHosts() ([]models.Host, error) {
  10. currHostMap, err := GetHostsMap()
  11. if err != nil {
  12. return nil, err
  13. }
  14. var currentHosts = []models.Host{}
  15. for k := range currHostMap {
  16. var h = *currHostMap[k]
  17. currentHosts = append(currentHosts, h)
  18. }
  19. return currentHosts, nil
  20. }
  21. // GetHostsMap - gets all the current hosts on machine in a map
  22. func GetHostsMap() (map[string]*models.Host, error) {
  23. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  24. if err != nil && !database.IsEmptyRecord(err) {
  25. return nil, err
  26. }
  27. currHostMap := make(map[string]*models.Host)
  28. for k := range records {
  29. var h models.Host
  30. err = json.Unmarshal([]byte(records[k]), &h)
  31. if err != nil {
  32. return nil, err
  33. }
  34. currHostMap[h.ID.String()] = &h
  35. }
  36. return currHostMap, nil
  37. }
  38. // GetHost - gets a host from db given id
  39. func GetHost(hostid string) (*models.Host, error) {
  40. record, err := database.FetchRecord(database.HOSTS_TABLE_NAME, hostid)
  41. if err != nil {
  42. return nil, err
  43. }
  44. var h models.Host
  45. if err = json.Unmarshal([]byte(record), &h); err != nil {
  46. return nil, err
  47. }
  48. return &h, nil
  49. }
  50. // CreateHost - creates a host if not exist
  51. func CreateHost(h *models.Host) error {
  52. _, err := GetHost(h.ID.String())
  53. if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
  54. return fmt.Errorf("host already exists")
  55. }
  56. return UpsertHost(h)
  57. }
  58. // UpdateHost - updates host data by field
  59. func UpdateHost(newHost, currentHost *models.Host) {
  60. // unchangeable fields via API here
  61. newHost.DaemonInstalled = currentHost.DaemonInstalled
  62. newHost.OS = currentHost.OS
  63. newHost.IPForwarding = currentHost.IPForwarding
  64. newHost.HostPass = currentHost.HostPass
  65. newHost.MacAddress = currentHost.MacAddress
  66. newHost.Debug = currentHost.Debug
  67. newHost.Nodes = currentHost.Nodes
  68. newHost.PublicKey = currentHost.PublicKey
  69. newHost.InternetGateway = currentHost.InternetGateway
  70. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  71. // changeable fields
  72. if len(newHost.Version) == 0 {
  73. newHost.Version = currentHost.Version
  74. }
  75. if len(newHost.Name) == 0 {
  76. newHost.Name = currentHost.Name
  77. }
  78. if newHost.LocalAddress.String() != currentHost.LocalAddress.String() {
  79. newHost.LocalAddress = currentHost.LocalAddress
  80. }
  81. if newHost.LocalRange.String() != currentHost.LocalRange.String() {
  82. newHost.LocalRange = currentHost.LocalRange
  83. }
  84. if newHost.MTU == 0 {
  85. newHost.MTU = currentHost.MTU
  86. }
  87. if newHost.ListenPort == 0 {
  88. newHost.ListenPort = currentHost.ListenPort
  89. }
  90. if newHost.ProxyListenPort == 0 {
  91. newHost.ProxyListenPort = currentHost.ProxyListenPort
  92. }
  93. }
  94. // UpsertHost - upserts into DB a given host model, does not check for existence*
  95. func UpsertHost(h *models.Host) error {
  96. data, err := json.Marshal(h)
  97. if err != nil {
  98. return err
  99. }
  100. return database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  101. }
  102. // RemoveHost - removes a given host from server
  103. func RemoveHost(h *models.Host) error {
  104. if len(h.Nodes) > 0 {
  105. for i := range h.Nodes {
  106. id := h.Nodes[i]
  107. n, err := GetNodeByID(id)
  108. if err == nil {
  109. if err = DeleteNodeByID(&n); err != nil {
  110. return err // must remove associated nodes before removing a host
  111. }
  112. }
  113. }
  114. }
  115. return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  116. }