|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 NetEase Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: CurveAdm |
| 19 | + * Created Date: 2023-12-20 |
| 20 | + * Author: Caoxianfei |
| 21 | + */ |
| 22 | + |
| 23 | +package common |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "strconv" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/opencurve/curveadm/cli/cli" |
| 31 | + comm "github.com/opencurve/curveadm/internal/common" |
| 32 | + "github.com/opencurve/curveadm/internal/configure" |
| 33 | + "github.com/opencurve/curveadm/internal/configure/topology" |
| 34 | + "github.com/opencurve/curveadm/internal/task/step" |
| 35 | + "github.com/opencurve/curveadm/internal/task/task" |
| 36 | + tui "github.com/opencurve/curveadm/internal/tui/common" |
| 37 | +) |
| 38 | + |
| 39 | +const ( |
| 40 | + AMEND_NAME = "name" |
| 41 | + AMEND_ENDPOINTS = "initial-cluster" |
| 42 | + AMEND_STATE = "initial-cluster-state" |
| 43 | +) |
| 44 | + |
| 45 | +var options = make(map[string]interface{}) |
| 46 | + |
| 47 | +func mutateEtcdConf(dc *topology.DeployConfig, delimiter string, forceRender bool) step.Mutate { |
| 48 | + return func(in, key, value string) (out string, err error) { |
| 49 | + if len(key) == 0 { |
| 50 | + out = in |
| 51 | + return |
| 52 | + } |
| 53 | + if key == AMEND_NAME { |
| 54 | + value = options[AMEND_NAME].(string) |
| 55 | + } else if key == AMEND_ENDPOINTS { |
| 56 | + value = options[AMEND_ENDPOINTS].(string) |
| 57 | + } else if key == AMEND_STATE { |
| 58 | + value = "existing" |
| 59 | + } |
| 60 | + |
| 61 | + out = fmt.Sprintf("%s%s%s", key, delimiter, value) |
| 62 | + return |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func NewAmendEtcdConfigTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) { |
| 67 | + serviceId := curveadm.GetServiceId(dc.GetId()) |
| 68 | + containerId, err := curveadm.GetContainerId(serviceId) |
| 69 | + if curveadm.IsSkip(dc) { |
| 70 | + return nil, nil |
| 71 | + } else if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + hc, err := curveadm.GetHost(dc.GetHost()) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + subname := fmt.Sprintf("host=%s role=%s containerId=%s", |
| 80 | + dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId)) |
| 81 | + t := task.NewTask("Override Etcd configure", subname, hc.GetSSHConfig()) |
| 82 | + |
| 83 | + layout := dc.GetProjectLayout() |
| 84 | + migrates := []*configure.MigrateServer{} |
| 85 | + if curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS) != nil { |
| 86 | + migrates = curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS).([]*configure.MigrateServer) |
| 87 | + } |
| 88 | + dcs := []*topology.DeployConfig{} |
| 89 | + if curveadm.MemStorage().Get(comm.KEY_CLUSTER_DCS) != nil { |
| 90 | + dcs = curveadm.MemStorage().Get(comm.KEY_CLUSTER_DCS).([]*topology.DeployConfig) |
| 91 | + } |
| 92 | + endpoints := []string{} |
| 93 | + for _, dc := range dcs { |
| 94 | + ept := fmt.Sprint("etcd", strconv.Itoa(dc.GetHostSequence()), strconv.Itoa(dc.GetInstancesSequence()), |
| 95 | + "=", "http://", dc.GetListenIp(), ":", strconv.Itoa(dc.GetListenPort())) |
| 96 | + endpoints = append(endpoints, ept) |
| 97 | + } |
| 98 | + toService := migrates[0].To |
| 99 | + newName := fmt.Sprint("etcd", strconv.Itoa(toService.GetHostSequence()+3), strconv.Itoa(toService.GetInstancesSequence())) |
| 100 | + toSeriveEndpint := fmt.Sprint(newName, "=", "http://", |
| 101 | + toService.GetListenIp(), ":", strconv.Itoa(toService.GetListenPort())) |
| 102 | + endpoints = append(endpoints, toSeriveEndpint) |
| 103 | + endpointsStr := strings.Join(endpoints, ",") |
| 104 | + |
| 105 | + options[AMEND_NAME] = newName |
| 106 | + options[AMEND_ENDPOINTS] = endpointsStr |
| 107 | + |
| 108 | + t.AddStep(&step.SyncFile{ // sync etcd.conf config |
| 109 | + ContainerSrcId: &containerId, |
| 110 | + ContainerSrcPath: layout.ServiceConfPath, |
| 111 | + ContainerDestId: &containerId, |
| 112 | + ContainerDestPath: layout.ServiceConfPath, |
| 113 | + KVFieldSplit: ETCD_CONFIG_DELIMITER, |
| 114 | + Mutate: mutateEtcdConf(dc, ETCD_CONFIG_DELIMITER, false), |
| 115 | + ExecOptions: curveadm.ExecOptions(), |
| 116 | + }) |
| 117 | + |
| 118 | + return t, nil |
| 119 | +} |
0 commit comments