зеркало из https://github.com/microsoft/docker.git
Import ps name parsing for default name
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Родитель
4a5cefa173
Коммит
b10dfb7de3
|
@ -1475,57 +1475,68 @@ func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix stri
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *DockerCli) CmdPs(args ...string) error {
|
func (cli *DockerCli) CmdPs(args ...string) error {
|
||||||
cmd := cli.Subcmd("ps", "", "List containers")
|
var (
|
||||||
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
err error
|
||||||
size := cmd.Bool([]string{"s", "-size"}, false, "Display sizes")
|
|
||||||
all := cmd.Bool([]string{"a", "-all"}, false, "Show all containers. Only running containers are shown by default.")
|
psFilterArgs = filters.Args{}
|
||||||
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
v = url.Values{}
|
||||||
nLatest := cmd.Bool([]string{"l", "-latest"}, false, "Show only the latest created container, include non-running ones.")
|
|
||||||
since := cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show only containers created since Id or Name, include non-running ones.")
|
cmd = cli.Subcmd("ps", "", "List containers")
|
||||||
before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
|
quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
||||||
last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
|
size = cmd.Bool([]string{"s", "-size"}, false, "Display sizes")
|
||||||
|
all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers. Only running containers are shown by default.")
|
||||||
|
noTrunc = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
||||||
|
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show only the latest created container, include non-running ones.")
|
||||||
|
since = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show only containers created since Id or Name, include non-running ones.")
|
||||||
|
before = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
|
||||||
|
last = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
|
||||||
|
flFilter = opts.NewListOpts(nil)
|
||||||
|
)
|
||||||
|
|
||||||
flFilter := opts.NewListOpts(nil)
|
|
||||||
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>\nstatus=(restarting|running|paused|exited)")
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>\nstatus=(restarting|running|paused|exited)")
|
||||||
|
|
||||||
if err := cmd.Parse(args); err != nil {
|
if err := cmd.Parse(args); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
v := url.Values{}
|
|
||||||
if *last == -1 && *nLatest {
|
if *last == -1 && *nLatest {
|
||||||
*last = 1
|
*last = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if *all {
|
if *all {
|
||||||
v.Set("all", "1")
|
v.Set("all", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *last != -1 {
|
if *last != -1 {
|
||||||
v.Set("limit", strconv.Itoa(*last))
|
v.Set("limit", strconv.Itoa(*last))
|
||||||
}
|
}
|
||||||
|
|
||||||
if *since != "" {
|
if *since != "" {
|
||||||
v.Set("since", *since)
|
v.Set("since", *since)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *before != "" {
|
if *before != "" {
|
||||||
v.Set("before", *before)
|
v.Set("before", *before)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *size {
|
if *size {
|
||||||
v.Set("size", "1")
|
v.Set("size", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Consolidate all filter flags, and sanity check them.
|
// Consolidate all filter flags, and sanity check them.
|
||||||
// They'll get processed in the daemon/server.
|
// They'll get processed in the daemon/server.
|
||||||
psFilterArgs := filters.Args{}
|
|
||||||
for _, f := range flFilter.GetAll() {
|
for _, f := range flFilter.GetAll() {
|
||||||
var err error
|
if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
|
||||||
psFilterArgs, err = filters.ParseFlag(f, psFilterArgs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(psFilterArgs) > 0 {
|
if len(psFilterArgs) > 0 {
|
||||||
filterJson, err := filters.ToParam(psFilterArgs)
|
filterJson, err := filters.ToParam(psFilterArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.Set("filters", filterJson)
|
v.Set("filters", filterJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1538,9 +1549,11 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
||||||
if _, err := outs.ReadListFrom(body); err != nil {
|
if _, err := outs.ReadListFrom(body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
||||||
if !*quiet {
|
if !*quiet {
|
||||||
fmt.Fprint(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES")
|
fmt.Fprint(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES")
|
||||||
|
|
||||||
if *size {
|
if *size {
|
||||||
fmt.Fprintln(w, "\tSIZE")
|
fmt.Fprintln(w, "\tSIZE")
|
||||||
} else {
|
} else {
|
||||||
|
@ -1548,56 +1561,69 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stripNamePrefix := func(ss []string) []string {
|
||||||
|
for i, s := range ss {
|
||||||
|
ss[i] = s[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss
|
||||||
|
}
|
||||||
|
|
||||||
for _, out := range outs.Data {
|
for _, out := range outs.Data {
|
||||||
var (
|
outID := out.Get("Id")
|
||||||
outID = out.Get("Id")
|
|
||||||
outNames = out.GetList("Names")
|
|
||||||
)
|
|
||||||
|
|
||||||
if !*noTrunc {
|
if !*noTrunc {
|
||||||
outID = utils.TruncateID(outID)
|
outID = utils.TruncateID(outID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*quiet {
|
if *quiet {
|
||||||
|
fmt.Fprintln(w, outID)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
outCommand = out.Get("Command")
|
outNames = stripNamePrefix(out.GetList("Names"))
|
||||||
|
outCommand = strconv.Quote(out.Get("Command"))
|
||||||
ports = engine.NewTable("", 0)
|
ports = engine.NewTable("", 0)
|
||||||
)
|
)
|
||||||
outCommand = strconv.Quote(outCommand)
|
|
||||||
if !*noTrunc {
|
if !*noTrunc {
|
||||||
outCommand = utils.Trunc(outCommand, 20)
|
outCommand = utils.Trunc(outCommand, 20)
|
||||||
// Keep only the canonical name
|
|
||||||
for i := 0; i < len(outNames); i++ {
|
// only display the default name for the container with notrunc is passed
|
||||||
if !strings.Contains(outNames[i][1:], "/") {
|
for _, name := range outNames {
|
||||||
outNames = outNames[i : i+1]
|
if len(strings.Split(name, "/")) == 1 {
|
||||||
|
outNames = []string{name}
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Remove the leading / from the names
|
|
||||||
for i := 0; i < len(outNames); i++ {
|
|
||||||
outNames[i] = outNames[i][1:]
|
|
||||||
}
|
|
||||||
outNamesList := strings.Join(outNames, ",")
|
|
||||||
ports.ReadListFrom([]byte(out.Get("Ports")))
|
ports.ReadListFrom([]byte(out.Get("Ports")))
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, out.Get("Image"), outCommand, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), out.Get("Status"), api.DisplayablePorts(ports), outNamesList)
|
|
||||||
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, out.Get("Image"), outCommand,
|
||||||
|
units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))),
|
||||||
|
out.Get("Status"), api.DisplayablePorts(ports), strings.Join(outNames, ","))
|
||||||
|
|
||||||
if *size {
|
if *size {
|
||||||
if out.GetInt("SizeRootFs") > 0 {
|
if out.GetInt("SizeRootFs") > 0 {
|
||||||
fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(out.GetInt64("SizeRw")), units.HumanSize(out.GetInt64("SizeRootFs")))
|
fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(out.GetInt64("SizeRw")), units.HumanSize(out.GetInt64("SizeRootFs")))
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "%s\n", units.HumanSize(out.GetInt64("SizeRw")))
|
fmt.Fprintf(w, "%s\n", units.HumanSize(out.GetInt64("SizeRw")))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprint(w, "\n")
|
fmt.Fprint(w, "\n")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fmt.Fprintln(w, outID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !*quiet {
|
if !*quiet {
|
||||||
w.Flush()
|
w.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -282,50 +282,3 @@ func TestPsListContainersFilterStatus(t *testing.T) {
|
||||||
|
|
||||||
logDone("ps - test ps filter status")
|
logDone("ps - test ps filter status")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPsListContainersNames(t *testing.T) {
|
|
||||||
runCmd := exec.Command(dockerBinary, "run", "--name", "link_target", "-d", "busybox", "top")
|
|
||||||
out, _, err := runCommandWithOutput(runCmd)
|
|
||||||
errorOut(err, t, out)
|
|
||||||
|
|
||||||
runCmd = exec.Command(dockerBinary, "run", "--name", "link_source", "--link", "link_target:link_alias", "-d", "busybox", "top")
|
|
||||||
out, _, err = runCommandWithOutput(runCmd)
|
|
||||||
errorOut(err, t, out)
|
|
||||||
|
|
||||||
// non-truncated ps should return all names
|
|
||||||
runCmd = exec.Command(dockerBinary, "ps", "--no-trunc")
|
|
||||||
out, _, err = runCommandWithOutput(runCmd)
|
|
||||||
errorOut(err, t, out)
|
|
||||||
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
|
||||||
namesIndex := strings.Index(lines[0], "NAMES")
|
|
||||||
expectedNames := "link_source"
|
|
||||||
foundNames := strings.TrimSpace(lines[1][namesIndex:])
|
|
||||||
if foundNames != expectedNames {
|
|
||||||
t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
|
|
||||||
}
|
|
||||||
expectedNames = "link_source/link_alias,link_target"
|
|
||||||
foundNames = strings.TrimSpace(lines[2][namesIndex:])
|
|
||||||
if foundNames != expectedNames {
|
|
||||||
t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
|
|
||||||
}
|
|
||||||
|
|
||||||
// truncated ps should return canonical names only
|
|
||||||
runCmd = exec.Command(dockerBinary, "ps")
|
|
||||||
out, _, err = runCommandWithOutput(runCmd)
|
|
||||||
errorOut(err, t, out)
|
|
||||||
lines = strings.Split(strings.Trim(out, "\n "), "\n")
|
|
||||||
namesIndex = strings.Index(lines[0], "NAMES")
|
|
||||||
expectedNames = "link_source"
|
|
||||||
foundNames = strings.TrimSpace(lines[1][namesIndex:])
|
|
||||||
if foundNames != expectedNames {
|
|
||||||
t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
|
|
||||||
}
|
|
||||||
expectedNames = "link_target"
|
|
||||||
foundNames = strings.TrimSpace(lines[2][namesIndex:])
|
|
||||||
if foundNames != expectedNames {
|
|
||||||
t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteAllContainers()
|
|
||||||
logDone("ps - test ps names")
|
|
||||||
}
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче