Skip to content

Commit

Permalink
Merge pull request #13 from trivago/optional-command
Browse files Browse the repository at this point in the history
Set command stanza to optional in the pot driver
  • Loading branch information
ebarriosjr authored Dec 17, 2019
2 parents 9d13590 + 44e3912 commit ad193b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 9 additions & 3 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
"image": hclspec.NewAttr("image", "string", true),
"pot": hclspec.NewAttr("pot", "string", true),
"tag": hclspec.NewAttr("tag", "string", true),
"command": hclspec.NewAttr("command", "string", true),
"command": hclspec.NewAttr("command", "string", false),
"args": hclspec.NewAttr("args", "list(string)", false),
"port_map": hclspec.NewAttr("port_map", "list(map(string))", false),
"network_mode": hclspec.NewAttr("network_mode", "string", false),
Expand Down Expand Up @@ -281,7 +281,10 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return fmt.Errorf("failed to decode driver config in RESTORETASK: %v", err)
}*/

se := prepareContainer(handle.Config, driverConfig)
se, err := prepareContainer(handle.Config, driverConfig)
if err != nil {
return err
}
se.logger = d.logger

alive := se.checkContainerAlive(handle.Config)
Expand Down Expand Up @@ -368,7 +371,10 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
handle := drivers.NewTaskHandle(taskHandleVersion)
handle.Config = cfg

se := prepareContainer(cfg, driverConfig)
se, err := prepareContainer(cfg, driverConfig)
if err != nil {
return nil, nil, err
}

se.logger = d.logger
alive := se.checkContainerAlive(cfg)
Expand Down
20 changes: 14 additions & 6 deletions driver/prepare.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pot

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -12,7 +13,7 @@ import (
)

// prepareContainer preloads the taskcnf into args to be passed to a execCmd
func prepareContainer(cfg *drivers.TaskConfig, taskCfg TaskConfig) syexec {
func prepareContainer(cfg *drivers.TaskConfig, taskCfg TaskConfig) (syexec, error) {
argv := make([]string, 0, 50)
var se syexec
se.taskConfig = taskCfg
Expand All @@ -28,14 +29,21 @@ func prepareContainer(cfg *drivers.TaskConfig, taskCfg TaskConfig) syexec {
}

if len(taskCfg.Args) > 0 {
if taskCfg.Command == "" {
err := errors.New("command can not be empty if arguments are provided")
fmt.Println("command can not be empty if arguments are provided")
return se, err
}

for _, arg := range taskCfg.Args {
taskCfg.Command = taskCfg.Command + " " + arg
}
}

taskCfg.Command = "\"" + taskCfg.Command + "\""
argv = append(argv, "-c", taskCfg.Command)

if taskCfg.Command != "" {
taskCfg.Command = "\"" + taskCfg.Command + "\""
argv = append(argv, "-c", taskCfg.Command)
}
if taskCfg.NetworkMode != "" {
argv = append(argv, "-N", taskCfg.NetworkMode)
} else if len(taskCfg.PortMap) > 0 {
Expand All @@ -54,7 +62,7 @@ func prepareContainer(cfg *drivers.TaskConfig, taskCfg TaskConfig) syexec {
completeName := cfg.JobName + cfg.Name
argv = append(argv, "-n", completeName, "-v")

se.argvCreate = append(argv, taskCfg.Args...)
se.argvCreate = argv

potName := completeName + "_" + cfg.AllocID

Expand Down Expand Up @@ -140,7 +148,7 @@ func prepareContainer(cfg *drivers.TaskConfig, taskCfg TaskConfig) syexec {
argvStats = append(argvStats, "get-rss", "-p", potName, "-J")
se.argvStats = argvStats

return se
return se, nil
}

type nopCloser struct {
Expand Down

0 comments on commit ad193b6

Please sign in to comment.