diff --git a/cmd/client/main.go b/cmd/client/main.go index 4a855c56..fcf3e3b1 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -195,6 +195,21 @@ func RemoveContainer(client pb.RuntimeServiceClient, ID string) error { 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. func Version(client pb.RuntimeServiceClient, version string) error { r, err := client.Version(context.Background(), &pb.VersionRequest{Version: &version}) @@ -401,6 +416,7 @@ var containerCommand = cli.Command{ startContainerCommand, stopContainerCommand, removeContainerCommand, + containerStatusCommand, }, } @@ -519,3 +535,30 @@ var removeContainerCommand = cli.Command{ 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 + }, +} diff --git a/server/runtime.go b/server/runtime.go index 708b316a..20cf4445 100644 --- a/server/runtime.go +++ b/server/runtime.go @@ -627,7 +627,31 @@ func (s *Server) ListContainers(context.Context, *pb.ListContainersRequest) (*pb } // 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 }