hosts.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.NodePassword = currentHost.NodePassword
  66. newHost.MacAddress = currentHost.MacAddress
  67. newHost.Debug = currentHost.Debug
  68. newHost.Nodes = currentHost.Nodes
  69. newHost.PublicKey = currentHost.PublicKey
  70. newHost.InternetGateway = currentHost.InternetGateway
  71. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  72. // changeable fields
  73. if len(newHost.Version) == 0 {
  74. newHost.Version = currentHost.Version
  75. }
  76. if len(newHost.Name) == 0 {
  77. newHost.Name = currentHost.Name
  78. }
  79. if newHost.LocalAddress.String() != currentHost.LocalAddress.String() {
  80. newHost.LocalAddress = currentHost.LocalAddress
  81. }
  82. if newHost.LocalRange.String() != currentHost.LocalRange.String() {
  83. newHost.LocalRange = currentHost.LocalRange
  84. }
  85. if newHost.MTU == 0 {
  86. newHost.MTU = currentHost.MTU
  87. }
  88. if newHost.ListenPort == 0 {
  89. newHost.ListenPort = currentHost.ListenPort
  90. }
  91. if newHost.ProxyListenPort == 0 {
  92. newHost.ProxyListenPort = currentHost.ProxyListenPort
  93. }
  94. }
  95. // UpsertHost - upserts into DB a given host model, does not check for existence*
  96. func UpsertHost(h *models.Host) error {
  97. data, err := json.Marshal(h)
  98. if err != nil {
  99. return err
  100. }
  101. return database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  102. }
  103. // RemoveHost - removes a given host from server
  104. func RemoveHost(h *models.Host) error {
  105. if len(h.Nodes) > 0 {
  106. for i := range h.Nodes {
  107. id := h.Nodes[i]
  108. n, err := GetNodeByID(id)
  109. if err == nil {
  110. if err = DeleteNodeByID(&n); err != nil {
  111. return err // must remove associated nodes before removing a host
  112. }
  113. }
  114. }
  115. }
  116. return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  117. }