2018-06-11 13:13:19 +00:00
|
|
|
package libcompose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-25 22:38:07 +00:00
|
|
|
"fmt"
|
2018-06-11 13:13:19 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
|
2018-07-12 07:17:07 +00:00
|
|
|
"github.com/portainer/libcompose/config"
|
2018-06-11 13:13:19 +00:00
|
|
|
"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"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ComposeStackManager represents a service for managing compose stacks.
|
|
|
|
type ComposeStackManager struct {
|
2019-07-25 22:38:07 +00:00
|
|
|
dataPath string
|
|
|
|
reverseTunnelService portainer.ReverseTunnelService
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewComposeStackManager initializes a new ComposeStackManager service.
|
2019-07-25 22:38:07 +00:00
|
|
|
func NewComposeStackManager(dataPath string, reverseTunnelService portainer.ReverseTunnelService) *ComposeStackManager {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &ComposeStackManager{
|
2019-07-25 22:38:07 +00:00
|
|
|
dataPath: dataPath,
|
|
|
|
reverseTunnelService: reverseTunnelService,
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
func (manager *ComposeStackManager) createClient(endpoint *portainer.Endpoint) (client.Factory, error) {
|
|
|
|
|
|
|
|
endpointURL := endpoint.URL
|
|
|
|
if endpoint.Type == portainer.EdgeAgentEnvironment {
|
|
|
|
tunnel := manager.reverseTunnelService.GetTunnelDetails(endpoint.ID)
|
|
|
|
endpointURL = fmt.Sprintf("tcp://localhost:%d", tunnel.Port)
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:11:47 +00:00
|
|
|
clientOpts := client.Options{
|
2019-07-25 22:38:07 +00:00
|
|
|
Host: endpointURL,
|
2018-07-24 09:11:47 +00:00
|
|
|
APIVersion: portainer.SupportedDockerAPIVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.TLSConfig.TLS {
|
|
|
|
clientOpts.TLS = endpoint.TLSConfig.TLS
|
|
|
|
clientOpts.TLSVerify = !endpoint.TLSConfig.TLSSkipVerify
|
|
|
|
clientOpts.TLSCAFile = endpoint.TLSConfig.TLSCACertPath
|
|
|
|
clientOpts.TLSCertFile = endpoint.TLSConfig.TLSCertPath
|
|
|
|
clientOpts.TLSKeyFile = endpoint.TLSConfig.TLSKeyPath
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewDefaultFactory(clientOpts)
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
// Up will deploy a compose stack (equivalent of docker-compose up)
|
|
|
|
func (manager *ComposeStackManager) Up(stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
2018-07-24 09:11:47 +00:00
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
clientFactory, err := manager.createClient(endpoint)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:17:07 +00:00
|
|
|
env := make(map[string]string)
|
|
|
|
for _, envvar := range stack.Env {
|
|
|
|
env[envvar.Name] = envvar.Value
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
composeFilePath := path.Join(stack.ProjectPath, stack.EntryPoint)
|
|
|
|
proj, err := docker.NewProject(&ctx.Context{
|
|
|
|
ConfigDir: manager.dataPath,
|
|
|
|
Context: project.Context{
|
|
|
|
ComposeFiles: []string{composeFilePath},
|
2018-07-12 07:17:07 +00:00
|
|
|
EnvironmentLookup: &lookup.ComposableEnvLookup{
|
|
|
|
Lookups: []config.EnvironmentLookup{
|
|
|
|
&lookup.EnvfileLookup{
|
|
|
|
Path: filepath.Join(stack.ProjectPath, ".env"),
|
|
|
|
},
|
|
|
|
&lookup.MapLookup{
|
|
|
|
Vars: env,
|
|
|
|
},
|
|
|
|
},
|
2018-06-11 13:13:19 +00:00
|
|
|
},
|
|
|
|
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 {
|
2019-07-25 22:38:07 +00:00
|
|
|
clientFactory, err := manager.createClient(endpoint)
|
2018-06-11 13:13:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:20:16 +00:00
|
|
|
return proj.Down(context.Background(), options.Down{RemoveVolume: false, RemoveOrphans: true})
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|