mirror of
https://github.com/portainer/portainer.git
synced 2025-07-22 06:49:40 +02:00
feat(stacks): support compose v2.0 stack (#1963)
This commit is contained in:
parent
ef15cd30eb
commit
e3d564325b
174 changed files with 7898 additions and 5849 deletions
91
api/libcompose/compose_stack.go
Normal file
91
api/libcompose/compose_stack.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
package libcompose
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/portainer/libcompose/docker"
|
||||
"github.com/portainer/libcompose/docker/client"
|
||||
"github.com/portainer/libcompose/docker/ctx"
|
||||
"github.com/portainer/libcompose/lookup"
|
||||
"github.com/portainer/libcompose/project"
|
||||
"github.com/portainer/libcompose/project/options"
|
||||
"github.com/portainer/portainer"
|
||||
)
|
||||
|
||||
// ComposeStackManager represents a service for managing compose stacks.
|
||||
type ComposeStackManager struct {
|
||||
dataPath string
|
||||
}
|
||||
|
||||
// NewComposeStackManager initializes a new ComposeStackManager service.
|
||||
func NewComposeStackManager(dataPath string) *ComposeStackManager {
|
||||
return &ComposeStackManager{
|
||||
dataPath: dataPath,
|
||||
}
|
||||
}
|
||||
|
||||
// Up will deploy a compose stack (equivalent of docker-compose up)
|
||||
func (manager *ComposeStackManager) Up(stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
||||
clientFactory, err := client.NewDefaultFactory(client.Options{
|
||||
TLS: endpoint.TLSConfig.TLS,
|
||||
TLSVerify: endpoint.TLSConfig.TLSSkipVerify,
|
||||
Host: endpoint.URL,
|
||||
TLSCAFile: endpoint.TLSCACertPath,
|
||||
TLSCertFile: endpoint.TLSCertPath,
|
||||
TLSKeyFile: endpoint.TLSKeyPath,
|
||||
APIVersion: "1.24",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
composeFilePath := path.Join(stack.ProjectPath, stack.EntryPoint)
|
||||
proj, err := docker.NewProject(&ctx.Context{
|
||||
ConfigDir: manager.dataPath,
|
||||
Context: project.Context{
|
||||
ComposeFiles: []string{composeFilePath},
|
||||
EnvironmentLookup: &lookup.EnvfileLookup{
|
||||
Path: filepath.Join(stack.ProjectPath, ".env"),
|
||||
},
|
||||
ProjectName: stack.Name,
|
||||
},
|
||||
ClientFactory: clientFactory,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return proj.Up(context.Background(), options.Up{})
|
||||
}
|
||||
|
||||
// Down will shutdown a compose stack (equivalent of docker-compose down)
|
||||
func (manager *ComposeStackManager) Down(stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
||||
clientFactory, err := client.NewDefaultFactory(client.Options{
|
||||
TLS: endpoint.TLSConfig.TLS,
|
||||
TLSVerify: endpoint.TLSConfig.TLSSkipVerify,
|
||||
Host: endpoint.URL,
|
||||
TLSCAFile: endpoint.TLSCACertPath,
|
||||
TLSCertFile: endpoint.TLSCertPath,
|
||||
TLSKeyFile: endpoint.TLSKeyPath,
|
||||
APIVersion: "1.24",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
composeFilePath := path.Join(stack.ProjectPath, stack.EntryPoint)
|
||||
proj, err := docker.NewProject(&ctx.Context{
|
||||
Context: project.Context{
|
||||
ComposeFiles: []string{composeFilePath},
|
||||
ProjectName: stack.Name,
|
||||
},
|
||||
ClientFactory: clientFactory,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return proj.Down(context.Background(), options.Down{RemoveVolume: true, RemoveOrphans: true})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue