StackWithResources: Update ComposeYML behaviour

Previously, there was a function to manually read bytes for a
docker-compose.yml. But this proved to be akward at runtime.

Instead, this code automatically reads an existing docker-compose.yml,
and takes care of marshalling and unmarshalling.
This commit is contained in:
Tom 2023-07-14 15:21:18 +02:00
parent e2f5c66b1c
commit 17d64826df
5 changed files with 94 additions and 90 deletions

View file

@ -1,15 +1,12 @@
package binder
import (
"bytes"
"io"
"embed"
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/tkw1536/pkglib/yamlx"
"gopkg.in/yaml.v3"
_ "embed"
)
type Binder struct {
@ -29,52 +26,25 @@ func (binder *Binder) Context(parent component.InstallationContext) component.In
}
//go:embed docker-compose.yml
var composeTemplate []byte
func (binder *Binder) buildYML() ([]byte, error) {
var dockerCompose yaml.Node
if err := yaml.Unmarshal(composeTemplate, &dockerCompose); err != nil {
return nil, err
}
for dockerCompose.Kind == yaml.DocumentNode {
dockerCompose = *dockerCompose.Content[0]
}
{
ports := binder.Config.Listen.ComposePorts("8000")
portsNode, err := yamlx.Marshal(ports)
if err != nil {
return nil, err
}
if err := yamlx.Replace(&dockerCompose, *portsNode, "services", "binder", "ports"); err != nil {
return nil, err
}
}
{
command := binder.Config.HTTP.TCPMuxCommand("0.0.0.0:8000", "http:80", "http:443", "ssh:2222")
commandNode, err := yamlx.Marshal(command)
if err != nil {
return nil, err
}
if err := yamlx.Replace(&dockerCompose, *commandNode, "services", "binder", "command"); err != nil {
return nil, err
}
}
// do the final marshal
return yaml.Marshal(dockerCompose)
}
var composeTemplate embed.FS
func (binder *Binder) Stack() component.StackWithResources {
return component.MakeStack(binder, component.StackWithResources{
ReadComposeFile: func() (io.Reader, error) {
data, err := binder.buildYML()
if err != nil {
ContextPath: ".",
Resources: composeTemplate,
ComposerYML: func(root *yaml.Node) (*yaml.Node, error) {
ports := binder.Config.Listen.ComposePorts("8000")
if err := yamlx.ReplaceWith(root, ports, "services", "binder", "ports"); err != nil {
return nil, err
}
return bytes.NewReader(data), nil
command := binder.Config.HTTP.TCPMuxCommand("0.0.0.0:8000", "http:80", "http:443", "ssh:2222")
if err := yamlx.ReplaceWith(root, command, "services", "binder", "command"); err != nil {
return nil, err
}
return root, nil
},
EnvContext: map[string]string{