94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"kube-forge/internal/files"
|
|
"kube-forge/internal/logging"
|
|
"path/filepath"
|
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
)
|
|
|
|
var (
|
|
ConfigFile string
|
|
Output string
|
|
Password string
|
|
Verbose bool
|
|
)
|
|
|
|
type Host struct {
|
|
Hostname string `yaml:"hostname"`
|
|
Ip string `yaml:"ip"`
|
|
Roles []string `yaml:"roles"`
|
|
User string `yaml:"user"`
|
|
Password string `yaml:"password"`
|
|
}
|
|
|
|
type Config struct {
|
|
BaseDir string
|
|
InventoryDir string
|
|
Verbose bool
|
|
KubeconfigFile string
|
|
Forks string `yaml:"forks" env-default:"10"`
|
|
InstallationName string `yaml:"installation_name" env-default:"cluster.local"`
|
|
Credentials struct {
|
|
User string `yaml:"user"`
|
|
Password string `yaml:"password"`
|
|
AskSudoPassword bool `yaml:"ask_sudo_password"`
|
|
PrivateKeyFile string `yaml:"private_key_file"`
|
|
} `yaml:"credentials"`
|
|
|
|
Hosts struct {
|
|
Masters []Host `yaml:"master"`
|
|
Workers []Host `yaml:"worker"`
|
|
} `yaml:"hosts"`
|
|
|
|
Orchestrator Orchestrator `yaml:"orchestrator"`
|
|
|
|
Modules struct {
|
|
Additional Additional `yaml:"additional"`
|
|
} `yaml:"modules"`
|
|
}
|
|
|
|
var instance *Config
|
|
|
|
func createConfig(configFile string, out string, password string, verbose bool) *Config {
|
|
instance = &Config{}
|
|
|
|
if err := cleanenv.ReadConfig(configFile, instance); err != nil {
|
|
helper, _ := cleanenv.GetDescription(instance, nil)
|
|
logging.Log.Fatal(fmt.Sprintf("%s\n%s", helper, err))
|
|
}
|
|
|
|
instance.BaseDir = files.CreateTempDir()
|
|
instance.InventoryDir = filepath.Join(instance.BaseDir, "inventory")
|
|
|
|
files.CreateDirWithAllParents(instance.InventoryDir)
|
|
|
|
if out != "" {
|
|
files.CreateDirWithAllParents(out)
|
|
instance.KubeconfigFile = filepath.Join(out, "k8s-admin.conf")
|
|
} else {
|
|
instance.KubeconfigFile = filepath.Join(files.GetWorkingDir(), "k8s-admin.conf")
|
|
}
|
|
|
|
if password != "" {
|
|
instance.Credentials.Password = password
|
|
}
|
|
|
|
instance.Verbose = verbose
|
|
|
|
err := validateConfig(*instance)
|
|
if err != nil {
|
|
logging.Log.Fatal(err.Error())
|
|
}
|
|
return instance
|
|
}
|
|
|
|
func GetConfig() *Config {
|
|
if instance == nil {
|
|
createConfig(ConfigFile, Output, Password, Verbose)
|
|
}
|
|
return instance
|
|
}
|