serverctl.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package serverctl
  2. import (
  3. "errors"
  4. "net"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/gravitl/netmaker/database"
  9. "github.com/gravitl/netmaker/logger"
  10. "github.com/gravitl/netmaker/logic"
  11. "github.com/gravitl/netmaker/logic/acls"
  12. "github.com/gravitl/netmaker/logic/acls/nodeacls"
  13. "github.com/gravitl/netmaker/models"
  14. "github.com/gravitl/netmaker/netclient/ncutils"
  15. "github.com/gravitl/netmaker/servercfg"
  16. )
  17. // COMMS_NETID - name of the comms network
  18. var COMMS_NETID string
  19. const (
  20. // NETMAKER_BINARY_NAME - name of netmaker binary
  21. NETMAKER_BINARY_NAME = "netmaker"
  22. )
  23. // InitializeCommsNetwork - Check if comms network exists (for MQ, DNS, SSH traffic), if not, create
  24. func InitializeCommsNetwork() error {
  25. setCommsID()
  26. _, err := logic.GetNetwork(COMMS_NETID)
  27. if err != nil {
  28. var network models.Network
  29. network.NetID = COMMS_NETID
  30. network.AddressRange = servercfg.GetCommsCIDR()
  31. network.IsPointToSite = "yes"
  32. network.DefaultUDPHolePunch = "yes"
  33. network.IsComms = "yes"
  34. logger.Log(1, "comms net does not exist, creating with ID,", network.NetID, "and CIDR,", network.AddressRange)
  35. _, err = logic.CreateNetwork(network)
  36. return err
  37. }
  38. time.Sleep(time.Second << 1)
  39. SyncServerNetwork(COMMS_NETID)
  40. return nil
  41. }
  42. // SetJWTSecret - sets the jwt secret on server startup
  43. func setCommsID() {
  44. currentid, idErr := logic.FetchCommsNetID()
  45. if idErr != nil {
  46. commsid := logic.RandomString(8)
  47. if err := logic.StoreCommsNetID(commsid); err != nil {
  48. logger.FatalLog("something went wrong when configuring comms id")
  49. }
  50. COMMS_NETID = commsid
  51. servercfg.SetCommsID(COMMS_NETID)
  52. return
  53. }
  54. COMMS_NETID = currentid
  55. servercfg.SetCommsID(COMMS_NETID)
  56. }
  57. // InitServerNetclient - intializes the server netclient
  58. // 1. Check if config directory exists, if not attempt to make
  59. // 2. Check current networks and run pull to get interface up to date in case of restart
  60. func InitServerNetclient() error {
  61. netclientDir := ncutils.GetNetclientPath()
  62. _, err := os.Stat(netclientDir + "/config")
  63. if os.IsNotExist(err) {
  64. os.MkdirAll(netclientDir+"/config", 0700)
  65. } else if err != nil {
  66. logger.Log(1, "could not find or create", netclientDir)
  67. return err
  68. }
  69. var networks, netsErr = logic.GetNetworks()
  70. if netsErr == nil || database.IsEmptyRecord(netsErr) {
  71. for _, network := range networks {
  72. var currentServerNode, nodeErr = logic.GetNetworkServerLocal(network.NetID)
  73. if nodeErr == nil {
  74. if err = logic.ServerPull(&currentServerNode, true); err != nil {
  75. logger.Log(1, "failed pull for network", network.NetID, ", on server node", currentServerNode.ID)
  76. }
  77. }
  78. }
  79. }
  80. return nil
  81. }
  82. // SyncServerNetwork - ensures a wg interface and node exists for server
  83. func SyncServerNetwork(network string) error {
  84. serverNetworkSettings, err := logic.GetNetwork(network)
  85. if err != nil {
  86. return err
  87. }
  88. localnets, err := net.Interfaces()
  89. if err != nil {
  90. return err
  91. }
  92. ifaceExists := false
  93. for _, localnet := range localnets {
  94. if serverNetworkSettings.DefaultInterface == localnet.Name {
  95. ifaceExists = true
  96. }
  97. }
  98. serverNode, err := logic.GetNetworkServerLocal(network)
  99. if !ifaceExists && (err == nil && serverNode.ID != "") {
  100. return logic.ServerUpdate(&serverNode, true)
  101. } else if !ifaceExists {
  102. _, err := logic.ServerJoin(&serverNetworkSettings)
  103. if err != nil {
  104. if err == nil {
  105. err = errors.New("network add failed for " + serverNetworkSettings.NetID)
  106. }
  107. if !strings.Contains(err.Error(), "macaddress_unique") { // ignore macaddress unique error throws
  108. logger.Log(1, "error adding network", serverNetworkSettings.NetID, "during sync:", err.Error())
  109. }
  110. }
  111. }
  112. // remove networks locally that do not exist in database
  113. /*
  114. for _, localnet := range localnets {
  115. if strings.Contains(localnet.Name, "nm-") {
  116. var exists = ""
  117. if serverNetworkSettings.DefaultInterface == localnet.Name {
  118. exists = serverNetworkSettings.NetID
  119. }
  120. if exists == "" {
  121. err := logic.DeleteNodeByID(serverNode, true)
  122. if err != nil {
  123. if err == nil {
  124. err = errors.New("network delete failed for " + exists)
  125. }
  126. logger.Log(1, "error removing network", exists, "during sync", err.Error())
  127. }
  128. }
  129. }
  130. }
  131. */
  132. return nil
  133. }
  134. // SetDefaultACLS - runs through each network to see if ACL's are set. If not, goes through each node in network and adds the default ACL
  135. func SetDefaultACLS() error {
  136. // upgraded systems will not have ACL's set, which is why we need this function
  137. nodes, err := logic.GetAllNodes()
  138. if err != nil {
  139. return err
  140. }
  141. for i := range nodes {
  142. currentNodeACL, err := nodeacls.FetchNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID))
  143. if (err != nil && (database.IsEmptyRecord(err) || strings.Contains(err.Error(), "no node ACL present"))) || currentNodeACL == nil {
  144. if _, err = nodeacls.CreateNodeACL(nodeacls.NetworkID(nodes[i].Network), nodeacls.NodeID(nodes[i].ID), acls.Allowed); err != nil {
  145. logger.Log(1, "could not create a default ACL for node", nodes[i].ID)
  146. }
  147. }
  148. }
  149. return nil
  150. }