refactor(libstack): move library to portainer [EE-5474] (#9120)

pull/9122/head
Chaim Lev-Ari 2023-06-26 08:11:05 +07:00 committed by GitHub
parent 11571fd6ea
commit 8c16fbb8aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 691 additions and 0 deletions

View File

@ -4,5 +4,6 @@ use (
./api
./pkg/featureflags
./pkg/libhelm
./pkg/libstack
./third_party/digest
)

7
pkg/libstack/LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright 2021 Portainer.io
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3
pkg/libstack/README.md Normal file
View File

@ -0,0 +1,3 @@
# LibStack
LibStack is a library that provides an abstraction to run stacks. Currently it supports Docker Compose, but we plan to support other formats in the future.

View File

@ -0,0 +1,11 @@
package compose
import (
"github.com/portainer/portainer/pkg/libstack"
"github.com/portainer/portainer/pkg/libstack/compose/internal/composeplugin"
)
// NewComposeDeployer will try to create a wrapper for docker-compose plugin
func NewComposeDeployer(binaryPath, configPath string) (libstack.Deployer, error) {
return composeplugin.NewPluginWrapper(binaryPath, configPath)
}

View File

@ -0,0 +1,102 @@
package compose_test
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/portainer/portainer/pkg/libstack"
"github.com/portainer/portainer/pkg/libstack/compose"
)
func checkPrerequisites(t *testing.T) {
if _, err := os.Stat("docker-compose"); errors.Is(err, os.ErrNotExist) {
t.Fatal("docker-compose binary not found, please run download.sh and re-run this suite")
}
}
func Test_UpAndDown(t *testing.T) {
checkPrerequisites(t)
deployer, _ := compose.NewComposeDeployer("", "")
const composeFileContent = `
version: "3.9"
services:
busybox:
image: "alpine:3.7"
container_name: "test_container_one"
`
const overrideComposeFileContent = `
version: "3.9"
services:
busybox:
image: "alpine:latest"
container_name: "test_container_two"
`
const composeContainerName = "test_container_two"
dir := t.TempDir()
filePathOriginal, err := createFile(dir, "docker-compose.yml", composeFileContent)
if err != nil {
t.Fatal(err)
}
filePathOverride, err := createFile(dir, "docker-compose-override.yml", overrideComposeFileContent)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
err = deployer.Deploy(ctx, []string{filePathOriginal, filePathOverride}, libstack.DeployOptions{})
if err != nil {
t.Fatal(err)
}
if !containerExists(composeContainerName) {
t.Fatal("container should exist")
}
err = deployer.Remove(ctx, "", []string{filePathOriginal, filePathOverride}, libstack.Options{})
if err != nil {
t.Fatal(err)
}
if containerExists(composeContainerName) {
t.Fatal("container should be removed")
}
}
func createFile(dir, fileName, content string) (string, error) {
filePath := filepath.Join(dir, fileName)
f, err := os.Create(filePath)
if err != nil {
return "", err
}
f.WriteString(content)
f.Close()
return filePath, nil
}
func containerExists(containerName string) bool {
cmd := exec.Command("docker", "ps", "-a", "-f", fmt.Sprintf("name=%s", containerName))
out, err := cmd.Output()
if err != nil {
log.Fatalf("failed to list containers: %s", err)
}
return strings.Contains(string(out), containerName)
}

View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -euo pipefail
PLATFORM=$(go env GOOS)
ARCH=$(go env GOARCH)
COMPOSE_VERSION=v2.5.1
if [[ ${ARCH} == "amd64" ]]; then
ARCH="x86_64"
elif [[ ${ARCH} == "arm" ]]; then
ARCH="armv7"
elif [[ ${ARCH} == "arm64" ]]; then
ARCH="aarch64"
fi
if [[ "$PLATFORM" == "windows" ]]; then
wget -O "docker-compose.exe" "https://github.com/docker/compose/releases/download/$COMPOSE_VERSION/docker-compose-windows-${ARCH}.exe"
chmod +x "docker-compose.exe"
else
wget -O "docker-compose" "https://github.com/docker/compose/releases/download/$COMPOSE_VERSION/docker-compose-${PLATFORM}-${ARCH}"
chmod +x "docker-compose"
fi

