uncloud CLI tool and 'machine add' command to bootstrap a new cluster

This commit is contained in:
Pavel Sviderski
2024-08-24 20:14:47 +10:00
parent 3f13f6c0e6
commit d369884f7a
9 changed files with 425 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
package config
import "uncloud/internal/secret"
type Cluster struct {
Name string `toml:"-"`
Machines []MachineConnection `toml:"machines"`
Secret secret.Secret `toml:"secret"`
}
+63
View File
@@ -0,0 +1,63 @@
package config
import (
"fmt"
"github.com/BurntSushi/toml"
"os"
"path/filepath"
)
type Config struct {
Clusters map[string]*Cluster `toml:"clusters"`
CurrentCluster string `toml:"current_cluster"`
// path is the file path config is read from.
path string
}
func NewFromFile(path string) (*Config, error) {
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("check file permissions %q: %w", path, err)
}
c := &Config{
Clusters: map[string]*Cluster{},
path: path,
}
if os.IsNotExist(err) {
return c, nil
}
if err = c.Read(); err != nil {
return nil, err
}
return c, nil
}
func (c *Config) Read() error {
_, err := toml.DecodeFile(c.path, c)
if err != nil {
return fmt.Errorf("read config file %q: %w", c.path, err)
}
return nil
}
func (c *Config) Save() error {
dir, _ := filepath.Split(c.path)
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("create config directory %q: %w", dir, err)
}
f, err := os.OpenFile(c.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("write config file %q: %w", c.path, err)
}
encoder := toml.NewEncoder(f)
encoder.Indent = ""
if err = encoder.Encode(c); err != nil {
_ = f.Close()
return fmt.Errorf("encode config file %q: %w", c.path, err)
}
return f.Close()
}
+8
View File
@@ -0,0 +1,8 @@
package config
type MachineConnection struct {
User string `toml:"user,omitempty"`
Host string `toml:"host"`
Port int `toml:"port"`
SSHKey string `toml:"ssh_key,omitempty"`
}