diff options
author | Nick Thomas <nick@gitlab.com> | 2021-07-15 12:44:48 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2021-07-15 12:44:48 +0000 |
commit | 60a91e977cb76e2cc49faeeac134640dbf2e2e6c (patch) | |
tree | b693a2244f3d715d48df83eac70bdf6630e51d3a /cmd/gitlab-sshd/main.go | |
parent | d3711d8d7e781dbff01d8ae5c7a1d5b800c5c8a2 (diff) | |
parent | 569a0197cacc75270776217c27e9d709907a9dfa (diff) | |
download | gitlab-shell-60a91e977cb76e2cc49faeeac134640dbf2e2e6c.tar.gz |
Merge branch 'id-cancelable-sshd' into 'main'
Shutdown sshd gracefully
See merge request gitlab-org/gitlab-shell!484
Diffstat (limited to 'cmd/gitlab-sshd/main.go')
-rw-r--r-- | cmd/gitlab-sshd/main.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/cmd/gitlab-sshd/main.go b/cmd/gitlab-sshd/main.go index 866bc8d..7cecbf5 100644 --- a/cmd/gitlab-sshd/main.go +++ b/cmd/gitlab-sshd/main.go @@ -3,6 +3,10 @@ package main import ( "flag" "os" + "os/signal" + "context" + "syscall" + "time" log "github.com/sirupsen/logrus" @@ -63,6 +67,8 @@ func main() { ctx, finished := command.Setup("gitlab-sshd", cfg) defer finished() + server := sshd.Server{Config: cfg} + // Startup monitoring endpoint. if cfg.Server.WebListen != "" { go func() { @@ -75,7 +81,27 @@ func main() { }() } - if err := sshd.Run(ctx, cfg); err != nil { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) + + go func() { + sig := <-done + signal.Reset(syscall.SIGINT, syscall.SIGTERM) + + log.WithFields(log.Fields{"shutdown_timeout_s": cfg.Server.GracePeriodSeconds, "signal": sig.String()}).Infof("Shutdown initiated") + + server.Shutdown() + + <-time.After(cfg.Server.GracePeriod()) + + cancel() + + }() + + if err := server.ListenAndServe(ctx); err != nil { log.Fatalf("Failed to start GitLab built-in sshd: %v", err) } } |