View File

@ -0,0 +1,8 @@
package errors
import "errors"
var (
// ErrBinaryNotFound is returned when docker-compose binary is not found
ErrBinaryNotFound = errors.New("docker-compose binary not found")
)

View File

@ -0,0 +1,237 @@
package composeplugin
import (
"bytes"
"context"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/portainer/portainer/pkg/libstack"
"github.com/portainer/portainer/pkg/libstack/compose/internal/utils"
"github.com/rs/zerolog/log"
)
var (
MissingDockerComposePluginErr = errors.New("docker-compose plugin is missing from config path")
)
// PluginWrapper provide a type for managing docker compose commands
type PluginWrapper struct {
binaryPath string
configPath string
}
// NewPluginWrapper initializes a new ComposeWrapper service with local docker-compose binary.
func NewPluginWrapper(binaryPath, configPath string) (libstack.Deployer, error) {
if !utils.IsBinaryPresent(utils.ProgramPath(binaryPath, "docker-compose")) {
return nil, MissingDockerComposePluginErr
}
return &PluginWrapper{binaryPath: binaryPath, configPath: configPath}, nil
}
// Up create and start containers
func (wrapper *PluginWrapper) Deploy(ctx context.Context, filePaths []string, options libstack.DeployOptions) error {
output, err := wrapper.command(newUpCommand(filePaths, upOptions{
forceRecreate: options.ForceRecreate,
abortOnContainerExit: options.AbortOnContainerExit,
}), options.Options)
if len(output) != 0 {
if err != nil {
return err
}
log.Info().Msg("Stack deployment successful")
log.Debug().
Str("output", string(output)).
Msg("docker compose")
}
return err
}
// Down stop and remove containers
func (wrapper *PluginWrapper) Remove(ctx context.Context, projectName string, filePaths []string, options libstack.Options) error {
output, err := wrapper.command(newDownCommand(projectName, filePaths), options)
if len(output) != 0 {
if err != nil {
return err
}
log.Info().Msg("Stack removal successful")
log.Debug().
Str("output", string(output)).
Msg("docker compose")
}
return err
}
// Pull images
func (wrapper *PluginWrapper) Pull(ctx context.Context, filePaths []string, options libstack.Options) error {
output, err := wrapper.command(newPullCommand(filePaths), options)
if len(output) != 0 {
if err != nil {
return err
}
log.Info().Msg("Stack pull successful")
log.Debug().
Str("output", string(output)).
Msg("docker compose")
}
return err
}
// Validate stack file
func (wrapper *PluginWrapper) Validate(ctx context.Context, filePaths []string, options libstack.Options) error {
output, err := wrapper.command(newValidateCommand(filePaths), options)
if len(output) != 0 {
if err != nil {
return err
}
log.Info().Msg("Valid stack format")
log.Debug().
Str("output", string(output)).
Msg("docker compose")
}
return err
}
// Command execute a docker-compose command
func (wrapper *PluginWrapper) command(command composeCommand, options libstack.Options) ([]byte, error) {
program := utils.ProgramPath(wrapper.binaryPath, "docker-compose")
if options.ProjectName != "" {
command.WithProjectName(options.ProjectName)
}
if options.EnvFilePath != "" {
command.WithEnvFilePath(options.EnvFilePath)
}
if options.Host != "" {
command.WithHost(options.Host)
}
var stderr bytes.Buffer
args := []string{}
args = append(args, command.ToArgs()...)
cmd := exec.Command(program, args...)
cmd.Dir = options.WorkingDir
if wrapper.configPath != "" || len(options.Env) > 0 {
cmd.Env = os.Environ()
}
if wrapper.configPath != "" {
cmd.Env = append(cmd.Env, "DOCKER_CONFIG="+wrapper.configPath)
}
cmd.Env = append(cmd.Env, options.Env...)
log.Debug().
Str("command", program).
Strs("args", args).
Interface("env", cmd.Env).
Msg("run command")
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
errOutput := stderr.String()
log.Warn().
Str("output", string(output)).
Str("error_output", errOutput).
Err(err).
Msg("docker compose command failed")
return nil, errors.New(errOutput)
}
return output, nil
}
type composeCommand struct {
globalArgs []string // docker-compose global arguments: --host host -f file.yaml
subCommandAndArgs []string // docker-compose subcommand: up, down folllowed by subcommand arguments
}
func newCommand(command []string, filePaths []string) composeCommand {
args := []string{}
for _, path := range filePaths {
args = append(args, "-f")
args = append(args, strings.TrimSpace(path))
}
return composeCommand{
globalArgs: args,
subCommandAndArgs: command,
}
}
type upOptions struct {
forceRecreate bool
abortOnContainerExit bool
}
func newUpCommand(filePaths []string, options upOptions) composeCommand {
args := []string{"up"}
if options.abortOnContainerExit {
args = append(args, "--abort-on-container-exit")
} else { // detach by default, not working with --abort-on-container-exit
args = append(args, "-d")
}
if options.forceRecreate {
args = append(args, "--force-recreate")
}
return newCommand(args, filePaths)
}
func newDownCommand(projectName string, filePaths []string) composeCommand {
cmd := newCommand([]string{"down", "--remove-orphans"}, filePaths)
cmd.WithProjectName(projectName)
return cmd
}
func newPullCommand(filePaths []string) composeCommand {
return newCommand([]string{"pull"}, filePaths)
}
func newValidateCommand(filePaths []string) composeCommand {
return newCommand([]string{"config", "--quiet"}, filePaths)
}
func (command *composeCommand) WithHost(host string) {
// prepend compatibility flags such as this one as they must appear before the
// regular global args otherwise docker-compose will throw an error
command.globalArgs = append([]string{"--host", host}, command.globalArgs...)
}
func (command *composeCommand) WithProjectName(projectName string) {
command.globalArgs = append(command.globalArgs, "--project-name", projectName)
}
func (command *composeCommand) WithEnvFilePath(envFilePath string) {
command.globalArgs = append(command.globalArgs, "--env-file", envFilePath)
}
func (command *composeCommand) ToArgs() []string {
return append(command.globalArgs, command.subCommandAndArgs...)
}

