2021-05-02 22:49:16 +00:00
|
|
|
package gounicorn
|
|
|
|
|
|
|
|
import (
|
2021-10-05 20:19:33 +00:00
|
|
|
"net/http"
|
2021-05-02 22:49:16 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-10-05 20:19:33 +00:00
|
|
|
"time"
|
2021-05-02 22:49:16 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-10-05 20:19:33 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoUnicorn struct {
|
2021-07-17 14:59:31 +00:00
|
|
|
log *log.Entry
|
|
|
|
p *exec.Cmd
|
|
|
|
started bool
|
2021-07-17 15:02:24 +00:00
|
|
|
killed bool
|
2021-10-05 20:19:33 +00:00
|
|
|
alive bool
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGoUnicorn() *GoUnicorn {
|
2021-10-11 15:51:49 +00:00
|
|
|
logger := log.WithField("logger", "authentik.router.unicorn")
|
2021-07-17 14:59:31 +00:00
|
|
|
g := &GoUnicorn{
|
|
|
|
log: logger,
|
|
|
|
started: false,
|
2021-07-17 15:02:24 +00:00
|
|
|
killed: false,
|
2021-10-05 20:19:33 +00:00
|
|
|
alive: false,
|
2021-07-17 14:59:31 +00:00
|
|
|
}
|
|
|
|
g.initCmd()
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoUnicorn) initCmd() {
|
2021-05-02 22:49:16 +00:00
|
|
|
command := "gunicorn"
|
2021-08-23 13:15:12 +00:00
|
|
|
args := []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi.app:application"}
|
2021-07-17 14:59:31 +00:00
|
|
|
g.log.WithField("args", args).WithField("cmd", command).Debug("Starting gunicorn")
|
|
|
|
g.p = exec.Command(command, args...)
|
2021-07-17 15:02:24 +00:00
|
|
|
g.p.Env = os.Environ()
|
2021-07-17 14:59:31 +00:00
|
|
|
g.p.Stdout = os.Stdout
|
|
|
|
g.p.Stderr = os.Stderr
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:19:33 +00:00
|
|
|
func (g *GoUnicorn) IsRunning() bool {
|
|
|
|
return g.alive
|
|
|
|
}
|
|
|
|
|
2021-06-23 18:40:51 +00:00
|
|
|
func (g *GoUnicorn) Start() error {
|
2021-07-17 15:02:24 +00:00
|
|
|
if g.killed {
|
2021-07-17 16:04:09 +00:00
|
|
|
g.log.Debug("Not restarting gunicorn since we're killed")
|
2021-07-17 15:02:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-17 14:59:31 +00:00
|
|
|
if g.started {
|
|
|
|
g.initCmd()
|
|
|
|
}
|
|
|
|
g.started = true
|
2021-10-05 20:19:33 +00:00
|
|
|
go g.healthcheck()
|
2021-06-23 18:40:51 +00:00
|
|
|
return g.p.Run()
|
|
|
|
}
|
|
|
|
|
2021-10-05 20:19:33 +00:00
|
|
|
func (g *GoUnicorn) healthcheck() {
|
|
|
|
g.log.Debug("starting healthcheck")
|
|
|
|
h := &http.Client{
|
|
|
|
Transport: ak.NewUserAgentTransport("goauthentik.io go proxy healthcheck", http.DefaultTransport),
|
|
|
|
}
|
|
|
|
check := func() bool {
|
|
|
|
res, err := h.Get("http://localhost:8000/-/health/live/")
|
|
|
|
if err == nil && res.StatusCode == 204 {
|
|
|
|
g.alive = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default healthcheck is every 1 second on startup
|
|
|
|
// once we've been healthy once, increase to 30 seconds
|
|
|
|
for range time.Tick(time.Second) {
|
|
|
|
if check() {
|
|
|
|
g.log.Info("backend is alive, backing off with healthchecks")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
g.log.Debug("backend not alive yet")
|
|
|
|
}
|
|
|
|
for range time.Tick(30 * time.Second) {
|
|
|
|
check()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:12:57 +00:00
|
|
|
func (g *GoUnicorn) Kill() {
|
2021-07-17 15:02:24 +00:00
|
|
|
g.killed = true
|
2021-07-18 14:12:57 +00:00
|
|
|
err := g.p.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
g.log.WithError(err).Warning("failed to kill gunicorn")
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|