|
| 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-08-02 |
| 20 | +* Author: wanghai (SeanHai) |
| 21 | + */ |
| 22 | + |
| 23 | +package common |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + |
| 28 | + "github.com/opencurve/curveadm/cli/cli" |
| 29 | + "github.com/opencurve/curveadm/internal/configure/topology" |
| 30 | + "github.com/opencurve/curveadm/internal/errno" |
| 31 | + "github.com/opencurve/curveadm/internal/task/context" |
| 32 | + "github.com/opencurve/curveadm/internal/task/scripts" |
| 33 | + "github.com/opencurve/curveadm/internal/task/step" |
| 34 | + "github.com/opencurve/curveadm/internal/task/task" |
| 35 | + tui "github.com/opencurve/curveadm/internal/tui/common" |
| 36 | +) |
| 37 | + |
| 38 | +func checkEnableEtcdAuthStatus(success *bool, out *string) step.LambdaType { |
| 39 | + return func(ctx *context.Context) error { |
| 40 | + if !*success { |
| 41 | + return errno.ERR_ENABLE_ETCD_AUTH_FAILED.S(*out) |
| 42 | + } |
| 43 | + return nil |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func NewEnableEtcdAuthTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) { |
| 48 | + serviceId := curveadm.GetServiceId(dc.GetId()) |
| 49 | + containerId, err := curveadm.GetContainerId(serviceId) |
| 50 | + if curveadm.IsSkip(dc) { |
| 51 | + return nil, nil |
| 52 | + } else if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + hc, err := curveadm.GetHost(dc.GetHost()) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + var success bool |
| 61 | + var out string |
| 62 | + host, role := dc.GetHost(), dc.GetRole() |
| 63 | + // new task |
| 64 | + subname := fmt.Sprintf("host=%s role=%s containerId=%s", |
| 65 | + dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId)) |
| 66 | + t := task.NewTask("Enable Etcd Auth", subname, hc.GetSSHConfig()) |
| 67 | + |
| 68 | + script := scripts.ENABLE_ETCD_AUTH |
| 69 | + layout := dc.GetProjectLayout() |
| 70 | + scriptPath := fmt.Sprintf("%s/enable_auth.sh", layout.ServiceBinDir) |
| 71 | + |
| 72 | + etcdEndPoints, err := dc.GetVariables().Get("cluster_etcd_addr") |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + t.AddStep(&step.ListContainers{ |
| 78 | + ShowAll: true, |
| 79 | + Format: `"{{.ID}}"`, |
| 80 | + Filter: fmt.Sprintf("id=%s", containerId), |
| 81 | + Out: &out, |
| 82 | + ExecOptions: curveadm.ExecOptions(), |
| 83 | + }) |
| 84 | + t.AddStep(&step.Lambda{ |
| 85 | + Lambda: CheckContainerExist(host, role, containerId, &out), |
| 86 | + }) |
| 87 | + t.AddStep(&step.InstallFile{ // install /curvebs(fs)/etcd/sbin/enable_auth.sh |
| 88 | + ContainerId: &containerId, |
| 89 | + ContainerDestPath: scriptPath, |
| 90 | + Content: &script, |
| 91 | + ExecOptions: curveadm.ExecOptions(), |
| 92 | + }) |
| 93 | + command := fmt.Sprintf("/bin/bash %s %s %s %s", scriptPath, etcdEndPoints, dc.GetEtcdAuthUsername(), |
| 94 | + dc.GetEtcdAuthPassword()) |
| 95 | + t.AddStep(&step.ContainerExec{ |
| 96 | + ContainerId: &containerId, |
| 97 | + Success: &success, |
| 98 | + Out: &out, |
| 99 | + Command: command, |
| 100 | + ExecOptions: curveadm.ExecOptions(), |
| 101 | + }) |
| 102 | + t.AddStep(&step.Lambda{ |
| 103 | + Lambda: checkEnableEtcdAuthStatus(&success, &out), |
| 104 | + }) |
| 105 | + return t, nil |
| 106 | +} |
0 commit comments