2019-01-12 22:45:25 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-23 17:01:25 -06:00
|
|
|
"archive/tar"
|
2021-01-12 00:39:43 -06:00
|
|
|
"bufio"
|
2020-02-23 17:01:25 -06:00
|
|
|
"bytes"
|
2019-01-12 22:45:25 -06:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-24 18:38:49 -06:00
|
|
|
"io/ioutil"
|
2019-01-12 22:45:25 -06:00
|
|
|
"os"
|
2020-02-24 18:38:49 -06:00
|
|
|
"path/filepath"
|
2021-01-12 00:39:43 -06:00
|
|
|
"regexp"
|
2021-01-12 00:41:35 -06:00
|
|
|
"runtime"
|
2021-01-14 23:37:38 -06:00
|
|
|
"strings"
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-04-16 18:24:30 -05:00
|
|
|
"github.com/go-git/go-billy/v5/helper/polyfill"
|
|
|
|
"github.com/go-git/go-billy/v5/osfs"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
|
|
|
2020-05-03 23:15:42 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper"
|
2019-01-12 22:45:25 -06:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2020-02-23 17:01:25 -06:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2019-01-12 22:45:25 -06:00
|
|
|
"github.com/docker/docker/client"
|
2020-02-11 11:10:35 -06:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2021-03-28 23:08:40 -05:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
|
2021-03-30 12:10:42 -05:00
|
|
|
"github.com/Masterminds/semver"
|
2020-02-07 00:17:58 -06:00
|
|
|
"github.com/pkg/errors"
|
2020-02-23 17:01:25 -06:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-01-12 00:39:43 -06:00
|
|
|
"golang.org/x/term"
|
2021-03-28 23:08:40 -05:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
// NewContainerInput the input for the New function
|
|
|
|
type NewContainerInput struct {
|
2020-03-09 19:43:24 -05:00
|
|
|
Image string
|
2021-05-05 11:37:17 -05:00
|
|
|
Username string
|
|
|
|
Password string
|
2020-03-09 19:43:24 -05:00
|
|
|
Entrypoint []string
|
|
|
|
Cmd []string
|
|
|
|
WorkingDir string
|
|
|
|
Env []string
|
|
|
|
Binds []string
|
|
|
|
Mounts map[string]string
|
|
|
|
Name string
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
NetworkMode string
|
2020-08-01 15:21:49 -05:00
|
|
|
Privileged bool
|
2021-02-27 10:31:25 -06:00
|
|
|
UsernsMode string
|
2021-03-28 23:08:40 -05:00
|
|
|
Platform string
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileEntry is a file to copy to a container
|
|
|
|
type FileEntry struct {
|
|
|
|
Name string
|
|
|
|
Mode int64
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container for managing docker run containers
|
|
|
|
type Container interface {
|
|
|
|
Create() common.Executor
|
|
|
|
Copy(destPath string, files ...*FileEntry) common.Executor
|
2021-05-03 09:37:20 -05:00
|
|
|
CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor
|
2020-02-23 17:01:25 -06:00
|
|
|
Pull(forcePull bool) common.Executor
|
|
|
|
Start(attach bool) common.Executor
|
2021-05-24 12:09:03 -05:00
|
|
|
Exec(command []string, env map[string]string, user string) common.Executor
|
2021-05-05 18:11:43 -05:00
|
|
|
UpdateFromEnv(srcPath string, env *map[string]string) common.Executor
|
2021-05-06 08:30:12 -05:00
|
|
|
UpdateFromPath(env *map[string]string) common.Executor
|
2020-02-23 17:01:25 -06:00
|
|
|
Remove() common.Executor
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContainer creates a reference to a container
|
|
|
|
func NewContainer(input *NewContainerInput) Container {
|
2020-02-07 00:17:58 -06:00
|
|
|
cr := new(containerReference)
|
|
|
|
cr.input = input
|
2020-02-23 17:01:25 -06:00
|
|
|
return cr
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
|
2021-03-30 12:10:42 -05:00
|
|
|
// supportsContainerImagePlatform returns true if the underlying Docker server
|
|
|
|
// API version is 1.41 and beyond
|
|
|
|
func supportsContainerImagePlatform(cli *client.Client) bool {
|
|
|
|
ctx := context.TODO()
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
ver, err := cli.ServerVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("Failed to get Docker API Version: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sv, err := semver.NewVersion(ver.APIVersion)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("Failed to unmarshal Docker Version: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
constraint, _ := semver.NewConstraint(">= 1.41")
|
|
|
|
return constraint.Check(sv)
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (cr *containerReference) Create() common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return common.
|
2021-03-28 23:08:40 -05:00
|
|
|
NewDebugExecutor("%sdocker create image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd).
|
2020-02-07 00:17:58 -06:00
|
|
|
Then(
|
|
|
|
common.NewPipelineExecutor(
|
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
|
|
|
cr.create(),
|
2020-02-23 17:01:25 -06:00
|
|
|
).IfNot(common.Dryrun),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
func (cr *containerReference) Start(attach bool) common.Executor {
|
|
|
|
return common.
|
2021-03-28 23:08:40 -05:00
|
|
|
NewInfoExecutor("%sdocker run image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd).
|
2020-02-23 17:01:25 -06:00
|
|
|
Then(
|
|
|
|
common.NewPipelineExecutor(
|
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
|
|
|
cr.attach().IfBool(attach),
|
2020-02-07 00:17:58 -06:00
|
|
|
cr.start(),
|
2020-02-23 17:01:25 -06:00
|
|
|
cr.wait().IfBool(attach),
|
2020-02-07 00:17:58 -06:00
|
|
|
).IfNot(common.Dryrun),
|
|
|
|
)
|
|
|
|
}
|
2020-02-23 17:01:25 -06:00
|
|
|
func (cr *containerReference) Pull(forcePull bool) common.Executor {
|
|
|
|
return NewDockerPullExecutor(NewDockerPullExecutorInput{
|
|
|
|
Image: cr.input.Image,
|
|
|
|
ForcePull: forcePull,
|
2021-03-28 23:08:40 -05:00
|
|
|
Platform: cr.input.Platform,
|
2021-05-05 11:37:17 -05:00
|
|
|
Username: cr.input.Username,
|
|
|
|
Password: cr.input.Password,
|
2020-02-23 17:01:25 -06:00
|
|
|
})
|
|
|
|
}
|
2021-05-05 18:11:43 -05:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common.Executor {
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
|
|
|
cr.copyContent(destPath, files...),
|
|
|
|
).IfNot(common.Dryrun)
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:37:20 -05:00
|
|
|
func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor {
|
2020-02-24 18:38:49 -06:00
|
|
|
return common.NewPipelineExecutor(
|
2020-02-24 19:48:21 -06:00
|
|
|
common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath),
|
2021-05-24 12:09:03 -05:00
|
|
|
cr.Exec([]string{"mkdir", "-p", destPath}, nil, ""),
|
2021-05-03 09:37:20 -05:00
|
|
|
cr.copyDir(destPath, srcPath, useGitIgnore),
|
2020-02-24 18:38:49 -06:00
|
|
|
).IfNot(common.Dryrun)
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:11:43 -05:00
|
|
|
func (cr *containerReference) UpdateFromEnv(srcPath string, env *map[string]string) common.Executor {
|
|
|
|
return cr.extractEnv(srcPath, env).IfNot(common.Dryrun)
|
2021-01-12 00:39:43 -06:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:30:12 -05:00
|
|
|
func (cr *containerReference) UpdateFromPath(env *map[string]string) common.Executor {
|
|
|
|
return cr.extractPath(env).IfNot(common.Dryrun)
|
|
|
|
}
|
|
|
|
|
2021-05-24 12:09:03 -05:00
|
|
|
func (cr *containerReference) Exec(command []string, env map[string]string, user string) common.Executor {
|
2020-02-23 17:01:25 -06:00
|
|
|
return common.NewPipelineExecutor(
|
2021-05-24 12:09:03 -05:00
|
|
|
common.NewInfoExecutor("%sdocker exec cmd=[%s] user=%s", logPrefix, strings.Join(command, " "), user),
|
2020-02-23 17:01:25 -06:00
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
2021-05-24 12:09:03 -05:00
|
|
|
cr.exec(command, env, user),
|
2020-02-23 17:01:25 -06:00
|
|
|
).IfNot(common.Dryrun)
|
|
|
|
}
|
2021-05-05 18:11:43 -05:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (cr *containerReference) Remove() common.Executor {
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
|
|
|
).Finally(
|
|
|
|
cr.remove(),
|
|
|
|
).IfNot(common.Dryrun)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
type containerReference struct {
|
|
|
|
cli *client.Client
|
|
|
|
id string
|
2020-02-23 17:01:25 -06:00
|
|
|
input *NewContainerInput
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-05-03 23:15:42 -05:00
|
|
|
func GetDockerClient(ctx context.Context) (*client.Client, error) {
|
|
|
|
var err error
|
|
|
|
var cli *client.Client
|
|
|
|
|
|
|
|
// TODO: this should maybe need to be a global option, not hidden in here?
|
|
|
|
// though i'm not sure how that works out when there's another Executor :D
|
|
|
|
// I really would like something that works on OSX native for eg
|
|
|
|
dockerHost := os.Getenv("DOCKER_HOST")
|
|
|
|
|
|
|
|
if strings.HasPrefix(dockerHost, "ssh://") {
|
|
|
|
var helper *connhelper.ConnectionHelper
|
|
|
|
|
|
|
|
helper, err = connhelper.GetConnectionHelper(dockerHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cli, err = client.NewClientWithOpts(
|
|
|
|
client.WithHost(helper.Host),
|
|
|
|
client.WithDialContext(helper.Dialer),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
cli, err = client.NewClientWithOpts(client.FromEnv)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
cli.NegotiateAPIVersion(ctx)
|
|
|
|
|
|
|
|
return cli, err
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) connect() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2020-02-23 17:01:25 -06:00
|
|
|
if cr.cli != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-03 23:15:42 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
2020-05-03 23:15:42 -05:00
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
cr.cli = cli
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) find() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2020-02-23 17:01:25 -06:00
|
|
|
if cr.id != "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
containers, err := cr.cli.ContainerList(ctx, types.ContainerListOptions{
|
|
|
|
All: true,
|
|
|
|
})
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
2020-02-07 00:17:58 -06:00
|
|
|
return errors.WithStack(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2021-01-12 00:39:43 -06:00
|
|
|
for _, c := range containers {
|
|
|
|
for _, name := range c.Names {
|
2020-02-07 00:17:58 -06:00
|
|
|
if name[1:] == cr.input.Name {
|
2021-01-12 00:39:43 -06:00
|
|
|
cr.id = c.ID
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-17 02:45:37 -06:00
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
cr.id = ""
|
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) remove() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if cr.id == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger := common.Logger(ctx)
|
|
|
|
err := cr.cli.ContainerRemove(context.Background(), cr.id, types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-24 00:34:48 -06:00
|
|
|
logger.Error(errors.WithStack(err))
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("Removed container: %v", cr.id)
|
2020-02-23 18:36:44 -06:00
|
|
|
cr.id = ""
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) create() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if cr.id != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logger := common.Logger(ctx)
|
2021-01-12 00:39:43 -06:00
|
|
|
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
|
2020-02-07 00:17:58 -06:00
|
|
|
|
|
|
|
input := cr.input
|
|
|
|
config := &container.Config{
|
|
|
|
Image: input.Image,
|
|
|
|
Cmd: input.Cmd,
|
|
|
|
Entrypoint: input.Entrypoint,
|
|
|
|
WorkingDir: input.WorkingDir,
|
|
|
|
Env: input.Env,
|
|
|
|
Tty: isTerminal,
|
|
|
|
}
|
2019-01-17 02:45:37 -06:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
mounts := make([]mount.Mount, 0)
|
|
|
|
for mountSource, mountTarget := range input.Mounts {
|
|
|
|
mounts = append(mounts, mount.Mount{
|
|
|
|
Type: mount.TypeVolume,
|
|
|
|
Source: mountSource,
|
|
|
|
Target: mountTarget,
|
|
|
|
})
|
2019-01-17 02:45:37 -06:00
|
|
|
}
|
|
|
|
|
2021-03-30 12:10:42 -05:00
|
|
|
var platSpecs *specs.Platform
|
2021-05-02 10:15:13 -05:00
|
|
|
if supportsContainerImagePlatform(cr.cli) && cr.input.Platform != "" {
|
2021-03-30 12:10:42 -05:00
|
|
|
desiredPlatform := strings.SplitN(cr.input.Platform, `/`, 2)
|
2021-03-28 23:08:40 -05:00
|
|
|
|
2021-03-30 12:10:42 -05:00
|
|
|
if len(desiredPlatform) != 2 {
|
|
|
|
logger.Panicf("Incorrect container platform option. %s is not a valid platform.", cr.input.Platform)
|
|
|
|
}
|
2021-03-28 23:08:40 -05:00
|
|
|
|
2021-03-30 12:10:42 -05:00
|
|
|
platSpecs = &specs.Platform{
|
|
|
|
Architecture: desiredPlatform[1],
|
|
|
|
OS: desiredPlatform[0],
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
resp, err := cr.cli.ContainerCreate(ctx, config, &container.HostConfig{
|
2020-03-09 19:43:24 -05:00
|
|
|
Binds: input.Binds,
|
|
|
|
Mounts: mounts,
|
|
|
|
NetworkMode: container.NetworkMode(input.NetworkMode),
|
2020-08-01 15:21:49 -05:00
|
|
|
Privileged: input.Privileged,
|
2021-02-27 10:31:25 -06:00
|
|
|
UsernsMode: container.UsernsMode(input.UsernsMode),
|
2021-03-30 12:10:42 -05:00
|
|
|
}, nil, platSpecs, input.Name)
|
2020-02-07 00:17:58 -06:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2021-03-28 23:08:40 -05:00
|
|
|
logger.Debugf("Created container name=%s id=%v from image %v (platform: %s)", input.Name, resp.ID, input.Image, input.Platform)
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("ENV ==> %v", input.Env)
|
2019-01-17 02:45:37 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
cr.id = resp.ID
|
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 00:39:43 -06:00
|
|
|
var singleLineEnvPattern, mulitiLineEnvPattern *regexp.Regexp
|
|
|
|
|
2021-05-05 18:11:43 -05:00
|
|
|
func (cr *containerReference) extractEnv(srcPath string, env *map[string]string) common.Executor {
|
2021-01-12 00:39:43 -06:00
|
|
|
if singleLineEnvPattern == nil {
|
|
|
|
singleLineEnvPattern = regexp.MustCompile("^([^=]+)=([^=]+)$")
|
|
|
|
mulitiLineEnvPattern = regexp.MustCompile(`^([^<]+)<<(\w+)$`)
|
|
|
|
}
|
|
|
|
|
|
|
|
localEnv := *env
|
|
|
|
return func(ctx context.Context) error {
|
2021-05-05 18:11:43 -05:00
|
|
|
envTar, _, err := cr.cli.CopyFromContainer(ctx, cr.id, srcPath)
|
2021-01-12 00:39:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-06 08:30:12 -05:00
|
|
|
defer envTar.Close()
|
2021-05-05 18:11:43 -05:00
|
|
|
reader := tar.NewReader(envTar)
|
2021-01-12 00:39:43 -06:00
|
|
|
_, err = reader.Next()
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
s := bufio.NewScanner(reader)
|
|
|
|
multiLineEnvKey := ""
|
|
|
|
multiLineEnvDelimiter := ""
|
|
|
|
multiLineEnvContent := ""
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
if singleLineEnv := singleLineEnvPattern.FindStringSubmatch(line); singleLineEnv != nil {
|
|
|
|
localEnv[singleLineEnv[1]] = singleLineEnv[2]
|
|
|
|
}
|
|
|
|
if line == multiLineEnvDelimiter {
|
|
|
|
localEnv[multiLineEnvKey] = multiLineEnvContent
|
|
|
|
multiLineEnvKey, multiLineEnvDelimiter, multiLineEnvContent = "", "", ""
|
|
|
|
}
|
|
|
|
if multiLineEnvKey != "" && multiLineEnvDelimiter != "" {
|
|
|
|
if multiLineEnvContent != "" {
|
|
|
|
multiLineEnvContent += "\n"
|
|
|
|
}
|
|
|
|
multiLineEnvContent += line
|
|
|
|
}
|
|
|
|
if mulitiLineEnvStart := mulitiLineEnvPattern.FindStringSubmatch(line); mulitiLineEnvStart != nil {
|
|
|
|
multiLineEnvKey = mulitiLineEnvStart[1]
|
|
|
|
multiLineEnvDelimiter = mulitiLineEnvStart[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
env = &localEnv
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 08:30:12 -05:00
|
|
|
func (cr *containerReference) extractPath(env *map[string]string) common.Executor {
|
|
|
|
localEnv := *env
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
pathTar, _, err := cr.cli.CopyFromContainer(ctx, cr.id, localEnv["GITHUB_PATH"])
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
defer pathTar.Close()
|
|
|
|
|
|
|
|
reader := tar.NewReader(pathTar)
|
|
|
|
_, err = reader.Next()
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
s := bufio.NewScanner(reader)
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
2021-05-18 08:22:39 -05:00
|
|
|
localEnv["PATH"] = fmt.Sprintf("%s:%s", line, localEnv["PATH"])
|
2021-05-06 08:30:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
env = &localEnv
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 12:09:03 -05:00
|
|
|
func (cr *containerReference) exec(cmd []string, env map[string]string, user string) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2021-01-12 00:41:35 -06:00
|
|
|
// Fix slashes when running on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
var newCmd []string
|
|
|
|
for _, v := range cmd {
|
|
|
|
newCmd = append(newCmd, strings.ReplaceAll(v, `\`, `/`))
|
|
|
|
}
|
|
|
|
cmd = newCmd
|
|
|
|
}
|
2021-01-14 23:37:38 -06:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
logger.Debugf("Exec command '%s'", cmd)
|
2021-01-12 00:39:43 -06:00
|
|
|
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
|
2020-02-23 17:01:25 -06:00
|
|
|
envList := make([]string, 0)
|
|
|
|
for k, v := range env {
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
idResp, err := cr.cli.ContainerExecCreate(ctx, cr.id, types.ExecConfig{
|
2021-05-24 12:09:03 -05:00
|
|
|
User: user,
|
2020-02-23 17:01:25 -06:00
|
|
|
Cmd: cmd,
|
|
|
|
WorkingDir: cr.input.WorkingDir,
|
|
|
|
Env: envList,
|
|
|
|
Tty: isTerminal,
|
|
|
|
AttachStderr: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cr.cli.ContainerExecAttach(ctx, idResp.ID, types.ExecStartCheck{
|
|
|
|
Tty: isTerminal,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2021-05-06 08:30:12 -05:00
|
|
|
defer resp.Close()
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
var outWriter io.Writer
|
|
|
|
outWriter = cr.input.Stdout
|
|
|
|
if outWriter == nil {
|
|
|
|
outWriter = os.Stdout
|
|
|
|
}
|
|
|
|
errWriter := cr.input.Stderr
|
|
|
|
if errWriter == nil {
|
|
|
|
errWriter = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isTerminal || os.Getenv("NORAW") != "" {
|
|
|
|
_, err = stdcopy.StdCopy(outWriter, errWriter, resp.Reader)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(outWriter, resp.Reader)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inspectResp, err := cr.cli.ContainerExecInspect(ctx, idResp.ID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inspectResp.ExitCode == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("exit with `FAILURE`: %v", inspectResp.ExitCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:32:48 -05:00
|
|
|
// nolint: gocyclo
|
2021-05-03 09:37:20 -05:00
|
|
|
func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor {
|
2020-02-24 18:38:49 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
tarFile, err := ioutil.TempFile("", "act")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("Writing tarball %s from %s", tarFile.Name(), srcPath)
|
|
|
|
defer tarFile.Close()
|
2020-02-24 19:48:21 -06:00
|
|
|
defer os.Remove(tarFile.Name())
|
2020-02-24 18:38:49 -06:00
|
|
|
tw := tar.NewWriter(tarFile)
|
|
|
|
|
|
|
|
srcPrefix := filepath.Dir(srcPath)
|
|
|
|
if !strings.HasSuffix(srcPrefix, string(filepath.Separator)) {
|
|
|
|
srcPrefix += string(filepath.Separator)
|
|
|
|
}
|
2020-02-24 19:48:21 -06:00
|
|
|
log.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
2020-02-24 18:38:49 -06:00
|
|
|
|
2021-05-03 09:37:20 -05:00
|
|
|
var ignorer gitignore.Matcher
|
|
|
|
if useGitIgnore {
|
|
|
|
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Error loading .gitignore: %v", err)
|
|
|
|
}
|
2020-03-09 20:32:48 -05:00
|
|
|
|
2021-05-03 09:37:20 -05:00
|
|
|
ignorer = gitignore.NewMatcher(ps)
|
|
|
|
}
|
2020-03-18 08:55:39 -05:00
|
|
|
|
2020-02-24 18:38:49 -06:00
|
|
|
err = filepath.Walk(srcPath, func(file string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:55:39 -05:00
|
|
|
sansPrefix := strings.TrimPrefix(file, srcPrefix)
|
|
|
|
split := strings.Split(sansPrefix, string(filepath.Separator))
|
2021-05-03 09:37:20 -05:00
|
|
|
if ignorer != nil && ignorer.Match(split, fi.IsDir()) {
|
2020-03-18 08:55:39 -05:00
|
|
|
if fi.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2020-02-24 18:38:49 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:55:39 -05:00
|
|
|
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
|
2020-04-23 01:24:31 -05:00
|
|
|
linkName := fi.Name()
|
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
linkName, err = os.Readlink(file)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessagef(err, "unable to readlink %s", file)
|
|
|
|
}
|
|
|
|
} else if !fi.Mode().IsRegular() {
|
2020-03-09 20:32:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:38:49 -06:00
|
|
|
// create a new dir/file header
|
2020-04-23 01:24:31 -05:00
|
|
|
header, err := tar.FileInfoHeader(fi, linkName)
|
2020-02-24 18:38:49 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the name to correctly reflect the desired destination when untaring
|
2020-09-15 09:00:15 -05:00
|
|
|
header.Name = filepath.ToSlash(sansPrefix)
|
2020-03-14 01:21:25 -05:00
|
|
|
header.Mode = int64(fi.Mode())
|
|
|
|
header.ModTime = fi.ModTime()
|
2020-02-24 18:38:49 -06:00
|
|
|
|
|
|
|
// write the header
|
|
|
|
if err := tw.WriteHeader(header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// open files for taring
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy file data into tar writer
|
|
|
|
if _, err := io.Copy(tw, f); err != nil {
|
2020-04-23 01:48:25 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2020-10-06 12:02:37 -05:00
|
|
|
// symlinks don't need to be copied, ignore this error
|
2020-04-23 01:48:25 -05:00
|
|
|
err = nil
|
|
|
|
}
|
2020-02-24 18:38:49 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-18 11:53:42 -05:00
|
|
|
// manually close here after each file operation; deferring would cause each file close
|
2020-02-24 18:38:49 -06:00
|
|
|
// to wait until all operations have completed.
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("Extracting content from '%s' to '%s'", tarFile.Name(), dstPath)
|
|
|
|
_, err = tarFile.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
err = cr.cli.CopyToContainer(ctx, cr.id, dstPath, tarFile, types.CopyToContainerOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (cr *containerReference) copyContent(dstPath string, files ...*FileEntry) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
tw := tar.NewWriter(&buf)
|
|
|
|
for _, file := range files {
|
|
|
|
log.Debugf("Writing entry to tarball %s len:%d", file.Name, len(file.Body))
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: file.Name,
|
|
|
|
Mode: file.Mode,
|
|
|
|
Size: int64(len(file.Body)),
|
|
|
|
}
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2020-02-23 17:01:25 -06:00
|
|
|
if _, err := tw.Write([]byte(file.Body)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("Extracting content to '%s'", dstPath)
|
|
|
|
err := cr.cli.CopyToContainer(ctx, cr.id, dstPath, &buf, types.CopyToContainerOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) attach() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
out, err := cr.cli.ContainerAttach(ctx, cr.id, types.ContainerAttachOptions{
|
|
|
|
Stream: true,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2021-01-12 00:39:43 -06:00
|
|
|
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
|
2020-02-11 11:10:35 -06:00
|
|
|
|
|
|
|
var outWriter io.Writer
|
|
|
|
outWriter = cr.input.Stdout
|
|
|
|
if outWriter == nil {
|
|
|
|
outWriter = os.Stdout
|
|
|
|
}
|
|
|
|
errWriter := cr.input.Stderr
|
|
|
|
if errWriter == nil {
|
|
|
|
errWriter = os.Stderr
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2020-02-11 11:10:35 -06:00
|
|
|
go func() {
|
|
|
|
if !isTerminal || os.Getenv("NORAW") != "" {
|
|
|
|
_, err = stdcopy.StdCopy(outWriter, errWriter, out.Reader)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(outWriter, out.Reader)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
common.Logger(ctx).Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) start() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2020-02-23 17:01:25 -06:00
|
|
|
logger.Debugf("Starting container: %v", cr.id)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
if err := cr.cli.ContainerStart(ctx, cr.id, types.ContainerStartOptions{}); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("Started container: %v", cr.id)
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) wait() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
statusCh, errCh := cr.cli.ContainerWait(ctx, cr.id, container.WaitConditionNotRunning)
|
|
|
|
var statusCode int64
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
case status := <-statusCh:
|
|
|
|
statusCode = status.StatusCode
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("Return status: %v", statusCode)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
if statusCode == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
return fmt.Errorf("exit with `FAILURE`: %v", statusCode)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|