component/web: Implement nicer compose reader
This commit is contained in:
parent
1855090f26
commit
668f1dd193
6 changed files with 130 additions and 85 deletions
|
|
@ -2,55 +2,18 @@ package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/cli"
|
|
||||||
"github.com/compose-spec/compose-go/loader"
|
|
||||||
ctypes "github.com/compose-spec/compose-go/types"
|
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/compose"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ComposeProject represents a compose project
|
|
||||||
type ComposeProject = *ctypes.Project
|
|
||||||
|
|
||||||
// LoadComposeProject loads a docker compose project from the given path.
|
|
||||||
// The returned name indicates the name, as would be found by the 'docker compose' executable.
|
|
||||||
// If the project could not be found, an appropriate error is returned.
|
|
||||||
//
|
|
||||||
// NOTE: This intentionally omits using any kind of api for docker compose.
|
|
||||||
// This saves a *a lot* of dependencies./
|
|
||||||
func (docker *Docker) LoadComposeProject(path string) (project ComposeProject, err error) {
|
|
||||||
ppath, err := filepath.Abs(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
opts, err := cli.NewProjectOptions(
|
|
||||||
/* configs = */ nil,
|
|
||||||
cli.WithWorkingDirectory(ppath),
|
|
||||||
cli.WithDefaultConfigPath,
|
|
||||||
cli.WithName(loader.NormalizeProjectName(filepath.Base(ppath))),
|
|
||||||
cli.WithDotEnv,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
proj, err := cli.ProjectFromOptions(opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return proj, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Containers loads the compose project at path, connects to the docker daemon, and then lists all containers belonging to the given services.
|
// Containers loads the compose project at path, connects to the docker daemon, and then lists all containers belonging to the given services.
|
||||||
// If services is empty, all containers belonging to any service are returned.
|
// If services is empty, all containers belonging to any service are returned.
|
||||||
func (docker *Docker) Containers(ctx context.Context, path string, services ...string) (containers []types.Container, err error) {
|
func (docker *Docker) Containers(ctx context.Context, path string, services ...string) (containers []types.Container, err error) {
|
||||||
proj, err := docker.LoadComposeProject(path)
|
proj, err := compose.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +39,7 @@ const (
|
||||||
//
|
//
|
||||||
// services optionally filters the returned containers by the services they belong to.
|
// services optionally filters the returned containers by the services they belong to.
|
||||||
// If services is empty, all containers are returned, else containers belonging to any of the services included.
|
// If services is empty, all containers are returned, else containers belonging to any of the services included.
|
||||||
func (*Docker) containers(ctx context.Context, project ComposeProject, client DockerClient, all bool, services ...string) ([]types.Container, error) {
|
func (*Docker) containers(ctx context.Context, project compose.Project, client DockerClient, all bool, services ...string) ([]types.Container, error) {
|
||||||
// build filters
|
// build filters
|
||||||
f := filters.NewArgs(
|
f := filters.NewArgs(
|
||||||
filters.Arg("label", projectLabel+"="+project.Name),
|
filters.Arg("label", projectLabel+"="+project.Name),
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/compose"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/execx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/execx"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
|
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
|
||||||
|
|
@ -154,10 +155,13 @@ type StackWithResources struct {
|
||||||
//
|
//
|
||||||
// The Resources holds these, with appropriate resources specified below.
|
// The Resources holds these, with appropriate resources specified below.
|
||||||
// These all refer to paths within the Resource filesystem.
|
// These all refer to paths within the Resource filesystem.
|
||||||
Resources fs.FS
|
Resources fs.FS
|
||||||
ContextPath string // the 'docker compose' stack context, containing e.g. 'docker-compose.yml'.
|
|
||||||
EnvPath string // the '.env' template, will be installed using [unpack.InstallTemplate].
|
ContextPath string // the 'docker compose' stack context. Can, but does not have to, contain 'docker-compose.yml'
|
||||||
EnvContext map[string]string // context when instantiating the '.env' template
|
ReadComposeFile func() (io.Reader, error) // read the 'docker-compose.yml' (if not contained in context)
|
||||||
|
|
||||||
|
EnvPath string // the '.env' template, will be installed using [unpack.InstallTemplate].
|
||||||
|
EnvContext map[string]string // context when instantiating the '.env' template
|
||||||
|
|
||||||
CopyContextFiles []string // Files to copy from the installation context
|
CopyContextFiles []string // Files to copy from the installation context
|
||||||
|
|
||||||
|
|
@ -190,6 +194,40 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write the docker-compose.yml file
|
||||||
|
if is.ReadComposeFile != nil {
|
||||||
|
err := (func() error {
|
||||||
|
// find the file to install!
|
||||||
|
dst := filepath.Join(is.Dir, "docker-compose.yml")
|
||||||
|
defer logging.ProgressF(progress, ctx, "[install] %s\n", dst)
|
||||||
|
|
||||||
|
// create the file
|
||||||
|
yml, err := fsx.Create(dst, fsx.DefaultFilePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer yml.Close()
|
||||||
|
|
||||||
|
// open the source file from the context
|
||||||
|
src, err := is.ReadComposeFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if srcc, ok := src.(io.Closer); ok {
|
||||||
|
defer srcc.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy it over!
|
||||||
|
{
|
||||||
|
_, err := io.Copy(yml, src)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// configure .env
|
// configure .env
|
||||||
envDest := filepath.Join(is.Dir, ".env")
|
envDest := filepath.Join(is.Dir, ".env")
|
||||||
if is.EnvPath != "" && is.EnvContext != nil {
|
if is.EnvPath != "" && is.EnvContext != nil {
|
||||||
|
|
@ -247,5 +285,14 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check that the stack can be loaded
|
||||||
|
{
|
||||||
|
logging.ProgressF(progress, ctx, "[checking]")
|
||||||
|
_, err := compose.Open(is.Dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"embed"
|
"embed"
|
||||||
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
|
|
@ -26,46 +28,36 @@ func (*Web) Context(parent component.InstallationContext) component.Installation
|
||||||
return parent
|
return parent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (web Web) Stack() component.StackWithResources {
|
//go:embed web.env
|
||||||
if web.Config.HTTP.HTTPSEnabled() {
|
var webEnv embed.FS
|
||||||
return web.stackHTTPS()
|
|
||||||
} else {
|
//go:embed docker-compose-http.yml
|
||||||
return web.stackHTTP()
|
var dockerComposeHTTP []byte
|
||||||
|
|
||||||
|
//go:embed docker-compose-https.yml
|
||||||
|
var dockerComposeHTTPS []byte
|
||||||
|
|
||||||
|
func (web *Web) Stack() component.StackWithResources {
|
||||||
|
var stack component.StackWithResources
|
||||||
|
stack.Resources = webEnv
|
||||||
|
stack.EnvPath = "web.env"
|
||||||
|
|
||||||
|
stack.EnvContext = map[string]string{
|
||||||
|
"DOCKER_NETWORK_NAME": web.Config.Docker.Network,
|
||||||
|
"CERT_EMAIL": web.Config.HTTP.CertbotEmail,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if web.Config.HTTP.HTTPSEnabled() {
|
||||||
//go:embed all:web-https
|
stack.ReadComposeFile = func() (io.Reader, error) {
|
||||||
//go:embed web.env
|
return bytes.NewReader(dockerComposeHTTPS), nil
|
||||||
var httpsResources embed.FS
|
}
|
||||||
|
stack.TouchFilesPerm = 0600
|
||||||
func (web *Web) stackHTTPS() component.StackWithResources {
|
stack.TouchFiles = []string{"acme.json"}
|
||||||
return component.MakeStack(web, component.StackWithResources{
|
} else {
|
||||||
Resources: httpsResources,
|
stack.ReadComposeFile = func() (io.Reader, error) {
|
||||||
ContextPath: "web-https",
|
return bytes.NewReader(dockerComposeHTTP), nil
|
||||||
EnvPath: "web.env",
|
}
|
||||||
|
}
|
||||||
EnvContext: map[string]string{
|
|
||||||
"DOCKER_NETWORK_NAME": web.Config.Docker.Network,
|
return component.MakeStack(web, stack)
|
||||||
"CERT_EMAIL": web.Config.HTTP.CertbotEmail,
|
|
||||||
},
|
|
||||||
TouchFilesPerm: 0600,
|
|
||||||
TouchFiles: []string{"acme.json"},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:embed all:web-http
|
|
||||||
//go:embed web.env
|
|
||||||
var httpResources embed.FS
|
|
||||||
|
|
||||||
func (web *Web) stackHTTP() component.StackWithResources {
|
|
||||||
return component.MakeStack(web, component.StackWithResources{
|
|
||||||
Resources: httpResources,
|
|
||||||
ContextPath: "web-http",
|
|
||||||
EnvPath: "web.env",
|
|
||||||
|
|
||||||
EnvContext: map[string]string{
|
|
||||||
"DOCKER_NETWORK_NAME": web.Config.Docker.Network,
|
|
||||||
"CERT_EMAIL": web.Config.HTTP.CertbotEmail,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
43
pkg/compose/compose.go
Normal file
43
pkg/compose/compose.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package compose
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/cli"
|
||||||
|
"github.com/compose-spec/compose-go/loader"
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ComposeProject represents a compose project
|
||||||
|
type Project = *types.Project
|
||||||
|
|
||||||
|
// Open loads a docker compose project from the given path.
|
||||||
|
// The returned name indicates the name, as would be found by the 'docker compose' executable.
|
||||||
|
// If the project could not be found, an appropriate error is returned.
|
||||||
|
//
|
||||||
|
// NOTE: This intentionally omits using any kind of api for docker compose.
|
||||||
|
// This saves a *a lot* of dependencies.
|
||||||
|
func Open(path string) (project Project, err error) {
|
||||||
|
ppath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts, err := cli.NewProjectOptions(
|
||||||
|
/* configs = */ nil,
|
||||||
|
cli.WithWorkingDirectory(ppath),
|
||||||
|
cli.WithDefaultConfigPath,
|
||||||
|
cli.WithName(loader.NormalizeProjectName(filepath.Base(ppath))),
|
||||||
|
cli.WithDotEnv,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
proj, err := cli.ProjectFromOptions(opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return proj, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue