Skip to content

Commit

Permalink
Add flag for setting log levels and log errors for debugging. (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
YrrepNoj authored Oct 30, 2021
1 parent 03e2e04 commit 5825443
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cli/cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var prepareTransformGitLinks = &cobra.Command{
// Overwrite the file
err = ioutil.WriteFile(fileName, []byte(processedText), 0640)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to write the changes back to the file")
}
}
Expand All @@ -59,6 +60,7 @@ var prepareComputeFileSha256sum = &cobra.Command{
fileName := args[0]
hash, err := utils.GetSha256Sum(fileName)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to compute the hash")
} else {
fmt.Println(hash)
Expand Down
10 changes: 9 additions & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"os"
"strings"

"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/packager"

"github.com/spf13/cobra"
)

var zarfLogLevel = ""

var rootCmd = &cobra.Command{
Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML",
Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLogLevel(zarfLogLevel)
},
Short: "Small tool to bundle dependencies with K3s for airgapped deployments",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -36,4 +43,5 @@ func Execute() {

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "info", "Log level when runnning Zarf. Valid options are: debug, info, warn, error, fatal")
}
4 changes: 4 additions & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func Load(path string) {
file, err := ioutil.ReadFile(path)

if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to load the config file")
}

err = yaml.Unmarshal(file, &config)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to parse the config file")
}
}
Expand Down Expand Up @@ -88,11 +90,13 @@ func WriteConfig(path string) {
// Save the parsed output to the config path given
content, err := yaml.Marshal(config)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to process the config data")
}

err = ioutil.WriteFile(path, content, 0400)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to write the config file")
}
}
3 changes: 3 additions & 0 deletions cli/internal/git/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ func CheckoutTag(path string, tag string) {
// Open the given repo
repo, err := git.PlainOpen(path)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Not a valid git repo or unable to open")
return
}

// Get the working tree so we can change refs
tree, err := repo.Worktree()
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to load the git repo")
}

Expand All @@ -31,6 +33,7 @@ func CheckoutTag(path string, tag string) {
Branch: plumbing.ReferenceName("refs/tags/" + tag),
})
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to checkout the given tag")
}
}
1 change: 1 addition & 0 deletions cli/internal/git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func pull(gitUrl string, targetFolder string) {
if err == git.ErrRepositoryAlreadyExists {
logContext.Info("Repo already cloned")
} else if err != nil {
logContext.Debug(err)
logContext.Fatal("Not a valid git repo or unable to clone")
}

Expand Down
3 changes: 3 additions & 0 deletions cli/internal/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func CredentialsGenerator() string {

credentialsFile, err := os.OpenFile(credentialsPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to access the git credentials file")
}
defer credentialsFile.Close()
Expand All @@ -137,12 +138,14 @@ func CredentialsGenerator() string {
// Write the entry to the file
_, err = credentialsFile.WriteString(credentialsText)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to update the git credentials file")
}

// Save the change
err = credentialsFile.Sync()
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to update the git credentials file")
}

Expand Down
2 changes: 2 additions & 0 deletions cli/internal/helm/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ func DownloadPublishedChart(chart config.ZarfChart, destination string) {
// Perform simple chart download
chartURL, err := repo.FindChartInRepoURL(chart.Url, chart.Name, chart.Version, pull.CertFile, pull.KeyFile, pull.CaFile, getter.All(pull.Settings))
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to pull the helm chart")
}

// Download the file (we don't control what name helm creates here)
saved, _, err := downloader.DownloadTo(chartURL, pull.Version, destination)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to download the helm chart")
}

Expand Down
1 change: 1 addition & 0 deletions cli/internal/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func PullAll(buildImageList []string, imageTarballPath string) {

logrus.Info("Creating image tarball (this will take a while)")
if err := crane.MultiSave(imageMap, imageTarballPath); err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to save the tarball")
}
}
5 changes: 3 additions & 2 deletions cli/internal/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ func PushAll(imageTarballPath string, buildImageList []string) {
logContext.Info("Updating image")
img, err := crane.LoadTag(imageTarballPath, src, cranePlatformOptions)
if err != nil {
logContext.Error(err)
logContext.Debug(err)
logContext.Warn("Unable to load the image from the update package")
return
}

onlineName, err := docker.ParseNormalizedNamed(src)
if err != nil {
logContext.Debug(err)
logContext.Warn("Unable to parse the image domain")
return
}
Expand All @@ -34,7 +35,7 @@ func PushAll(imageTarballPath string, buildImageList []string) {
logrus.Info(offlineName)
err = crane.Push(img, offlineName, cranePlatformOptions)
if err != nil {
logContext.Error(err)
logContext.Debug(err)
logContext.Warn("Unable to push the image to the registry")
}
}
Expand Down
3 changes: 3 additions & 0 deletions cli/internal/k3s/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func createK3sSymlinks() {
// Make the k3s kubeconfig available to other standard K8s tools that bind to the default ~/.kube/config
err := utils.CreateDirectory("/root/.kube", 0700)
if err != nil {
logrus.Debug(err)
logrus.Warn("Unable to create the root kube config directory")
} else {
// Dont log an error for now since re-runs throw an invalid error
Expand All @@ -70,11 +71,13 @@ func createService() {

_, err := utils.ExecCommand(nil, "systemctl", "daemon-reload")
if err != nil {
logrus.Debug(err)
logrus.Warn("Unable to reload systemd")
}

_, err = utils.ExecCommand(nil, "systemctl", "enable", "--now", "k3s")
if err != nil {
logrus.Debug(err)
logrus.Warn("Unable to enable or start k3s via systemd")
}
}
1 change: 1 addition & 0 deletions cli/internal/k3s/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func configureRHEL() {
// @todo: k3s docs recommend disabling this, but we should look at just tuning it appropriately
_, err := utils.ExecCommand(nil, "systemctl", "disable", "firewalld", "--now")
if err != nil {
logrus.Debug(err)
logrus.Warn("Unable to disable the firewall")
}
}
1 change: 1 addition & 0 deletions cli/internal/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func connect() *kubernetes.Clientset {
func readFile(file string) ([]byte, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
logrus.Debug(err)
return []byte{}, fmt.Errorf("cannot read file %v, %v", file, err)
}
return b, nil
Expand Down
5 changes: 5 additions & 0 deletions cli/internal/k8s/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ func ReplaceTLSSecret(namespace string, name string, certPath string, keyPath st

err := namespaceSecrets.Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
logContext.Debug(err)
logContext.Warn("Error deleting the secret")
}

tlsCert, err := readFile(certPath)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to read the TLS public certificate")
}
tlsKey, err := readFile(keyPath)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to read the TLS private key")
}
if _, err := tls.X509KeyPair(tlsCert, tlsKey); err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to create the TLS keypair")
}

Expand All @@ -57,6 +61,7 @@ func ReplaceTLSSecret(namespace string, name string, certPath string, keyPath st

_, err = namespaceSecrets.Create(context.TODO(), secretTLS, metav1.CreateOptions{})
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to create the secret", err)
}
}
26 changes: 26 additions & 0 deletions cli/internal/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package log

