-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cyber-SiKu <[email protected]>
- Loading branch information
1 parent
7f61105
commit 8d6caf7
Showing
10 changed files
with
158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters