2016-08-05 07:47:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-12-05 10:10:57 +01:00
|
|
|
"runtime"
|
2016-08-05 07:47:04 +02:00
|
|
|
"runtime/pprof"
|
2018-07-04 05:14:33 +02:00
|
|
|
"runtime/trace"
|
2017-05-01 21:48:26 +02:00
|
|
|
"strings"
|
2016-08-05 07:47:04 +02:00
|
|
|
"syscall"
|
2018-08-31 02:50:04 +02:00
|
|
|
"time"
|
2016-08-05 07:47:04 +02:00
|
|
|
|
2017-07-04 00:57:13 +02:00
|
|
|
"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-06-20 14:58:44 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/log"
|
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-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
|
|
|
|
2017-06-20 14:58:44 +02:00
|
|
|
log.Info("starting Prometheus server", log.Fields{"addr": cfg.PrometheusAddr})
|
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-09-03 11:44:12 +02:00
|
|
|
log.Info("starting storage", log.Fields{"name": cfg.Storage.Name})
|
2017-02-21 06:58:57 +01:00
|
|
|
ps, err = storage.NewPeerStore(cfg.Storage.Name, cfg.Storage.Config)
|
2017-05-01 21:37:16 +02:00
|
|
|
if err != nil {
|
2017-09-03 11:44:12 +02:00
|
|
|
return errors.New("failed to create storage: " + err.Error())
|
2017-05-01 21:37:16 +02:00
|
|
|
}
|
2017-09-19 21:27:52 +02:00
|
|
|
log.Info("started storage", ps)
|
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-12-23 20:54:51 +01:00
|
|
|
preHooks, err := middleware.HooksFromHookConfigs(cfg.PreHooks)
|
2017-05-07 00:48:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to validate hook config: " + err.Error())
|
|
|
|
}
|
2017-12-23 20:54:51 +01:00
|
|
|
postHooks, err := middleware.HooksFromHookConfigs(cfg.PostHooks)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to validate hook config: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("starting tracker logic", log.Fields{
|
|
|
|
"prehooks": cfg.PreHookNames(),
|
|
|
|
"posthooks": cfg.PostHookNames(),
|
2017-06-20 14:58:44 +02:00
|
|
|
})
|
2017-12-23 20:54:51 +01:00
|
|
|
r.logic = middleware.NewLogic(cfg.ResponseConfig, 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-09-19 21:27:52 +02:00
|
|
|
log.Info("starting HTTP frontend", cfg.HTTPConfig)
|
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-09-19 21:27:52 +02:00
|
|
|
log.Info("starting UDP frontend", cfg.UDPConfig)
|
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")
|
2018-09-09 17:14:24 +02:00
|
|
|
if errs := r.sg.Stop().Wait(); 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")
|
2018-09-09 17:14:24 +02:00
|
|
|
if errs := r.logic.Stop().Wait(); 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")
|
2018-09-09 17:14:24 +02:00
|
|
|
if errs := r.peerStore.Stop().Wait(); len(errs) != 0 {
|
|
|
|
return nil, combineErrors("failed while shutting down peer store", errs)
|
2017-05-01 21:37:16 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
// RootRunCmdFunc implements a Cobra command that runs an instance of Chihaya
|
|
|
|
// and handles reloading and shutdown via process signals.
|
|
|
|
func RootRunCmdFunc(cmd *cobra.Command, args []string) error {
|
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-12-05 10:10:57 +01:00
|
|
|
reload := makeReloadChan()
|
2017-04-30 04:29:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
// RootPreRunCmdFunc handles command line flags for the Run command.
|
|
|
|
func RootPreRunCmdFunc(cmd *cobra.Command, args []string) error {
|
2017-12-23 05:53:58 +01:00
|
|
|
noColors, err := cmd.Flags().GetBool("nocolors")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if noColors {
|
|
|
|
log.SetFormatter(&logrus.TextFormatter{DisableColors: true})
|
|
|
|
}
|
2017-12-05 10:10:57 +01:00
|
|
|
|
2017-12-23 05:53:58 +01:00
|
|
|
jsonLog, err := cmd.Flags().GetBool("json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if jsonLog {
|
|
|
|
log.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
log.Info("enabled JSON logging")
|
|
|
|
}
|
2017-05-07 10:38:31 +02:00
|
|
|
|
2017-12-23 05:53:58 +01:00
|
|
|
debugLog, err := cmd.Flags().GetBool("debug")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if debugLog {
|
|
|
|
log.SetDebug(true)
|
|
|
|
log.Info("enabled debug logging")
|
|
|
|
}
|
2017-05-08 00:52:17 +02:00
|
|
|
|
2018-07-04 05:14:33 +02:00
|
|
|
tracePath, err := cmd.Flags().GetString("trace")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tracePath != "" {
|
|
|
|
f, err := os.Create(tracePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
trace.Start(f)
|
|
|
|
log.Info("enabled tracing", log.Fields{"path": tracePath})
|
|
|
|
}
|
|
|
|
|
2017-12-23 05:53:58 +01:00
|
|
|
cpuProfilePath, err := cmd.Flags().GetString("cpuprofile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cpuProfilePath != "" {
|
|
|
|
f, err := os.Create(cpuProfilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
log.Info("enabled CPU profiling", log.Fields{"path": cpuProfilePath})
|
|
|
|
}
|
2017-05-08 00:52:17 +02:00
|
|
|
|
2017-12-23 05:53:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
// RootPostRunCmdFunc handles clean up of any state initialized by command line
|
2017-12-23 05:53:58 +01:00
|
|
|
// flags.
|
2018-08-31 02:50:04 +02:00
|
|
|
func RootPostRunCmdFunc(cmd *cobra.Command, args []string) error {
|
2018-07-04 05:14:33 +02:00
|
|
|
// These can be called regardless because it noops when not profiling.
|
2017-12-23 05:53:58 +01:00
|
|
|
pprof.StopCPUProfile()
|
2018-07-04 05:14:33 +02:00
|
|
|
trace.Stop()
|
2017-12-23 05:53:58 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "chihaya",
|
|
|
|
Short: "BitTorrent Tracker",
|
|
|
|
Long: "A customizable, multi-protocol BitTorrent Tracker",
|
2018-08-31 02:50:04 +02:00
|
|
|
PersistentPreRunE: RootPreRunCmdFunc,
|
|
|
|
RunE: RootRunCmdFunc,
|
|
|
|
PersistentPostRunE: RootPostRunCmdFunc,
|
2016-08-05 07:47:04 +02:00
|
|
|
}
|
2017-12-23 05:53:58 +01:00
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
rootCmd.PersistentFlags().String("cpuprofile", "", "location to save a CPU profile")
|
|
|
|
rootCmd.PersistentFlags().String("trace", "", "location to save a trace")
|
|
|
|
rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging")
|
|
|
|
rootCmd.PersistentFlags().Bool("json", false, "enable json logging")
|
2017-12-05 10:10:57 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
2018-08-31 02:50:04 +02:00
|
|
|
rootCmd.PersistentFlags().Bool("nocolors", true, "disable log coloring")
|
2017-12-05 10:10:57 +01:00
|
|
|
} else {
|
2018-08-31 02:50:04 +02:00
|
|
|
rootCmd.PersistentFlags().Bool("nocolors", false, "disable log coloring")
|
2017-12-05 10:10:57 +01:00
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
|
2018-08-31 02:50:04 +02:00
|
|
|
rootCmd.Flags().String("config", "/etc/chihaya.yaml", "location of configuration file")
|
|
|
|
|
|
|
|
var e2eCmd = &cobra.Command{
|
|
|
|
Use: "e2e",
|
|
|
|
Short: "exec e2e tests",
|
|
|
|
Long: "Execute the Chihaya end-to-end test suite",
|
|
|
|
RunE: EndToEndRunCmdFunc,
|
|
|
|
}
|
|
|
|
|
|
|
|
e2eCmd.Flags().String("httpaddr", "http://127.0.0.1:6969/announce", "address of the HTTP tracker")
|
|
|
|
e2eCmd.Flags().String("udpaddr", "udp://127.0.0.1:6969", "address of the UDP tracker")
|
|
|
|
e2eCmd.Flags().Duration("delay", time.Second, "delay between announces")
|
|
|
|
|
|
|
|
rootCmd.AddCommand(e2eCmd)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|