hosts.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.NodePassword = currentHost.NodePassword
  67. newHost.MacAddress = currentHost.MacAddress
  68. newHost.Debug = currentHost.Debug
  69. newHost.Nodes = currentHost.Nodes
  70. newHost.PublicKey = currentHost.PublicKey
  71. newHost.InternetGateway = currentHost.InternetGateway
  72. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  73. // changeable fields
  74. if len(newHost.Version) == 0 {
  75. newHost.Version = currentHost.Version
  76. }
  77. if len(newHost.Name) == 0 {
  78. newHost.Name = currentHost.Name
  79. }
  80. if newHost.LocalAddress.String() != currentHost.LocalAddress.String() {
  81. newHost.LocalAddress = currentHost.LocalAddress
  82. }
  83. if newHost.LocalRange.String() != currentHost.LocalRange.String() {
  84. newHost.LocalRange = currentHost.LocalRange
  85. }
  86. if newHost.MTU == 0 {
  87. newHost.MTU = currentHost.MTU
  88. }
  89. if newHost.ListenPort == 0 {
  90. newHost.ListenPort = currentHost.ListenPort
  91. }
  92. if newHost.ProxyListenPort == 0 {
  93. newHost.ProxyListenPort = currentHost.ProxyListenPort
  94. }
  95. }
  96. // UpsertHost - upserts into DB a given host model, does not check for existence*
  97. func UpsertHost(h *models.Host) error {
  98. data, err := json.Marshal(h)
  99. if err != nil {
  100. return err
  101. }
  102. return database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  103. }
  104. // RemoveHost - removes a given host from server
  105. func RemoveHost(h *models.Host) error {
  106. if len(h.Nodes) > 0 {
  107. for i := range h.Nodes {
  108. id := h.Nodes[i]
  109. n, err := GetNodeByID(id)
  110. if err == nil {
  111. if err = DeleteNodeByID(&n); err != nil {
  112. return err // must remove associated nodes before removing a host
  113. }
  114. }
  115. }
  116. }
  117. return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  118. }
  119. // UpdateHostNetworks - updates a given host's networks
  120. func UpdateHostNetworks(h *models.Host, nets []string) error {
  121. if len(h.Nodes) > 0 {
  122. for i := range h.Nodes {
  123. n, err := GetNodeByID(h.Nodes[i])
  124. if err != nil {
  125. return err
  126. }
  127. // loop through networks and remove need for updating existing networks
  128. found := false
  129. for j := range nets {
  130. if len(nets[j]) > 0 && nets[j] == n.Network {
  131. nets[j] = "" // mark as ignore
  132. found = true
  133. }
  134. }
  135. if !found { // remove the node/host from that network
  136. if err = DeleteNodeByID(&n); err != nil {
  137. return err
  138. }
  139. }
  140. }
  141. } else {
  142. h.Nodes = []string{}
  143. }
  144. for i := range nets {
  145. // create a node for each non zero network remaining
  146. if len(nets[i]) > 0 {
  147. // TODO create a node with given hostid
  148. logger.Log(0, "I will create a node here")
  149. }
  150. }
  151. return nil
  152. }