Skip to content

Commit

Permalink
Log error messages in Debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
YrrepNoj committed Oct 28, 2021
1 parent f76e6c8 commit 304dec6
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 1 deletion.
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 {
log.Logger.Debug(err)
log.Logger.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 {
log.Logger.Debug(err)
log.Logger.Fatal("Unable to compute the hash")
} else {
fmt.Println(hash)
Expand Down
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 @@ -17,13 +17,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 @@ -32,6 +34,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 @@ -47,6 +47,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 @@ -122,6 +122,7 @@ func CredentialsGenerator() string {

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

// Save the change
err = credentialsFile.Sync()
if err != nil {
log.Logger.Debug(err)
log.Logger.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 @@ -67,12 +67,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) {

log.Logger.Info("Creating image tarball (this will take a while)")
if err := crane.MultiSave(imageMap, imageTarballPath); err != nil {
log.Logger.Debug(err)
log.Logger.Fatal("Unable to save the tarball")
}
}
3 changes: 3 additions & 0 deletions cli/internal/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ func PushAll(imageTarballPath string, buildImageList []string) {
logContext.Info("Updating image")
img, err := crane.LoadTag(imageTarballPath, src, cranePlatformOptions)
if err != nil {
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 @@ -33,6 +35,7 @@ func PushAll(imageTarballPath string, buildImageList []string) {
log.Logger.Info(offlineName)
err = crane.Push(img, offlineName, cranePlatformOptions)
if err != nil {
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 @@ -52,6 +52,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 {
log.Logger.Debug(err)
log.Logger.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 @@ -71,11 +72,13 @@ func createService() {

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

_, err = utils.ExecCommand(nil, "systemctl", "enable", "--now", "k3s")
if err != nil {
log.Logger.Debug(err)
log.Logger.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 {
log.Logger.Debug(err)
log.Logger.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 {
log.Logger.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 @@ -25,18 +25,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 @@ -58,6 +62,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)
}
}
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 {
log.Logger.Debug(err)
log.Logger.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 {
log.Logger.Debug(err)
log.Logger.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 {
log.Logger.Debug(err)
log.Logger.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 @@ -21,6 +21,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 @@ -51,6 +52,7 @@ func ListDirectories(directory string) []string {
var directories []string
paths, err := os.ReadDir(directory)
if err != nil {
log.Logger.Debug(err)
log.Logger.WithField("path", directory).Fatal("Unable to load the directory")
}

Expand All @@ -69,17 +71,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 @@ -89,12 +94,14 @@ func ReplaceText(path string, old string, new string) {
logContext := log.Logger.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 @@ -114,6 +121,7 @@ func RecursiveFileList(root string) []string {
})

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

Expand All @@ -124,6 +132,7 @@ func CreateFilePath(destination string) {
parentDest := path.Dir(destination)
err := CreateDirectory(parentDest, 0700)
if err != nil {
log.Logger.Debug(err)
log.Logger.WithField("path", parentDest).Fatal("Unable to create the destination path")
}
}
Expand All @@ -141,6 +150,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 @@ -63,13 +63,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 @@ -82,6 +84,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)
}
}
3 changes: 3 additions & 0 deletions cli/internal/utils/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func RandomString(length int) string {
bytes := make([]byte, length)

if _, err := rand.Read(bytes); err != nil {
log.Logger.Debug(err)
log.Logger.Fatal("unable to generate a random secret")
}

Expand Down Expand Up @@ -112,12 +113,14 @@ func addCAToTrustStore(caFilePath string) {
CreatePathAndCopy(caFilePath, "/etc/pki/ca-trust/source/anchors/zarf-ca.crt")
_, err := ExecCommand(nil, rhelBinary, "extract")
if err != nil {
log.Logger.Debug(err)
log.Logger.Warn("Error adding the ephemeral CA to the RHEL root trust")
}
} else if VerifyBinary(debianBinary) {
CreatePathAndCopy(caFilePath, "/usr/local/share/ca-certificates/extra/zarf-ca.crt")
_, err := ExecCommand(nil, debianBinary)
if err != nil {
log.Logger.Debug(err)
log.Logger.Warn("Error adding the ephemeral CA to the trust store")
}
}
Expand Down

0 comments on commit 304dec6

Please sign in to comment.