3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-15 00:19:29 +01:00

Merge pull request #1735 from slingamn/systemd

support systemd notifications
This commit is contained in:
Shivaram Lingamneni 2021-07-05 03:10:27 -04:00 committed by GitHub
commit 98c4d0e399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 138 additions and 17 deletions

View File

@ -8,13 +8,14 @@ After=network.target
# After=network.target mysql.service
[Service]
Type=simple
Type=notify
User=ergo
WorkingDirectory=/home/ergo
ExecStart=/home/ergo/ergo run --conf /home/ergo/ircd.yaml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=1048576
NotifyAccess=main
# Uncomment this for a hidden service:
# PrivateNetwork=true

View File

@ -192,10 +192,6 @@ Options:
logman.Error("server", fmt.Sprintf("Could not load server: %s", err.Error()))
os.Exit(1)
}
if !arguments["--quiet"].(bool) {
logman.Info("server", "Server running")
defer logman.Info("server", fmt.Sprintf("%s exiting", irc.Ver))
}
if !arguments["--smoke"].(bool) {
server.Run()
}

1
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/go-test/deep v1.0.6 // indirect
github.com/gorilla/websocket v1.4.2
github.com/goshuirc/irc-go v0.0.0-20210318074529-bdc2c2cd2fef // indirect
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/oragono/confusables v0.0.0-20201108231250-4ab98ab61fb1 // indirect

2
go.sum
View File

@ -58,6 +58,8 @@ github.com/goshuirc/irc-go v0.0.0-20210318074529-bdc2c2cd2fef h1:07e6GcSuNh1BoZJ
github.com/goshuirc/irc-go v0.0.0-20210318074529-bdc2c2cd2fef/go.mod h1:q/JhvvKLmif3y9q8MDQM+gRCnjEKnu5ClF298TTXJug=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd h1:+iAPaTbi1gZpcpDwe/BW1fx7Xoesv69hLNGPheoyhBs=
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd/go.mod h1:4soZNh0zW0LtYGdQ416i0jO0EIqMGcbtaspRS4BDvRQ=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=

View File

