Merge pull request #65 from mrunalp/ctr_list

List containers
This commit is contained in:
Antonio Murdaca 2016-09-28 08:49:18 +02:00 committed by GitHub
commit 4f80ffd1a2
2 changed files with 76 additions and 2 deletions

View file

@ -18,6 +18,7 @@ var containerCommand = cli.Command{
stopContainerCommand, stopContainerCommand,
removeContainerCommand, removeContainerCommand,
containerStatusCommand, containerStatusCommand,
listContainersCommand,
}, },
} }
@ -164,6 +165,26 @@ var containerStatusCommand = cli.Command{
}, },
} }
var listContainersCommand = cli.Command{
Name: "list",
Usage: "list containers",
Action: func(context *cli.Context) error {
// Set up a connection to the server.
conn, err := getClientConnection(context)
if err != nil {
return fmt.Errorf("failed to connect: %v", err)
}
defer conn.Close()
client := pb.NewRuntimeServiceClient(conn)
err = ListContainers(client)
if err != nil {
return fmt.Errorf("listing containers failed: %v", err)
}
return nil
},
}
// CreateContainer sends a CreateContainerRequest to the server, and parses // CreateContainer sends a CreateContainerRequest to the server, and parses
// the returned CreateContainerResponse. // the returned CreateContainerResponse.
func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string) error { func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string) error {
@ -264,3 +285,24 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID string) error {
return nil return nil
} }
// ListContainers sends a ListContainerRequest to the server, and parses
// the returned ListContainerResponse.
func ListContainers(client pb.RuntimeServiceClient) error {
r, err := client.ListContainers(context.Background(), &pb.ListContainersRequest{})
if err != nil {
return err
}
for _, c := range r.GetContainers() {
fmt.Printf("ID: %s\n", *c.Id)
fmt.Printf("Pod: %s\n", *c.PodSandboxId)
if c.State != nil {
fmt.Printf("Status: %s\n", *c.State)
}
if c.CreatedAt != nil {
ctm := time.Unix(*c.CreatedAt, 0)
fmt.Printf("Created: %v\n", ctm)
}
}
return nil
}

View file

@ -367,8 +367,40 @@ func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerReq
} }
// ListContainers lists all containers by filters. // ListContainers lists all containers by filters.
func (s *Server) ListContainers(context.Context, *pb.ListContainersRequest) (*pb.ListContainersResponse, error) { func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersRequest) (*pb.ListContainersResponse, error) {
return nil, nil var ctrs []*pb.Container
for _, ctr := range s.state.containers.List() {
if err := s.runtime.UpdateStatus(ctr); err != nil {
return nil, err
}
podSandboxID := ctr.Sandbox()
cState := s.runtime.ContainerStatus(ctr)
created := cState.Created.Unix()
rState := pb.ContainerState_UNKNOWN
c := &pb.Container{
Id: &cState.ID,
PodSandboxId: &podSandboxID,
CreatedAt: int64Ptr(created),
}
switch cState.Status {
case ContainerStateCreated:
rState = pb.ContainerState_CREATED
case ContainerStateRunning:
rState = pb.ContainerState_RUNNING
case ContainerStateStopped:
rState = pb.ContainerState_EXITED
}
c.State = &rState
ctrs = append(ctrs, c)
}
return &pb.ListContainersResponse{
Containers: ctrs,
}, nil
} }
// ContainerStatus returns status of the container. // ContainerStatus returns status of the container.