2016-04-22 08:06:35 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-04-26 06:21:07 +08:00
|
|
|
"encoding/json"
|
2016-04-22 08:06:35 +08:00
|
|
|
"fmt"
|
2016-04-26 06:21:07 +08:00
|
|
|
"os"
|
2016-04-22 08:06:35 +08:00
|
|
|
"os/exec"
|
2016-10-10 09:22:16 +08:00
|
|
|
"strconv"
|
2016-04-22 08:06:35 +08:00
|
|
|
"strings"
|
|
|
|
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-04-22 08:06:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var psCommand = cli.Command{
|
|
|
|
Name: "ps",
|
|
|
|
Usage: "ps displays the processes running inside a container",
|
2016-09-24 12:20:52 +08:00
|
|
|
ArgsUsage: `<container-id> [ps options]`,
|
2016-04-26 06:21:07 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format, f",
|
|
|
|
Value: "",
|
2016-05-24 18:52:29 +08:00
|
|
|
Usage: `select one of: ` + formatOptions,
|
2016-04-26 06:21:07 +08:00
|
|
|
},
|
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-04-22 08:06:35 +08:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-04-22 08:06:35 +08:00
|
|
|
}
|
|
|
|
|
2016-06-06 02:17:44 +08:00
|
|
|
pids, err := container.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-26 06:21:07 +08:00
|
|
|
if context.String("format") == "json" {
|
|
|
|
if err := json.NewEncoder(os.Stdout).Encode(pids); err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2016-04-26 06:21:07 +08:00
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-04-26 06:21:07 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 18:36:26 +08:00
|
|
|
// [1:] is to remove command name, ex:
|
|
|
|
// context.Args(): [containet_id ps_arg1 ps_arg2 ...]
|
|
|
|
// psArgs: [ps_arg1 ps_arg2 ...]
|
|
|
|
//
|
|
|
|
psArgs := context.Args()[1:]
|
|
|
|
if len(psArgs) == 0 {
|
2016-10-10 09:22:16 +08:00
|
|
|
psArgs = []string{"-ef"}
|
2016-04-22 08:06:35 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 12:26:07 +08:00
|
|
|
cmd := exec.Command("ps", psArgs...)
|
|
|
|
output, err := cmd.CombinedOutput()
|
2016-04-22 08:06:35 +08:00
|
|
|
if err != nil {
|
2016-09-29 12:26:07 +08:00
|
|
|
return fmt.Errorf("%s: %s", err, output)
|
2016-04-22 08:06:35 +08:00
|
|
|
}
|
|
|
|
|
2016-10-10 09:22:16 +08:00
|
|
|
lines := strings.Split(string(output), "\n")
|
|
|
|
pidIndex, err := getPidIndex(lines[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(lines[0])
|
|
|
|
for _, line := range lines[1:] {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
p, err := strconv.Atoi(fields[pidIndex])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected pid '%s': %s", fields[pidIndex], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pid := range pids {
|
|
|
|
if pid == p {
|
|
|
|
fmt.Println(line)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2016-04-22 08:06:35 +08:00
|
|
|
},
|
2016-09-18 09:34:06 +08:00
|
|
|
SkipArgReorder: true,
|
2016-04-22 08:06:35 +08:00
|
|
|
}
|
2016-10-10 09:22:16 +08:00
|
|
|
|
|
|
|
func getPidIndex(title string) (int, error) {
|
|
|
|
titles := strings.Fields(title)
|
|
|
|
|
|
|
|
pidIndex := -1
|
|
|
|
for i, name := range titles {
|
|
|
|
if name == "PID" {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pidIndex, fmt.Errorf("couldn't find PID field in ps output")
|
|
|
|
}
|