2021-05-02 23:49:16 +01:00
|
|
|
package gounicorn
|
|
|
|
|
|
|
|
import (
|
2021-10-05 21:19:33 +01:00
|
|
|
"net/http"
|
2021-05-02 23:49:16 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-11-06 12:54:59 +00:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2021-10-05 21:19:33 +01:00
|
|
|
"time"
|
2021-05-02 23:49:16 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-15 13:04:10 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-10-05 21:19:33 +01:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2021-05-02 23:49:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoUnicorn struct {
|
2021-11-04 12:16:53 +00:00
|
|
|
HealthyCallback func()
|
|
|
|
|
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-10-05 21:19:33 +01:00
|
|
|
alive bool
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGoUnicorn() *GoUnicorn {
|
2021-10-11 16:51:49 +01:00
|
|
|
logger := log.WithField("logger", "authentik.router.unicorn")
|
2021-07-17 15:59:31 +01:00
|
|
|
g := &GoUnicorn{
|
2021-11-04 12:16:53 +00:00
|
|
|
log: logger,
|
|
|
|
started: false,
|
|
|
|
killed: false,
|
|
|
|
alive: false,
|
|
|
|
HealthyCallback: func() {},
|
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"
|
2021-11-15 15:32:56 +00:00
|
|
|
args := []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi:application"}
|
2021-11-15 13:04:10 +00:00
|
|
|
if config.G.Debug {
|
|
|
|
command = "./manage.py"
|
|
|
|
args = []string{"runserver"}
|
|
|
|
}
|
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()
|
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
|
|
|
}
|
|
|
|
|
2021-10-05 21:19:33 +01:00
|
|
|
func (g *GoUnicorn) IsRunning() bool {
|
|
|
|
return g.alive
|
|
|
|
}
|
|
|
|
|
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-11-06 12:54:59 +00:00
|
|
|
g.log.Debug("Not restarting gunicorn since we're shutdown")
|
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-10-05 21:19:33 +01:00
|
|
|
go g.healthcheck()
|
2021-06-23 19:40:51 +01:00
|
|
|
return g.p.Run()
|
|
|
|
}
|
|
|
|
|
2021-10-05 21:19:33 +01:00
|
|
|
func (g *GoUnicorn) healthcheck() {
|
|
|
|
g.log.Debug("starting healthcheck")
|
|
|
|
h := &http.Client{
|
2022-04-02 18:48:17 +01:00
|
|
|
Transport: ak.NewUserAgentTransport("goauthentik.io/proxy/healthcheck", http.DefaultTransport),
|
2021-10-05 21:19:33 +01:00
|
|
|
}
|
|
|
|
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
|
2022-04-29 12:15:54 +01:00
|
|
|
for range time.Tick(time.Second) {
|
|
|
|
if check() {
|
|
|
|
g.log.Info("backend is alive, backing off with healthchecks")
|
|
|
|
g.HealthyCallback()
|
|
|
|
break
|
2021-10-05 21:19:33 +01:00
|
|
|
}
|
2022-04-29 12:15:54 +01:00
|
|
|
g.log.Debug("backend not alive yet")
|
|
|
|
}
|
2021-10-05 21:19:33 +01:00
|
|
|
for range time.Tick(30 * time.Second) {
|
|
|
|
check()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 15:12:57 +01:00
|
|
|
func (g *GoUnicorn) Kill() {
|
2021-07-17 16:02:24 +01:00
|
|
|
g.killed = true
|
2021-11-06 12:54:59 +00:00
|
|
|
var err error
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
g.log.WithField("method", "kill").Warning("stopping gunicorn")
|
|
|
|
err = g.p.Process.Kill()
|
|
|
|
} else {
|
|
|
|
g.log.WithField("method", "sigterm").Warning("stopping gunicorn")
|
|
|
|
err = syscall.Kill(g.p.Process.Pid, syscall.SIGTERM)
|
|
|
|
}
|
2021-07-18 15:12:57 +01:00
|
|
|
if err != nil {
|
2021-11-06 12:54:59 +00:00
|
|
|
g.log.WithError(err).Warning("failed to stop gunicorn")
|
2021-07-18 15:12:57 +01:00
|
|
|
}
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|