2021-07-21 01:56:28 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
2021-09-06 07:58:26 +00:00
|
|
|
"context"
|
2021-07-21 01:56:28 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-09-12 23:11:22 +00:00
|
|
|
"path/filepath"
|
2021-07-21 01:56:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-06 07:58:26 +00:00
|
|
|
|
|
|
|
libstack "github.com/portainer/docker-compose-wrapper"
|
|
|
|
"github.com/portainer/docker-compose-wrapper/compose"
|
|
|
|
|
2021-07-21 01:56:28 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/proxy"
|
|
|
|
"github.com/portainer/portainer/api/http/proxy/factory"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ComposeStackManager is a wrapper for docker-compose binary
|
|
|
|
type ComposeStackManager struct {
|
2021-09-06 07:58:26 +00:00
|
|
|
deployer libstack.Deployer
|
2021-07-21 01:56:28 +00:00
|
|
|
proxyManager *proxy.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewComposeStackManager returns a docker-compose wrapper if corresponding binary present, otherwise nil
|
|
|
|
func NewComposeStackManager(binaryPath string, configPath string, proxyManager *proxy.Manager) (*ComposeStackManager, error) {
|
2021-09-06 07:58:26 +00:00
|
|
|
deployer, err := compose.NewComposeDeployer(binaryPath, configPath)
|
2021-07-21 01:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ComposeStackManager{
|
2021-09-06 07:58:26 +00:00
|
|
|
deployer: deployer,
|
2021-07-21 01:56:28 +00:00
|
|
|
proxyManager: proxyManager,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComposeSyntaxMaxVersion returns the maximum supported version of the docker compose syntax
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *ComposeStackManager) ComposeSyntaxMaxVersion() string {
|
2021-07-21 01:56:28 +00:00
|
|
|
return portainer.ComposeSyntaxMaxVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up builds, (re)creates and starts containers in the background. Wraps `docker-compose up -d` command
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *ComposeStackManager) Up(ctx context.Context, stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
|
|
|
url, proxy, err := manager.fetchEndpointProxy(endpoint)
|
2021-07-21 01:56:28 +00:00
|
|
|
if err != nil {
|
2021-09-29 23:58:10 +00:00
|
|
|
return errors.Wrap(err, "failed to fetch environment proxy")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if proxy != nil {
|
|
|
|
defer proxy.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
envFilePath, err := createEnvFile(stack)
|
|
|
|
if err != nil {
|
2021-08-17 01:12:07 +00:00
|
|
|
return errors.Wrap(err, "failed to create env file")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 23:11:22 +00:00
|
|
|
filePaths := getStackFiles(stack)
|
|
|
|
err = manager.deployer.Deploy(ctx, stack.ProjectPath, url, stack.Name, filePaths, envFilePath)
|
2021-08-17 01:12:07 +00:00
|
|
|
return errors.Wrap(err, "failed to deploy a stack")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Down stops and removes containers, networks, images, and volumes. Wraps `docker-compose down --remove-orphans` command
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *ComposeStackManager) Down(ctx context.Context, stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
|
|
|
url, proxy, err := manager.fetchEndpointProxy(endpoint)
|
2021-07-21 01:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if proxy != nil {
|
|
|
|
defer proxy.Close()
|
|
|
|
}
|
|
|
|
|
2021-09-12 23:11:22 +00:00
|
|
|
filePaths := getStackFiles(stack)
|
|
|
|
err = manager.deployer.Remove(ctx, stack.ProjectPath, url, stack.Name, filePaths)
|
|
|
|
return errors.Wrap(err, "failed to remove a stack")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
// NormalizeStackName returns a new stack name with unsupported characters replaced
|
2021-09-29 23:58:10 +00:00
|
|
|
func (manager *ComposeStackManager) NormalizeStackName(name string) string {
|
2021-10-01 03:56:34 +00:00
|
|
|
return stackNameNormalizeRegex.ReplaceAllString(strings.ToLower(name), "")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *ComposeStackManager) fetchEndpointProxy(endpoint *portainer.Endpoint) (string, *factory.ProxyServer, error) {
|
2021-07-21 01:56:28 +00:00
|
|
|
if strings.HasPrefix(endpoint.URL, "unix://") || strings.HasPrefix(endpoint.URL, "npipe://") {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
proxy, err := manager.proxyManager.CreateAgentProxyServer(endpoint)
|
2021-07-21 01:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-07 22:59:50 +00:00
|
|
|
return fmt.Sprintf("tcp://127.0.0.1:%d", proxy.Port), proxy, nil
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createEnvFile(stack *portainer.Stack) (string, error) {
|
|
|
|
if stack.Env == nil || len(stack.Env) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
envFilePath := path.Join(stack.ProjectPath, "stack.env")
|
|
|
|
|
|
|
|
envfile, err := os.OpenFile(envFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range stack.Env {
|
|
|
|
envfile.WriteString(fmt.Sprintf("%s=%s\n", v.Name, v.Value))
|
|
|
|
}
|
|
|
|
envfile.Close()
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
return "stack.env", nil
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
2021-09-12 23:11:22 +00:00
|
|
|
|
|
|
|
// getStackFiles returns list of stack's confile file paths.
|
|
|
|
// items in the list would be sanitized according to following criterias:
|
|
|
|
// 1. no empty paths
|
|
|
|
// 2. no "../xxx" paths that are trying to escape stack folder
|
|
|
|
// 3. no dir paths
|
|
|
|
// 4. root paths would be made relative
|
|
|
|
func getStackFiles(stack *portainer.Stack) []string {
|
|
|
|
paths := make([]string, 0, len(stack.AdditionalFiles)+1)
|
|
|
|
|
|
|
|
for _, p := range append([]string{stack.EntryPoint}, stack.AdditionalFiles...) {
|
|
|
|
if strings.HasPrefix(p, "/") {
|
|
|
|
p = `.` + p
|
|
|
|
}
|
|
|
|
|
|
|
|
if p == `` || p == `.` || strings.HasPrefix(p, `..`) || strings.HasSuffix(p, string(filepath.Separator)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
paths = append(paths, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths
|
|
|
|
}
|