2016-08-05 07:47:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime/pprof"
|
2017-05-01 21:48:26 +02:00
|
|
|
"strings"
|
2016-08-05 07:47:04 +02:00
|
|
|
"syscall"
|
|
|
|
|
2016-09-05 18:10:42 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-08-05 07:47:04 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
"github.com/chihaya/chihaya/frontend/http"
|
|
|
|
"github.com/chihaya/chihaya/frontend/udp"
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/middleware"
|
2017-04-30 04:29:27 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/prometheus"
|
|
|
|
"github.com/chihaya/chihaya/pkg/stop"
|
2016-11-27 10:57:32 +01:00
|
|
|
"github.com/chihaya/chihaya/storage"
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/storage/memory"
|
2016-08-05 07:47:04 +02:00
|
|
|
)
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Run represents the state of a running instance of Chihaya.
|
|
|
|
type Run struct {
|
|
|
|
configFilePath string
|
|
|
|
peerStore storage.PeerStore
|
|
|
|
logic *middleware.Logic
|
|
|
|
sg *stop.Group
|
|
|
|
}
|
2016-08-05 09:35:17 +02:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// NewRun runs an instance of Chihaya.
|
|
|
|
func NewRun(configFilePath string) (*Run, error) {
|
|
|
|
r := &Run{
|
|
|
|
configFilePath: configFilePath,
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-05-01 21:37:16 +02:00
|
|
|
return r, r.Start(nil)
|
2017-04-30 04:29:27 +02:00
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Start begins an instance of Chihaya.
|
2017-05-01 21:37:16 +02:00
|
|
|
// It is optional to provide an instance of the peer store to avoid the
|
|
|
|
// creation of a new one.
|
|
|
|
func (r *Run) Start(ps storage.PeerStore) error {
|
2017-04-30 04:29:27 +02:00
|
|
|
configFile, err := ParseConfigFile(r.configFilePath)
|
2016-08-05 09:35:17 +02:00
|
|
|
if err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
return errors.New("failed to read config: " + err.Error())
|
2016-08-05 09:35:17 +02:00
|
|
|
}
|
2017-05-02 17:03:49 +02:00
|
|
|
cfg := configFile.Chihaya
|
2016-09-01 03:09:46 +02:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
r.sg = stop.NewGroup()
|
2017-05-07 00:48:44 +02:00
|
|
|
|
|
|
|
log.WithFields(log.Fields{"addr": cfg.PrometheusAddr}).Info("starting Prometheus server")
|
2017-05-02 17:03:49 +02:00
|
|
|
r.sg.Add(prometheus.NewServer(cfg.PrometheusAddr))
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-05-01 21:37:16 +02:00
|
|
|
if ps == nil {
|
2017-05-07 00:48:44 +02:00
|
|
|
log.WithFields(cfg.Storage.LogFields()).Info("starting storage")
|
2017-05-02 17:03:49 +02:00
|
|
|
ps, err = memory.New(cfg.Storage)
|
2017-05-01 21:37:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to create memory storage: " + err.Error())
|
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
}
|
2017-05-01 21:37:16 +02:00
|
|
|
r.peerStore = ps
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-05-07 00:48:44 +02:00
|
|
|
preHooks, postHooks, err := cfg.CreateHooks()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to validate hook config: " + err.Error())
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"preHooks": preHooks,
|
|
|
|
"postHooks": postHooks,
|
|
|
|
}).Info("starting middleware")
|
2017-05-02 17:03:49 +02:00
|
|
|
r.logic = middleware.NewLogic(cfg.Config, r.peerStore, preHooks, postHooks)
|
2016-08-05 09:35:17 +02:00
|
|
|
|
2017-05-02 17:03:49 +02:00
|
|
|
if cfg.HTTPConfig.Addr != "" {
|
2017-05-07 00:48:44 +02:00
|
|
|
log.WithFields(cfg.HTTPConfig.LogFields()).Info("starting HTTP frontend")
|
2017-05-02 17:03:49 +02:00
|
|
|
httpfe, err := http.NewFrontend(r.logic, cfg.HTTPConfig)
|
2017-04-30 04:29:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.sg.Add(httpfe)
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-05-02 17:03:49 +02:00
|
|
|
if cfg.UDPConfig.Addr != "" {
|
2017-05-07 00:48:44 +02:00
|
|
|
log.WithFields(cfg.UDPConfig.LogFields()).Info("starting UDP frontend")
|
2017-05-02 17:03:49 +02:00
|
|
|
udpfe, err := udp.NewFrontend(r.logic, cfg.UDPConfig)
|
2017-04-30 04:29:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.sg.Add(udpfe)
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-05-01 21:48:26 +02:00
|
|
|
func combineErrors(prefix string, errs []error) error {
|
|
|
|
var errStrs []string
|
|
|
|
for _, err := range errs {
|
|
|
|
errStrs = append(errStrs, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(prefix + ": " + strings.Join(errStrs, "; "))
|
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Stop shuts down an instance of Chihaya.
|
2017-05-01 21:37:16 +02:00
|
|
|
func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) {
|
2017-04-30 04:29:27 +02:00
|
|
|
log.Debug("stopping frontends and prometheus endpoint")
|
|
|
|
if errs := r.sg.Stop(); len(errs) != 0 {
|
2017-05-01 21:48:26 +02:00
|
|
|
return nil, combineErrors("failed while shutting down frontends", errs)
|
2017-04-30 04:29:27 +02:00
|
|
|
}
|
2016-09-05 18:10:42 +02:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
log.Debug("stopping logic")
|
|
|
|
if errs := r.logic.Stop(); len(errs) != 0 {
|
2017-05-01 21:48:26 +02:00
|
|
|
return nil, combineErrors("failed while shutting down middleware", errs)
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 21:37:16 +02:00
|
|
|
if !keepPeerStore {
|
|
|
|
log.Debug("stopping peer store")
|
|
|
|
if err, closed := <-r.peerStore.Stop(); !closed {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.peerStore = nil
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 21:37:16 +02:00
|
|
|
return r.peerStore, nil
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// RunCmdFunc implements a Cobra command that runs an instance of Chihaya and
|
|
|
|
// handles reloading and shutdown via process signals.
|
|
|
|
func RunCmdFunc(cmd *cobra.Command, args []string) error {
|
|
|
|
cpuProfilePath, _ := cmd.Flags().GetString("cpuprofile")
|
|
|
|
if cpuProfilePath != "" {
|
2017-05-07 00:48:44 +02:00
|
|
|
log.WithFields(log.Fields{"path": cpuProfilePath}).Info("enabling CPU profiling")
|
2017-04-30 04:29:27 +02:00
|
|
|
f, err := os.Create(cpuProfilePath)
|
2016-11-27 10:57:32 +01:00
|
|
|
if err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
return err
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
configFilePath, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-27 10:57:32 +01:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
r, err := NewRun(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
quit := make(chan os.Signal)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
2016-11-27 10:57:32 +01:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
reload := make(chan os.Signal)
|
|
|
|
signal.Notify(reload, syscall.SIGUSR1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-reload:
|
2017-05-07 00:48:44 +02:00
|
|
|
log.Info("reloading; received SIGUSR1")
|
2017-05-01 21:37:16 +02:00
|
|
|
peerStore, err := r.Stop(true)
|
|
|
|
if err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-05-01 21:37:16 +02:00
|
|
|
|
|
|
|
if err := r.Start(peerStore); err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-quit:
|
2017-05-07 00:48:44 +02:00
|
|
|
log.Info("shutting down; received SIGINT/SIGTERM")
|
2017-05-01 21:37:16 +02:00
|
|
|
if _, err := r.Stop(false); err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
return err
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
func main() {
|
|
|
|
var rootCmd = &cobra.Command{
|
2016-08-17 03:42:08 +02:00
|
|
|
Use: "chihaya",
|
2016-08-05 07:47:04 +02:00
|
|
|
Short: "BitTorrent Tracker",
|
2017-01-23 01:06:13 +01:00
|
|
|
Long: "A customizable, multi-protocol BitTorrent Tracker",
|
2017-04-29 19:46:01 +02:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2017-05-07 10:38:31 +02:00
|
|
|
jsonLog, _ := cmd.Flags().GetBool("json")
|
|
|
|
if jsonLog {
|
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:46:01 +02:00
|
|
|
debugLog, _ := cmd.Flags().GetBool("debug")
|
|
|
|
if debugLog {
|
2017-05-07 00:48:44 +02:00
|
|
|
log.Info("enabling debug logging")
|
2017-04-29 19:46:01 +02:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
},
|
2017-04-30 04:29:27 +02:00
|
|
|
RunE: RunCmdFunc,
|
2016-08-05 07:47:04 +02:00
|
|
|
}
|
2016-08-24 23:21:06 +02:00
|
|
|
rootCmd.Flags().String("config", "/etc/chihaya.yaml", "location of configuration file")
|
|
|
|
rootCmd.Flags().String("cpuprofile", "", "location to save a CPU profile")
|
2016-09-05 18:19:54 +02:00
|
|
|
rootCmd.Flags().Bool("debug", false, "enable debug logging")
|
2017-05-07 10:38:31 +02:00
|
|
|
rootCmd.Flags().Bool("json", false, "enable json logging")
|
2016-08-05 07:47:04 +02:00
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2017-04-30 04:29:27 +02:00
|
|
|
log.Fatal("failed when executing root cobra command: " + err.Error())
|
2016-08-05 07:47:04 +02:00
|
|
|
}
|
|
|
|
}
|