@ -21,6 +21,7 @@ import (
"unsafe"
"github.com/ergochat/irc-go/ircfmt"
"github.com/okzk/sdnotify"
"github.com/ergochat/ergo/irc/caps"
"github.com/ergochat/ergo/irc/connection_limits"
@ -80,7 +81,7 @@ type Server struct {
rehashMutex sync.Mutex // tier 4
rehashSignal chan os.Signal
pprofServer *http.Server
signals chan os.Signal
exitSignals chan os.Signal
snomasks SnoManager
store *buntdb.DB
historyDB mysql.MySQL
@ -99,7 +100,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
listeners: make(map[string]IRCListener),
logger: logger,
rehashSignal: make(chan os.Signal, 1),
signals: make(chan os.Signal, len(ServerExitSignals)),
exitSignals: make(chan os.Signal, len(ServerExitSignals)),
defcon: 5,
}
@ -114,7 +115,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
}
// Attempt to clean up when receiving these signals.
signal.Notify(server.signals, ServerExitSignals...)
signal.Notify(server.exitSignals, ServerExitSignals...)
signal.Notify(server.rehashSignal, syscall.SIGHUP)
time.AfterFunc(alwaysOnExpirationPollPeriod, server.handleAlwaysOnExpirations)
@ -124,6 +125,9 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
// Shutdown shuts down the server.
func (server *Server) Shutdown() {
sdnotify.Stopping()
server.logger.Info("server", "Stopping server")
//TODO(dan): Make sure we disallow new nicks
for _, client := range server.clients.AllClients() {
client.Notice("Server is shutting down")
@ -137,19 +141,17 @@ func (server *Server) Shutdown() {
}
server.historyDB.Close()
server.logger.Info("server", fmt.Sprintf("%s exiting", Ver))
}
// Run starts the server.
func (server *Server) Run() {
// defer closing db/store
defer server.store.Close()
defer server.Shutdown()
for {
select {
case <-server.signals:
server.Shutdown()
case <-server.exitSignals:
return
case <-server.rehashSignal:
server.logger.Info("server", "Rehashing due to SIGHUP")
go server.rehash()
@ -528,6 +530,9 @@ func (server *Server) rehash() error {
server.rehashMutex.Lock()
defer server.rehashMutex.Unlock()
sdnotify.Reloading()
defer sdnotify.Ready()
config, err := LoadConfig(server.configFilename)
if err != nil {
server.logger.Error("server", "failed to load config file", err.Error())
@ -709,11 +714,12 @@ func (server *Server) applyConfig(config *Config) (err error) {
server.logger.Info("server", "Proxied IPs will be accepted from", strings.Join(config.Server.ProxyAllowedFrom, ", "))
}
// we are now open for business
// we are now ready to receive connections:
err = server.setupListeners(config)
// send other config warnings
if config.Accounts.RequireSasl.Enabled && config.Accounts.Registration.Enabled {
server.logger.Warning("server", "Warning: although require-sasl is enabled, users can still register accounts. If your server is not intended to be public, you must set accounts.registration.enabled to false.")
if initial && err == nil {
server.logger.Info("server", "Server running")
sdnotify.Ready()
}
if !initial {
@ -729,6 +735,11 @@ func (server *Server) applyConfig(config *Config) (err error) {
}
}
// send other config warnings
if config.Accounts.RequireSasl.Enabled && config.Accounts.Registration.Enabled {
server.logger.Warning("server", "Warning: although require-sasl is enabled, users can still register accounts. If your server is not intended to be public, you must set accounts.registration.enabled to false.")
}
return err
}

21
vendor/github.com/okzk/sdnotify/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 okzk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

15
vendor/github.com/okzk/sdnotify/README.md generated vendored Normal file
View File

@ -0,0 +1,15 @@
# sdnotify
sd_notify utility for golang.
## Installation
go get github.com/okzk/sdnotify
## Example
see [sample/main.go](sample/main.go)
## License
MIT

9
vendor/github.com/okzk/sdnotify/notify.go generated vendored Normal file
View File

@ -0,0 +1,9 @@
// +build !linux
package sdnotify
// SdNotify sends a specified string to the systemd notification socket.
func SdNotify(state string) error {
// do nothing
return nil
}

23
vendor/github.com/okzk/sdnotify/notify_linux.go generated vendored Normal file
View File

@ -0,0 +1,23 @@
package sdnotify
import (
"net"
"os"
)
// SdNotify sends a specified string to the systemd notification socket.
func SdNotify(state string) error {
name := os.Getenv("NOTIFY_SOCKET")
if name == "" {
return ErrSdNotifyNoSocket
}
conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: name, Net: "unixgram"})
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write([]byte(state))
return err
}

39
vendor/github.com/okzk/sdnotify/util.go generated vendored Normal file
View File

@ -0,0 +1,39 @@
package sdnotify
import (
"errors"
"fmt"
)
// ErrSdNotifyNoSocket is the error returned when the NOTIFY_SOCKET does not exist.
var ErrSdNotifyNoSocket = errors.New("No socket")
// Ready sends READY=1 to the systemd notify socket.
func Ready() error {
return SdNotify("READY=1")
}
// Stopping sends STOPPING=1 to the systemd notify socket.
func Stopping() error {
return SdNotify("STOPPING=1")
}
// Reloading sends RELOADING=1 to the systemd notify socket.
func Reloading() error {
return SdNotify("RELOADING=1")
}
// Errno sends ERRNO=? to the systemd notify socket.
func Errno(errno int) error {
return SdNotify(fmt.Sprintf("ERRNO=%d", errno))
}
// Status sends STATUS=? to the systemd notify socket.
func Status(status string) error {
return SdNotify("STATUS=" + status)
}
// Watchdog sends WATCHDOG=1 to the systemd notify socket.
func Watchdog() error {
return SdNotify("WATCHDOG=1")
}

3
vendor/modules.txt vendored
View File

@ -35,6 +35,9 @@ github.com/go-sql-driver/mysql
github.com/gorilla/websocket
# github.com/goshuirc/irc-go v0.0.0-20210318074529-bdc2c2cd2fef
## explicit
# github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
## explicit
github.com/okzk/sdnotify
# github.com/onsi/ginkgo v1.12.0
## explicit
# github.com/onsi/gomega v1.9.0