Flesh out service config generation, add support for converting our intermediate JSON to YAML.
This commit is contained in:
parent
51c304307f
commit
bf7d6c856e
57
main.go
57
main.go
|
@ -4,10 +4,12 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/hjson/hjson-go/v4"
|
||||
"github.com/ghodss/yaml"
|
||||
"dario.cat/mergo"
|
||||
)
|
||||
|
||||
|
@ -73,11 +75,13 @@ func main() {
|
|||
// Parse service config
|
||||
err = hjson.Unmarshal(c, &s)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing config file")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
mergo.Merge(&s, service, mergo.WithOverride)
|
||||
config.Services[name] = s
|
||||
|
||||
fmt.Printf(" %v\n", s)
|
||||
|
||||
// Add service packages to the global list
|
||||
|
@ -98,12 +102,61 @@ func main() {
|
|||
|
||||
tmpl, err := template.New(name).Parse(string(t))
|
||||
if err != nil {
|
||||
fmt.Println("Error reading template")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf(" Installing %s to %s\n", f[0], f[1])
|
||||
err = tmpl.Execute(os.Stdout, s)
|
||||
|
||||
err = os.MkdirAll("/config" + path.Dir(f[1]), 0555)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating config directory.")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fd, err := os.Create("/config" + f[1])
|
||||
defer fd.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("No permission to create config file")
|
||||
return
|
||||
}
|
||||
fd.Chmod(0444)
|
||||
|
||||
err = tmpl.Execute(fd, s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert service configs from JSON to YAML
|
||||
fmt.Println("Converting docker service configs to YAML")
|
||||
servicesPath := "/config/services/docker/"
|
||||
dirents, err := os.ReadDir(servicesPath)
|
||||
for _, d := range dirents {
|
||||
if d.IsDir() == true { continue }
|
||||
if strings.HasSuffix(d.Name(), ".json") != true { continue }
|
||||
f := servicesPath + d.Name()
|
||||
|
||||
j, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
y, err := yaml.JSONToYAML(j)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
f, _ = strings.CutSuffix(f, ".json")
|
||||
fd, _ := os.Create(f + ".yaml")
|
||||
defer fd.Close()
|
||||
fd.Chmod(0444)
|
||||
fd.Write(y)
|
||||
}
|
||||
|
||||
// Parse package list
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
enable: "false",
|
||||
|
||||
ports: ["4533:4533"],
|
||||
|
||||
provider: "docker",
|
||||
packages: [],
|
||||
src: "deluan/navidrome:latest",
|
||||
configFiles: [
|
||||
"navidrome.template:/services/docker/navidrome.json",
|
||||
]
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"services": {
|
||||
"navidrome": {
|
||||
"image": "{{.Src}}",
|
||||
{{with .Config.Id}}"user": "{{.}}:{{.}}",{{end}}
|
||||
"ports": [
|
||||
{{range .Ports}}
|
||||
"{{.}}",
|
||||
{{end}}
|
||||
],
|
||||
{{with .Config.Restart}}"restart": "{{.}}",{{end}}
|
||||
"environment": {
|
||||
"ND_SCANSCHEDULE": "1h",
|
||||
"ND_LOGLEVEL": "info",
|
||||
"ND_SESSIONTIMEOUT": "24h",
|
||||
"ND_BASEURL": "",
|
||||
},
|
||||
"volumes": [
|
||||
{{range .Volumes}}
|
||||
"{{.}}",
|
||||
{{end}}
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
8
services/proxy/caddy.template
Normal file
8
services/proxy/caddy.template
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
}
|
||||
|
||||
{{range .Config.proxy}}
|
||||
{{.domain}} {
|
||||
reverse_proxy localhost:{{.port}}
|
||||
}
|
||||
{{end}}
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
enable: "false",
|
||||
|
||||
ports: [ "80", "443" ],
|
||||
|
||||
provider: "system",
|
||||
packages: [ "caddy" ],
|
||||
configFiles: [
|
||||
"caddy.template:/etc/caddy/Caddyfile",
|
||||
]
|
||||
}
|
1
services/ssh/banner.template
Normal file
1
services/ssh/banner.template
Normal file
|
@ -0,0 +1 @@
|
|||
{{with .Config.banner}}{{.}}{{end}}
|
Loading…
Reference in a new issue