From 8d6caf78c0aad0e08d367f98da661a678d7eb3ec Mon Sep 17 00:00:00 2001 From: Cyber-SiKu Date: Thu, 16 Mar 2023 11:41:52 +0800 Subject: [PATCH] [feat]curve: run deamon after start Signed-off-by: Cyber-SiKu --- curvefs/docker/debian9/entrypoint.sh | 2 + docker/debian9/Dockerfile | 2 +- docker/debian9/entrypoint.sh | 4 +- nebd/src/part2/file_entity.cpp | 9 +++ tools-v2/Makefile | 6 +- tools-v2/cmd/{curvecli => curve}/main.go | 0 tools-v2/cmd/daemon/main.go | 28 +++++++ tools-v2/pkg/daemon/daemon.go | 13 ++++ tools-v2/pkg/daemon/task.go | 97 ++++++++++++++++++++++++ util/install.sh | 1 + 10 files changed, 158 insertions(+), 4 deletions(-) rename tools-v2/cmd/{curvecli => curve}/main.go (100%) create mode 100644 tools-v2/cmd/daemon/main.go create mode 100644 tools-v2/pkg/daemon/daemon.go create mode 100644 tools-v2/pkg/daemon/task.go diff --git a/curvefs/docker/debian9/entrypoint.sh b/curvefs/docker/debian9/entrypoint.sh index e95e09ed6d..0ca397dace 100755 --- a/curvefs/docker/debian9/entrypoint.sh +++ b/curvefs/docker/debian9/entrypoint.sh @@ -8,6 +8,7 @@ g_args="" g_prefix="" g_binary="" g_start_args="" +g_preexec="/curvefs/tools-v2/sbin/daemon" ############################ BASIC FUNCTIONS function msg() { @@ -119,6 +120,7 @@ function main() { prepare create_directory [[ $(command -v crontab) ]] && cron + [[ ! -z $g_preexec ]] && $g_preexec & if [ $g_role == "etcd" ]; then exec $g_binary $g_start_args >>$g_prefix/logs/etcd.log 2>&1 elif [ $g_role == "monitor" ]; then diff --git a/docker/debian9/Dockerfile b/docker/debian9/Dockerfile index 4158cf71d8..a9d4e67810 100644 --- a/docker/debian9/Dockerfile +++ b/docker/debian9/Dockerfile @@ -1,7 +1,7 @@ FROM opencurvedocker/curve-base:debian9 COPY entrypoint.sh / COPY curvebs /curvebs -RUN mkdir -p /etc/curve /etc/nebd \ +RUN mkdir -p /etc/curve /etc/nebd /curve/init.d/ \ && chmod a+x /entrypoint.sh \ && cp curvebs/nbd/sbin/curve-nbd /usr/bin/ \ && cp curvebs/tools/sbin/curve_ops_tool /usr/bin/ \ diff --git a/docker/debian9/entrypoint.sh b/docker/debian9/entrypoint.sh index cb150b4b86..16d47fce63 100644 --- a/docker/debian9/entrypoint.sh +++ b/docker/debian9/entrypoint.sh @@ -6,7 +6,7 @@ g_role="" g_args="" g_prefix="" -g_preexec="" +g_preexec="/curvebs/tools-v2/sbin/daemon" g_binary="" g_start_args="" @@ -123,7 +123,7 @@ function main() { prepare create_directory [[ $(command -v crontab) ]] && cron - [[ ! -z $g_preexec ]] && $g_preexec + [[ ! -z $g_preexec ]] && $g_preexec & if [ $g_role == "etcd" ]; then exec $g_binary $g_start_args >>$g_prefix/logs/etcd.log 2>&1 elif [ $g_role == "monitor" ]; then diff --git a/nebd/src/part2/file_entity.cpp b/nebd/src/part2/file_entity.cpp index 6bd655e18b..48a7f77508 100644 --- a/nebd/src/part2/file_entity.cpp +++ b/nebd/src/part2/file_entity.cpp @@ -33,6 +33,8 @@ namespace nebd { namespace server { +extern const char* kOpenFlagsAttrKey; + bool IsOpenFlagsExactlySame(const OpenFlags* lhs, const OpenFlags* rhs) { if (lhs == nullptr && rhs == nullptr) { return true; @@ -145,6 +147,13 @@ int NebdFileEntity::Reopen(const ExtendAttribute& xattr) { << "filename: " << fileName_; return -1; } + + OpenFlags flags; + if (fileInstance->xattr.count(kOpenFlagsAttrKey) && + flags.ParseFromString(fileInstance->xattr.at(kOpenFlagsAttrKey))) { + openFlags_.reset(new OpenFlags{flags}); + } + LOG(INFO) << "Reopen file success. " << "fd: " << fd_ << ", filename: " << fileName_; diff --git a/tools-v2/Makefile b/tools-v2/Makefile index e125987843..ed33716180 100644 --- a/tools-v2/Makefile +++ b/tools-v2/Makefile @@ -25,6 +25,7 @@ GO := go # output OUTPUT := sbin/curve +DAEMON_OUTPUT := sbin/daemon # version VERSION_FLAG := -X github.com/opencurve/curve/tools-v2/pkg/cli/command/version.Version=$(version) @@ -50,13 +51,16 @@ DEBUG_FLAGS := -gcflags=$(GCFLAGS) DEBUG_FLAGS += $(CGO_DEBUG_FLAG) # packages -PACKAGES := $(PWD)/cmd/curvecli/main.go +PACKAGES := $(PWD)/cmd/curve/main.go +DAEMON_PACKAGES := $(PWD)/cmd/daemon/main.go build: proto $(GOENV) $(GO) build -o $(OUTPUT) $(BUILD_FLAGS) $(PACKAGES) + $(GOENV) $(GO) build -o $(DAEMON_OUTPUT) $(BUILD_FLAGS) $(DAEMON_PACKAGES) debug: proto $(GOENV) $(GO) build -o $(OUTPUT) $(DEBUG_FLAGS) $(PACKAGES) + $(GOENV) $(GO) build -o $(DAEMON_OUTPUT) $(DEBUG_FLAGS) $(DAEMON_PACKAGES) init: proto go mod init github.com/opencurve/curve/tools-v2 diff --git a/tools-v2/cmd/curvecli/main.go b/tools-v2/cmd/curve/main.go similarity index 100% rename from tools-v2/cmd/curvecli/main.go rename to tools-v2/cmd/curve/main.go diff --git a/tools-v2/cmd/daemon/main.go b/tools-v2/cmd/daemon/main.go new file mode 100644 index 0000000000..42a575dfb5 --- /dev/null +++ b/tools-v2/cmd/daemon/main.go @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 NetEase Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Project: CurveCli + * Created Date: 2023-03-16 + * Author: chengyi (Cyber-SiKu) + */ +package main + +import "github.com/opencurve/curve/tools-v2/pkg/daemon" + +func main() { + daemon.Execute() +} diff --git a/tools-v2/pkg/daemon/daemon.go b/tools-v2/pkg/daemon/daemon.go new file mode 100644 index 0000000000..0f53540b38 --- /dev/null +++ b/tools-v2/pkg/daemon/daemon.go @@ -0,0 +1,13 @@ +package daemon + +import "fmt" + +func Execute() { + tasks := GetTasks() + for _, t := range tasks { + err := t.Run() + if err != nil { + fmt.Println(err.Error()) + } + } +} diff --git a/tools-v2/pkg/daemon/task.go b/tools-v2/pkg/daemon/task.go new file mode 100644 index 0000000000..5d11d2f900 --- /dev/null +++ b/tools-v2/pkg/daemon/task.go @@ -0,0 +1,97 @@ +package daemon + +import ( + "bytes" + "encoding/json" + "fmt" + "io/fs" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "sort" + "strings" +) + +type Task struct { + ID int `json:"ID"` + Path string `json:"Path"` + Args []string `json:"Args"` + Env []string `json:"Env"` + Dir string `json:"Dir"` + OutputPath string `json:"OutputPath"` + InputPath string `json:"InputPath"` +} + +func NewTask(str []byte) *Task { + task := Task{} + json.Unmarshal(str, &task) + return &task +} + +func (task *Task) Run() error { + cmd := exec.Command(task.Path, task.Args...) + if task.InputPath != "" { + inputData, err := ioutil.ReadFile(task.InputPath) + if err != nil { + return err + } + cmd.Stdin = strings.NewReader(string(inputData)) + } + var out bytes.Buffer + defer func() { + if task.OutputPath != "" { + ioutil.WriteFile(task.OutputPath, out.Bytes(), 0644) + } + }() + cmd.Stdout = &out + cmd.Env = append(cmd.Env, task.Env...) + err := cmd.Run() + fmt.Printf("cmd:\n%+v\nout:\n%s\n-----------\n", *task, out.String()) + return err +} + +func getFileList(path string) []string { + var fileList []string + fi, err := os.Stat(path) + if err != nil || !fi.IsDir() { + return fileList + } + filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { + if !info.IsDir() { + fileList = append(fileList, path) + } + return nil + }) + + return fileList +} + +const ( + WORK_DIRECTORY = "/curve/init.d/" +) + +func GetTasks() []*Task { + fileList := getFileList(WORK_DIRECTORY) + fmt.Println("fileList:", fileList) + var tasks []*Task + for _, file := range fileList { + fileData, err := ioutil.ReadFile(file) + if err == nil { + task := NewTask(fileData) + tasks = append(tasks, task) + } + } + sort.Slice(tasks, func(i, j int) bool { + return tasks[i].ID < tasks[j].ID + }) + return tasks +} + +func (task *Task)Write(path string) { + b, err := json.Marshal(task) + if err != nil { + return + } + ioutil.WriteFile(path, b, 0644) +} diff --git a/util/install.sh b/util/install.sh index 9c012b685d..3561d50136 100644 --- a/util/install.sh +++ b/util/install.sh @@ -323,6 +323,7 @@ install_tools-v2() { mkdir -p $project_prefix/conf copy_file "$project_name/sbin/curve" "$project_prefix/sbin" copy_file "$project_name/pkg/config/curve.yaml" "$g_prefix/conf" + copy_file "$project_name/sbin/daemon" "$project_prefix/sbin" } main() {