From 3dc1e454d58f2f9a5966b2c0790dadd8fca9d0e8 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 15 Dec 2022 21:33:55 +0000 Subject: [PATCH 1/3] wip --- main.go | 6 +++ pkg/utils/command/command_helper.go | 53 ++++++++++++++++++++++++ pkg/utils/command/command_helper_test.go | 13 ++++++ 3 files changed, 72 insertions(+) create mode 100644 pkg/utils/command/command_helper_test.go diff --git a/main.go b/main.go index 7f747c7c..6fe39b99 100644 --- a/main.go +++ b/main.go @@ -9,11 +9,13 @@ import ( "time" "github.com/IceWhaleTech/CasaOS-Common/model" + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" "github.com/IceWhaleTech/CasaOS/model/notify" "github.com/IceWhaleTech/CasaOS/pkg/cache" "github.com/IceWhaleTech/CasaOS/pkg/config" "github.com/IceWhaleTech/CasaOS/pkg/sqlite" + "github.com/IceWhaleTech/CasaOS/pkg/utils/command" "github.com/IceWhaleTech/CasaOS/pkg/utils/file" "github.com/IceWhaleTech/CasaOS/route" "github.com/IceWhaleTech/CasaOS/service" @@ -140,6 +142,10 @@ func main() { ) } + // run any script that needs to be executed + scriptDirectory := filepath.Join(constants.DefaultConfigPath, "start.d") + command.ExecuteScripts(scriptDirectory) + if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil { logger.Error("Failed to notify systemd that casaos main service is ready", zap.Any("error", err)) } else if supported { diff --git a/pkg/utils/command/command_helper.go b/pkg/utils/command/command_helper.go index a47b2888..703a8b46 100644 --- a/pkg/utils/command/command_helper.go +++ b/pkg/utils/command/command_helper.go @@ -5,7 +5,10 @@ import ( "context" "fmt" "io/ioutil" + "os" "os/exec" + "path/filepath" + "strings" "time" ) @@ -111,3 +114,53 @@ func ExecSmartCTLByPath(path string) []byte { func ExecEnabledSMART(path string) { exec.Command("smartctl", "-s on", path).Output() } + +func ExecuteScripts(scriptDirectory string) { + if _, err := os.Stat(scriptDirectory); os.IsNotExist(err) { + fmt.Printf("No post-start scripts at %s\n", scriptDirectory) + return + } + + files, err := os.ReadDir(scriptDirectory) + if err != nil { + fmt.Printf("Failed to read from script directory %s: %s\n", scriptDirectory, err.Error()) + return + } + + for _, file := range files { + if file.IsDir() { + continue + } + + scriptFilepath := filepath.Join(scriptDirectory, file.Name()) + + f, err := os.Open(scriptFilepath) + if err != nil { + fmt.Printf("Failed to open script file %s: %s\n", scriptFilepath, err.Error()) + continue + } + f.Close() + + scanner := bufio.NewScanner(f) + scanner.Scan() + shebang := scanner.Text() + + interpreter := "/bin/sh" + if strings.HasPrefix(shebang, "#!") { + interpreter = shebang[2:] + } + + cmd := exec.Command(interpreter, scriptFilepath) + + fmt.Printf("Executing post-start script %s using %s\n", scriptFilepath, interpreter) + + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + err = cmd.Run() + if err != nil { + fmt.Printf("Failed to execute post-start script %s: %s\n", scriptFilepath, err.Error()) + } + } + fmt.Println("Finished executing post-start scripts.") +} diff --git a/pkg/utils/command/command_helper_test.go b/pkg/utils/command/command_helper_test.go new file mode 100644 index 00000000..b5a6bdda --- /dev/null +++ b/pkg/utils/command/command_helper_test.go @@ -0,0 +1,13 @@ +package command + +import ( + "path/filepath" + "testing" + + "github.com/IceWhaleTech/CasaOS-Common/utils/constants" +) + +func TestExecutePostStartScripts(t *testing.T) { + scriptDirectory := filepath.Join(constants.DefaultConfigPath, "start.d") + ExecuteScripts(scriptDirectory) +} From 99127b20885f14399c94b976bca2be208043a033 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 15 Dec 2022 18:23:57 -0500 Subject: [PATCH 2/3] wip --- pkg/utils/command/command_helper_test.go | 26 +++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/utils/command/command_helper_test.go b/pkg/utils/command/command_helper_test.go index b5a6bdda..713ea0c4 100644 --- a/pkg/utils/command/command_helper_test.go +++ b/pkg/utils/command/command_helper_test.go @@ -1,13 +1,29 @@ package command import ( - "path/filepath" + "os" "testing" - "github.com/IceWhaleTech/CasaOS-Common/utils/constants" + "gotest.tools/assert" ) -func TestExecutePostStartScripts(t *testing.T) { - scriptDirectory := filepath.Join(constants.DefaultConfigPath, "start.d") - ExecuteScripts(scriptDirectory) +func TestExecuteScripts(t *testing.T) { + // make a temp directory + tmpDir, err := os.MkdirTemp("", "casaos-test-*") + assert.NilError(t, err) + defer os.RemoveAll(tmpDir) + + ExecuteScripts(tmpDir) + + // create a sample script under tmpDir + script := tmpDir + "/test.sh" + f, err := os.Create(script) + assert.NilError(t, err) + defer f.Close() + + // write a sample script + _, err = f.WriteString("#!/bin/bash\necho 123") + assert.NilError(t, err) + + ExecuteScripts(tmpDir) } From 7baf6b59d53a2bbeab083c1b5a30d22030840435 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 15 Dec 2022 18:29:31 -0500 Subject: [PATCH 3/3] wip --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 380fb493..09a4beff 100644 --- a/go.mod +++ b/go.mod @@ -39,4 +39,5 @@ require ( golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 golang.org/x/tools v0.1.7 // indirect gorm.io/gorm v1.24.0 + gotest.tools v2.2.0+incompatible )