1
0
mirror of https://github.com/IceWhaleTech/CasaOS.git synced 2025-07-12 23:50:14 +02:00

add logic to run scripts under /etc/casaos/start.d when starting (#756)

This commit is contained in:
Tiger Wang
2022-12-15 18:31:06 -05:00
committed by GitHub
parent ba742b9fb2
commit 4b26631374
4 changed files with 89 additions and 0 deletions

View File

@ -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.")
}