merge ucind cluster configuration to the user’s uncloud config

This commit is contained in:
Pavel Sviderski
2024-11-29 13:30:15 +10:00
parent faf3378659
commit cf5818d963
4 changed files with 106 additions and 4 deletions
+18 -1
View File
@@ -5,11 +5,14 @@ import (
"fmt"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"os"
"strings"
"uncloud/cmd/ucind/cluster"
"uncloud/internal/ucind"
)
func main() {
var configPath string
cmd := &cobra.Command{
Use: "ucind",
Short: "A CLI tool for running Uncloud test clusters using Docker.",
@@ -24,13 +27,27 @@ func main() {
return fmt.Errorf("create Docker client: %w", err)
}
p := ucind.NewProvisioner(cli)
if strings.HasPrefix(configPath, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("get user home directory to resolve %q: %w", configPath, err)
}
configPath = strings.Replace(configPath, "~", home, 1)
}
configUpdater := ucind.NewConfigUpdater(configPath)
p := ucind.NewProvisioner(cli, configUpdater)
// Persist the provisioner in the context so it can be used by subcommands.
cmd.SetContext(context.WithValue(cmd.Context(), "provisioner", p))
return nil
},
}
// TODO: allow to override using UNCLOUD_CONFIG env var.
cmd.PersistentFlags().StringVar(&configPath, "uncloud-config", "~/.config/uncloud/config.toml",
"path to the Uncloud configuration file.")
_ = cmd.MarkPersistentFlagFilename("uncloud-config", "toml")
cmd.AddCommand(
cluster.NewRootCommand(),
)
+15
View File
@@ -75,6 +75,13 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat
return c, err
}
if p.configUpdater != nil {
if err = p.configUpdater.AddCluster(c); err != nil {
return c, fmt.Errorf("add cluster to Uncloud config: %w", err)
}
fmt.Printf("Cluster '%s' added to Uncloud config.\n", c.Name)
}
return c, nil
}
@@ -210,5 +217,13 @@ func (p *Provisioner) RemoveCluster(ctx context.Context, name string) error {
if err = p.dockerCli.NetworkRemove(ctx, name); err != nil {
return fmt.Errorf("remove Docker network '%s': %w", name, err)
}
if p.configUpdater != nil {
if err = p.configUpdater.RemoveCluster(name); err != nil {
return fmt.Errorf("remove cluster from Uncloud config: %w", err)
}
fmt.Printf("Cluster '%s' removed from Uncloud config.\n", name)
}
return nil
}
+68
View File
@@ -0,0 +1,68 @@
package ucind
import (
"fmt"
"uncloud/internal/cli/config"
)
type ConfigUpdater struct {
path string
}
func NewConfigUpdater(path string) *ConfigUpdater {
return &ConfigUpdater{path: path}
}
func (u *ConfigUpdater) AddCluster(c Cluster) error {
cfg, err := config.NewFromFile(u.path)
if err != nil {
return fmt.Errorf("read Uncloud config: %w", err)
}
if _, ok := cfg.Clusters[c.Name]; ok {
return fmt.Errorf("cluster '%s' already exists", c.Name)
}
clusterCfg := &config.Cluster{
Name: c.Name,
Connections: make([]config.MachineConnection, len(c.Machines)),
}
for i, m := range c.Machines {
clusterCfg.Connections[i] = config.MachineConnection{
TCP: m.APIAddress,
}
}
cfg.Clusters[c.Name] = clusterCfg
cfg.CurrentCluster = c.Name
if err = cfg.Save(); err != nil {
return fmt.Errorf("save config: %w", err)
}
return nil
}
func (u *ConfigUpdater) RemoveCluster(name string) error {
cfg, err := config.NewFromFile(u.path)
if err != nil {
return fmt.Errorf("read Uncloud config: %w", err)
}
if _, ok := cfg.Clusters[name]; !ok {
return nil
}
delete(cfg.Clusters, name)
if cfg.CurrentCluster == name {
cfg.CurrentCluster = ""
}
if _, ok := cfg.Clusters["default"]; ok {
cfg.CurrentCluster = "default"
}
if err = cfg.Save(); err != nil {
return fmt.Errorf("save config: %w", err)
}
return nil
}
+3 -1
View File
@@ -9,10 +9,12 @@ var ErrNotFound = errors.New("not found")
type Provisioner struct {
dockerCli *client.Client
configUpdater *ConfigUpdater
}
func NewProvisioner(cli *client.Client) *Provisioner {
func NewProvisioner(cli *client.Client, configUpdater *ConfigUpdater) *Provisioner {
return &Provisioner{
dockerCli: cli,
configUpdater: configUpdater,
}
}