Skip to content

Commit

Permalink
feature: add restart skeleton
Browse files Browse the repository at this point in the history
Signed-off-by: letty <[email protected]>
  • Loading branch information
Letty5411 committed Mar 14, 2018
1 parent 1564d7a commit f5952a7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func initRoute(s *Server) http.Handler {
r.Path("/containers/{name:.*}/exec").Methods(http.MethodPost).Handler(s.filter(s.createContainerExec))
r.Path("/exec/{name:.*}/start").Methods(http.MethodPost).Handler(s.filter(s.startContainerExec))
r.Path("/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("/containers/{name:.*}/pause").Methods(http.MethodPost).Handler(s.filter(s.pauseContainer))
r.Path("/containers/{name:.*}/unpause").Methods(http.MethodPost).Handler(s.filter(s.unpauseContainer))
r.Path("/containers/{name:.*}/update").Methods(http.MethodPost).Handler(s.filter(s.updateContainer))
Expand Down
1 change: 1 addition & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &ImageMgmtCommand{})
Expand Down
61 changes: 61 additions & 0 deletions cli/restart.go
Original file line number Diff line number Diff line change
@@ -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 a container.
type RestartCommand struct {
baseCommand
time 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.time, "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.time); 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`
}
6 changes: 6 additions & 0 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,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)
Expand Down
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f5952a7

Please sign in to comment.