1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove Job from docker top

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-04-09 15:13:01 -07:00
parent 404f81011b
commit 3e096cb9c9
5 changed files with 44 additions and 41 deletions

View file

@ -1,12 +1,13 @@
package client package client
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/docker/docker/engine" "github.com/docker/docker/api/types"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
) )
@ -28,17 +29,17 @@ func (cli *DockerCli) CmdTop(args ...string) error {
if err != nil { if err != nil {
return err return err
} }
var procs engine.Env
if err := procs.Decode(stream); err != nil { procList := types.ContainerProcessList{}
err = json.NewDecoder(stream).Decode(&procList)
if err != nil {
return err return err
} }
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0) w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, strings.Join(procs.GetList("Titles"), "\t")) fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
processes := [][]string{}
if err := procs.GetJson("Processes", &processes); err != nil { for _, proc := range procList.Processes {
return err
}
for _, proc := range processes {
fmt.Fprintln(w, strings.Join(proc, "\t")) fmt.Fprintln(w, strings.Join(proc, "\t"))
} }
w.Flush() w.Flush()

View file

@ -471,16 +471,21 @@ func getContainersTop(eng *engine.Engine, version version.Version, w http.Respon
if version.LessThan("1.4") { if version.LessThan("1.4") {
return fmt.Errorf("top was improved a lot since 1.3, Please upgrade your docker client.") return fmt.Errorf("top was improved a lot since 1.3, Please upgrade your docker client.")
} }
if vars == nil { if vars == nil {
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
if err := parseForm(r); err != nil { if err := parseForm(r); err != nil {
return err return err
} }
job := eng.Job("top", vars["name"], r.Form.Get("ps_args")) procList, err := getDaemon(eng).ContainerTop(vars["name"], r.Form.Get("ps_args"))
streamJSON(job, w, false) if err != nil {
return job.Run() return err
}
return writeJSON(w, http.StatusOK, procList)
} }
func getContainersJSON(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getContainersJSON(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -103,3 +103,9 @@ type Container struct {
type CopyConfig struct { type CopyConfig struct {
Resource string Resource string
} }
// GET "/containers/{name:.*}/top"
type ContainerProcessList struct {
Processes [][]string
Titles []string
}

View file

@ -128,7 +128,6 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
"restart": daemon.ContainerRestart, "restart": daemon.ContainerRestart,
"start": daemon.ContainerStart, "start": daemon.ContainerStart,
"stop": daemon.ContainerStop, "stop": daemon.ContainerStop,
"top": daemon.ContainerTop,
"wait": daemon.ContainerWait, "wait": daemon.ContainerWait,
"execCreate": daemon.ContainerExecCreate, "execCreate": daemon.ContainerExecCreate,
"execStart": daemon.ContainerExecStart, "execStart": daemon.ContainerExecStart,

View file

@ -6,54 +6,48 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/docker/docker/engine" "github.com/docker/docker/api/types"
) )
func (daemon *Daemon) ContainerTop(job *engine.Job) error { func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
if len(job.Args) != 1 && len(job.Args) != 2 { if psArgs == "" {
return fmt.Errorf("Not enough arguments. Usage: %s CONTAINER [PS_ARGS]\n", job.Name)
}
var (
name = job.Args[0]
psArgs = "-ef" psArgs = "-ef"
)
if len(job.Args) == 2 && job.Args[1] != "" {
psArgs = job.Args[1]
} }
container, err := daemon.Get(name) container, err := daemon.Get(name)
if err != nil { if err != nil {
return err return nil, err
} }
if !container.IsRunning() { if !container.IsRunning() {
return fmt.Errorf("Container %s is not running", name) return nil, fmt.Errorf("Container %s is not running", name)
} }
pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID) pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID)
if err != nil { if err != nil {
return err return nil, err
} }
output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output() output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
if err != nil { if err != nil {
return fmt.Errorf("Error running ps: %s", err) return nil, fmt.Errorf("Error running ps: %s", err)
} }
procList := &types.ContainerProcessList{}
lines := strings.Split(string(output), "\n") lines := strings.Split(string(output), "\n")
header := strings.Fields(lines[0]) procList.Titles = strings.Fields(lines[0])
out := &engine.Env{}
out.SetList("Titles", header)
pidIndex := -1 pidIndex := -1
for i, name := range header { for i, name := range procList.Titles {
if name == "PID" { if name == "PID" {
pidIndex = i pidIndex = i
} }
} }
if pidIndex == -1 { if pidIndex == -1 {
return fmt.Errorf("Couldn't find PID field in ps output") return nil, fmt.Errorf("Couldn't find PID field in ps output")
} }
processes := [][]string{}
for _, line := range lines[1:] { for _, line := range lines[1:] {
if len(line) == 0 { if len(line) == 0 {
continue continue
@ -61,20 +55,18 @@ func (daemon *Daemon) ContainerTop(job *engine.Job) error {
fields := strings.Fields(line) fields := strings.Fields(line)
p, err := strconv.Atoi(fields[pidIndex]) p, err := strconv.Atoi(fields[pidIndex])
if err != nil { if err != nil {
return fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err) return nil, fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err)
} }
for _, pid := range pids { for _, pid := range pids {
if pid == p { if pid == p {
// Make sure number of fields equals number of header titles // Make sure number of fields equals number of header titles
// merging "overhanging" fields // merging "overhanging" fields
process := fields[:len(header)-1] process := fields[:len(procList.Titles)-1]
process = append(process, strings.Join(fields[len(header)-1:], " ")) process = append(process, strings.Join(fields[len(procList.Titles)-1:], " "))
processes = append(processes, process) procList.Processes = append(procList.Processes, process)
} }
} }
} }
out.SetJson("Processes", processes) return procList, nil
out.WriteTo(job.Stdout)
return nil
} }