2017-10-15 17:24:40 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-15 12:12:49 +00:00
|
|
|
"encoding/json"
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2019-07-25 22:38:07 +00:00
|
|
|
"fmt"
|
2017-11-01 09:30:02 +00:00
|
|
|
"os"
|
2017-10-15 17:24:40 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
2021-07-22 21:53:42 +00:00
|
|
|
"strings"
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-08-17 01:12:07 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/stackutils"
|
2017-10-15 17:24:40 +00:00
|
|
|
)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
// SwarmStackManager represents a service for managing stacks.
|
|
|
|
type SwarmStackManager struct {
|
2019-07-25 22:38:07 +00:00
|
|
|
binaryPath string
|
2021-09-06 07:58:26 +00:00
|
|
|
configPath string
|
2019-07-25 22:38:07 +00:00
|
|
|
signatureService portainer.DigitalSignatureService
|
|
|
|
fileService portainer.FileService
|
|
|
|
reverseTunnelService portainer.ReverseTunnelService
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
// NewSwarmStackManager initializes a new SwarmStackManager service.
|
2018-05-06 07:15:57 +00:00
|
|
|
// It also updates the configuration of the Docker CLI binary.
|
2021-09-06 07:58:26 +00:00
|
|
|
func NewSwarmStackManager(binaryPath, configPath string, signatureService portainer.DigitalSignatureService, fileService portainer.FileService, reverseTunnelService portainer.ReverseTunnelService) (*SwarmStackManager, error) {
|
2018-06-11 13:13:19 +00:00
|
|
|
manager := &SwarmStackManager{
|
2019-07-25 22:38:07 +00:00
|
|
|
binaryPath: binaryPath,
|
2021-09-06 07:58:26 +00:00
|
|
|
configPath: configPath,
|
2019-07-25 22:38:07 +00:00
|
|
|
signatureService: signatureService,
|
|
|
|
fileService: fileService,
|
|
|
|
reverseTunnelService: reverseTunnelService,
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 07:58:26 +00:00
|
|
|
err := manager.updateDockerCLIConfiguration(manager.configPath)
|
2018-05-06 07:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
|
|
|
return manager, nil
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 12:22:09 +00:00
|
|
|
// Login executes the docker login command against a list of registries (including DockerHub).
|
2021-07-14 09:15:21 +00:00
|
|
|
func (manager *SwarmStackManager) Login(registries []portainer.Registry, endpoint *portainer.Endpoint) {
|
2021-09-06 07:58:26 +00:00
|
|
|
command, args := manager.prepareDockerCommandAndArgs(manager.binaryPath, manager.configPath, endpoint)
|
2017-10-26 12:22:09 +00:00
|
|
|
for _, registry := range registries {
|
|
|
|
if registry.Authentication {
|
|
|
|
registryArgs := append(args, "login", "--username", registry.Username, "--password", registry.Password, registry.URL)
|
2018-02-09 09:55:51 +00:00
|
|
|
runCommandAndCaptureStdErr(command, registryArgs, nil, "")
|
2017-10-26 12:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout executes the docker logout command.
|
2018-06-11 13:13:19 +00:00
|
|
|
func (manager *SwarmStackManager) Logout(endpoint *portainer.Endpoint) error {
|
2021-09-06 07:58:26 +00:00
|
|
|
command, args := manager.prepareDockerCommandAndArgs(manager.binaryPath, manager.configPath, endpoint)
|
2017-10-26 12:22:09 +00:00
|
|
|
args = append(args, "logout")
|
2018-02-09 09:55:51 +00:00
|
|
|
return runCommandAndCaptureStdErr(command, args, nil, "")
|
2017-10-26 12:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deploy executes the docker stack deploy command.
|
2018-06-11 13:13:19 +00:00
|
|
|
func (manager *SwarmStackManager) Deploy(stack *portainer.Stack, prune bool, endpoint *portainer.Endpoint) error {
|
2021-08-17 01:12:07 +00:00
|
|
|
filePaths := stackutils.GetStackFilePaths(stack)
|
2021-09-06 07:58:26 +00:00
|
|
|
command, args := manager.prepareDockerCommandAndArgs(manager.binaryPath, manager.configPath, endpoint)
|
2018-01-20 17:05:01 +00:00
|
|
|
|
|
|
|
if prune {
|
2021-08-17 01:12:07 +00:00
|
|
|
args = append(args, "stack", "deploy", "--prune", "--with-registry-auth")
|
2018-01-20 17:05:01 +00:00
|
|
|
} else {
|
2021-08-17 01:12:07 +00:00
|
|
|
args = append(args, "stack", "deploy", "--with-registry-auth")
|
2018-01-20 17:05:01 +00:00
|
|
|
}
|
2017-11-01 09:30:02 +00:00
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
args = configureFilePaths(args, filePaths)
|
|
|
|
args = append(args, stack.Name)
|
|
|
|
|
2017-11-01 09:30:02 +00:00
|
|
|
env := make([]string, 0)
|
|
|
|
for _, envvar := range stack.Env {
|
|
|
|
env = append(env, envvar.Name+"="+envvar.Value)
|
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
return runCommandAndCaptureStdErr(command, args, env, stack.ProjectPath)
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 12:22:09 +00:00
|
|
|
// Remove executes the docker stack rm command.
|
2018-06-11 13:13:19 +00:00
|
|
|
func (manager *SwarmStackManager) Remove(stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
2021-09-06 07:58:26 +00:00
|
|
|
command, args := manager.prepareDockerCommandAndArgs(manager.binaryPath, manager.configPath, endpoint)
|
2017-10-15 17:24:40 +00:00
|
|
|
args = append(args, "stack", "rm", stack.Name)
|
2018-02-09 09:55:51 +00:00
|
|
|
return runCommandAndCaptureStdErr(command, args, nil, "")
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 09:55:51 +00:00
|
|
|
func runCommandAndCaptureStdErr(command string, args []string, env []string, workingDir string) error {
|
2017-10-15 17:24:40 +00:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stderr = &stderr
|
2018-02-09 09:55:51 +00:00
|
|
|
cmd.Dir = workingDir
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2017-11-01 09:30:02 +00:00
|
|
|
if env != nil {
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New(stderr.String())
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *SwarmStackManager) prepareDockerCommandAndArgs(binaryPath, configPath string, endpoint *portainer.Endpoint) (string, []string) {
|
2017-10-15 17:24:40 +00:00
|
|
|
// Assume Linux as a default
|
|
|
|
command := path.Join(binaryPath, "docker")
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
command = path.Join(binaryPath, "docker.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := make([]string, 0)
|
2021-09-06 07:58:26 +00:00
|
|
|
args = append(args, "--config", configPath)
|
2019-07-25 22:38:07 +00:00
|
|
|
|
|
|
|
endpointURL := endpoint.URL
|
2020-07-05 23:21:03 +00:00
|
|
|
if endpoint.Type == portainer.EdgeAgentOnDockerEnvironment {
|
2019-07-25 22:38:07 +00:00
|
|
|
tunnel := manager.reverseTunnelService.GetTunnelDetails(endpoint.ID)
|
2020-04-27 01:54:37 +00:00
|
|
|
endpointURL = fmt.Sprintf("tcp://127.0.0.1:%d", tunnel.Port)
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, "-H", endpointURL)
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2018-05-19 14:25:11 +00:00
|
|
|
if endpoint.TLSConfig.TLS {
|
2017-10-15 17:24:40 +00:00
|
|
|
args = append(args, "--tls")
|
|
|
|
|
|
|
|
if !endpoint.TLSConfig.TLSSkipVerify {
|
|
|
|
args = append(args, "--tlsverify", "--tlscacert", endpoint.TLSConfig.TLSCACertPath)
|
2021-01-14 21:05:33 +00:00
|
|
|
} else {
|
|
|
|
args = append(args, "--tlscacert", "''")
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.TLSConfig.TLSCertPath != "" && endpoint.TLSConfig.TLSKeyPath != "" {
|
|
|
|
args = append(args, "--tlscert", endpoint.TLSConfig.TLSCertPath, "--tlskey", endpoint.TLSConfig.TLSKeyPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command, args
|
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2021-09-06 07:58:26 +00:00
|
|
|
func (manager *SwarmStackManager) updateDockerCLIConfiguration(configPath string) error {
|
|
|
|
configFilePath := path.Join(configPath, "config.json")
|
2018-05-15 12:12:49 +00:00
|
|
|
config, err := manager.retrieveConfigurationFromDisk(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-12-11 22:19:23 +00:00
|
|
|
signature, err := manager.signatureService.CreateSignature(portainer.PortainerAgentSignatureMessage)
|
2018-05-06 07:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-15 12:12:49 +00:00
|
|
|
if config["HttpHeaders"] == nil {
|
|
|
|
config["HttpHeaders"] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
headersObject := config["HttpHeaders"].(map[string]interface{})
|
|
|
|
headersObject["X-PortainerAgent-ManagerOperation"] = "1"
|
|
|
|
headersObject["X-PortainerAgent-Signature"] = signature
|
|
|
|
headersObject["X-PortainerAgent-PublicKey"] = manager.signatureService.EncodedPublicKey()
|
|
|
|
|
|
|
|
err = manager.fileService.WriteJSONToFile(configFilePath, config)
|
2018-05-06 07:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-15 12:12:49 +00:00
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
func (manager *SwarmStackManager) retrieveConfigurationFromDisk(path string) (map[string]interface{}, error) {
|
2018-05-15 12:12:49 +00:00
|
|
|
var config map[string]interface{}
|
|
|
|
|
|
|
|
raw, err := manager.fileService.GetFileContent(path)
|
|
|
|
if err != nil {
|
|
|
|
return make(map[string]interface{}), nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
err = json.Unmarshal(raw, &config)
|
2018-05-15 12:12:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
2021-07-22 21:53:42 +00:00
|
|
|
|
|
|
|
func (manager *SwarmStackManager) NormalizeStackName(name string) string {
|
2021-10-01 03:56:34 +00:00
|
|
|
return stackNameNormalizeRegex.ReplaceAllString(strings.ToLower(name), "")
|
2021-07-22 21:53:42 +00:00
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
|
|
|
|
func configureFilePaths(args []string, filePaths []string) []string {
|
|
|
|
for _, path := range filePaths {
|
|
|
|
args = append(args, "--compose-file", path)
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|