|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/docker/docker/api/types" |
| 6 | + "github.com/docker/docker/api/types/network" |
| 7 | + "github.com/docker/docker/api/types/swarm" |
| 8 | + "github.com/donknap/dpanel/app/common/logic" |
| 9 | + "github.com/donknap/dpanel/common/function" |
| 10 | + "github.com/donknap/dpanel/common/service/docker" |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "log/slog" |
| 13 | + "net" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func (self Swarm) NodeList(http *gin.Context) { |
| 18 | + list, err := docker.Sdk.Client.NodeList(docker.Sdk.Ctx, types.NodeListOptions{}) |
| 19 | + if err != nil { |
| 20 | + self.JsonResponseWithError(http, err, 500) |
| 21 | + return |
| 22 | + } |
| 23 | + self.JsonResponseWithoutError(http, gin.H{ |
| 24 | + "list": list, |
| 25 | + }) |
| 26 | + return |
| 27 | +} |
| 28 | + |
| 29 | +func (self Swarm) NodeUpdate(http *gin.Context) { |
| 30 | + type ParamsValidate struct { |
| 31 | + NodeId string `json:"nodeId" binding:"required"` |
| 32 | + Availability swarm.NodeAvailability `json:"availability" binding:"omitempty,oneof=active pause drain"` |
| 33 | + Role swarm.NodeRole `json:"role" binding:"omitempty,oneof=worker manager"` |
| 34 | + Labels []docker.ValueItem `json:"labels"` |
| 35 | + } |
| 36 | + params := ParamsValidate{} |
| 37 | + if !self.Validate(http, ¶ms) { |
| 38 | + return |
| 39 | + } |
| 40 | + node, _, err := docker.Sdk.Client.NodeInspectWithRaw(docker.Sdk.Ctx, params.NodeId) |
| 41 | + if err != nil { |
| 42 | + self.JsonResponseWithError(http, err, 500) |
| 43 | + return |
| 44 | + } |
| 45 | + if params.Availability != "" { |
| 46 | + node.Spec.Availability = params.Availability |
| 47 | + } |
| 48 | + if params.Role != "" { |
| 49 | + node.Spec.Role = params.Role |
| 50 | + } |
| 51 | + if !function.IsEmptyArray(params.Labels) { |
| 52 | + node.Spec.Labels = function.PluckArrayMapWalk(params.Labels, func(item docker.ValueItem) (string, string, bool) { |
| 53 | + return item.Name, item.Value, true |
| 54 | + }) |
| 55 | + } |
| 56 | + err = docker.Sdk.Client.NodeUpdate(docker.Sdk.Ctx, params.NodeId, node.Version, node.Spec) |
| 57 | + if err != nil { |
| 58 | + self.JsonResponseWithError(http, err, 500) |
| 59 | + return |
| 60 | + } |
| 61 | + self.JsonResponseWithoutError(http, gin.H{ |
| 62 | + "detail": node, |
| 63 | + }) |
| 64 | + return |
| 65 | +} |
| 66 | + |
| 67 | +func (self Swarm) NodeJoin(http *gin.Context) { |
| 68 | + type ParamsValidate struct { |
| 69 | + DockerEnvName string `json:"dockerEnvName" binding:"required"` |
| 70 | + Type string `json:"type" binding:"required,oneof=add join"` |
| 71 | + Role swarm.NodeRole `json:"role" binding:"omitempty,oneof=worker manager"` |
| 72 | + ListenAddr string `json:"listenAddr" binding:"required"` |
| 73 | + } |
| 74 | + params := ParamsValidate{} |
| 75 | + if !self.Validate(http, ¶ms) { |
| 76 | + return |
| 77 | + } |
| 78 | + dockerEnv, err := logic.DockerEnv{}.GetEnvByName(params.DockerEnvName) |
| 79 | + if err != nil { |
| 80 | + self.JsonResponseWithError(http, err, 500) |
| 81 | + return |
| 82 | + } |
| 83 | + dockerClient, err := docker.NewBuilderWithDockerEnv(dockerEnv) |
| 84 | + if err != nil { |
| 85 | + self.JsonResponseWithError(http, err, 500) |
| 86 | + return |
| 87 | + } |
| 88 | + defer func() { |
| 89 | + dockerClient.Close() |
| 90 | + }() |
| 91 | + |
| 92 | + var swarmDockerClient *docker.Builder |
| 93 | + var clientDockerClient *docker.Builder |
| 94 | + |
| 95 | + if params.Type == "join" { |
| 96 | + // join 是将当前环境添加到目标集群节点 |
| 97 | + swarmDockerClient = dockerClient |
| 98 | + clientDockerClient = docker.Sdk |
| 99 | + } else { |
| 100 | + swarmDockerClient = docker.Sdk |
| 101 | + clientDockerClient = dockerClient |
| 102 | + } |
| 103 | + swarmDockerInfo, err := swarmDockerClient.Client.Info(swarmDockerClient.Ctx) |
| 104 | + if err != nil { |
| 105 | + self.JsonResponseWithError(http, err, 500) |
| 106 | + return |
| 107 | + } |
| 108 | + if swarmDockerInfo.Swarm.LocalNodeState == swarm.LocalNodeStateInactive { |
| 109 | + self.JsonResponseWithError(http, function.ErrorMessage(".swarmNotInit"), 500) |
| 110 | + return |
| 111 | + } |
| 112 | + if !swarmDockerInfo.Swarm.ControlAvailable { |
| 113 | + self.JsonResponseWithError(http, function.ErrorMessage(".swarmNotManager"), 500) |
| 114 | + return |
| 115 | + } |
| 116 | + swarmInfo, err := swarmDockerClient.Client.SwarmInspect(swarmDockerClient.Ctx) |
| 117 | + if err != nil { |
| 118 | + self.JsonResponseWithError(http, err, 500) |
| 119 | + return |
| 120 | + } |
| 121 | + swarmManageNode, _, err := swarmDockerClient.Client.NodeInspectWithRaw(swarmDockerClient.Ctx, swarmDockerInfo.Swarm.NodeID) |
| 122 | + if err != nil { |
| 123 | + self.JsonResponseWithError(http, err, 500) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + joinRequest := swarm.JoinRequest{ |
| 128 | + ListenAddr: params.ListenAddr, |
| 129 | + AdvertiseAddr: swarmManageNode.ManagerStatus.Addr, |
| 130 | + Availability: swarm.NodeAvailabilityActive, |
| 131 | + RemoteAddrs: function.PluckArrayWalk(swarmDockerInfo.Swarm.RemoteManagers, func(item swarm.Peer) (string, bool) { |
| 132 | + return item.Addr, true |
| 133 | + }), |
| 134 | + } |
| 135 | + if ip, _, err := net.SplitHostPort(joinRequest.AdvertiseAddr); err == nil { |
| 136 | + joinRequest.DataPathAddr = ip |
| 137 | + } |
| 138 | + |
| 139 | + if params.Role == swarm.NodeRoleManager { |
| 140 | + joinRequest.JoinToken = swarmInfo.JoinTokens.Manager |
| 141 | + } else { |
| 142 | + joinRequest.JoinToken = swarmInfo.JoinTokens.Worker |
| 143 | + } |
| 144 | + |
| 145 | + err = clientDockerClient.Client.SwarmJoin(clientDockerClient.Ctx, joinRequest) |
| 146 | + if err != nil { |
| 147 | + self.JsonResponseWithError(http, err, 500) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + leave := func(nodeId string) { |
| 152 | + _ = clientDockerClient.Client.SwarmLeave(clientDockerClient.Ctx, true) |
| 153 | + if nodeId != "" { |
| 154 | + _ = swarmDockerClient.Client.NodeRemove(swarmDockerClient.Ctx, nodeId, types.NodeRemoveOptions{}) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + clientDockerInfo, err := clientDockerClient.Client.Info(clientDockerClient.Ctx) |
| 159 | + if err != nil { |
| 160 | + leave("") |
| 161 | + self.JsonResponseWithError(http, err, 500) |
| 162 | + return |
| 163 | + } |
| 164 | + node, _, err := docker.Sdk.Client.NodeInspectWithRaw(docker.Sdk.Ctx, clientDockerInfo.Swarm.NodeID) |
| 165 | + if err != nil { |
| 166 | + self.JsonResponseWithError(http, err, 500) |
| 167 | + return |
| 168 | + } |
| 169 | + for i := 0; i < 5; i++ { |
| 170 | + if _, err := clientDockerClient.Client.NetworkInspect(clientDockerClient.Ctx, "ingress", network.InspectOptions{ |
| 171 | + Scope: "swarm", |
| 172 | + }); err == nil { |
| 173 | + // 增加一条 envname label |
| 174 | + if node.Spec.Labels == nil { |
| 175 | + node.Spec.Labels = make(map[string]string) |
| 176 | + } |
| 177 | + node.Spec.Labels["com.dpanel.swarm.nodeEnvName"] = params.DockerEnvName |
| 178 | + err = swarmDockerClient.Client.NodeUpdate(swarmDockerClient.Ctx, node.ID, node.Version, node.Spec) |
| 179 | + if err != nil { |
| 180 | + slog.Debug("swarm node update label", "type", params.Type, "err", err) |
| 181 | + } |
| 182 | + self.JsonResponseWithoutError(http, gin.H{ |
| 183 | + "id": clientDockerInfo.Swarm.NodeID, |
| 184 | + }) |
| 185 | + return |
| 186 | + } |
| 187 | + time.Sleep(time.Second) |
| 188 | + } |
| 189 | + |
| 190 | + leave(clientDockerInfo.Swarm.NodeID) |
| 191 | + |
| 192 | + self.JsonResponseWithoutError(http, gin.H{ |
| 193 | + "id": "", |
| 194 | + "cmd": fmt.Sprintf("docker swarm join --token %s %s", joinRequest.JoinToken, joinRequest.AdvertiseAddr), |
| 195 | + }) |
| 196 | + return |
| 197 | +} |
| 198 | + |
| 199 | +func (self Swarm) NodeRemove(http *gin.Context) { |
| 200 | + type ParamsValidate struct { |
| 201 | + NodeId string `json:"nodeId"` |
| 202 | + Force bool `json:"force"` |
| 203 | + } |
| 204 | + params := ParamsValidate{} |
| 205 | + if !self.Validate(http, ¶ms) { |
| 206 | + return |
| 207 | + } |
| 208 | + info, err := docker.Sdk.Client.Info(docker.Sdk.Ctx) |
| 209 | + if err != nil { |
| 210 | + self.JsonResponseWithError(http, err, 500) |
| 211 | + return |
| 212 | + } |
| 213 | + if info.Swarm.ControlAvailable && params.NodeId != "" { |
| 214 | + err := docker.Sdk.Client.NodeRemove(docker.Sdk.Ctx, params.NodeId, types.NodeRemoveOptions{ |
| 215 | + Force: params.Force, |
| 216 | + }) |
| 217 | + if err != nil { |
| 218 | + self.JsonResponseWithError(http, err, 500) |
| 219 | + return |
| 220 | + } |
| 221 | + } else { |
| 222 | + err := docker.Sdk.Client.SwarmLeave(docker.Sdk.Ctx, params.Force) |
| 223 | + if err != nil { |
| 224 | + self.JsonResponseWithError(http, err, 500) |
| 225 | + return |
| 226 | + } |
| 227 | + } |
| 228 | + self.JsonSuccessResponse(http) |
| 229 | + return |
| 230 | +} |
| 231 | + |
| 232 | +func (self Swarm) NodePrune(http *gin.Context) { |
| 233 | + if nodeList, err := docker.Sdk.Client.NodeList(docker.Sdk.Ctx, types.NodeListOptions{}); err == nil { |
| 234 | + for _, node := range nodeList { |
| 235 | + if node.Status.State == swarm.NodeStateDown { |
| 236 | + _ = docker.Sdk.Client.NodeRemove(docker.Sdk.Ctx, node.ID, types.NodeRemoveOptions{}) |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + self.JsonSuccessResponse(http) |
| 241 | + return |
| 242 | +} |
0 commit comments