Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
log to stdout by default (#252)
Browse files Browse the repository at this point in the history
- Added and abstraction service on top of logrus which switches the `SetOutput` between `os.Stdout` and `os.Stderr` as necessary.
- Removed all references of logrus from the project and now reference the wrapper service.
- Added cleanup job to delete git clones to the host temp directory.
- Doc formatting
  • Loading branch information
evanlouie authored Sep 3, 2019
1 parent 14088b1 commit 542fcda
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 92 deletions.
4 changes: 2 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/kyokomi/emoji"
"github.com/microsoft/fabrikate/core"
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ $ fab add cloud-native --source https://github.com/microsoft/fabrikate-definitio
if cmd.Flags().Changed("method") && method != "git" {
// Warn users if they explicitly set --branch that the config is being removed
if cmd.Flags().Changed("branch") {
log.Warn(emoji.Sprintf(":exclamation: Non 'git' --method and explicit --branch specified. Removing --branch configuration of 'branch: %s'", branch))
logger.Warn(emoji.Sprintf(":exclamation: Non 'git' --method and explicit --branch specified. Removing --branch configuration of 'branch: %s'", branch))
}
branch = ""
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/kyokomi/emoji"
"github.com/microsoft/fabrikate/core"
"github.com/microsoft/fabrikate/generators"
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +28,7 @@ func writeGeneratedManifests(generationPath string, components []core.Component)
componentYAMLFilename := fmt.Sprintf("%s.yaml", component.Name)
componentYAMLFilePath := path.Join(componentGenerationPath, componentYAMLFilename)

log.Info(emoji.Sprintf(":floppy_disk: Writing %s", componentYAMLFilePath))
logger.Info(emoji.Sprintf(":floppy_disk: Writing %s", componentYAMLFilePath))

err = ioutil.WriteFile(componentYAMLFilePath, []byte(component.Manifest), 0644)
if err != nil {
Expand All @@ -40,10 +40,10 @@ func writeGeneratedManifests(generationPath string, components []core.Component)
}

func validateGeneratedManifests(generationPath string) (err error) {
log.Info(emoji.Sprintf(":microscope: Validating generated manifests in path %s", generationPath))
logger.Info(emoji.Sprintf(":microscope: Validating generated manifests in path %s", generationPath))
if output, err := exec.Command("kubectl", "apply", "--validate=true", "--dry-run", "--recursive", "-f", generationPath).Output(); err != nil {
if ee, ok := err.(*exec.ExitError); ok {
log.Errorf("Validating generated manifests failed with: %s: output: %s", ee.Stderr, output)
logger.Error(fmt.Sprintf("Validating generated manifests failed with: %s: output: %s", ee.Stderr, output))
return err
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func Generate(startPath string, environments []string, validate bool) (component
}

if err == nil {
log.Info(emoji.Sprintf(":raised_hands: Finished generate"))
logger.Info(emoji.Sprintf(":raised_hands: Finished generate"))
}

return components, err
Expand Down
16 changes: 8 additions & 8 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/kyokomi/emoji"
"github.com/microsoft/fabrikate/core"
"github.com/microsoft/fabrikate/generators"
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/spf13/cobra"
)

Expand All @@ -21,17 +21,17 @@ func Install(path string) (err error) {
if err != nil {
return err
}
log.Info(emoji.Sprintf(":mag: Using %s: %s", tool, path))
logger.Info(emoji.Sprintf(":mag: Using %s: %s", tool, path))
}

log.Info(emoji.Sprintf(":point_right: Initializing Helm"))
logger.Info(emoji.Sprintf(":point_right: Initializing Helm"))
if output, err := exec.Command("helm", "init", "--client-only").CombinedOutput(); err != nil {
log.Error(emoji.Sprintf(":no_entry_sign: %s: %s", err, output))
logger.Error(emoji.Sprintf(":no_entry_sign: %s: %s", err, output))
return err
}

results := core.WalkComponentTree(path, []string{}, func(path string, component *core.Component) (err error) {
log.Info(emoji.Sprintf(":point_right: Starting install for component: %s", component.Name))
logger.Info(emoji.Sprintf(":point_right: Starting install for component: %s", component.Name))

var generator core.Generator

Expand All @@ -55,7 +55,7 @@ func Install(path string) (err error) {
return err
}

log.Info(emoji.Sprintf(":point_left: Finished install for component: %s", component.Name))
logger.Info(emoji.Sprintf(":point_left: Finished install for component: %s", component.Name))

return err
})
Expand All @@ -66,9 +66,9 @@ func Install(path string) (err error) {
}

for _, component := range components {
log.Info(emoji.Sprintf(":white_check_mark: Installed successfully: %s", component.Name))
logger.Info(emoji.Sprintf(":white_check_mark: Installed successfully: %s", component.Name))
}
log.Info(emoji.Sprintf(":raised_hands: Finished install"))
logger.Info(emoji.Sprintf(":raised_hands: Finished install"))

return err
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -20,9 +21,9 @@ var rootCmd = &cobra.Command{
verbose := cmd.Flag("verbose").Value.String()

if verbose == "true" {
log.SetLevel(log.DebugLevel)
logger.SetLevelDebug()
} else {
log.SetLevel(log.InfoLevel)
logger.SetLevelInfo()
}

return nil
Expand All @@ -33,7 +34,7 @@ var rootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Error(err)
logger.Error(err)
os.Exit(1)
}
}
Expand All @@ -53,7 +54,7 @@ func initConfig() {
// Find home directory.
home, err := os.UserHomeDir()
if err != nil {
log.Errorf("Getting home directory failed with: %s\n", err)
logger.Error(fmt.Sprintf("Getting home directory failed with: %s\n", err))
os.Exit(1)
}

Expand All @@ -66,6 +67,6 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debugf("Using config file: %s\n", viper.ConfigFileUsed())
logger.Debug(fmt.Sprintf("Using config file: %s\n", viper.ConfigFileUsed()))
}
}
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/spf13/cobra"
)

Expand All @@ -30,7 +30,7 @@ var versionCmd = &cobra.Command{

// PrintVersion prints the current version of Fabrikate being used.
func PrintVersion() {
log.Info("fab version 0.15.3")
logger.Info("fab version 0.15.3")
}

func init() {
Expand Down
30 changes: 15 additions & 15 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sync"

"github.com/kyokomi/emoji"
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/timfpark/yaml"
)

Expand Down Expand Up @@ -56,15 +56,15 @@ func UnmarshalFile(path string, unmarshalFunc unmarshalFunction, output interfac
return err
}

log.Info(emoji.Sprintf(":floppy_disk: Loading %s", path))
logger.Info(emoji.Sprintf(":floppy_disk: Loading %s", path))

return unmarshalFunc(marshaled, output)
}

// UnmarshalComponent finds and unmarshal the component.<format> of a component using the
// provided `unmarshalFunc` function.
func (c *Component) UnmarshalComponent(serializationType string, unmarshalFunc unmarshalFunction, component *Component) error {
log.Debugf("Attempting to unmarshal %s for component '%s'", serializationType, c.Name)
logger.Debug(fmt.Sprintf("Attempting to unmarshal %s for component '%s'", serializationType, c.Name))

componentFilename := fmt.Sprintf("component.%s", serializationType)
componentPath := path.Join(c.PhysicalPath, componentFilename)
Expand All @@ -77,12 +77,12 @@ func (c *Component) UnmarshalComponent(serializationType string, unmarshalFunc u

func (c *Component) applyDefaultsAndMigrations() {
if len(c.Generator) > 0 {
log.Warn(emoji.Sprintf(":boom: DEPRECATION WARNING: Field 'generator' has been deprecated and will be removed in version v1.0.0; Update component '%s' to use 'type' in place of 'generator'", c.Name))
logger.Warn(emoji.Sprintf(":boom: DEPRECATION WARNING: Field 'generator' has been deprecated and will be removed in version v1.0.0; Update component '%s' to use 'type' in place of 'generator'", c.Name))
c.ComponentType = c.Generator
}

if len(c.Repositories) > 0 {
log.Warn(emoji.Sprintf(":boom: DEPRECATION WARNING: Field `repositories` has been deprecrated and will be removed in version v1.0.0; Update component '%s' to use `method: helm`, `source: <helm_repo_url>`, and `path: <chart_name>` and remove `repositories`", c.Name))
logger.Warn(emoji.Sprintf(":boom: DEPRECATION WARNING: Field `repositories` has been deprecrated and will be removed in version v1.0.0; Update component '%s' to use `method: helm`, `source: <helm_repo_url>`, and `path: <chart_name>` and remove `repositories`", c.Name))
}

if len(c.ComponentType) == 0 {
Expand Down Expand Up @@ -151,18 +151,18 @@ func (c *Component) ExecuteHook(hook string) (err error) {
}

for _, command := range c.Hooks[hook] {
log.Info(emoji.Sprintf(":fishing_pole_and_fish: Executing command in hook '%s' for component '%s': %s", hook, c.Name, command))
logger.Info(emoji.Sprintf(":fishing_pole_and_fish: Executing command in hook '%s' for component '%s': %s", hook, c.Name, command))
if len(command) != 0 {
cmd := exec.Command("sh", "-c", command)
cmd.Dir = c.PhysicalPath
output, err := cmd.CombinedOutput()
if err != nil {
log.Error(emoji.Sprintf(":no_entry_sign: Error occurred in hook '%s' for component '%s'\n%s: %s", hook, c.Name, err, output))
logger.Error(emoji.Sprintf(":no_entry_sign: Error occurred in hook '%s' for component '%s'\n%s: %s", hook, c.Name, err, output))
return err
}
if len(output) > 0 {
outstring := emoji.Sprintf(":mag_right: Completed hook '%s' for component '%s':\n%s", hook, c.Name, output)
log.Trace(strings.TrimSpace(outstring))
logger.Trace(strings.TrimSpace(outstring))
}
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func (c *Component) InstallComponent(componentPath string) (err error) {
return err
}

log.Info(emoji.Sprintf(":helicopter: Installing component '%s' with git from '%s'", c.Name, c.Source))
logger.Info(emoji.Sprintf(":helicopter: Installing component '%s' with git from '%s'", c.Name, c.Source))

if err = CloneRepo(c.Source, c.Version, subcomponentPath, c.Branch); err != nil {
return err
Expand Down Expand Up @@ -281,7 +281,7 @@ func WalkComponentTree(startingPath string, environments []string, iterator comp
// Prepares `component` by loading/de-serializing the component.yaml/json and configs
// Note: this is only needed for non-inlined components
prepareComponent := func(c Component) Component {
log.Debugf("Preparing component '%s'", c.Name)
logger.Debug(fmt.Sprintf("Preparing component '%s'", c.Name))
// 1. Parse the component at that path into a Component
c, err := c.LoadComponent()
if err != nil {
Expand All @@ -299,7 +299,7 @@ func WalkComponentTree(startingPath string, environments []string, iterator comp
enqueue := func(c Component) {
// Increment working counter; MUST happen BEFORE sending to queue or race condition can occur
walking.Add(1)
log.Debugf("Adding subcomponent '%s' to queue with physical path '%s' and logical path '%s'\n", c.Name, c.PhysicalPath, c.LogicalPath)
logger.Debug(fmt.Sprintf("Adding subcomponent '%s' to queue with physical path '%s' and logical path '%s'\n", c.Name, c.PhysicalPath, c.LogicalPath))
queue <- c
}

Expand Down Expand Up @@ -359,7 +359,7 @@ func WalkComponentTree(startingPath string, environments []string, iterator comp
subcomponent.LogicalPath = c.LogicalPath
}

log.Debugf("Adding subcomponent '%s' to queue with physical path '%s' and logical path '%s'\n", subcomponent.Name, subcomponent.PhysicalPath, subcomponent.LogicalPath)
logger.Debug(fmt.Sprintf("Adding subcomponent '%s' to queue with physical path '%s' and logical path '%s'\n", subcomponent.Name, subcomponent.PhysicalPath, subcomponent.LogicalPath))
enqueue(subcomponent)
}
}(queuedComponent)
Expand Down Expand Up @@ -402,7 +402,7 @@ func (c *Component) Write() (err error) {
filename := fmt.Sprintf("component.%s", c.Serialization)
componentPath := path.Join(c.PhysicalPath, filename)

log.Info(emoji.Sprintf(":floppy_disk: Writing '%s'", componentPath))
logger.Info(emoji.Sprintf(":floppy_disk: Writing '%s'", componentPath))

return ioutil.WriteFile(componentPath, marshaledComponent, 0644)
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func (c *Component) GetAccessTokens() (tokens map[string]string, err error) {
// If the file is not found, return an empty map with no error
return map[string]string{}, nil
} else if err != nil {
log.Error(emoji.Sprintf(":no_entry_sign: Error unmarshalling access.yaml in '%s'", accessYamlPath))
logger.Error(emoji.Sprintf(":no_entry_sign: Error unmarshalling access.yaml in '%s'", accessYamlPath))
return nil, err
}

Expand All @@ -487,7 +487,7 @@ func (c *Component) GetAccessTokens() (tokens map[string]string, err error) {
if token == "" {
// Give warning that failed to load env var; but continue and attempt clone
msg := fmt.Sprintf("Component '%s' attempted to load environment variable '%s'; but is either not set or an empty string. Components with source '%s' may fail to install", c.Name, envVar, repo)
log.Warn(emoji.Sprintf(":no_entry_sign: %s", msg))
logger.Warn(emoji.Sprintf(":no_entry_sign: %s", msg))
} else {
tokens[repo] = token
}
Expand Down
6 changes: 3 additions & 3 deletions core/componentConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/kyokomi/emoji"
log "github.com/sirupsen/logrus"
"github.com/microsoft/fabrikate/logger"
"github.com/timfpark/conjungo"
yaml "github.com/timfpark/yaml"
)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (cc *ComponentConfig) SetComponentConfig(path []string, value string) {
configLevel = configLevel[pathPart].(map[string]interface{})
} else {
if createdNewConfig {
log.Info(emoji.Sprintf(":seedling: Created new value for %s", strings.Join(path, ".")))
logger.Info(emoji.Sprintf(":seedling: Created new value for %s", strings.Join(path, ".")))
}
configLevel[pathPart] = value
}
Expand All @@ -135,7 +135,7 @@ func (cc *ComponentConfig) GetSubcomponentConfig(subcomponentPath []string) (sub
}

if _, ok := subcomponentConfig.Subcomponents[subcomponentName]; !ok {
log.Info(emoji.Sprintf(":seedling: Creating new subcomponent configuration for %s", subcomponentName))
logger.Info(emoji.Sprintf(":seedling: Creating new subcomponent configuration for %s", subcomponentName))
subcomponentConfig.Subcomponents[subcomponentName] = NewComponentConfig(".")
}

Expand Down
Loading

0 comments on commit 542fcda

Please sign in to comment.