Skip to content

Commit

Permalink
[feat]curve: run deamon after start
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu committed Mar 29, 2023
1 parent 7f61105 commit 8d6caf7
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 4 deletions.
2 changes: 2 additions & 0 deletions curvefs/docker/debian9/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ g_args=""
g_prefix=""
g_binary=""
g_start_args=""
g_preexec="/curvefs/tools-v2/sbin/daemon"

############################ BASIC FUNCTIONS
function msg() {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docker/debian9/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/ \
Expand Down
4 changes: 2 additions & 2 deletions docker/debian9/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
g_role=""
g_args=""
g_prefix=""
g_preexec=""
g_preexec="/curvebs/tools-v2/sbin/daemon"
g_binary=""
g_start_args=""

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions nebd/src/part2/file_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
Expand Down
6 changes: 5 additions & 1 deletion tools-v2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions tools-v2/cmd/daemon/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
13 changes: 13 additions & 0 deletions tools-v2/pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
}
97 changes: 97 additions & 0 deletions tools-v2/pkg/daemon/task.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions util/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8d6caf7

Please sign in to comment.