|  | 
|  | 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-03-16 | 
|  | 20 | + * Author: Cyber-SiKu | 
|  | 21 | + */ | 
|  | 22 | + | 
|  | 23 | +package step | 
|  | 24 | + | 
|  | 25 | +import ( | 
|  | 26 | +	"encoding/json" | 
|  | 27 | +	"fmt" | 
|  | 28 | +	"strconv" | 
|  | 29 | +	"strings" | 
|  | 30 | + | 
|  | 31 | +	comm "github.com/opencurve/curveadm/internal/common" | 
|  | 32 | +	"github.com/opencurve/curveadm/internal/task/context" | 
|  | 33 | +	"github.com/opencurve/curveadm/internal/utils" | 
|  | 34 | +	"github.com/opencurve/curveadm/pkg/module" | 
|  | 35 | +) | 
|  | 36 | + | 
|  | 37 | +const ( | 
|  | 38 | +	AFTER_TASK_DIR = "/curve/init.d/" | 
|  | 39 | +) | 
|  | 40 | + | 
|  | 41 | +var ALLOC_ID int = 0 | 
|  | 42 | + | 
|  | 43 | +type afterRunTask struct { | 
|  | 44 | +	ID         int      `json:"ID"` | 
|  | 45 | +	Path       string   `json:"Path"` | 
|  | 46 | +	Args       []string `json:"Args"` | 
|  | 47 | +	Env        []string `json:"Env"` | 
|  | 48 | +	Dir        string   `json:"Dir"` | 
|  | 49 | +	OutputPath string   `json:"OutputPath"` | 
|  | 50 | +	InputPath  string   `json:"InputPath"` | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | +func (task afterRunTask) ToString() string { | 
|  | 54 | +	b, err := json.Marshal(task) | 
|  | 55 | +	if err != nil { | 
|  | 56 | +		return "" | 
|  | 57 | +	} | 
|  | 58 | +	return string(b) | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +func newDaemonTask(path string, args ...string) *afterRunTask { | 
|  | 62 | +	task := afterRunTask{ | 
|  | 63 | +		ID:   ALLOC_ID, | 
|  | 64 | +		Path: path, | 
|  | 65 | +		Args: args, | 
|  | 66 | +	} | 
|  | 67 | +	return &task | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | +type AddDaemonTask struct { | 
|  | 71 | +	ContainerId *string | 
|  | 72 | +	Cmd         string | 
|  | 73 | +	Args        []string | 
|  | 74 | +	TaskName    string | 
|  | 75 | +	module.ExecOptions | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +type DelDaemonTask struct { | 
|  | 79 | +	ContainerId *string | 
|  | 80 | +	Tid         string | 
|  | 81 | +	MemStorage  *utils.SafeMap | 
|  | 82 | +	module.ExecOptions | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +func (s *AddDaemonTask) getAllocId(ctx *context.Context) { | 
|  | 86 | +	if ALLOC_ID != 0 { | 
|  | 87 | +		ALLOC_ID++ | 
|  | 88 | +		return | 
|  | 89 | +	} else { | 
|  | 90 | +		// first add daemon task | 
|  | 91 | +		// create dir AFTER_TASK_DIR | 
|  | 92 | +		step := ContainerExec{ | 
|  | 93 | +			ContainerId: s.ContainerId, | 
|  | 94 | +			Command:     fmt.Sprintf("mkdir -p %s", AFTER_TASK_DIR), | 
|  | 95 | +			ExecOptions: s.ExecOptions, | 
|  | 96 | +		} | 
|  | 97 | +		err := step.Execute(ctx) | 
|  | 98 | +		if err != nil { | 
|  | 99 | +			return | 
|  | 100 | +		} | 
|  | 101 | +	} | 
|  | 102 | +	var count string | 
|  | 103 | +	// get max id | 
|  | 104 | +	// The contents of the file are as follows: | 
|  | 105 | +	// {"ID":1,"Path":"tgtd","Args":null,"Env":null,"Dir":"","OutputPath":"","InputPath":""} | 
|  | 106 | +	step := ContainerExec{ | 
|  | 107 | +		ContainerId: s.ContainerId, | 
|  | 108 | +		Command:     fmt.Sprintf("grep -r '\"ID\":[0-9]*,' %s | awk -F \":\" '{print $3}' | awk -F \",\" '{print $1}' | sort -n | tail -1", AFTER_TASK_DIR), | 
|  | 109 | +		Out:         &count, | 
|  | 110 | +		ExecOptions: s.ExecOptions, | 
|  | 111 | +	} | 
|  | 112 | +	err := step.Execute(ctx) | 
|  | 113 | +	if err != nil { | 
|  | 114 | +		ALLOC_ID = 1 | 
|  | 115 | +	} | 
|  | 116 | +	id, err := strconv.Atoi(count) | 
|  | 117 | +	if err != nil { | 
|  | 118 | +		ALLOC_ID = 1 | 
|  | 119 | +	} | 
|  | 120 | +	ALLOC_ID = id + 1 | 
|  | 121 | +} | 
|  | 122 | + | 
|  | 123 | +func (s *AddDaemonTask) Execute(ctx *context.Context) error { | 
|  | 124 | +	s.getAllocId(ctx) | 
|  | 125 | +	content := newDaemonTask(s.Cmd, s.Args...).ToString() | 
|  | 126 | +	step := InstallFile{ | 
|  | 127 | +		Content:           &content, | 
|  | 128 | +		ContainerId:       s.ContainerId, | 
|  | 129 | +		ContainerDestPath: AFTER_TASK_DIR + s.TaskName + ".task", | 
|  | 130 | +		ExecOptions:       s.ExecOptions, | 
|  | 131 | +	} | 
|  | 132 | +	return step.Execute(ctx) | 
|  | 133 | +} | 
|  | 134 | + | 
|  | 135 | +type Target struct { | 
|  | 136 | +	Host   string | 
|  | 137 | +	Tid    string | 
|  | 138 | +	Name   string | 
|  | 139 | +	Store  string | 
|  | 140 | +	Portal string | 
|  | 141 | +} | 
|  | 142 | + | 
|  | 143 | +func (s *DelDaemonTask) Execute(ctx *context.Context) error { | 
|  | 144 | +	v := s.MemStorage.Get(comm.KEY_ALL_TARGETS) | 
|  | 145 | +	target := v.(map[string]*Target)[s.Tid] | 
|  | 146 | +	if target == nil { | 
|  | 147 | +		return nil | 
|  | 148 | +	} | 
|  | 149 | +	stores := strings.Split(target.Store, "//") | 
|  | 150 | +	if len(stores) < 2 { | 
|  | 151 | +		// unable to recognize cbd:pool | 
|  | 152 | +		return nil | 
|  | 153 | +	} | 
|  | 154 | +	path := AFTER_TASK_DIR + "addTarget_" + stores[1] + ".task" | 
|  | 155 | +	step := ContainerExec{ | 
|  | 156 | +		ContainerId: s.ContainerId, | 
|  | 157 | +		Command:     "rm -f " + path, | 
|  | 158 | +		ExecOptions: s.ExecOptions, | 
|  | 159 | +	} | 
|  | 160 | +	return step.Execute(ctx) | 
|  | 161 | +} | 
0 commit comments