Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Implement --auto-restart flag for the create command #61

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func CreateCluster(c *cli.Context) error {
c.String("name"),
c.StringSlice("volume"),
portmap,
c.Bool("auto-restart"),
)
if err != nil {
log.Printf("ERROR: failed to create cluster\n%+v", err)
Expand Down Expand Up @@ -196,6 +197,7 @@ func CreateCluster(c *cli.Context) error {
c.String("api-port"),
portmap,
c.Int("port-auto-offset"),
c.Bool("auto-restart"),
)
if err != nil {
return fmt.Errorf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err)
Expand Down
12 changes: 10 additions & 2 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func startContainer(verbose bool, config *container.Config, hostConfig *containe
}

func createServer(verbose bool, image string, apiPort string, args []string, env []string,
name string, volumes []string, nodeToPortSpecMap map[string][]string) (string, error) {
name string, volumes []string, nodeToPortSpecMap map[string][]string, autoRestart bool) (string, error) {
log.Printf("Creating server using %s...\n", image)

containerLabels := make(map[string]string)
Expand Down Expand Up @@ -95,6 +95,10 @@ func createServer(verbose bool, image string, apiPort string, args []string, env
Privileged: true,
}

if autoRestart {
hostConfig.RestartPolicy.Name = "unless-stopped"
}

if len(volumes) > 0 && volumes[0] != "" {
hostConfig.Binds = volumes
}
Expand Down Expand Up @@ -125,7 +129,7 @@ func createServer(verbose bool, image string, apiPort string, args []string, env

// createWorker creates/starts a k3s agent node that connects to the server
func createWorker(verbose bool, image string, args []string, env []string, name string, volumes []string,
postfix int, serverPort string, nodeToPortSpecMap map[string][]string, portAutoOffset int) (string, error) {
postfix int, serverPort string, nodeToPortSpecMap map[string][]string, portAutoOffset int, autoRestart bool) (string, error) {
containerLabels := make(map[string]string)
containerLabels["app"] = "k3d"
containerLabels["component"] = "worker"
Expand Down Expand Up @@ -161,6 +165,10 @@ func createWorker(verbose bool, image string, args []string, env []string, name
Privileged: true,
}

if autoRestart {
hostConfig.RestartPolicy.Name = "unless-stopped"
}

if len(volumes) > 0 && volumes[0] != "" {
hostConfig.Binds = volumes
}
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func main() {
Value: 0,
Usage: "Specify how many worker nodes you want to spawn",
},
cli.BoolFlag{
Name: "auto-restart",
Usage: "Set docker's --restart=unless-stopped flag on the containers",
},
},
Action: run.CreateCluster,
},
Expand Down