Skip to content

Commit

Permalink
feat(edge-stack): relative path support for edge stack EE-5521 (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmenginnz authored Jun 22, 2023
1 parent 50127f4 commit d083787
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 108 deletions.
6 changes: 6 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ const (
DefaultAWSClientCertPath = "/certs/aws-client.crt"
// DefaultAWSClientKeyPath is the default path to the AWS client key file
DefaultAWSClientKeyPath = "/certs/aws-client.key"
// DefaultUnpackerImage is the default name of unpacker image
DefaultUnpackerImage = "portainer/compose-unpacker:latest"
// ComposeUnpackerImageEnvVar is the default environment variable name of the unpacker image
ComposeUnpackerImageEnvVar = "COMPOSE_UNPACKER_IMAGE"
// ComposePathPrefix is the folder name of compose path in unpacker
ComposePathPrefix = "portainer-compose-unpacker"
)

const (
Expand Down
20 changes: 20 additions & 0 deletions docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
)

func ContainerCreate(
config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
platform *specs.Platform,
containerName string,
) (container.CreateResponse, error) {
var err error
var container container.CreateResponse

err = withCli(func(cli *client.Client) error {
container, err = cli.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, platform, containerName)
return err
})

return container, err
}

func ContainerStart(name string, opts types.ContainerStartOptions) error {
return withCli(func(cli *client.Client) error {
return cli.ContainerStart(context.Background(), name, opts)
Expand Down
73 changes: 73 additions & 0 deletions docker/docker_copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package docker

import (
"fmt"
"github.com/portainer/agent"
"math/rand"
"os"
"os/exec"
"path"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/rs/zerolog/log"
)

// CopyToHostViaUnpacker copies src folder to composeDestination folder in the host
func CopyToHostViaUnpacker(src, dst string, stackID int, stackName, composeDestination, assetPath string) error {
unpackerContainer, err := createUnpackerContainer(stackID, stackName, composeDestination)
if err != nil {
return err
}

err = copyToContainer(assetPath, src, unpackerContainer.ID, dst)
if err != nil {
return err
}

err = ContainerDelete(unpackerContainer.ID, types.ContainerRemoveOptions{})
return err
}

func getUnpackerImage() string {
image := os.Getenv(agent.ComposeUnpackerImageEnvVar)
if image == "" {
image = agent.DefaultUnpackerImage
}

return image
}

func createUnpackerContainer(stackID int, stackName, composeDestination string) (container.CreateResponse, error) {
image := getUnpackerImage()

r := rand.New(rand.NewSource(time.Now().UnixNano()))
containerName := fmt.Sprintf("portainer-unpacker-%d-%s-%d", stackID, stackName, r.Intn(100))

return ContainerCreate(
&container.Config{
Image: image,
},
&container.HostConfig{
Binds: []string{
fmt.Sprintf("%s:%s", composeDestination, composeDestination),
},
},
nil,
nil,
containerName,
)
}

func copyToContainer(assetPath, src, containerID, dst string) error {
dockerBinaryPath := path.Join(assetPath, "docker")
fullDst := fmt.Sprintf("%s:%s", containerID, dst)
cmd := exec.Command(dockerBinaryPath, "cp", src, fullDst)
output, err := cmd.Output()
if err != nil {
return err
}
log.Debug().Str("output", string(output)).Msg("Copy stack to host filesystem")
return nil
}
1 change: 0 additions & 1 deletion edge/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type StackStatus struct {
ID int
Version int
Name string // used in async mode
FileContent string // used in async mode
CommandOperation string // used in async mode
}

Expand Down
1 change: 0 additions & 1 deletion edge/client/portainer_edge_async_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/portainer/agent/kubernetes"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/edge"

"github.com/rs/zerolog/log"
"github.com/wI2L/jsondiff"
)
Expand Down
Loading

0 comments on commit d083787

Please sign in to comment.