diff --git a/apis/server/container_bridge.go b/apis/server/container_bridge.go index ae1a74e41..c70f3b7d4 100644 --- a/apis/server/container_bridge.go +++ b/apis/server/container_bridge.go @@ -48,6 +48,11 @@ func (s *Server) renameContainer(ctx context.Context, rw http.ResponseWriter, re return nil } +func (s *Server) restartContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { + // TODO + return nil +} + func (s *Server) createContainerExec(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { config := &types.ExecCreateConfig{} // decode request body diff --git a/apis/server/router.go b/apis/server/router.go index 62e1b9ea4..b02db091a 100644 --- a/apis/server/router.go +++ b/apis/server/router.go @@ -39,6 +39,7 @@ func initRoute(s *Server) http.Handler { r.Path(versionMatcher + "/containers/{name:.*}/exec").Methods(http.MethodPost).Handler(s.filter(s.createContainerExec)) r.Path(versionMatcher + "/exec/{name:.*}/start").Methods(http.MethodPost).Handler(s.filter(s.startContainerExec)) r.Path(versionMatcher + "/containers/{id:.*}/rename").Methods(http.MethodPost).Handler(s.filter(s.renameContainer)) + r.Path("/containers/{id:.*}/restart").Methods(http.MethodPost).Handler(s.filter(s.restartContainer)) r.Path(versionMatcher + "/containers/{name:.*}/pause").Methods(http.MethodPost).Handler(s.filter(s.pauseContainer)) r.Path(versionMatcher + "/containers/{name:.*}/unpause").Methods(http.MethodPost).Handler(s.filter(s.unpauseContainer)) r.Path(versionMatcher + "/containers/{name:.*}/update").Methods(http.MethodPost).Handler(s.filter(s.updateContainer)) diff --git a/apis/swagger.yml b/apis/swagger.yml index 82e5a2a59..064530ed1 100644 --- a/apis/swagger.yml +++ b/apis/swagger.yml @@ -382,6 +382,30 @@ paths: $ref: "#/responses/500ErrorResponse" tags: ["Container"] + /containers/{id}/restart: + post: + summary: "Restart a container" + operationId: "ContainerRestart" + parameters: + - $ref: "#/parameters/id" + - name: "name" + in: "query" + required: true + description: "New name for the container" + type: "string" + - name: "t" + in: "query" + description: "Number of seconds to wait before killing the container" + type: "integer" + responses: + 204: + description: "no error" + 404: + $ref: "#/responses/404ErrorResponse" + 500: + $ref: "#/responses/500ErrorResponse" + tags: ["Container"] + /containers/{id}/start: post: summary: "Start a container" diff --git a/cli/main.go b/cli/main.go index ba825190e..674005a12 100644 --- a/cli/main.go +++ b/cli/main.go @@ -20,6 +20,7 @@ func main() { cli.AddCommand(base, &StopCommand{}) cli.AddCommand(base, &PsCommand{}) cli.AddCommand(base, &RmCommand{}) + cli.AddCommand(base, &RestartCommand{}) cli.AddCommand(base, &ExecCommand{}) cli.AddCommand(base, &VersionCommand{}) cli.AddCommand(base, &InfoCommand{}) diff --git a/cli/restart.go b/cli/restart.go new file mode 100644 index 000000000..c4c551d88 --- /dev/null +++ b/cli/restart.go @@ -0,0 +1,61 @@ +package main + +import ( + "context" + + "fmt" + "github.com/spf13/cobra" +) + +// restartDescription is used to describe restart command in detail and auto generate command doc. +var restartDescription = "restart one or more containers" + +// RestartCommand uses to implement 'restart' command, it restarts one or more containers. +type RestartCommand struct { + baseCommand + timeout int +} + +// Init initialize restart command. +func (rc *RestartCommand) Init(c *Cli) { + rc.cli = c + + rc.cmd = &cobra.Command{ + Use: "restart [OPTION] CONTAINER [CONTAINERS]", + Short: "restart one or more containers", + Long: restartDescription, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return rc.runRestart(args) + }, + Example: restartExample(), + } + rc.addFlags() +} + +// addFlags adds flags for specific command. +func (rc *RestartCommand) addFlags() { + flagSet := rc.cmd.Flags() + flagSet.IntVarP(&rc.timeout, "time", "t", 10, + "Seconds to wait for stop before killing the container (default 10)") +} + +// runRestart is the entry of restart command. +func (rc *RestartCommand) runRestart(args []string) error { + ctx := context.Background() + apiClient := rc.cli.Client() + + for _, name := range args { + if err := apiClient.ContainerRestart(ctx, name, rc.timeout); err != nil { + return fmt.Errorf("failed to restart container: %v", err) + } + fmt.Printf("%s\n", name) + } + + return nil +} + +// restartExample shows examples in restart command, and is used in auto-generated cli docs. +func restartExample() string { + return `//TODO` +} diff --git a/client/container.go b/client/container.go index 6ab53d952..850b2af64 100644 --- a/client/container.go +++ b/client/container.go @@ -159,6 +159,12 @@ func (client *APIClient) ContainerRename(ctx context.Context, id string, name st return err } +// ContainerRestart restarts a contianer. +func (client *APIClient) ContainerRestart(ctx context.Context, name string, time int) error { + // TODO + return nil +} + // ContainerPause pauses a container. func (client *APIClient) ContainerPause(ctx context.Context, name string) error { resp, err := client.post(ctx, "/containers/"+name+"/pause", nil, nil, nil) diff --git a/client/interface.go b/client/interface.go index b27eec6dc..6f6f976ba 100644 --- a/client/interface.go +++ b/client/interface.go @@ -30,6 +30,7 @@ type ContainerAPIClient interface { ContainerStartExec(ctx context.Context, execid string, config *types.ExecStartConfig) (net.Conn, *bufio.Reader, error) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) ContainerRename(ctx context.Context, id string, name string) error + ContainerRestart(ctx context.Context, name string, time int) error ContainerPause(ctx context.Context, name string) error ContainerUnpause(ctx context.Context, name string) error ContainerUpdate(ctx context.Context, name string, config *types.UpdateConfig) error