Update ctl init to support env file
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package initcmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -12,18 +14,28 @@ import (
|
||||
|
||||
"undecided.project/monok8s/pkg/bootstrap"
|
||||
"undecided.project/monok8s/pkg/config"
|
||||
|
||||
types "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1"
|
||||
"undecided.project/monok8s/pkg/templates"
|
||||
)
|
||||
|
||||
func NewCmdInit(_ *genericclioptions.ConfigFlags) *cobra.Command {
|
||||
var configPath string
|
||||
var envFile string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "init [list|STEPSEL]",
|
||||
Short: "Start the bootstrap process for this node",
|
||||
Use: "init [list|STEPSEL] [--config path | --env-file path]",
|
||||
Short: "Bootstrap this node (from config file or env file)",
|
||||
Long: `Run the node bootstrap process.
|
||||
|
||||
You can provide configuration in two ways:
|
||||
|
||||
--config PATH Load MonoKSConfig YAML
|
||||
--env-file PATH Load MKS_* variables from env file and render config
|
||||
|
||||
STEPSEL allows running specific steps instead of the full sequence.
|
||||
It supports:
|
||||
|
||||
Supported formats:
|
||||
|
||||
3 Run step 3
|
||||
1-3 Run steps 1 through 3
|
||||
@@ -33,23 +45,54 @@ It supports:
|
||||
9-10,15 Combine ranges and individual steps
|
||||
`,
|
||||
Example: `
|
||||
ctl init
|
||||
# Run full bootstrap using config file
|
||||
ctl init --config /etc/monok8s/config.yaml
|
||||
|
||||
# Run full bootstrap using env file
|
||||
ctl init --env-file /opt/monok8s/config/cluster.env
|
||||
|
||||
# List steps
|
||||
ctl init list
|
||||
ctl init 1-3
|
||||
ctl init -3
|
||||
ctl init 3-
|
||||
ctl init 1,3,5
|
||||
ctl init 9-10,15
|
||||
|
||||
# Run selected steps
|
||||
ctl init 1-3 --env-file /opt/monok8s/config/cluster.env
|
||||
ctl init 3- --config /etc/monok8s/config.yaml
|
||||
`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
path, err := (config.Loader{}).ResolvePath(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
if strings.TrimSpace(configPath) != "" && strings.TrimSpace(envFile) != "" {
|
||||
return fmt.Errorf("--config and --env-file are mutually exclusive")
|
||||
}
|
||||
cfg, err := (config.Loader{}).Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
if strings.TrimSpace(envFile) != "" {
|
||||
if err := loadEnvFile(envFile); err != nil {
|
||||
return fmt.Errorf("load env file %q: %w", envFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfg *types.MonoKSConfig // or value, depending on your API
|
||||
|
||||
switch {
|
||||
case strings.TrimSpace(envFile) != "":
|
||||
if err := loadEnvFile(envFile); err != nil {
|
||||
return fmt.Errorf("load env file %q: %w", envFile, err)
|
||||
}
|
||||
vals := templates.LoadTemplateValuesFromEnv()
|
||||
rendered := templates.DefaultMonoKSConfig(vals)
|
||||
cfg = &rendered
|
||||
|
||||
default:
|
||||
path, err := (config.Loader{}).ResolvePath(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loaded, err := (config.Loader{}).Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg = loaded
|
||||
klog.InfoS("starting init", "config", path, "node", cfg.Spec.NodeName, "envFile", envFile)
|
||||
}
|
||||
|
||||
runner := bootstrap.NewRunner(cfg)
|
||||
@@ -59,18 +102,15 @@ It supports:
|
||||
|
||||
fmt.Fprintln(cmd.OutOrStdout(), "Showing current bootstrap sequence")
|
||||
|
||||
// width = number of digits of max step number
|
||||
width := len(fmt.Sprintf("%d", len(steps)))
|
||||
|
||||
for i, s := range steps {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "\n %*d. %s\n", width, i+1, s.Name)
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " %s\n", s.Desc)
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
klog.InfoS("starting init", "config", path, "node", cfg.Spec.NodeName)
|
||||
|
||||
if len(args) == 0 {
|
||||
return runner.Init(cmd.Context())
|
||||
}
|
||||
@@ -87,9 +127,60 @@ It supports:
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to MonoKSConfig yaml")
|
||||
cmd.Flags().StringVar(&envFile, "env-file", "", "path to env file containing MKS_* variables")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func loadEnvFile(path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
lineNum := 0
|
||||
|
||||
for scanner.Scan() {
|
||||
lineNum++
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
key, val, ok := strings.Cut(line, "=")
|
||||
if !ok {
|
||||
return fmt.Errorf("line %d: expected KEY=VALUE", lineNum)
|
||||
}
|
||||
|
||||
key = strings.TrimSpace(key)
|
||||
val = strings.TrimSpace(val)
|
||||
|
||||
if key == "" {
|
||||
return fmt.Errorf("line %d: empty variable name", lineNum)
|
||||
}
|
||||
|
||||
// Remove matching single or double quotes around the whole value.
|
||||
if len(val) >= 2 {
|
||||
if (val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'') {
|
||||
val = val[1 : len(val)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.Setenv(key, val); err != nil {
|
||||
return fmt.Errorf("line %d: set %q: %w", lineNum, key, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseStepSelection(raw string, max int) (bootstrap.StepSelection, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
Reference in New Issue
Block a user