Skip to content

Commit

Permalink
[ADD] Configuration to send logs to stdout
Browse files Browse the repository at this point in the history
Before the logs were sent directly to the file logs, now with this
commit the logs can be sent to stdout when the environment variable
`ORCHESTSH_STDOUT` is set to any non false value.

Useful when we deploy in k8s or something similar.
  • Loading branch information
ruiztulio committed Oct 18, 2024
1 parent c003265 commit 0ec8038
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ The entry_point can be tuned using any of the following env vars:
```AUTOSTART```: If False will set all the processed from supervisor to autostart false, if true basically won't change
anything because is assumed that all processes are autostart true by default

```ORCHESTSH_STDOUT```: Will force all the instance logs to the standard output, this is usefully when deploying the
container in kubernetes or docker swarm, all the logs can be read by any collector that can read the container logs.

Configuring Odoo instance
--

Expand Down
29 changes: 24 additions & 5 deletions utils/odoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,36 @@ func Odoo() error {
log.Debugf("Instance type: %s", instanceType)
UpdateSentry(odooCfg, instanceType)
SetDefaults(odooCfg)
autostart := true
if vr.readValue("AUTOSTART") != "" {
autostart, err = strconv.ParseBool(vr.readValue("AUTOSTART"))
autostart, err := strconv.ParseBool(vr.readValue("AUTOSTART"))
if err != nil {
autostart = true
log.Errorf("Error parsing AUTOSTART with value '%s': %s", vr.readValue("AUTOSTART"), err.Error())
return err
}
log.Debugf("Autostart: %v", autostart)

if autostart {
if err := UpdateSupervisor("/etc/supervisor/conf.d", SetAutostart); err != nil {
return err
}
}
}
if err := UpdateAutostart(autostart, "/etc/supervisor/conf.d"); err != nil {
return err

log.Debugf("Orchestsh stdout: %v", vr.readValue("ORCHESTSH_STDOUT"))
if vr.readValue("ORCHESTSH_STDOUT") != "" {
stdout, err := strconv.ParseBool(vr.readValue("ORCHESTSH_STDOUT"))
if err != nil {
log.Errorf("Error parsing ORCHESTSH_STDOUT with value '%s': %s", vr.readValue("ORCHESTSH_STDOUT"), err.Error())
return err
}
if stdout {
if err := UpdateSupervisor("/etc/supervisor/conf.d", SetStout); err != nil {
return err
}
}
log.Debugf("Orchestsh stdout: %v", stdout)
}

log.Info("Saving new Odoo configuration")
if err := UpdateOdooConfig(odooCfg, vr); err != nil {
return err
Expand Down
37 changes: 27 additions & 10 deletions utils/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import (
"gopkg.in/ini.v1"
)

// UpdateAutostart will look for all the services configuration in the supervisor conf.d directory and
// update autostart. If autostart is true nothing will change, if false all values will be set to false
func UpdateAutostart(autostart bool, confPath string) error {
// if autostart is true we do nothing because should use the default configuration
if autostart {
return nil
}
// UpdateSupervisor will look for all the services configuration in the supervisor conf.d directory and
// update the configuration accordingly applying the setter function.
func UpdateSupervisor(confPath string, setter func(content []byte, w io.Writer) error) error {

files, err := os.ReadDir(confPath)
if err != nil {
Expand All @@ -38,17 +34,17 @@ func UpdateAutostart(autostart bool, confPath string) error {
return err
}
defer w.Close()
err = setAutostart(content, w)
err = setter(content, w)
if err != nil {
return err
}
}
return nil
}

// setAutostart will enable autostart in a particular file, but won't touch the default section neither supervisor one
// SetAutostart will enable autostart in a particular file, but won't touch the default section neither supervisor one
// only in the services section
func setAutostart(content []byte, w io.Writer) error {
func SetAutostart(content []byte, w io.Writer) error {
cfg, err := ini.Load(content)
if err != nil {
return err
Expand All @@ -64,3 +60,24 @@ func setAutostart(content []byte, w io.Writer) error {
}
return nil
}

// SetStout will enable stdout_logfile and stderr_logfile in a particular configuration file so all the output will be
// sent to the standard output instead of a file as usually is set
func SetStout(content []byte, w io.Writer) error {
cfg, err := ini.Load(content)
if err != nil {
return err
}
for _, section := range cfg.Sections() {
if section.Name() == "supervisord" || section.Name() == "DEFAULT" {
continue
}
section.Key("stdout_logfile").SetValue("/dev/fd/1")
section.Key("stderr_logfile").SetValue("/dev/fd/1")
section.Key("stdout_logfile_maxbytes").SetValue("0")
}
if _, err := cfg.WriteTo(w); err != nil {
return err
}
return nil
}

0 comments on commit 0ec8038

Please sign in to comment.