diff --git a/README.md b/README.md index f9743d3..ad1d0d1 100644 --- a/README.md +++ b/README.md @@ -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 -- diff --git a/utils/odoo.go b/utils/odoo.go index 4650f6c..491b42b 100644 --- a/utils/odoo.go +++ b/utils/odoo.go @@ -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 diff --git a/utils/supervisor.go b/utils/supervisor.go index 926383c..3d9caf2 100644 --- a/utils/supervisor.go +++ b/utils/supervisor.go @@ -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 { @@ -38,7 +34,7 @@ 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 } @@ -46,9 +42,9 @@ func UpdateAutostart(autostart bool, confPath string) error { 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 @@ -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 +}