import (
"github.com/sirupsen/logrus"
)

var Logger = logrus.New()

func SetLogLevel(logLevel string) {
switch logLevel {
case "debug":
Logger.SetLevel(logrus.DebugLevel)
case "info":
Logger.SetLevel(logrus.InfoLevel)
case "warn":
Logger.SetLevel(logrus.WarnLevel)
case "error":
Logger.SetLevel(logrus.ErrorLevel)
case "fatal":
Logger.SetLevel(logrus.FatalLevel)
case "panic":
Logger.SetLevel(logrus.PanicLevel)
default:
Logger.Fatal("Unrecongized log level entry: %s", logLevel)
}
}
1 change: 1 addition & 0 deletions cli/internal/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Create(confirm bool) {
_ = os.RemoveAll(packageName)
err := archiver.Archive([]string{tempPath.base + "/"}, packageName)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to create the package archive")
}

Expand Down
2 changes: 2 additions & 0 deletions cli/internal/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Deploy(packageName string, confirm bool, componentRequest string) {
// Extract the archive
err := archiver.Unarchive(packageName, tempPath.base)
if err != nil {
logrus.Debug(err)
logrus.Fatal("Unable to extract the package contents")
}

Expand Down Expand Up @@ -124,6 +125,7 @@ func deployComponents(tempPath componentPaths, assets config.ZarfComponent) {
}
err := copy.Copy(sourceFile, file.Target)
if err != nil {
logrus.Debug(err)
logrus.WithField("file", file.Target).Fatal("Unable to copy the contents of the asset")
}
// Cleanup now to reduce disk pressure
Expand Down
12 changes: 11 additions & 1 deletion cli/internal/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func MakeTempDir() string {
logContext.Info("Creating temp path")

if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to create temp directory")
}
return tmp
Expand Down Expand Up @@ -50,6 +51,7 @@ func ListDirectories(directory string) []string {
var directories []string
paths, err := os.ReadDir(directory)
if err != nil {
logrus.Debug(err)
logrus.WithField("path", directory).Fatal("Unable to load the directory")
}

Expand All @@ -68,17 +70,20 @@ func WriteFile(path string, data []byte) {

f, err := os.Create(path)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to create the file to write the contents")
}

_, err = f.Write(data)
if err != nil {
logContext.Fatal("Unable to write the file contents")
_ = f.Close()
logContext.Debug(err)
logContext.Fatal("Unable to write the file contents")
}

err = f.Close()
if err != nil {
logContext.Debug(err)
logContext.Fatal("Error saving file")
}

Expand All @@ -88,12 +93,14 @@ func ReplaceText(path string, old string, new string) {
logContext := logrus.WithField("path", path)
input, err := ioutil.ReadFile(path)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to load the given file")
}

output := bytes.Replace(input, []byte(old), []byte(new), -1)

if err = ioutil.WriteFile(path, output, 0600); err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to update the given file")
}
}
Expand All @@ -113,6 +120,7 @@ func RecursiveFileList(root string) []string {
})

if err != nil {
logrus.Debug(err)
logrus.WithField("path", root).Fatal("Unable to complete directory walking")
}

Expand All @@ -123,6 +131,7 @@ func CreateFilePath(destination string) {
parentDest := path.Dir(destination)
err := CreateDirectory(parentDest, 0700)
if err != nil {
logrus.Debug(err)
logrus.WithField("path", parentDest).Fatal("Unable to create the destination path")
}
}
Expand All @@ -140,6 +149,7 @@ func CreatePathAndCopy(source string, destination string) {
// Copy the asset
err := copy.Copy(source, destination)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to copy the contents of the asset")
}
}
3 changes: 3 additions & 0 deletions cli/internal/utils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ func DownloadToFile(url string, target string) {
// Create the file
destinationFile, err := os.Create(target)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to create the destination file")
}
defer destinationFile.Close()

// Get the data
resp, err := http.Get(url)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to download the file", err)
}
defer resp.Body.Close()
Expand All @@ -81,6 +83,7 @@ func DownloadToFile(url string, target string) {
// Writer the body to file
_, err = io.Copy(destinationFile, resp.Body)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to save the file", err)
}
}
Loading

0 comments on commit 5825443

Please sign in to comment.