View File

@ -0,0 +1,136 @@
package composeplugin
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/portainer/portainer/pkg/libstack"
)
func checkPrerequisites(t *testing.T) {
if _, err := os.Stat("docker-compose"); errors.Is(err, os.ErrNotExist) {
t.Fatal("docker-compose binary not found, please run download.sh and re-run this test suite")
}
}
func setup(t *testing.T) libstack.Deployer {
w, err := NewPluginWrapper("", "")
if err != nil {
t.Fatal(err)
}
return w
}
func Test_NewCommand_SingleFilePath(t *testing.T) {
checkPrerequisites(t)
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml"})
expected := []string{"-f", "docker-compose.yml"}
if !reflect.DeepEqual(cmd.globalArgs, expected) {
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
}
}
func Test_NewCommand_MultiFilePaths(t *testing.T) {
checkPrerequisites(t)
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml", "docker-compose-override.yml"})
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
if !reflect.DeepEqual(cmd.globalArgs, expected) {
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
}
}
func Test_NewCommand_MultiFilePaths_WithSpaces(t *testing.T) {
checkPrerequisites(t)
cmd := newCommand([]string{"up", "-d"}, []string{" docker-compose.yml", "docker-compose-override.yml "})
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
if !reflect.DeepEqual(cmd.globalArgs, expected) {
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
}
}
func Test_UpAndDown(t *testing.T) {
checkPrerequisites(t)
const composeFileContent = `version: "3.9"
services:
busybox:
image: "alpine:3.7"
container_name: "test_container_one"`
const overrideComposeFileContent = `version: "3.9"
services:
busybox:
image: "alpine:latest"
container_name: "test_container_two"`
const composeContainerName = "test_container_two"
w := setup(t)
dir := t.TempDir()
filePathOriginal, err := createFile(dir, "docker-compose.yml", composeFileContent)
if err != nil {
t.Fatal(err)
}
filePathOverride, err := createFile(dir, "docker-compose-override.yml", overrideComposeFileContent)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
err = w.Deploy(ctx, []string{filePathOriginal, filePathOverride}, libstack.DeployOptions{})
if err != nil {
t.Fatal(err)
}
if !containerExists(composeContainerName) {
t.Fatal("container should exist")
}
err = w.Remove(ctx, "", []string{filePathOriginal, filePathOverride}, libstack.Options{})
if err != nil {
t.Fatal(err)
}
if containerExists(composeContainerName) {
t.Fatal("container should be removed")
}
}
func createFile(dir, fileName, content string) (string, error) {
filePath := filepath.Join(dir, fileName)
f, err := os.Create(filePath)
if err != nil {
return "", err
}
f.WriteString(content)
f.Close()
return filePath, nil
}
func containerExists(containerName string) bool {
cmd := exec.Command("docker", "ps", "-a", "-f", fmt.Sprintf("name=%s", containerName))
out, err := cmd.Output()
if err != nil {
log.Fatalf("failed to list containers: %s", err)
}
return strings.Contains(string(out), containerName)
}

