hosts.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package logic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gravitl/netmaker/database"
  6. "github.com/gravitl/netmaker/logger"
  7. "github.com/gravitl/netmaker/models"
  8. )
  9. // GetAllHosts - returns all hosts in flat list or error
  10. func GetAllHosts() ([]models.Host, error) {
  11. currHostMap, err := GetHostsMap()
  12. if err != nil {
  13. return nil, err
  14. }
  15. var currentHosts = []models.Host{}
  16. for k := range currHostMap {
  17. var h = *currHostMap[k]
  18. currentHosts = append(currentHosts, h)
  19. }
  20. return currentHosts, nil
  21. }
  22. // GetHostsMap - gets all the current hosts on machine in a map
  23. func GetHostsMap() (map[string]*models.Host, error) {
  24. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  25. if err != nil && !database.IsEmptyRecord(err) {
  26. return nil, err
  27. }
  28. currHostMap := make(map[string]*models.Host)
  29. for k := range records {
  30. var h models.Host
  31. err = json.Unmarshal([]byte(records[k]), &h)
  32. if err != nil {
  33. return nil, err
  34. }
  35. currHostMap[h.ID.String()] = &h
  36. }
  37. return currHostMap, nil
  38. }
  39. // GetHost - gets a host from db given id
  40. func GetHost(hostid string) (*models.Host, error) {
  41. record, err := database.FetchRecord(database.HOSTS_TABLE_NAME, hostid)
  42. if err != nil {
  43. return nil, err
  44. }
  45. var h models.Host
  46. if err = json.Unmarshal([]byte(record), &h); err != nil {
  47. return nil, err
  48. }
  49. return &h, nil
  50. }
  51. // CreateHost - creates a host if not exist
  52. func CreateHost(h *models.Host) error {
  53. _, err := GetHost(h.ID.String())
  54. if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
  55. return fmt.Errorf("host already exists")
  56. }
  57. return UpsertHost(h)
  58. }
  59. // UpdateHost - updates host data by field
  60. func UpdateHost(newHost, currentHost *models.Host) {
  61. // unchangeable fields via API here
  62. newHost.DaemonInstalled = currentHost.DaemonInstalled
  63. newHost.OS = currentHost.OS
  64. newHost.IPForwarding = currentHost.IPForwarding
  65. newHost.HostPass = currentHost.HostPass
  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. }
  118. // UpdateHostNetworks - updates a given host's networks
  119. func UpdateHostNetworks(h *models.Host, nets []string) error {
  120. if len(h.Nodes) > 0 {
  121. for i := range h.Nodes {
  122. n, err := GetNodeByID(h.Nodes[i])
  123. if err != nil {
  124. return err
  125. }
  126. // loop through networks and remove need for updating existing networks
  127. found := false
  128. for j := range nets {
  129. if len(nets[j]) > 0 && nets[j] == n.Network {
  130. nets[j] = "" // mark as ignore
  131. found = true
  132. }
  133. }
  134. if !found { // remove the node/host from that network
  135. if err = DeleteNodeByID(&n); err != nil {
  136. return err
  137. }
  138. }
  139. }
  140. } else {
  141. h.Nodes = []string{}
  142. }
  143. for i := range nets {
  144. // create a node for each non zero network remaining
  145. if len(nets[i]) > 0 {
  146. // TODO create a node with given hostid
  147. logger.Log(0, "I will create a node here")
  148. }
  149. }
  150. return nil
  151. }