Skip to content

Commit

Permalink
Add support for both 'command' and 'commands'
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushjn20 committed May 19, 2019
1 parent f4692ca commit 966c4f4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .dunner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ build:
- ["npm", "--version"]
- image: alpine
dir: pkg
commands:
- ["pwd"]
command: ["pwd"]
- image: alpine
commands:
- ["apk", "update"]
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"

G "github.com/leopardslab/dunner/pkg/global"
"github.com/spf13/cobra"
)

func init() {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Task struct {
Name string `yaml:"name"`
Image string `yaml:"image"`
SubDir string `yaml:"dir"`
Command []string `yaml:"command"`
Commands [][]string `yaml:"commands"`
Envs []string `yaml:"envs"`
Mounts []string `yaml:"mounts"`
Expand Down
48 changes: 40 additions & 8 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -28,6 +29,7 @@ type Step struct {
Task string
Name string
Image string
Command []string
Commands [][]string
Env []string
WorkDir string
Expand All @@ -50,6 +52,8 @@ func (step Step) Exec() (*[]Result, error) {
hostMountFilepath = "./"
containerDefaultWorkingDir = "/dunner"
hostMountTarget = "/dunner"
defaultCommand = []string{"tail", "-f", "/dev/null"}
multipleCommands = false
)

ctx := context.Background()
Expand All @@ -67,7 +71,6 @@ func (step Step) Exec() (*[]Result, error) {
}

log.Infof("Pulling an image: '%s'", step.Image)

out, err := cli.ImagePull(ctx, step.Image, types.ImagePullOptions{})
if err != nil {
log.Fatal(err)
Expand All @@ -94,11 +97,15 @@ func (step Step) Exec() (*[]Result, error) {
containerWorkingDir = filepath.Join(hostMountTarget, step.WorkDir)
}

multipleCommands = len(step.Commands) > 0
if !multipleCommands {
defaultCommand = step.Command
}
resp, err := cli.ContainerCreate(
ctx,
&container.Config{
Image: step.Image,
Cmd: []string{"tail", "-f", "/dev/null"},
Cmd: defaultCommand,
Env: step.Env,
WorkingDir: containerWorkingDir,
},
Expand Down Expand Up @@ -135,14 +142,34 @@ func (step Step) Exec() (*[]Result, error) {
}()

var results []Result
for _, cmd := range step.Commands {
r, err := runCmd(ctx, cli, resp.ID, cmd)
if multipleCommands {
for _, cmd := range step.Commands {
r, err := runCmd(ctx, cli, resp.ID, cmd)
if err != nil {
log.Fatal(err)
}
results = append(results, *r)
}
} else {
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err = <-errCh:
if err != nil {
log.Fatal(err)
}
case <-statusCh:
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
log.Fatal(err)
}
results = append(results, *r)
}

results = []Result{*extractResult(out, step.Command)}
}
return &results, nil
}

Expand All @@ -166,14 +193,19 @@ func runCmd(ctx context.Context, cli *client.Client, containerID string, command
}
defer resp.Close()

return extractResult(resp.Reader, command), nil
}

func extractResult(reader io.Reader, command []string) *Result {

var out, errOut bytes.Buffer
if _, err = stdcopy.StdCopy(&out, &errOut, resp.Reader); err != nil {
if _, err := stdcopy.StdCopy(&out, &errOut, reader); err != nil {
log.Fatal(err)
}
var result = Result{
Command: strings.Join(command, " "),
Output: out.String(),
Error: errOut.String(),
}
return &result, nil
return &result
}
1 change: 1 addition & 0 deletions pkg/dunner/dunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func execTask(configs *config.Configs, taskName string, args []string) {
Task: taskName,
Name: stepDefinition.Name,
Image: stepDefinition.Image,
Command: stepDefinition.Command,
Commands: stepDefinition.Commands,
Env: stepDefinition.Envs,
WorkDir: stepDefinition.SubDir,
Expand Down

0 comments on commit 966c4f4

Please sign in to comment.