Merge pull request #2 from mrunalp/ctr_status

Support for container status
This commit is contained in:
Mrunal Patel 2016-09-12 15:18:15 -07:00 committed by GitHub
commit b145b16ac0
2 changed files with 68 additions and 1 deletions

View file

@ -195,6 +195,21 @@ func RemoveContainer(client pb.RuntimeServiceClient, ID string) error {
return nil return nil
} }
// ContainerStatus sends a ContainerStatusRequest to the server, and parses
// the returned ContainerStatusResponse.
func ContainerStatus(client pb.RuntimeServiceClient, ID string) error {
if ID == "" {
return fmt.Errorf("ID cannot be empty")
}
r, err := client.ContainerStatus(context.Background(), &pb.ContainerStatusRequest{
ContainerId: &ID})
if err != nil {
return err
}
fmt.Println(r)
return nil
}
// Version sends a VersionRequest to the server, and parses the returned VersionResponse. // Version sends a VersionRequest to the server, and parses the returned VersionResponse.
func Version(client pb.RuntimeServiceClient, version string) error { func Version(client pb.RuntimeServiceClient, version string) error {
r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version}) r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version})
@ -401,6 +416,7 @@ var containerCommand = cli.Command{
startContainerCommand, startContainerCommand,
stopContainerCommand, stopContainerCommand,
removeContainerCommand, removeContainerCommand,
containerStatusCommand,
}, },
} }
@ -519,3 +535,30 @@ var removeContainerCommand = cli.Command{
return nil return nil
}, },
} }
var containerStatusCommand = cli.Command{
Name: "status",
Usage: "get the status of a container",
Flags: []cli.Flag{
cli.StringFlag{
Name: "id",
Value: "",
Usage: "id of the container",
},
},
Action: func(context *cli.Context) error {
// Set up a connection to the server.
conn, err := getClientConnection()
if err != nil {
return fmt.Errorf("Failed to connect: %v", err)
}
defer conn.Close()
client := pb.NewRuntimeServiceClient(conn)
err = ContainerStatus(client, context.String("id"))
if err != nil {
return fmt.Errorf("Getting the status of the container failed: %v", err)
}
return nil
},
}

View file

@ -627,7 +627,31 @@ func (s *Server) ListContainers(context.Context, *pb.ListContainersRequest) (*pb
} }
// ContainerStatus returns status of the container. // ContainerStatus returns status of the container.
func (s *Server) ContainerStatus(context.Context, *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) { func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
containerName := req.ContainerId
if *containerName == "" {
return nil, fmt.Errorf("container ID should not be empty")
}
c := s.state.containers[*containerName]
if c == nil {
return nil, fmt.Errorf("specified container not found: %s", *containerName)
}
if err := s.runtime.UpdateStatus(c); err != nil {
return nil, err
}
cState := s.runtime.ContainerStatus(c)
created := cState.Created.Unix()
return &pb.ContainerStatusResponse{
Status: &pb.ContainerStatus{
Id: containerName,
CreatedAt: int64Ptr(created),
},
}, nil
return nil, nil return nil, nil
} }