Skip to content

Commit

Permalink
Merge pull request moby#11665 from runcom/11613-api-exec-start-struct…
Browse files Browse the repository at this point in the history
…ured-response

Return ContainerExecCreateResponse from container exec create API endpoint
  • Loading branch information
Jessie Frazelle committed Mar 24, 2015
2 parents f586145 + 0c3d2f6 commit e6b7d93
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
9 changes: 6 additions & 3 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,12 +2677,15 @@ func (cli *DockerCli) CmdExec(args ...string) error {
return err
}

var execResult engine.Env
if err := execResult.Decode(stream); err != nil {
var response types.ContainerExecCreateResponse
if err := json.NewDecoder(stream).Decode(&response); err != nil {
return err
}
for _, warning := range response.Warnings {
fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
}

execID := execResult.Get("Id")
execID := response.ID

if execID == "" {
fmt.Fprintf(cli.out, "exec ID empty")
Expand Down
17 changes: 13 additions & 4 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,26 +1155,35 @@ func postContainerExecCreate(eng *engine.Engine, version version.Version, w http
return nil
}
var (
out engine.Env
name = vars["name"]
job = eng.Job("execCreate", name)
stdoutBuffer = bytes.NewBuffer(nil)
outWarnings []string
warnings = bytes.NewBuffer(nil)
)

if err := job.DecodeEnv(r.Body); err != nil {
return err
}

job.Stdout.Add(stdoutBuffer)
// Read warnings from stderr
job.Stderr.Add(warnings)
// Register an instance of Exec in container.
if err := job.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error setting up exec command in container %s: %s\n", name, err)
return err
}
// Return the ID
out.Set("Id", engine.Tail(stdoutBuffer, 1))
// Parse warnings from stderr
scanner := bufio.NewScanner(warnings)
for scanner.Scan() {
outWarnings = append(outWarnings, scanner.Text())
}

return writeJSONEnv(w, http.StatusCreated, out)
return writeJSON(w, http.StatusCreated, &types.ContainerExecCreateResponse{
ID: engine.Tail(stdoutBuffer, 1),
Warnings: outWarnings,
})
}

// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
Expand Down
9 changes: 9 additions & 0 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ type ContainerCreateResponse struct {
// Warnings are any warnings encountered during the creation of the container.
Warnings []string `json:"Warnings"`
}

// POST /containers/{name:.*}/exec
type ContainerExecCreateResponse struct {
// ID is the exec ID.
ID string `json:"Id"`

// Warnings are any warnings encountered during the execution of the command.
Warnings []string `json:"Warnings"`
}

0 comments on commit e6b7d93

Please sign in to comment.