Add debug flag in root cmd (#31)

This commit is contained in:
Itxaka
2023-05-24 08:39:17 +00:00
committed by GitHub
parent e614b35fda
commit 3c484d9885
4 changed files with 39 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/sirupsen/logrus"
"net/url" "net/url"
"os" "os"
"strings" "strings"
@@ -74,7 +75,7 @@ func mergeOption(cloudConfig string, r map[string]string) {
} }
} }
func ManualInstall(c string, options map[string]string, strictValidations bool) error { func ManualInstall(c string, options map[string]string, strictValidations, debug bool) error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@@ -98,13 +99,17 @@ func ManualInstall(c string, options map[string]string, strictValidations bool)
mergeOption(configStr, options) mergeOption(configStr, options)
if options["device"] == "" { if options["device"] == "" {
options["device"] = cc.Install.Device if cc.Install.Device == "" {
options["device"] = detectDevice()
} else {
options["device"] = cc.Install.Device
}
} }
return RunInstall(options) return RunInstall(debug, options)
} }
func Install(dir ...string) error { func Install(debug bool, dir ...string) error {
utils.OnSignal(func() { utils.OnSignal(func() {
svc, err := machine.Getty(1) svc, err := machine.Getty(1)
if err == nil { if err == nil {
@@ -140,7 +145,7 @@ func Install(dir ...string) error {
r["device"] = cc.Install.Device r["device"] = cc.Install.Device
mergeOption(configStr, r) mergeOption(configStr, r)
err = RunInstall(r) err = RunInstall(debug, r)
if err != nil { if err != nil {
return err return err
} }
@@ -234,7 +239,7 @@ func Install(dir ...string) error {
pterm.Info.Println("Starting installation") pterm.Info.Println("Starting installation")
if err := RunInstall(r); err != nil { if err := RunInstall(debug, r); err != nil {
return err return err
} }
@@ -251,12 +256,15 @@ func Install(dir ...string) error {
return nil return nil
} }
func RunInstall(options map[string]string) error { func RunInstall(debug bool, options map[string]string) error {
// Load the installation Config from the system // Load the installation Config from the system
installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental") installConfig, err := elementalConfig.ReadConfigRun("/etc/elemental")
if err != nil { if err != nil {
return err return err
} }
if debug {
installConfig.Logger.SetLevel(logrus.DebugLevel)
}
// Run pre-install stage // Run pre-install stage
_ = elementalUtils.RunStage(&installConfig.Config, "kairos-install.pre", installConfig.Strict, installConfig.CloudInitPaths...) _ = elementalUtils.RunStage(&installConfig.Config, "kairos-install.pre", installConfig.Strict, installConfig.CloudInitPaths...)

View File

@@ -127,7 +127,7 @@ func detectDevice() string {
return preferedDevice return preferedDevice
} }
func InteractiveInstall(spawnShell bool) error { func InteractiveInstall(debug, spawnShell bool) error {
bus.Manager.Initialize() bus.Manager.Initialize()
cmd.PrintBranding(DefaultBanner) cmd.PrintBranding(DefaultBanner)
@@ -220,7 +220,7 @@ func InteractiveInstall(spawnShell bool) error {
} }
if !isYes(allGood) { if !isYes(allGood) {
return InteractiveInstall(spawnShell) return InteractiveInstall(debug, spawnShell)
} }
c := &config.Config{ c := &config.Config{
@@ -261,7 +261,7 @@ func InteractiveInstall(spawnShell bool) error {
pterm.Info.Println("Starting installation") pterm.Info.Println("Starting installation")
pterm.Info.Println(finalCloudConfig) pterm.Info.Println(finalCloudConfig)
err = RunInstall(map[string]string{ err = RunInstall(debug, map[string]string{
"device": device, "device": device,
"cc": finalCloudConfig, "cc": finalCloudConfig,
}) })

View File

@@ -3,6 +3,7 @@ package agent
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/sirupsen/logrus"
"os" "os"
"sync" "sync"
"time" "time"
@@ -22,7 +23,7 @@ import (
"github.com/pterm/pterm" "github.com/pterm/pterm"
) )
func Reset(dir ...string) error { func Reset(debug bool, dir ...string) error {
// TODO: Enable args? No args for now so no possibility of reset persistent or overriding the source for the reset // TODO: Enable args? No args for now so no possibility of reset persistent or overriding the source for the reset
// Nor the auto-reboot via cmd? // Nor the auto-reboot via cmd?
// This comment pertains calling reset via cmdline when wanting to override configs // This comment pertains calling reset via cmdline when wanting to override configs
@@ -85,6 +86,9 @@ func Reset(dir ...string) error {
if err != nil { if err != nil {
return err return err
} }
if debug {
resetConfig.Logger.SetLevel(logrus.DebugLevel)
}
resetSpec, err := elementalConfig.ReadResetSpec(resetConfig) resetSpec, err := elementalConfig.ReadResetSpec(resetConfig)
if err != nil { if err != nil {
return err return err

24
main.go
View File

@@ -7,6 +7,7 @@ import (
"github.com/kairos-io/kairos/v2/pkg/elementalConfig" "github.com/kairos-io/kairos/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos/v2/pkg/types/v1" v1 "github.com/kairos-io/kairos/v2/pkg/types/v1"
"github.com/kairos-io/kairos/v2/pkg/utils" "github.com/kairos-io/kairos/v2/pkg/utils"
"github.com/sirupsen/logrus"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -59,10 +60,6 @@ var cmds = []*cli.Command{
Name: "force", Name: "force",
Usage: "Force an upgrade", Usage: "Force an upgrade",
}, },
&cli.BoolFlag{
Name: "debug",
Usage: "Show debug output",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "image", Name: "image",
Usage: "Specify an full image reference, e.g.: quay.io/some/image:tag", Usage: "Specify an full image reference, e.g.: quay.io/some/image:tag",
@@ -360,7 +357,7 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
}, },
Usage: "Starts interactive installation", Usage: "Starts interactive installation",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return agent.InteractiveInstall(c.Bool("shell")) return agent.InteractiveInstall(c.Bool("debug"), c.Bool("shell"))
}, },
}, },
{ {
@@ -396,7 +393,7 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
options["reboot"] = "true" options["reboot"] = "true"
} }
return agent.ManualInstall(config, options, c.Bool("strict-validation")) return agent.ManualInstall(config, options, c.Bool("strict-validation"), c.Bool("debug"))
}, },
}, },
{ {
@@ -412,7 +409,7 @@ See also https://kairos.io/docs/installation/qrcode/ for documentation.
This command is meant to be used from the boot GRUB menu, but can be started manually`, This command is meant to be used from the boot GRUB menu, but can be started manually`,
Aliases: []string{"i"}, Aliases: []string{"i"},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return agent.Install(configScanDir...) return agent.Install(c.Bool("debug"), configScanDir...)
}, },
}, },
{ {
@@ -435,7 +432,7 @@ This command is meant to be used from the boot GRUB menu, but can likely be used
{ {
Name: "reset", Name: "reset",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return agent.Reset(configScanDir...) return agent.Reset(c.Bool("debug"), configScanDir...)
}, },
Usage: "Starts kairos reset mode", Usage: "Starts kairos reset mode",
Description: ` Description: `
@@ -519,6 +516,9 @@ The validate command expects a configuration file as its only argument. Local fi
if len(c.StringSlice("cloud-init-paths")) > 0 { if len(c.StringSlice("cloud-init-paths")) > 0 {
cfg.CloudInitPaths = append(cfg.CloudInitPaths, c.StringSlice("cloud-init-paths")...) cfg.CloudInitPaths = append(cfg.CloudInitPaths, c.StringSlice("cloud-init-paths")...)
} }
if c.Bool("debug") {
cfg.Logger.SetLevel(logrus.DebugLevel)
}
if err != nil { if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err) cfg.Logger.Errorf("Error reading config: %s\n", err)
@@ -566,6 +566,9 @@ The validate command expects a configuration file as its only argument. Local fi
if err != nil { if err != nil {
return err return err
} }
if c.Bool("debug") {
cfg.Logger.SetLevel(logrus.DebugLevel)
}
cfg.Logger.Infof("Starting download and extraction for image %s to %s\n", image, destination) cfg.Logger.Infof("Starting download and extraction for image %s to %s\n", image, destination)
e := v1.OCIImageExtractor{} e := v1.OCIImageExtractor{}
@@ -588,6 +591,11 @@ func main() {
Usage: "Fail instead of warn on validation errors.", Usage: "Fail instead of warn on validation errors.",
EnvVars: []string{"STRICT_VALIDATIONS"}, EnvVars: []string{"STRICT_VALIDATIONS"},
}, },
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug output",
EnvVars: []string{"KAIROS_AGENT_DEBUG"},
},
}, },
Name: "kairos-agent", Name: "kairos-agent",
Version: common.VERSION, Version: common.VERSION,