Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
53 changes: 53 additions & 0 deletions pkg/utils/command/command_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -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.")
}
29 changes: 29 additions & 0 deletions pkg/utils/command/command_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package command

import (
"os"
"testing"

"gotest.tools/assert"
)

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)
}