2021-05-02 23:49:16 +01:00
|
|
|
package gounicorn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-07-17 16:02:24 +01:00
|
|
|
"syscall"
|
2021-05-02 23:49:16 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GoUnicorn struct {
|
2021-07-17 15:59:31 +01:00
|
|
|
log *log.Entry
|
|
|
|
p *exec.Cmd
|
|
|
|
started bool
|
2021-07-17 16:02:24 +01:00
|
|
|
killed bool
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGoUnicorn() *GoUnicorn {
|
2021-06-23 19:40:51 +01:00
|
|
|
logger := log.WithField("logger", "authentik.g.unicorn")
|
2021-07-17 15:59:31 +01:00
|
|
|
g := &GoUnicorn{
|
|
|
|
log: logger,
|
|
|
|
started: false,
|
2021-07-17 16:02:24 +01:00
|
|
|
killed: false,
|
2021-07-17 15:59:31 +01:00
|
|
|
}
|
|
|
|
g.initCmd()
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoUnicorn) initCmd() {
|
2021-05-02 23:49:16 +01:00
|
|
|
command := "gunicorn"
|
|
|
|
args := []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi:application"}
|
|
|
|
if config.G.Debug {
|
|
|
|
command = "python"
|
|
|
|
args = []string{"manage.py", "runserver", "localhost:8000"}
|
|
|
|
}
|
2021-07-17 15:59:31 +01:00
|
|
|
g.log.WithField("args", args).WithField("cmd", command).Debug("Starting gunicorn")
|
|
|
|
g.p = exec.Command(command, args...)
|
2021-07-17 16:02:24 +01:00
|
|
|
g.p.Env = os.Environ()
|
|
|
|
// Don't pass ctrl-c to child
|
|
|
|
// since we handle it ourselves
|
|
|
|
g.p.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setpgid: true,
|
|
|
|
}
|
2021-07-17 15:59:31 +01:00
|
|
|
g.p.Stdout = os.Stdout
|
|
|
|
g.p.Stderr = os.Stderr
|
2021-06-23 19:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoUnicorn) Start() error {
|
2021-07-17 16:02:24 +01:00
|
|
|
if g.killed {
|
2021-07-17 17:04:09 +01:00
|
|
|
g.log.Debug("Not restarting gunicorn since we're killed")
|
2021-07-17 16:02:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-17 15:59:31 +01:00
|
|
|
if g.started {
|
|
|
|
g.initCmd()
|
|
|
|
}
|
|
|
|
g.started = true
|
2021-06-23 19:40:51 +01:00
|
|
|
return g.p.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoUnicorn) Kill() error {
|
2021-07-17 16:02:24 +01:00
|
|
|
g.killed = true
|
2021-06-23 19:40:51 +01:00
|
|
|
return g.p.Process.Kill()
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|