From 198a22f446f749ccd502280c95178fac2b26c15a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 13 Jun 2020 10:26:35 -0700 Subject: [PATCH] Replace even more zap occurences --- installer/installer.go | 21 +++++++----- parser/helpers.go | 16 +++------ parser/parser.go | 14 ++++---- router/error.go | 22 ++++++------- router/router_server.go | 21 ++++-------- router/router_server_ws.go | 4 +-- router/router_system.go | 8 ++--- router/websocket/websocket.go | 14 +++----- server/config_parser.go | 7 ++-- server/crash.go | 7 ++-- server/install.go | 62 ++++++++++++----------------------- server/listeners.go | 9 ++--- 12 files changed, 80 insertions(+), 125 deletions(-) diff --git a/installer/installer.go b/installer/installer.go index 9f7f868..b2fdf09 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -2,13 +2,13 @@ package installer import ( "encoding/json" + "github.com/apex/log" "github.com/asaskevich/govalidator" "github.com/buger/jsonparser" "github.com/pkg/errors" "github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/server" - "go.uber.org/zap" "os" "path" ) @@ -108,24 +108,27 @@ func (i *Installer) Server() *server.Server { // associated installation process based on the parameters passed through for // the server instance. func (i *Installer) Execute() { - zap.S().Debugw("creating required server data directory", zap.String("server", i.Uuid())) - if err := os.MkdirAll(path.Join(config.Get().System.Data, i.Uuid()), 0755); err != nil { - zap.S().Errorw("failed to create server data directory", zap.String("server", i.Uuid()), zap.Error(errors.WithStack(err))) + p := path.Join(config.Get().System.Data, i.Uuid()) + l := log.WithFields(log.Fields{"server": i.Uuid(), "process": "installer"}) + + l.WithField("path", p).Debug("creating required server data directory") + if err := os.MkdirAll(p, 0755); err != nil { + l.WithFields(log.Fields{"path": p, "error": errors.WithStack(err)}).Error("failed to create server data directory") return } - if err := os.Chown(path.Join(config.Get().System.Data, i.Uuid()), config.Get().System.User.Uid, config.Get().System.User.Gid); err != nil { - zap.S().Errorw("failed to chown server data directory", zap.String("server", i.Uuid()), zap.Error(errors.WithStack(err))) + if err := os.Chown(p, config.Get().System.User.Uid, config.Get().System.User.Gid); err != nil { + l.WithField("error", errors.WithStack(err)).Error("failed to chown server data directory") return } - zap.S().Debugw("creating required environment for server instance", zap.String("server", i.Uuid())) + l.Debug("creating required environment for server instance") if err := i.server.Environment.Create(); err != nil { - zap.S().Errorw("failed to create environment for server", zap.String("server", i.Uuid()), zap.Error(err)) + l.WithField("error", err).Error("failed to create environment for server") return } - zap.S().Debugw("created environment for server during install process", zap.String("server", i.Uuid())) + l.Info("successfully created environment for server during install process") } // Returns a string value from the JSON data provided. diff --git a/parser/helpers.go b/parser/helpers.go index a7c4fc8..d66d2b0 100644 --- a/parser/helpers.go +++ b/parser/helpers.go @@ -3,10 +3,10 @@ package parser import ( "bytes" "github.com/Jeffail/gabs/v2" + "github.com/apex/log" "github.com/buger/jsonparser" "github.com/iancoleman/strcase" "github.com/pkg/errors" - "go.uber.org/zap" "io/ioutil" "os" "regexp" @@ -120,11 +120,9 @@ func (cfr *ConfigurationFileReplacement) SetAtPathway(c *gabs.Container, path st // We're doing some regex here. r, err := regexp.Compile(strings.TrimPrefix(cfr.IfValue, "regex:")) if err != nil { - zap.S().Warnw( - "configuration if_value using invalid regexp, cannot do replacement", - zap.String("if_value", strings.TrimPrefix(cfr.IfValue, "regex:")), - zap.Error(err), - ) + log.WithFields(log.Fields{"if_value": strings.TrimPrefix(cfr.IfValue, "regex:"), "error": err}). + Warn("configuration if_value using invalid regexp, cannot perform replacement") + return nil } @@ -179,11 +177,7 @@ func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplac return match, errors.WithStack(err) } - zap.S().Debugw( - "attempted to load a configuration value that does not exist", - zap.Strings("path", path), - zap.String("filename", f.FileName), - ) + log.WithFields(log.Fields{"path": path, "filename": f.FileName}).Debug("attempted to load a configuration value that does not exist") // If there is no key, keep the original value intact, that way it is obvious there // is a replace issue at play. diff --git a/parser/parser.go b/parser/parser.go index 71177b7..af2aef4 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -3,13 +3,13 @@ package parser import ( "bufio" "encoding/json" + "github.com/apex/log" "github.com/beevik/etree" "github.com/buger/jsonparser" "github.com/icza/dyno" "github.com/magiconair/properties" "github.com/pkg/errors" "github.com/pterodactyl/wings/config" - "go.uber.org/zap" "gopkg.in/ini.v1" "gopkg.in/yaml.v2" "io/ioutil" @@ -30,6 +30,10 @@ const ( type ConfigurationParser string +func (cp ConfigurationParser) String() string { + return string(cp) +} + // Defines a configuration file for the server startup. These will be looped over // and modified before the server finishes booting. type ConfigurationFile struct { @@ -63,11 +67,7 @@ func (f *ConfigurationFile) UnmarshalJSON(data []byte) error { } if err := json.Unmarshal(*m["replace"], &f.Replace); err != nil { - zap.S().Warnw( - "failed to unmarshal configuration file replacement", - zap.String("file", f.FileName), - zap.Error(err), - ) + log.WithField("file", f.FileName).WithField("error", err).Warn("failed to unmarshal configuration file replacement") f.Replace = []ConfigurationFileReplacement{} } @@ -131,7 +131,7 @@ func (cfr *ConfigurationFileReplacement) UnmarshalJSON(data []byte) error { // Parses a given configuration file and updates all of the values within as defined // in the API response from the Panel. func (f *ConfigurationFile) Parse(path string, internal bool) error { - zap.S().Debugw("parsing configuration file", zap.String("path", path), zap.String("parser", string(f.Parser))) + log.WithField("path", path).WithField("parser", f.Parser.String()).Debug("parsing server configuration file") if mb, err := json.Marshal(config.Get()); err != nil { return err diff --git a/router/error.go b/router/error.go index 9097b6e..aaecb04 100644 --- a/router/error.go +++ b/router/error.go @@ -2,11 +2,11 @@ package router import ( "fmt" + "github.com/apex/log" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/pkg/errors" "github.com/pterodactyl/wings/server" - "go.uber.org/zap" "net/http" "os" ) @@ -40,6 +40,14 @@ func TrackedServerError(err error, s *server.Server) *RequestError { } } +func (e *RequestError) logger() *log.Entry { + if e.server != nil { + return e.server.Log().WithField("error_id", e.Uuid) + } + + return log.WithField("error_id", e.Uuid) +} + // Sets the output message to display to the user in the error. func (e *RequestError) SetMessage(msg string) *RequestError { e.Message = msg @@ -61,19 +69,11 @@ func (e *RequestError) AbortWithStatus(status int, c *gin.Context) { // Otherwise, log the error to zap, and then report the error back to the user. if status >= 500 { - if e.server != nil { - zap.S().Errorw("encountered error while handling HTTP request", zap.String("server", e.server.Uuid), zap.String("error_id", e.Uuid), zap.Error(e.Err)) - } else { - zap.S().Errorw("encountered error while handling HTTP request", zap.String("error_id", e.Uuid), zap.Error(e.Err)) - } + e.logger().WithField("error", e.Err).Error("encountered HTTP/500 error while handling request") c.Error(errors.WithStack(e)) } else { - if e.server != nil { - zap.S().Debugw("encountered error while handling HTTP request", zap.String("server", e.server.Uuid), zap.String("error_id", e.Uuid), zap.Error(e.Err)) - } else { - zap.S().Debugw("encountered error while handling HTTP request", zap.String("error_id", e.Uuid), zap.Error(e.Err)) - } + e.logger().WithField("error", e.Err).Debug("encountered non-HTTP/500 error while handling request") } msg := "An unexpected error was encountered while processing this request." diff --git a/router/router_server.go b/router/router_server.go index c866495..8018add 100644 --- a/router/router_server.go +++ b/router/router_server.go @@ -2,6 +2,7 @@ package router import ( "bytes" + "github.com/apex/log" "github.com/gin-gonic/gin" "github.com/pkg/errors" "github.com/pterodactyl/wings/server" @@ -74,15 +75,12 @@ func postServerPower(c *gin.Context) { // Pass the actual heavy processing off to a seperate thread to handle so that // we can immediately return a response from the server. Some of these actions // can take quite some time, especially stopping or restarting. - go func() { - if err := s.HandlePowerAction(data); err != nil { - zap.S().Errorw( - "encountered an error processing a server power action", - zap.String("server", s.Uuid), - zap.Error(err), - ) + go func(server *server.Server) { + if err := server.HandlePowerAction(data); err != nil { + server.Log().WithFields(log.Fields{"action": data, "error": err}). + Error("encountered error processing a server power action in the background") } - }() + }(s) c.Status(http.StatusAccepted) } @@ -111,12 +109,7 @@ func postServerCommands(c *gin.Context) { for _, command := range data.Commands { if err := s.Environment.SendCommand(command); err != nil { - zap.S().Warnw( - "failed to send command to server", - zap.String("server", s.Uuid), - zap.String("command", command), - zap.Error(err), - ) + s.Log().WithFields(log.Fields{"command": command, "error": err}).Warn("failed to send command to server instance") } } diff --git a/router/router_server_ws.go b/router/router_server_ws.go index 7fe476a..4f60f93 100644 --- a/router/router_server_ws.go +++ b/router/router_server_ws.go @@ -6,7 +6,6 @@ import ( "github.com/gin-gonic/gin" ws "github.com/gorilla/websocket" "github.com/pterodactyl/wings/router/websocket" - "go.uber.org/zap" ) // Upgrades a connection to a websocket and passes events along between. @@ -40,7 +39,7 @@ func getServerWebsocket(c *gin.Context) { ws.CloseServiceRestart, ws.CloseAbnormalClosure, ) { - zap.S().Warnw("error handling websocket message", zap.Error(err)) + s.Log().WithField("error", err).Warn("error handling websocket message for server") } break } @@ -57,4 +56,3 @@ func getServerWebsocket(c *gin.Context) { } } } - diff --git a/router/router_system.go b/router/router_system.go index 07553f6..f5dfed3 100644 --- a/router/router_system.go +++ b/router/router_system.go @@ -2,12 +2,12 @@ package router import ( "bytes" + "github.com/apex/log" "github.com/gin-gonic/gin" "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/installer" "github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/system" - "go.uber.org/zap" "net/http" ) @@ -59,11 +59,7 @@ func postCreateServer(c *gin.Context) { i.Execute() if err := i.Server().Install(); err != nil { - zap.S().Errorw( - "failed to run install process for server", - zap.String("server", i.Uuid()), - zap.Error(err), - ) + log.WithFields(log.Fields{"server": i.Uuid(), "error": err}).Error("failed to run install process for server") } }(install) diff --git a/router/websocket/websocket.go b/router/websocket/websocket.go index 4a329b6..674b188 100644 --- a/router/websocket/websocket.go +++ b/router/websocket/websocket.go @@ -3,6 +3,7 @@ package websocket import ( "encoding/json" "fmt" + "github.com/apex/log" "github.com/gbrlsnchs/jwt/v3" "github.com/google/uuid" "github.com/gorilla/websocket" @@ -10,7 +11,6 @@ import ( "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/router/tokens" "github.com/pterodactyl/wings/server" - "go.uber.org/zap" "net/http" "os" "strings" @@ -85,7 +85,6 @@ func (h *Handler) SendJson(v *Message) error { // If we're sending installation output but the user does not have the required // permissions to see the output, don't send it down the line. if v.Event == server.InstallOutputEvent { - zap.S().Debugf("%+v", v.Args) if !j.HasPermission(PermissionReceiveInstall) { return nil } @@ -152,13 +151,8 @@ func (h *Handler) SendErrorJson(msg Message, err error) error { wsm.Args = []string{m} if !server.IsSuspendedError(err) { - zap.S().Errorw( - "an error was encountered in the websocket process", - zap.String("event", msg.Event), - zap.String("server", h.server.Uuid), - zap.String("error_identifier", u.String()), - zap.Error(err), - ) + h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}). + Error("failed to handle websocket process; an error was encountered processing an event") } return h.unsafeSendJson(wsm) @@ -192,7 +186,7 @@ func (h *Handler) GetJwt() *tokens.WebsocketPayload { func (h *Handler) HandleInbound(m Message) error { if m.Event != AuthenticationEvent { if err := h.TokenValid(); err != nil { - zap.S().Debugw("jwt token is no longer valid", zap.String("message", err.Error())) + log.WithField("message", err.Error()).Debug("jwt for server websocket is no longer valid") h.unsafeSendJson(Message{ Event: ErrorEvent, diff --git a/server/config_parser.go b/server/config_parser.go index 76decf2..a0f1f11 100644 --- a/server/config_parser.go +++ b/server/config_parser.go @@ -2,7 +2,6 @@ package server import ( "github.com/pterodactyl/wings/parser" - "go.uber.org/zap" "sync" ) @@ -17,15 +16,15 @@ func (s *Server) UpdateConfigurationFiles() { go func(f parser.ConfigurationFile, server *Server) { defer wg.Done() - p, err := s.Filesystem.SafePath(f.FileName) + p, err := server.Filesystem.SafePath(f.FileName) if err != nil { - zap.S().Errorw("failed to generate safe path for configuration file", zap.String("server", server.Uuid), zap.Error(err)) + server.Log().WithField("error", err).Error("failed to generate safe path for configuration file") return } if err := f.Parse(p, false); err != nil { - zap.S().Errorw("failed to parse and update server configuration file", zap.String("server", server.Uuid), zap.Error(err)) + server.Log().WithField("error", err).Error("failed to parse and update server configuration file") } }(v, s) } diff --git a/server/crash.go b/server/crash.go index 0f08079..649580f 100644 --- a/server/crash.go +++ b/server/crash.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/pkg/errors" "github.com/pterodactyl/wings/config" - "go.uber.org/zap" "time" ) @@ -27,15 +26,13 @@ type CrashDetection struct { // // If the server is determined to have crashed, the process will be restarted and the // counter for the server will be incremented. -// -// @todo output event to server console func (s *Server) handleServerCrash() error { // No point in doing anything here if the server isn't currently offline, there // is no reason to do a crash detection event. If the server crash detection is // disabled we want to skip anything after this as well. if s.GetState() != ProcessOfflineState || !s.CrashDetection.Enabled { if !s.CrashDetection.Enabled { - zap.S().Debugw("server triggered crash detection but handler is disabled for server process", zap.String("server", s.Uuid)) + s.Log().Debug("server triggered crash detection but handler is disabled for server process") s.PublishConsoleOutputFromDaemon("Server detected as crashed; crash detection is disabled for this instance.") } @@ -51,7 +48,7 @@ func (s *Server) handleServerCrash() error { // If the system is not configured to detect a clean exit code as a crash, and the // crash is not the result of the program running out of memory, do nothing. if exitCode == 0 && !oomKilled && !config.Get().System.DetectCleanExitAsCrash { - zap.S().Debugw("server exited with successful code; system configured to not detect as crash", zap.String("server", s.Uuid)) + s.Log().Debug("server exited with successful exit code; system is configured to not detect this as a crash") return nil } diff --git a/server/install.go b/server/install.go index 324ced3..efa6c92 100644 --- a/server/install.go +++ b/server/install.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "github.com/apex/log" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" @@ -11,7 +12,6 @@ import ( "github.com/pkg/errors" "github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/config" - "go.uber.org/zap" "io" "io/ioutil" "os" @@ -25,14 +25,12 @@ import ( func (s *Server) Install() error { err := s.internalInstall() - zap.S().Debugw("notifying panel of server install state", zap.String("server", s.Uuid)) + s.Log().Debug("notifying panel of server install state") if serr := s.SyncInstallState(err == nil); serr != nil { - zap.S().Warnw( - "failed to notify panel of server install state", - zap.String("server", s.Uuid), - zap.Bool("was_successful", err == nil), - zap.Error(serr), - ) + s.Log().WithFields(log.Fields{ + "was_successful": err == nil, + "error": serr, + }).Warn("failed to notify panel of server install state") } return err @@ -42,7 +40,7 @@ func (s *Server) Install() error { // does not touch any existing files for the server, other than what the script modifies. func (s *Server) Reinstall() error { if s.GetState() != ProcessOfflineState { - zap.S().Debugw("waiting for server instance to enter a stopped state", zap.String("server", s.Uuid)) + s.Log().Debug("waiting for server instance to enter a stopped state") if err := s.Environment.WaitForStop(10, true); err != nil { return err } @@ -67,14 +65,12 @@ func (s *Server) internalInstall() error { return errors.WithStack(err) } - zap.S().Infow("beginning installation process for server", zap.String("server", s.Uuid)) - + s.Log().Info("beginning installation process for server") if err := p.Run(); err != nil { return err } - zap.S().Infow("completed installation process for server", zap.String("server", s.Uuid)) - + s.Log().Info("completed installation process for server") return nil } @@ -106,13 +102,13 @@ func NewInstallationProcess(s *Server, script *api.InstallationScript) (*Install // Removes the installer container for the server. func (ip *InstallationProcess) RemoveContainer() { - err := ip.client.ContainerRemove(context.Background(), ip.Server.Uuid + "_installer", types.ContainerRemoveOptions{ + err := ip.client.ContainerRemove(context.Background(), ip.Server.Uuid+"_installer", types.ContainerRemoveOptions{ RemoveVolumes: true, Force: true, }) if err != nil && !client.IsErrNotFound(err) { - zap.S().Warnw("failed to delete server installer container", zap.String("server", ip.Server.Uuid), zap.Error(errors.WithStack(err))) + ip.Server.Log().WithField("error", errors.WithStack(err)).Warn("failed to delete server install container") } } @@ -137,7 +133,7 @@ func (ip *InstallationProcess) Run() error { // If this step fails, log a warning but don't exit out of the process. This is completely // internal to the daemon's functionality, and does not affect the status of the server itself. if err := ip.AfterExecute(cid); err != nil { - zap.S().Warnw("failed to complete after-execute step of installation process", zap.String("server", ip.Server.Uuid), zap.Error(err)) + ip.Server.Log().WithField("error", err).Warn("failed to complete after-execute step of installation process") } return nil @@ -189,7 +185,7 @@ func (ip *InstallationProcess) pullInstallationImage() error { // Block continuation until the image has been pulled successfully. scanner := bufio.NewScanner(r) for scanner.Scan() { - zap.S().Debugw(scanner.Text()) + log.Debug(scanner.Text()) } if err := scanner.Err(); err != nil { @@ -265,7 +261,7 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error { ctx := context.Background() defer ip.RemoveContainer() - zap.S().Debugw("pulling installation logs for server", zap.String("server", ip.Server.Uuid), zap.String("container_id", containerId)) + ip.Server.Log().WithField("container_id", containerId).Debug("pulling installation logs for server") reader, err := ip.client.ContainerLogs(ctx, containerId, types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, @@ -295,12 +291,6 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error { func (ip *InstallationProcess) Execute(installPath string) (string, error) { ctx := context.Background() - zap.S().Debugw( - "creating server installer container", - zap.String("server", ip.Server.Uuid), - zap.String("script_path", installPath+"/install.sh"), - ) - conf := &container.Config{ Hostname: "installer", AttachStdout: true, @@ -348,17 +338,13 @@ func (ip *InstallationProcess) Execute(installPath string) (string, error) { NetworkMode: container.NetworkMode(config.Get().Docker.Network.Mode), } - zap.S().Infow("creating installer container for server process", zap.String("server", ip.Server.Uuid)) + ip.Server.Log().WithField("install_script", installPath+"/install.sh").Info("creating install container for server process") r, err := ip.client.ContainerCreate(ctx, conf, hostConf, nil, ip.Server.Uuid+"_installer") if err != nil { return "", errors.WithStack(err) } - zap.S().Infow( - "running installation script for server in container", - zap.String("server", ip.Server.Uuid), - zap.String("container_id", r.ID), - ) + ip.Server.Log().WithField("container_id", r.ID).Info("running installation script for server in container") if err := ip.client.ContainerStart(ctx, r.ID, types.ContainerStartOptions{}); err != nil { return "", err } @@ -366,11 +352,7 @@ func (ip *InstallationProcess) Execute(installPath string) (string, error) { go func(id string) { ip.Server.Events().Publish(DaemonMessageEvent, "Starting installation process, this could take a few minutes...") if err := ip.StreamOutput(id); err != nil { - zap.S().Errorw( - "error handling streaming output for server install process", - zap.String("container_id", id), - zap.Error(err), - ) + ip.Server.Log().WithField("error", err).Error("error while handling output stream for server install process") } ip.Server.Events().Publish(DaemonMessageEvent, "Installation process completed.") }(r.ID) @@ -409,12 +391,10 @@ func (ip *InstallationProcess) StreamOutput(id string) error { } if err := s.Err(); err != nil { - zap.S().Warnw( - "error processing scanner line in installation output for server", - zap.String("server", ip.Server.Uuid), - zap.String("container_id", id), - zap.Error(errors.WithStack(err)), - ) + ip.Server.Log().WithFields(log.Fields{ + "container_id": id, + "error": errors.WithStack(err), + }).Warn("error processing scanner line in installation output for server") } return nil diff --git a/server/listeners.go b/server/listeners.go index c164c99..73df884 100644 --- a/server/listeners.go +++ b/server/listeners.go @@ -1,8 +1,8 @@ package server import ( + "github.com/apex/log" "github.com/pterodactyl/wings/api" - "go.uber.org/zap" "strings" ) @@ -28,9 +28,10 @@ func (s *Server) onConsoleOutput(data string) { // set the server to that state. Only do this if the server is not currently stopped // or stopping. if s.GetState() == ProcessStartingState && strings.Contains(data, s.processConfiguration.Startup.Done) { - zap.S().Debugw( - "detected server in running state based on line output", zap.String("match", s.processConfiguration.Startup.Done), zap.String("against", data), - ) + s.Log().WithFields(log.Fields{ + "match": s.processConfiguration.Startup.Done, + "against": data, + }).Debug("detected server in running state based on console line output") s.SetState(ProcessRunningState) }