2019-01-12 22:45:25 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/client"
|
2020-02-04 18:38:41 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2020-02-07 00:17:58 -06:00
|
|
|
"github.com/pkg/errors"
|
2019-01-12 22:45:25 -06:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerRunExecutorInput the input for the NewDockerRunExecutor function
|
|
|
|
type NewDockerRunExecutorInput struct {
|
2019-01-17 02:45:37 -06:00
|
|
|
Image string
|
|
|
|
Entrypoint []string
|
|
|
|
Cmd []string
|
|
|
|
WorkingDir string
|
|
|
|
Env []string
|
|
|
|
Binds []string
|
|
|
|
Content map[string]io.Reader
|
|
|
|
Volumes []string
|
|
|
|
Name string
|
|
|
|
ReuseContainers bool
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerRunExecutor function to create a run executor for the container
|
|
|
|
func NewDockerRunExecutor(input NewDockerRunExecutorInput) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
cr := new(containerReference)
|
|
|
|
cr.input = input
|
|
|
|
|
|
|
|
return common.
|
|
|
|
NewInfoExecutor("docker run image=%s entrypoint=%+q cmd=%+q", input.Image, input.Entrypoint, input.Cmd).
|
|
|
|
Then(
|
|
|
|
common.NewPipelineExecutor(
|
|
|
|
cr.connect(),
|
|
|
|
cr.find(),
|
|
|
|
cr.remove().IfBool(!input.ReuseContainers),
|
|
|
|
cr.create(),
|
|
|
|
cr.copyContent(),
|
|
|
|
cr.attach(),
|
|
|
|
cr.start(),
|
|
|
|
cr.wait(),
|
|
|
|
).Finally(
|
|
|
|
cr.remove().IfBool(!input.ReuseContainers),
|
|
|
|
).IfNot(common.Dryrun),
|
|
|
|
)
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
type containerReference struct {
|
|
|
|
input NewDockerRunExecutorInput
|
|
|
|
cli *client.Client
|
|
|
|
id string
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) connect() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2019-01-15 23:54:37 -06:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
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
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
cli.NegotiateAPIVersion(ctx)
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
for _, container := range containers {
|
|
|
|
for _, name := range container.Names {
|
|
|
|
if name[1:] == cr.input.Name {
|
|
|
|
cr.id = container.ID
|
|
|
|
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 {
|
|
|
|
return errors.WithStack(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
cr.id = ""
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("Removed 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) create() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if cr.id != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
|
|
|
|
|
|
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-07 00:17:58 -06:00
|
|
|
if len(input.Volumes) > 0 {
|
|
|
|
config.Volumes = make(map[string]struct{})
|
|
|
|
for _, vol := range input.Volumes {
|
|
|
|
config.Volumes[vol] = struct{}{}
|
2019-01-17 02:45:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
resp, err := cr.cli.ContainerCreate(ctx, config, &container.HostConfig{
|
|
|
|
Binds: input.Binds,
|
|
|
|
}, nil, input.Name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
logger.Debugf("Created container name=%s id=%v from image %v", input.Name, resp.ID, input.Image)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (cr *containerReference) copyContent() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
for dstPath, srcReader := range cr.input.Content {
|
|
|
|
logger.Debugf("Extracting content to '%s'", dstPath)
|
|
|
|
err := cr.cli.CopyToContainer(ctx, cr.id, dstPath, srcReader, 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)
|
|
|
|
}
|
|
|
|
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
|
|
if !isTerminal || os.Getenv("NORAW") != "" {
|
|
|
|
go logDockerOutput(ctx, out.Reader)
|
|
|
|
} else {
|
|
|
|
go streamDockerOutput(ctx, out.Reader)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
logger.Debugf("STARTING image=%s entrypoint=%s cmd=%v", cr.input.Image, cr.input.Entrypoint, cr.input.Cmd)
|
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
|
|
|
|
} else if statusCode == 78 {
|
|
|
|
return fmt.Errorf("exit with `NEUTRAL`: 78")
|
|
|
|
}
|
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
|
|
|
}
|