|
| 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 | + |
| 29 | + "github.com/opencurve/curveadm/cli/cli" |
| 30 | + comm "github.com/opencurve/curveadm/internal/common" |
| 31 | + "github.com/opencurve/curveadm/internal/configure" |
| 32 | + "github.com/opencurve/curveadm/internal/configure/topology" |
| 33 | + "github.com/opencurve/curveadm/internal/errno" |
| 34 | + "github.com/opencurve/curveadm/internal/task/context" |
| 35 | + "github.com/opencurve/curveadm/internal/task/scripts" |
| 36 | + "github.com/opencurve/curveadm/internal/task/step" |
| 37 | + "github.com/opencurve/curveadm/internal/task/task" |
| 38 | + tui "github.com/opencurve/curveadm/internal/tui/common" |
| 39 | +) |
| 40 | + |
| 41 | +func checkAddEtcdMemberStatus(success *bool, out *string) step.LambdaType { |
| 42 | + return func(ctx *context.Context) error { |
| 43 | + if !*success { |
| 44 | + return errno.ERR_ADD_ETCD_MEMEBER.S(*out) |
| 45 | + } |
| 46 | + return nil |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func NewAddEtcdMemberTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) { |
| 51 | + serviceId := curveadm.GetServiceId(dc.GetId()) |
| 52 | + containerId, err := curveadm.GetContainerId(serviceId) |
| 53 | + if curveadm.IsSkip(dc) { |
| 54 | + return nil, nil |
| 55 | + } else if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + hc, err := curveadm.GetHost(dc.GetHost()) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + subname := fmt.Sprintf("host=%s role=%s containerId=%s", |
| 64 | + dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId)) |
| 65 | + t := task.NewTask("Backup Etcd Data", subname, hc.GetSSHConfig()) |
| 66 | + |
| 67 | + host, role := dc.GetHost(), dc.GetRole() |
| 68 | + script := scripts.ADD_ETCD |
| 69 | + layout := dc.GetProjectLayout() |
| 70 | + scriptPath := fmt.Sprintf("%s/add_etcd.sh", layout.ServiceBinDir) |
| 71 | + etcdctlPath := layout.ServiceBinDir + "/etcdctl" |
| 72 | + endpoints, err := dc.GetVariables().Get("cluster_etcd_addr") |
| 73 | + if err != nil { |
| 74 | + return nil, errno.ERR_GET_CLUSTER_ETCD_ADDR |
| 75 | + } |
| 76 | + oldName := fmt.Sprint("etcd", strconv.Itoa(dc.GetHostSequence()), strconv.Itoa(dc.GetInstancesSequence())) |
| 77 | + newName := fmt.Sprint("etcd", strconv.Itoa(dc.GetHostSequence()+3), strconv.Itoa(dc.GetInstancesSequence())) |
| 78 | + migrates := []*configure.MigrateServer{} |
| 79 | + if curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS) != nil { |
| 80 | + migrates = curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS).([]*configure.MigrateServer) |
| 81 | + } |
| 82 | + toService := migrates[0].To |
| 83 | + peerUrl := fmt.Sprint("http://", toService.GetListenIp(), ":", strconv.Itoa(toService.GetListenPort())) |
| 84 | + amendEtcdConfCmd := fmt.Sprintf("/bin/bash %s %s %s %s %s %s", scriptPath, etcdctlPath, endpoints, oldName, newName, peerUrl) |
| 85 | + |
| 86 | + var success bool |
| 87 | + var out string |
| 88 | + t.AddStep(&step.ListContainers{ |
| 89 | + ShowAll: true, |
| 90 | + Format: `"{{.ID}}"`, |
| 91 | + Filter: fmt.Sprintf("id=%s", containerId), |
| 92 | + Out: &out, |
| 93 | + ExecOptions: curveadm.ExecOptions(), |
| 94 | + }) |
| 95 | + t.AddStep(&step.Lambda{ |
| 96 | + Lambda: CheckContainerExist(host, role, containerId, &out), |
| 97 | + }) |
| 98 | + t.AddStep(&step.InstallFile{ |
| 99 | + ContainerId: &containerId, |
| 100 | + ContainerDestPath: scriptPath, |
| 101 | + Content: &script, |
| 102 | + ExecOptions: curveadm.ExecOptions(), |
| 103 | + }) |
| 104 | + t.AddStep(&step.ContainerExec{ |
| 105 | + ContainerId: &containerId, |
| 106 | + Success: &success, |
| 107 | + Out: &out, |
| 108 | + Command: amendEtcdConfCmd, |
| 109 | + ExecOptions: curveadm.ExecOptions(), |
| 110 | + }) |
| 111 | + t.AddStep(&step.Lambda{ |
| 112 | + Lambda: checkAddEtcdMemberStatus(&success, &out), |
| 113 | + }) |
| 114 | + |
| 115 | + return t, nil |
| 116 | +} |
0 commit comments