hosts.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/gravitl/netmaker/database"
  6. "github.com/gravitl/netmaker/logger"
  7. "github.com/gravitl/netmaker/models"
  8. "golang.org/x/crypto/bcrypt"
  9. )
  10. // ErrHostExists error indicating that host exists when trying to create new host
  11. var ErrHostExists error = errors.New("host already exists")
  12. // GetAllHosts - returns all hosts in flat list or error
  13. func GetAllHosts() ([]models.Host, error) {
  14. currHostMap, err := GetHostsMap()
  15. if err != nil {
  16. return nil, err
  17. }
  18. var currentHosts = []models.Host{}
  19. for k := range currHostMap {
  20. var h = *currHostMap[k]
  21. currentHosts = append(currentHosts, h)
  22. }
  23. return currentHosts, nil
  24. }
  25. // GetHostsMap - gets all the current hosts on machine in a map
  26. func GetHostsMap() (map[string]*models.Host, error) {
  27. records, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
  28. if err != nil && !database.IsEmptyRecord(err) {
  29. return nil, err
  30. }
  31. currHostMap := make(map[string]*models.Host)
  32. for k := range records {
  33. var h models.Host
  34. err = json.Unmarshal([]byte(records[k]), &h)
  35. if err != nil {
  36. return nil, err
  37. }
  38. currHostMap[h.ID.String()] = &h
  39. }
  40. return currHostMap, nil
  41. }
  42. // GetHost - gets a host from db given id
  43. func GetHost(hostid string) (*models.Host, error) {
  44. record, err := database.FetchRecord(database.HOSTS_TABLE_NAME, hostid)
  45. if err != nil {
  46. return nil, err
  47. }
  48. var h models.Host
  49. if err = json.Unmarshal([]byte(record), &h); err != nil {
  50. return nil, err
  51. }
  52. return &h, nil
  53. }
  54. // CreateHost - creates a host if not exist
  55. func CreateHost(h *models.Host) error {
  56. _, err := GetHost(h.ID.String())
  57. if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
  58. return ErrHostExists
  59. }
  60. //encrypt that password so we never see it
  61. hash, err := bcrypt.GenerateFromPassword([]byte(h.HostPass), 5)
  62. if err != nil {
  63. return err
  64. }
  65. h.HostPass = string(hash)
  66. return UpsertHost(h)
  67. }
  68. // UpdateHost - updates host data by field
  69. func UpdateHost(newHost, currentHost *models.Host) {
  70. // unchangeable fields via API here
  71. newHost.DaemonInstalled = currentHost.DaemonInstalled
  72. newHost.OS = currentHost.OS
  73. newHost.IPForwarding = currentHost.IPForwarding
  74. newHost.HostPass = currentHost.HostPass
  75. newHost.MacAddress = currentHost.MacAddress
  76. newHost.Debug = currentHost.Debug
  77. newHost.Nodes = currentHost.Nodes
  78. newHost.PublicKey = currentHost.PublicKey
  79. newHost.InternetGateway = currentHost.InternetGateway
  80. newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
  81. // changeable fields
  82. if len(newHost.Version) == 0 {
  83. newHost.Version = currentHost.Version
  84. }
  85. if len(newHost.Name) == 0 {
  86. newHost.Name = currentHost.Name
  87. }
  88. if newHost.LocalAddress.String() != currentHost.LocalAddress.String() {
  89. newHost.LocalAddress = currentHost.LocalAddress
  90. }
  91. if newHost.LocalRange.String() != currentHost.LocalRange.String() {
  92. newHost.LocalRange = currentHost.LocalRange
  93. }
  94. if newHost.MTU == 0 {
  95. newHost.MTU = currentHost.MTU
  96. }
  97. if newHost.ListenPort == 0 {
  98. newHost.ListenPort = currentHost.ListenPort
  99. }
  100. if newHost.ProxyListenPort == 0 {
  101. newHost.ProxyListenPort = currentHost.ProxyListenPort
  102. }
  103. }
  104. // UpsertHost - upserts into DB a given host model, does not check for existence*
  105. func UpsertHost(h *models.Host) error {
  106. data, err := json.Marshal(h)
  107. if err != nil {
  108. return err
  109. }
  110. return database.Insert(h.ID.String(), string(data), database.HOSTS_TABLE_NAME)
  111. }
  112. // RemoveHost - removes a given host from server
  113. func RemoveHost(h *models.Host) error {
  114. if len(h.Nodes) > 0 {
  115. for i := range h.Nodes {
  116. id := h.Nodes[i]
  117. n, err := GetNodeByID(id)
  118. if err == nil {
  119. if err = DeleteNodeByID(&n); err != nil {
  120. return err // must remove associated nodes before removing a host
  121. }
  122. }
  123. }
  124. }
  125. return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
  126. }
  127. // UpdateHostNetworks - updates a given host's networks
  128. func UpdateHostNetworks(h *models.Host, nets []string) error {
  129. if len(h.Nodes) > 0 {
  130. for i := range h.Nodes {
  131. n, err := GetNodeByID(h.Nodes[i])
  132. if err != nil {
  133. return err
  134. }
  135. // loop through networks and remove need for updating existing networks
  136. found := false
  137. for j := range nets {
  138. if len(nets[j]) > 0 && nets[j] == n.Network {
  139. nets[j] = "" // mark as ignore
  140. found = true
  141. }
  142. }
  143. if !found { // remove the node/host from that network
  144. if err = DeleteNodeByID(&n); err != nil {
  145. return err
  146. }
  147. }
  148. }
  149. } else {
  150. h.Nodes = []string{}
  151. }
  152. for i := range nets {
  153. // create a node for each non zero network remaining
  154. if len(nets[i]) > 0 {
  155. // TODO create a node with given hostid
  156. logger.Log(0, "I will create a node here")
  157. }
  158. }
  159. return nil
  160. }