View File

@ -0,0 +1,4 @@
version: "3.9"
services:
redis:
image: "redis:alpine"

View File

@ -0,0 +1,60 @@
package utils
import (
"os"
"os/exec"
"path"
"runtime"
"github.com/pkg/errors"
)
func osProgram(program string) string {
if runtime.GOOS == "windows" {
program += ".exe"
}
return program
}
func ProgramPath(rootPath, program string) string {
return path.Join(rootPath, osProgram(program))
}
// IsBinaryPresent check if docker compose binary is present
func IsBinaryPresent(program string) bool {
_, err := exec.LookPath(program)
return err == nil
}
// Copy copies sourcePath to destinationPath
func Copy(sourcePath, destinationPath string) error {
si, err := os.Stat(sourcePath)
if err != nil {
return errors.WithMessage(err, "file check failed")
}
input, err := os.ReadFile(sourcePath)
if err != nil {
return errors.WithMessage(err, "failed reading file")
}
err = os.WriteFile(destinationPath, input, si.Mode())
if err != nil {
return errors.WithMessage(err, "failed writing file")
}
return nil
}
// Move sourcePath to destinationPath
func Move(sourcePath, destinationPath string) error {
if err := Copy(sourcePath, destinationPath); err != nil {
return err
}
if err := os.Remove(sourcePath); err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,33 @@
package utils
import (
"testing"
)
func TestIsBinaryPresent(t *testing.T) {
type testCase struct {
Name string
Binary string
Expected bool
}
testCases := []testCase{
{
Name: "not existing",
Binary: "qwgq-er-gerw",
Expected: false,
},
{
Name: "docker-compose exists",
Binary: "docker-compose",
Expected: true,
},
}
for _, tc := range testCases {
got := IsBinaryPresent(tc.Binary)
if got != tc.Expected {
t.Errorf("Error in test %s got = %v, and Expected = %v.", tc.Name, got, tc.Expected)
}
}
}

14
pkg/libstack/go.mod Normal file
View File

@ -0,0 +1,14 @@
module github.com/portainer/portainer/pkg/libstack
go 1.19
require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.28.0
)
require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)

14
pkg/libstack/go.sum Normal file
View File

@ -0,0 +1,14 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

36
pkg/libstack/libstack.go Normal file
View File

@ -0,0 +1,36 @@
package libstack
import (
"context"
)
type Deployer interface {
Deploy(ctx context.Context, filePaths []string, options DeployOptions) error
// Remove stops and removes containers
//
// projectName or filePaths are required
// if projectName is supplied filePaths will be ignored
Remove(ctx context.Context, projectName string, filePaths []string, options Options) error
Pull(ctx context.Context, filePaths []string, options Options) error
Validate(ctx context.Context, filePaths []string, options Options) error
}
type Options struct {
WorkingDir string
Host string
ProjectName string
// EnvFilePath is the path to a .env file
EnvFilePath string
// Env is a list of environment variables to pass to the command, example: "FOO=bar"
Env []string
}
type DeployOptions struct {
Options
ForceRecreate bool
// AbortOnContainerExit will stop the deployment if a container exits.
// This is useful when running a onetime task.
//
// When this is set, docker compose will output its logs to stdout
AbortOnContainerExit bool ``
}