feat: support multiple unit directories (#19)

This commit is contained in:
GUO YANKE
2024-08-22 11:09:10 +08:00
parent 8fdc1adc52
commit edbaa283d1
5 changed files with 30 additions and 15 deletions

View File

@@ -38,7 +38,7 @@ ADD my-service.yml /etc/minit.d/my-service.yml
Add Unit `YAML` files to `/etc/minit.d`
Override default directory by environment variable `MINIT_UNIT_DIR`
Override default directory by environment variable `MINIT_UNIT_DIR`, multiple directories are supported by separating with `:`
Use `---` to separate multiple units in single `YAML` file

View File

@@ -27,7 +27,7 @@ ENTRYPOINT ["/minit"]
## 配置文件
配置文件默认从 `/etc/minit.d/*.yml` 读取
配置文件默认从 `/etc/minit.d` 读取,可以通过环境变量 `MINIT_UNIT_DIR` 来指定配置文件目录,支持多个目录,使用 `:` 分隔
允许使用 `---` 分割在单个 `yaml` 文件中,写入多条配置单元

View File

@@ -23,7 +23,7 @@ const (
type LoadOptions struct {
Args []string
Env map[string]string
Dir string
Dirs []string
}
func Load(opts LoadOptions) (output []Unit, skipped []Unit, err error) {
@@ -31,8 +31,8 @@ func Load(opts LoadOptions) (output []Unit, skipped []Unit, err error) {
var units []Unit
if opts.Dir != "" {
units = append(units, rg.Must(LoadDir(opts.Dir))...)
for _, dir := range opts.Dirs {
units = append(units, rg.Must(LoadDir(dir))...)
}
if len(opts.Args) > 0 {

View File

@@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"gopkg.in/yaml.v3"
)
@@ -44,6 +45,7 @@ func LoadDir(dir string) (units []Unit, err error) {
if files, err = filepath.Glob(filepath.Join(dir, ext)); err != nil {
return
}
sort.Strings(files)
for _, file := range files {
var _units []Unit
if _units, err = LoadFile(file); err != nil {

33
main.go
View File

@@ -68,27 +68,40 @@ func main() {
var (
optPprofPort = ""
optUnitDir = "/etc/minit.d"
optLogDir = dirNone
optQuickExit bool
)
envStr("MINIT_PPROF_PORT", &optPprofPort)
envStr("MINIT_UNIT_DIR", &optUnitDir)
envStr("MINIT_LOG_DIR", &optLogDir)
envBool("MINIT_QUICK_EXIT", &optQuickExit)
if optPprofPort != "" {
// pprof
if envStr("MINIT_PPROF_PORT", &optPprofPort); optPprofPort != "" {
go func() {
_ = http.ListenAndServe(":"+optPprofPort, nil)
}()
}
mkdirUnlessNone(&optUnitDir)
// unit dir, be careful, empty string should be treated as current directory
envStr("MINIT_UNIT_DIR", &optUnitDir)
var unitDirs []string
for _, _dir := range strings.Split(optUnitDir, ":") {
dir := strings.TrimSpace(_dir)
if mkdirUnlessNone(&dir); dir == dirNone {
continue
}
unitDirs = append(unitDirs, dir)
}
// log dir
envStr("MINIT_LOG_DIR", &optLogDir)
mkdirUnlessNone(&optLogDir)
// quick exit
envBool("MINIT_QUICK_EXIT", &optQuickExit)
createLogger := func(name string, pfx string) (mlog.ProcLogger, error) {
var rfo *mlog.RotatingFileOptions
if optLogDir != dirNone {
@@ -118,7 +131,7 @@ func main() {
munit.LoadOptions{
Args: os.Args[1:],
Env: menv.Environ(),
Dir: optUnitDir,
Dirs: unitDirs,
},
),
)