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"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/hjson/hjson-go/v4"
|
"github.com/hjson/hjson-go/v4"
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
"dario.cat/mergo"
|
"dario.cat/mergo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,11 +75,13 @@ func main() {
|
||||||
// Parse service config
|
// Parse service config
|
||||||
err = hjson.Unmarshal(c, &s)
|
err = hjson.Unmarshal(c, &s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error parsing config file")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mergo.Merge(&s, service, mergo.WithOverride)
|
mergo.Merge(&s, service, mergo.WithOverride)
|
||||||
config.Services[name] = s
|
config.Services[name] = s
|
||||||
|
|
||||||
fmt.Printf(" %v\n", s)
|
fmt.Printf(" %v\n", s)
|
||||||
|
|
||||||
// Add service packages to the global list
|
// Add service packages to the global list
|
||||||
|
@ -97,15 +101,64 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := template.New(name).Parse(string(t))
|
tmpl, err := template.New(name).Parse(string(t))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading template")
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf(" Installing %s to %s\n", f[0], f[1])
|
|
||||||
err = tmpl.Execute(os.Stdout, s)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Parse package list
|
||||||
|
|
||||||
// Add extra packages needed
|
// Add extra packages needed
|
||||||
|
|
|
@ -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