-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
- Loading branch information
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/containerd/containerd" | ||
"github.com/pkg/errors" | ||
"github.com/urfave/cli/v2" | ||
"regexp" | ||
) | ||
|
||
var topCommand = &cli.Command{ | ||
Name: "top", | ||
Usage: "docker top CONTAINER", | ||
ArgsUsage: "CONTAINER [ps OPTIONS]", | ||
Action: topAction, | ||
BashComplete: topBashComplete, | ||
} | ||
|
||
func topAction(clicontext *cli.Context) error { | ||
if clicontext.NArg() < 1 { | ||
return errors.Errorf("requires at least 1 argument") | ||
} | ||
//TODO | ||
} | ||
|
||
//function from moby/moby | ||
func validatePSArgs(psArgs string) error { | ||
// NOTE: \\s does not detect unicode whitespaces. | ||
// So we use fieldsASCII instead of strings.Fields in parsePSOutput. | ||
// See https://github.com/docker/docker/pull/24358 | ||
// nolint: gosimple | ||
re := regexp.MustCompile("\\s+([^\\s]*)=\\s*(PID[^\\s]*)") | ||
for _, group := range re.FindAllStringSubmatch(psArgs, -1) { | ||
if len(group) >= 3 { | ||
k := group[1] | ||
v := group[2] | ||
if k != "pid" { | ||
return fmt.Errorf("specifying \"%s=%s\" is not allowed", k, v) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
//function from moby/moby | ||
// ContainerTop lists the processes running inside of the given | ||
// container by calling ps with the given args, or with the flags | ||
// "-ef" if no args are given. An error is returned if the container | ||
// is not found, or is not running, or if there are any problems | ||
// running ps, or parsing the output. | ||
func (daemon *Daemon) ContainerTop(clicontext *cli.Context, name string, psArgs string) (*container.ContainerTopOKBody, error) { | ||
if psArgs == "" { | ||
psArgs = "-ef" | ||
} | ||
|
||
if err := validatePSArgs(psArgs); err != nil { | ||
return nil, err | ||
} | ||
|
||
client, ctx, cancel, err := newClient(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
container, err := client.LoadContainer(ctx, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
task, err := container.Task(ctx, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
status, err := task.Status(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if status.Status != containerd.Running { | ||
return nil, errNotRunning(ctr.ID) | ||
} | ||
|
||
if ctr.IsRestarting() { | ||
return nil, errContainerIsRestarting(ctr.ID) | ||
} | ||
|
||
procs, err := containerd.ListPids(context.Background(), ctr.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
args := strings.Split(psArgs, " ") | ||
pids := psPidsArg(procs) | ||
output, err := exec.Command("ps", append(args, pids)...).Output() | ||
if err != nil { | ||
// some ps options (such as f) can't be used together with q, | ||
// so retry without it | ||
output, err = exec.Command("ps", args...).Output() | ||
if err != nil { | ||
if ee, ok := err.(*exec.ExitError); ok { | ||
// first line of stderr shows why ps failed | ||
line := bytes.SplitN(ee.Stderr, []byte{'\n'}, 2) | ||
if len(line) > 0 && len(line[0]) > 0 { | ||
err = errors.New(string(line[0])) | ||
} | ||
} | ||
return nil, errdefs.System(errors.Wrap(err, "ps")) | ||
} | ||
} | ||
procList, err := parsePSOutput(output, procs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
//daemon.LogContainerEvent(ctr, "top") | ||
return procList, nil | ||
} | ||
|