Wrap errors to ensure a stacktrack is returned where possible
This commit is contained in:
		
							parent
							
								
									cae0090763
								
							
						
					
					
						commit
						48a303608a
					
				| 
						 | 
				
			
			@ -120,7 +120,7 @@ func (d *DockerEnvironment) IsRunning() (bool, error) {
 | 
			
		|||
func (d *DockerEnvironment) OnBeforeStart() error {
 | 
			
		||||
	c, err := d.Server.GetProcessConfiguration()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	d.Server.processConfiguration = c
 | 
			
		||||
| 
						 | 
				
			
			@ -143,7 +143,7 @@ func (d *DockerEnvironment) Start() error {
 | 
			
		|||
 | 
			
		||||
	c, err := d.Client.ContainerInspect(context.Background(), d.Server.Uuid)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// No reason to try starting a container that is already running.
 | 
			
		||||
| 
						 | 
				
			
			@ -163,7 +163,7 @@ func (d *DockerEnvironment) Start() error {
 | 
			
		|||
 | 
			
		||||
	// Run the before start function and wait for it to finish.
 | 
			
		||||
	if err := d.OnBeforeStart(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Truncate the log file so we don't end up outputting a bunch of useless log information
 | 
			
		||||
| 
						 | 
				
			
			@ -171,19 +171,19 @@ func (d *DockerEnvironment) Start() error {
 | 
			
		|||
	// to truncate them.
 | 
			
		||||
	if _, err := os.Stat(c.LogPath); err == nil {
 | 
			
		||||
		if err := os.Truncate(c.LogPath, 0); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Reset the permissions on files for the server before actually trying
 | 
			
		||||
	// to start it.
 | 
			
		||||
	if err := d.Server.Filesystem.Chown("/"); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	opts := types.ContainerStartOptions{}
 | 
			
		||||
	if err := d.Client.ContainerStart(context.Background(), d.Server.Uuid, opts); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// No errors, good to continue through.
 | 
			
		||||
| 
						 | 
				
			
			@ -216,7 +216,7 @@ func (d *DockerEnvironment) Terminate(signal os.Signal) error {
 | 
			
		|||
 | 
			
		||||
	c, err := d.Client.ContainerInspect(ctx, d.Server.Uuid)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !c.State.Running {
 | 
			
		||||
| 
						 | 
				
			
			@ -240,7 +240,7 @@ func (d *DockerEnvironment) Attach() error {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	if err := d.FollowConsoleOutput(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx := context.Background()
 | 
			
		||||
| 
						 | 
				
			
			@ -254,7 +254,7 @@ func (d *DockerEnvironment) Attach() error {
 | 
			
		|||
	})
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	console := Console{
 | 
			
		||||
| 
						 | 
				
			
			@ -283,7 +283,7 @@ func (d *DockerEnvironment) Attach() error {
 | 
			
		|||
func (d *DockerEnvironment) FollowConsoleOutput() error {
 | 
			
		||||
	if exists, err := d.Exists(); !exists {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return errors.New(fmt.Sprintf("no such container: %s", d.Server.Uuid))
 | 
			
		||||
| 
						 | 
				
			
			@ -312,7 +312,7 @@ func (d *DockerEnvironment) FollowConsoleOutput() error {
 | 
			
		|||
		}
 | 
			
		||||
	}(reader)
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
	return errors.WithStack(err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Enables resource polling on the docker instance. Except we aren't actually polling Docker for this
 | 
			
		||||
| 
						 | 
				
			
			@ -327,7 +327,7 @@ func (d *DockerEnvironment) EnableResourcePolling() error {
 | 
			
		|||
 | 
			
		||||
	stats, err := d.Client.ContainerStats(ctx, d.Server.Uuid, true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
	d.stats = stats.Body
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -383,7 +383,7 @@ func (d *DockerEnvironment) DisableResourcePolling() error {
 | 
			
		|||
	d.Server.Resources.Network.TxBytes = 0
 | 
			
		||||
	d.Server.Resources.Network.RxBytes = 0
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
	return errors.WithStack(err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Creates a new container for the server using all of the data that is currently
 | 
			
		||||
| 
						 | 
				
			
			@ -394,14 +394,14 @@ func (d *DockerEnvironment) Create() error {
 | 
			
		|||
	ctx := context.Background()
 | 
			
		||||
	cli, err := client.NewClientWithOpts(client.FromEnv)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var oomDisabled = true
 | 
			
		||||
 | 
			
		||||
	// Ensure the data directory exists before getting too far through this process.
 | 
			
		||||
	if err := d.Server.Filesystem.EnsureDataDirectory(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// If the container already exists don't hit the user with an error, just return
 | 
			
		||||
| 
						 | 
				
			
			@ -410,7 +410,7 @@ func (d *DockerEnvironment) Create() error {
 | 
			
		|||
	if _, err := cli.ContainerInspect(ctx, d.Server.Uuid); err == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	} else if !client.IsErrNotFound(err) {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conf := &container.Config{
 | 
			
		||||
| 
						 | 
				
			
			@ -501,7 +501,7 @@ func (d *DockerEnvironment) Create() error {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			@ -516,7 +516,7 @@ func (d *DockerEnvironment) SendCommand(c string) error {
 | 
			
		|||
 | 
			
		||||
	_, err := d.stream.Conn.Write([]byte(c + "\n"))
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
	return errors.WithStack(err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Reads the log file for the server. This does not care if the server is running or not, it will
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -205,21 +205,21 @@ func (fs *Filesystem) Readfile(p string) (io.Reader, error) {
 | 
			
		|||
func (fs *Filesystem) Writefile(p string, r io.Reader) error {
 | 
			
		||||
	cleaned, err := fs.SafePath(p)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// If the file does not exist on the system already go ahead and create the pathway
 | 
			
		||||
	// to it and an empty file. We'll then write to it later on after this completes.
 | 
			
		||||
	if stat, err := os.Stat(cleaned); err != nil && os.IsNotExist(err) {
 | 
			
		||||
		if err := os.MkdirAll(filepath.Dir(cleaned), 0755); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := fs.Chown(filepath.Dir(cleaned)); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	} else if stat.IsDir() {
 | 
			
		||||
		return errors.New("cannot use a directory as a file for writing")
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -228,7 +228,7 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
 | 
			
		|||
	// truncate the existing file.
 | 
			
		||||
	file, err := os.OpenFile(cleaned, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -240,7 +240,7 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
 | 
			
		|||
	for {
 | 
			
		||||
		n, err := r.Read(buf)
 | 
			
		||||
		if err != nil && err != io.EOF {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if n == 0 {
 | 
			
		||||
| 
						 | 
				
			
			@ -248,12 +248,12 @@ func (fs *Filesystem) Writefile(p string, r io.Reader) error {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		if _, err := w.Write(buf[:n]); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := w.Flush(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Finally, chown the file to ensure the permissions don't end up out-of-whack
 | 
			
		||||
| 
						 | 
				
			
			@ -324,7 +324,7 @@ func (fs *Filesystem) Stat(p string) (*Stat, error) {
 | 
			
		|||
func (fs *Filesystem) CreateDirectory(name string, p string) error {
 | 
			
		||||
	cleaned, err := fs.SafePath(path.Join(p, name))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return os.MkdirAll(cleaned, 0755)
 | 
			
		||||
| 
						 | 
				
			
			@ -334,12 +334,12 @@ func (fs *Filesystem) CreateDirectory(name string, p string) error {
 | 
			
		|||
func (fs *Filesystem) Rename(from string, to string) error {
 | 
			
		||||
	cleanedFrom, err := fs.SafePath(from)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cleanedTo, err := fs.SafePath(to)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return os.Rename(cleanedFrom, cleanedTo)
 | 
			
		||||
| 
						 | 
				
			
			@ -350,11 +350,11 @@ func (fs *Filesystem) Rename(from string, to string) error {
 | 
			
		|||
func (fs *Filesystem) Chown(path string) error {
 | 
			
		||||
	cleaned, err := fs.SafePath(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if s, err := os.Stat(cleaned); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	} else if !s.IsDir() {
 | 
			
		||||
		return os.Chown(cleaned, fs.Configuration.User.Uid, fs.Configuration.User.Gid)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -370,12 +370,12 @@ func (fs *Filesystem) chownDirectory(path string) error {
 | 
			
		|||
 | 
			
		||||
	cleaned, err := fs.SafePath(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	files, err := ioutil.ReadDir(cleaned)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, f := range files {
 | 
			
		||||
| 
						 | 
				
			
			@ -403,7 +403,7 @@ func (fs *Filesystem) chownDirectory(path string) error {
 | 
			
		|||
func (fs *Filesystem) Copy(p string) error {
 | 
			
		||||
	cleaned, err := fs.SafePath(p)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if s, err := os.Stat(cleaned); (err != nil && os.IsNotExist(err)) || s.IsDir() || !s.Mode().IsRegular() {
 | 
			
		||||
| 
						 | 
				
			
			@ -412,7 +412,7 @@ func (fs *Filesystem) Copy(p string) error {
 | 
			
		|||
		// re-evaluate if this is a smart decision (I'm guessing not).
 | 
			
		||||
		return nil
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	base := filepath.Base(cleaned)
 | 
			
		||||
| 
						 | 
				
			
			@ -438,12 +438,12 @@ func (fs *Filesystem) Copy(p string) error {
 | 
			
		|||
		tryName := fmt.Sprintf("%s%s%s", name, copySuffix, extension)
 | 
			
		||||
		tryLocation, err := fs.SafePath(path.Join(relative, tryName))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// If the file exists, continue to the next loop, otherwise we're good to start a copy.
 | 
			
		||||
		if _, err := os.Stat(tryLocation); err != nil && !os.IsNotExist(err) {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		} else if os.IsNotExist(err) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -455,23 +455,23 @@ func (fs *Filesystem) Copy(p string) error {
 | 
			
		|||
 | 
			
		||||
	finalPath, err := fs.SafePath(path.Join(relative, fmt.Sprintf("%s%s%s", name, copySuffix, extension)))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	source, err := os.Open(cleaned)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer source.Close()
 | 
			
		||||
 | 
			
		||||
	dest, err := os.Create(finalPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer dest.Close()
 | 
			
		||||
 | 
			
		||||
	if _, err := io.Copy(dest, source); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			@ -482,7 +482,7 @@ func (fs *Filesystem) Copy(p string) error {
 | 
			
		|||
func (fs *Filesystem) Delete(p string) error {
 | 
			
		||||
	cleaned, err := fs.SafePath(p)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Block any whoopsies.
 | 
			
		||||
| 
						 | 
				
			
			@ -557,12 +557,12 @@ func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {
 | 
			
		|||
// Ensures that the data directory for the server instance exists.
 | 
			
		||||
func (fs *Filesystem) EnsureDataDirectory() error {
 | 
			
		||||
	if _, err := os.Stat(fs.Path()); err != nil && !os.IsNotExist(err) {
 | 
			
		||||
		return err
 | 
			
		||||
		return errors.WithStack(err)
 | 
			
		||||
	} else if err != nil {
 | 
			
		||||
		// Create the server data directory because it does not currently exist
 | 
			
		||||
		// on the system.
 | 
			
		||||
		if err := os.MkdirAll(fs.Path(), 0600); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			return errors.WithStack(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user