hosts.go 4.9 KB

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