mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
merge ucind cluster configuration to the user’s uncloud config
This commit is contained in:
+18
-1
@@ -5,11 +5,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"uncloud/cmd/ucind/cluster"
|
"uncloud/cmd/ucind/cluster"
|
||||||
"uncloud/internal/ucind"
|
"uncloud/internal/ucind"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var configPath string
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "ucind",
|
Use: "ucind",
|
||||||
Short: "A CLI tool for running Uncloud test clusters using Docker.",
|
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)
|
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.
|
// Persist the provisioner in the context so it can be used by subcommands.
|
||||||
cmd.SetContext(context.WithValue(cmd.Context(), "provisioner", p))
|
cmd.SetContext(context.WithValue(cmd.Context(), "provisioner", p))
|
||||||
return nil
|
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(
|
cmd.AddCommand(
|
||||||
cluster.NewRootCommand(),
|
cluster.NewRootCommand(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -75,6 +75,13 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat
|
|||||||
return c, err
|
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
|
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 {
|
if err = p.dockerCli.NetworkRemove(ctx, name); err != nil {
|
||||||
return fmt.Errorf("remove Docker network '%s': %w", name, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -8,11 +8,13 @@ import (
|
|||||||
var ErrNotFound = errors.New("not found")
|
var ErrNotFound = errors.New("not found")
|
||||||
|
|
||||||
type Provisioner struct {
|
type Provisioner struct {
|
||||||
dockerCli *client.Client
|
dockerCli *client.Client
|
||||||
|
configUpdater *ConfigUpdater
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProvisioner(cli *client.Client) *Provisioner {
|
func NewProvisioner(cli *client.Client, configUpdater *ConfigUpdater) *Provisioner {
|
||||||
return &Provisioner{
|
return &Provisioner{
|
||||||
dockerCli: cli,
|
dockerCli: cli,
|
||||||
|
configUpdater: configUpdater,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user