Replace error handling package with emperror; add better reporting for errors escaping server root

This commit is contained in:
Dane Everitt
2020-11-08 13:52:20 -08:00
parent 0989c78d4b
commit be9d1a3986
55 changed files with 396 additions and 367 deletions

View File

@@ -4,12 +4,12 @@ import (
"bufio"
"bytes"
"context"
"emperror.dev/errors"
"github.com/apex/log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/api"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment"
@@ -90,7 +90,7 @@ func (s *Server) internalInstall() error {
script, err := api.New().GetInstallationScript(s.Id())
if err != nil {
if !api.IsRequestError(err) {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
return errors.New(err.Error())
@@ -98,7 +98,7 @@ func (s *Server) internalInstall() error {
p, err := NewInstallationProcess(s, &script)
if err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
s.Log().Info("beginning installation process for server")
@@ -130,7 +130,7 @@ func NewInstallationProcess(s *Server, script *api.InstallationScript) (*Install
s.installer.cancel = &cancel
if c, err := environment.DockerClient(); err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStackIf(err)
} else {
proc.client = c
proc.context = ctx
@@ -193,7 +193,7 @@ func (ip *InstallationProcess) RemoveContainer() {
})
if err != nil && !client.IsErrNotFound(err) {
ip.Server.Log().WithField("error", errors.WithStack(err)).Warn("failed to delete server install container")
ip.Server.Log().WithField("error", errors.WithStackIf(err)).Warn("failed to delete server install container")
}
}
@@ -218,14 +218,14 @@ func (ip *InstallationProcess) Run() error {
}()
if err := ip.BeforeExecute(); err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
cid, err := ip.Execute()
if err != nil {
ip.RemoveContainer()
return errors.WithStack(err)
return errors.WithStackIf(err)
}
// If this step fails, log a warning but don't exit out of the process. This is completely
@@ -248,12 +248,12 @@ func (ip *InstallationProcess) writeScriptToDisk() error {
// Make sure the temp directory root exists before trying to make a directory within it. The
// ioutil.TempDir call expects this base to exist, it won't create it for you.
if err := os.MkdirAll(ip.tempDir(), 0700); err != nil {
return errors.Wrap(err, "could not create temporary directory for install process")
return errors.WrapIf(err, "could not create temporary directory for install process")
}
f, err := os.OpenFile(filepath.Join(ip.tempDir(), "install.sh"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return errors.Wrap(err, "failed to write server installation script to disk before mount")
return errors.WrapIf(err, "failed to write server installation script to disk before mount")
}
defer f.Close()
@@ -265,7 +265,7 @@ func (ip *InstallationProcess) writeScriptToDisk() error {
}
if err := scanner.Err(); err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
w.Flush()
@@ -277,7 +277,7 @@ func (ip *InstallationProcess) writeScriptToDisk() error {
func (ip *InstallationProcess) pullInstallationImage() error {
r, err := ip.client.ImagePull(ip.context, ip.Script.ContainerImage, types.ImagePullOptions{})
if err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
// Block continuation until the image has been pulled successfully.
@@ -287,7 +287,7 @@ func (ip *InstallationProcess) pullInstallationImage() error {
}
if err := scanner.Err(); err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
return nil
@@ -298,11 +298,11 @@ func (ip *InstallationProcess) pullInstallationImage() error {
// manner, if either one fails the error is returned.
func (ip *InstallationProcess) BeforeExecute() error {
if err := ip.writeScriptToDisk(); err != nil {
return errors.Wrap(err, "failed to write installation script to disk")
return errors.WrapIf(err, "failed to write installation script to disk")
}
if err := ip.pullInstallationImage(); err != nil {
return errors.Wrap(err, "failed to pull updated installation container image for server")
return errors.WrapIf(err, "failed to pull updated installation container image for server")
}
opts := types.ContainerRemoveOptions{
@@ -312,7 +312,7 @@ func (ip *InstallationProcess) BeforeExecute() error {
if err := ip.client.ContainerRemove(ip.context, ip.Server.Id()+"_installer", opts); err != nil {
if !client.IsErrNotFound(err) {
return errors.Wrap(err, "failed to remove existing install container for server")
return errors.WrapIf(err, "failed to remove existing install container for server")
}
}
@@ -338,12 +338,12 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error {
})
if err != nil && !client.IsErrNotFound(err) {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
f, err := os.OpenFile(ip.GetLogPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
defer f.Close()
@@ -372,15 +372,15 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error {
| ------------------------------
`)
if err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
if err := tmpl.Execute(f, ip); err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
if _, err := io.Copy(f, reader); err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
return nil
@@ -448,7 +448,7 @@ func (ip *InstallationProcess) Execute() (string, error) {
r, err := ip.client.ContainerCreate(ip.context, conf, hostConf, nil, ip.Server.Id()+"_installer")
if err != nil {
return "", errors.WithStack(err)
return "", errors.WithStackIf(err)
}
ip.Server.Log().WithField("container_id", r.ID).Info("running installation script for server in container")
@@ -468,7 +468,7 @@ func (ip *InstallationProcess) Execute() (string, error) {
select {
case err := <-eChan:
if err != nil {
return "", errors.WithStack(err)
return "", errors.WithStackIf(err)
}
case <-sChan:
}
@@ -487,7 +487,7 @@ func (ip *InstallationProcess) StreamOutput(id string) error {
})
if err != nil {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
defer reader.Close()
@@ -500,7 +500,7 @@ func (ip *InstallationProcess) StreamOutput(id string) error {
if err := s.Err(); err != nil {
ip.Server.Log().WithFields(log.Fields{
"container_id": id,
"error": errors.WithStack(err),
"error": errors.WithStackIf(err),
}).Warn("error processing scanner line in installation output for server")
}
@@ -515,7 +515,7 @@ func (s *Server) SyncInstallState(successful bool) error {
err := api.New().SendInstallationStatus(s.Id(), successful)
if err != nil {
if !api.IsRequestError(err) {
return errors.WithStack(err)
return errors.WithStackIf(err)
}
return errors.New(err.Error())