make start/stop functions start/stop workers as well

pull/12/head v1.0.0-beta0
iwilltry42 5 years ago
parent 6fdc1e4a70
commit e9206a74b2
  1. 100
      cli/commands.go
  2. 23
      cli/config.go

@ -202,39 +202,50 @@ func DeleteCluster(c *cli.Context) error {
// StopCluster stops a running cluster container (restartable) // StopCluster stops a running cluster container (restartable)
func StopCluster(c *cli.Context) error { func StopCluster(c *cli.Context) error {
ctx := context.Background() // operate on one or all clusters
docker, err := client.NewEnvClient() clusters := make(map[string]cluster)
if !c.Bool("all") {
cluster, err := getCluster(c.String("name"))
if err != nil { if err != nil {
return err return err
} }
clusters[c.String("name")] = cluster
clusterNames := []string{}
// operate on one or all clusters
if !c.Bool("all") {
clusterNames = append(clusterNames, c.String("name"))
} else { } else {
clusterList, err := getClusterNames() clusterMap, err := getClusters()
if err != nil { if err != nil {
return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err) return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err)
} }
clusterNames = append(clusterNames, clusterList...) // copy clusterMap
for k, v := range clusterMap {
clusters[k] = v
}
} }
// stop clusters one by one instead of appending all names to the docker command ctx := context.Background()
// this allows for more granular error handling and logging docker, err := client.NewEnvClient()
for _, name := range clusterNames {
log.Printf("Stopping cluster [%s]", name)
cluster, err := getCluster(name)
if err != nil { if err != nil {
log.Printf("WARNING: couldn't get docker info for %s", name) return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
}
// remove clusters one by one instead of appending all names to the docker command
// this allows for more granular error handling and logging
for _, cluster := range clusters {
log.Printf("Stopping cluster [%s]", cluster.name)
if len(cluster.workers) > 0 {
log.Printf("...Stopping %d workers\n", len(cluster.workers))
for _, worker := range cluster.workers {
if err := docker.ContainerStop(ctx, worker.ID, nil); err != nil {
log.Println(err)
continue continue
} }
}
}
log.Println("...Stopping server")
if err := docker.ContainerStop(ctx, cluster.server.ID, nil); err != nil { if err := docker.ContainerStop(ctx, cluster.server.ID, nil); err != nil {
fmt.Printf("WARNING: couldn't stop cluster %s\n%+v", cluster.name, err) return fmt.Errorf("ERROR: Couldn't stop server for cluster %s\n%+v", cluster.name, err)
continue
} }
log.Printf("SUCCESS: stopped cluster [%s]", cluster.name)
log.Printf("SUCCESS: Stopped cluster [%s]", cluster.name)
} }
return nil return nil
@ -242,39 +253,52 @@ func StopCluster(c *cli.Context) error {
// StartCluster starts a stopped cluster container // StartCluster starts a stopped cluster container
func StartCluster(c *cli.Context) error { func StartCluster(c *cli.Context) error {
ctx := context.Background() // operate on one or all clusters
docker, err := client.NewEnvClient() clusters := make(map[string]cluster)
if !c.Bool("all") {
cluster, err := getCluster(c.String("name"))
if err != nil { if err != nil {
return err return err
} }
clusters[c.String("name")] = cluster
clusterNames := []string{}
// operate on one or all clusters
if !c.Bool("all") {
clusterNames = append(clusterNames, c.String("name"))
} else { } else {
clusterList, err := getClusterNames() clusterMap, err := getClusters()
if err != nil { if err != nil {
return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err) return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err)
} }
clusterNames = append(clusterNames, clusterList...) // copy clusterMap
for k, v := range clusterMap {
clusters[k] = v
}
} }
// stop clusters one by one instead of appending all names to the docker command ctx := context.Background()
// this allows for more granular error handling and logging docker, err := client.NewEnvClient()
for _, name := range clusterNames {
log.Printf("Starting cluster [%s]", name)
cluster, err := getCluster(name)
if err != nil { if err != nil {
log.Printf("WARNING: couldn't get docker info for %s", name) return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
continue
} }
// remove clusters one by one instead of appending all names to the docker command
// this allows for more granular error handling and logging
for _, cluster := range clusters {
log.Printf("Starting cluster [%s]", cluster.name)
log.Println("...Starting server")
if err := docker.ContainerStart(ctx, cluster.server.ID, types.ContainerStartOptions{}); err != nil { if err := docker.ContainerStart(ctx, cluster.server.ID, types.ContainerStartOptions{}); err != nil {
fmt.Printf("WARNING: couldn't start cluster %s\n%+v", cluster.name, err) return fmt.Errorf("ERROR: Couldn't start server for cluster %s\n%+v", cluster.name, err)
}
if len(cluster.workers) > 0 {
log.Printf("...Starting %d workers\n", len(cluster.workers))
for _, worker := range cluster.workers {
if err := docker.ContainerStart(ctx, worker.ID, types.ContainerStartOptions{}); err != nil {
log.Println(err)
continue continue
} }
log.Printf("SUCCESS: started cluster [%s]", cluster.name) }
}
log.Printf("SUCCESS: Started cluster [%s]", cluster.name)
} }
return nil return nil

@ -3,7 +3,6 @@ package run
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path" "path"
@ -92,28 +91,6 @@ func printClusters(all bool) {
table.Render() table.Render()
} }
// getClusterNames returns a list of cluster names which are folder names in the config directory
func getClusterNames() ([]string, error) {
homeDir, err := homedir.Dir()
if err != nil {
log.Printf("ERROR: Couldn't get user's home directory")
return nil, err
}
configDir := path.Join(homeDir, ".config", "k3d")
files, err := ioutil.ReadDir(configDir)
if err != nil {
log.Printf("ERROR: Couldn't list files in [%s]", configDir)
return nil, err
}
clusters := []string{}
for _, file := range files {
if file.IsDir() {
clusters = append(clusters, file.Name())
}
}
return clusters, nil
}
// getClusters uses the docker API to get existing clusters and compares that with the list of cluster directories // getClusters uses the docker API to get existing clusters and compares that with the list of cluster directories
func getClusters() (map[string]cluster, error) { func getClusters() (map[string]cluster, error) {
ctx := context.Background() ctx := context.Background()

Loading…
Cancel
Save