Skip to content

Commit

Permalink
fix: rework services
Browse files Browse the repository at this point in the history
  • Loading branch information
hackercat committed Aug 11, 2021
1 parent eb5729a commit 7d0173f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 86 deletions.
56 changes: 52 additions & 4 deletions pkg/container/docker_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ package container

import (
"context"
"fmt"

"github.com/docker/docker/api/types"
// "github.com/docker/docker/api/types/network"
"github.com/nektos/act/pkg/common"
)

func NewDockerNetworkCreateExecutor(name string) common.Executor {
func NewDockerNetworkCreateExecutor(name, subnet, ipRange, gateway string) common.Executor {
return func(ctx context.Context) error {
if common.Dryrun(ctx) {
return nil
}

cli, err := GetDockerClient(ctx)
if err != nil {
return err
}

_, err = cli.NetworkCreate(ctx, name, types.NetworkCreate{})
if err != nil {
if exists := DockerNetworkExists(ctx, name); exists {
return nil
// return fmt.Errorf("network '%s' already exists", name)
}

if _, err = cli.NetworkCreate(ctx, name, types.NetworkCreate{}); err != nil {
return err
}

Expand All @@ -25,12 +35,50 @@ func NewDockerNetworkCreateExecutor(name string) common.Executor {

func NewDockerNetworkRemoveExecutor(name string) common.Executor {
return func(ctx context.Context) error {
if common.Dryrun(ctx) {
return nil
}

cli, err := GetDockerClient(ctx)
if err != nil {
return err
}

cli.NetworkRemove(ctx, name)
err = cli.NetworkRemove(ctx, name)
if err != nil {
return err
}

return nil
}
}

func DockerNetworkExists(ctx context.Context, name string) bool {
if exists, err := dockerNetworkExists(ctx, name); err != nil && !exists || err == nil && !exists {
return false
} else if exists && err == nil || exists && err != nil {
return true
}
return false
}

func dockerNetworkExists(ctx context.Context, name string) (bool, error) {
log := common.Logger(ctx)

cli, err := GetDockerClient(ctx)
if err != nil {
log.Debug(err)
return false, err
}

_, err = cli.NetworkInspect(ctx, name, types.NetworkInspectOptions{})
if err != nil {
if err.Error() == fmt.Sprintf("Error: No such %s: %s", "network", name) {
log.Debug(err)
return false, err
}
return false, err
}

return true, nil
}
106 changes: 61 additions & 45 deletions pkg/container/docker_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type NewContainerInput struct {
Image string
Username string
Password string
Hostname string
Entrypoint []string
Cmd []string
WorkingDir string
Expand Down Expand Up @@ -74,6 +75,7 @@ type Container interface {
UpdateFromEnv(srcPath string, env *map[string]string) common.Executor
UpdateFromPath(env *map[string]string) common.Executor
Remove() common.Executor
SetContainerNetworkMode(mode string) common.Executor
}

// NewContainer creates a reference to a container
Expand Down Expand Up @@ -102,63 +104,73 @@ func supportsContainerImagePlatform(cli *client.Client) bool {
return constraint.Check(sv)
}

func (cr *containerReference) SetContainerNetworkMode(mode string) common.Executor {
return common.NewPipelineExecutor(
common.NewDebugExecutor("Changed network mode for container '%s' from '%s' to '%s'", cr.input.Name, cr.input.NetworkMode, mode),
func(ctx context.Context) error {
cr.input.NetworkMode = mode
return nil
},
)
}

func (cr *containerReference) ConnectToNetwork(name string) common.Executor {
return common.
NewDebugExecutor("%sdocker network connect %s %s", logPrefix, name, cr.input.Name).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.connectToNetwork(name),
).IfNot(common.Dryrun),
)
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker network connect %s %s", logPrefix, name, cr.input.Name),
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.connectToNetwork(name),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) Create(capAdd []string, capDrop []string) common.Executor {
return common.
NewInfoExecutor("%sdocker create image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.create(capAdd, capDrop),
).IfNot(common.Dryrun),
)
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker create image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd),
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.create(capAdd, capDrop),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) Start(attach bool) common.Executor {
return common.
NewInfoExecutor("%sdocker run image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.attach().IfBool(attach),
cr.start(),
cr.wait().IfBool(attach),
).IfNot(common.Dryrun),
)
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker run image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd),
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.attach().IfBool(attach),
cr.start(),
cr.wait().IfBool(attach),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) Pull(forcePull bool) common.Executor {
return common.
NewInfoExecutor("%sdocker pull image=%s platform=%s username=%s forcePull=%t", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Username, forcePull).
Then(
NewDockerPullExecutor(NewDockerPullExecutorInput{
Image: cr.input.Image,
ForcePull: forcePull,
Platform: cr.input.Platform,
Username: cr.input.Username,
Password: cr.input.Password,
}),
)
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker pull image=%s platform=%s username=%s forcePull=%t", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Username, forcePull),
NewDockerPullExecutor(NewDockerPullExecutorInput{
Image: cr.input.Image,
ForcePull: forcePull,
Platform: cr.input.Platform,
Username: cr.input.Username,
Password: cr.input.Password,
}),
)
}

func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common.Executor {
return common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.copyContent(destPath, files...),
).IfNot(common.Dryrun)
common.NewInfoExecutor("%sdocker cp ... destPath=%s", logPrefix, destPath),
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.copyContent(destPath, files...),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor {
Expand All @@ -184,7 +196,7 @@ func (cr *containerReference) UpdateFromPath(env *map[string]string) common.Exec

func (cr *containerReference) Exec(command []string, env map[string]string, user, workdir string) common.Executor {
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker exec cmd=[%s] user=%s workdir=%s", logPrefix, strings.Join(command, " "), user, workdir),
common.NewInfoExecutor("%sdocker exec cmd=%v user=%s workdir=%s", logPrefix, command, user, workdir),
cr.connect(),
cr.find(),
cr.exec(command, env, user, workdir),
Expand Down Expand Up @@ -239,7 +251,7 @@ func GetDockerClient(ctx context.Context) (*client.Client, error) {

func (cr *containerReference) connectToNetwork(name string) common.Executor {
return func(ctx context.Context) error {
return cr.cli.NetworkConnect(ctx, name, cr.input.Name, nil)
return cr.cli.NetworkConnect(ctx, name, cr.id, nil)
}
}

Expand Down Expand Up @@ -314,14 +326,17 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E

input := cr.input
config := &container.Config{
Hostname: input.Hostname,
Image: input.Image,
WorkingDir: input.WorkingDir,
Env: input.Env,
Tty: isTerminal,
}

if len(input.Cmd) > 0 {
config.Cmd = input.Cmd
}

if len(input.Entrypoint) > 0 {
config.Entrypoint = input.Entrypoint
}
Expand Down Expand Up @@ -360,6 +375,7 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E
if err != nil {
return errors.WithStack(err)
}

logger.Debugf("Created container name=%s id=%v from image %v (platform: %s)", input.Name, resp.ID, input.Image, input.Platform)
logger.Debugf("ENV ==> %v", input.Env)

Expand Down
Loading

0 comments on commit 7d0173f

Please sign in to comment.