Skip to content

Commit

Permalink
[content-init] Propagate sensible error messages
Browse files Browse the repository at this point in the history
when the content initializer fails
  • Loading branch information
csweichel committed Jan 21, 2022
1 parent 9a7411f commit 60939d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion components/content-service/pkg/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewFromRequest(ctx context.Context, loc string, rs storage.DirectDownloader
initializer = &EmptyInitializer{}
}
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot initialize workspace: %v", err))
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return initializer, nil
}
Expand Down
4 changes: 4 additions & 0 deletions components/ws-daemon/cmd/content-initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"fmt"
"os"

"github.com/gitpod-io/gitpod/common-go/log"
Expand All @@ -18,6 +19,9 @@ func main() {

err := content.RunInitializerChild()
if err != nil {
errfd := os.NewFile(uintptr(3), "errout")
_, _ = fmt.Fprintf(errfd, err.Error())

os.Exit(42)
}
}
27 changes: 25 additions & 2 deletions components/ws-daemon/pkg/content/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/google/uuid"
"github.com/opencontainers/runc/libcontainer/specconv"
Expand Down Expand Up @@ -237,22 +239,43 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
name = "init-ws-" + opts.OWI.InstanceID
}

args = append(args, "--log-format", "json", "run", name)
args = append(args, "--log-format", "json", "run")
args = append(args, "--preserve-fds", "1")
args = append(args, name)

errIn, errOut, err := os.Pipe()
if err != nil {
return err
}
errch := make(chan []byte, 1)
go func() {
errmsg, _ := ioutil.ReadAll(errIn)
errch <- errmsg
}()

var cmdOut bytes.Buffer
cmd := exec.Command("runc", args...)
cmd.Dir = tmpdir
cmd.Stdout = &cmdOut
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.ExtraFiles = []*os.File{errOut}
err = cmd.Run()
log.FromBuffer(&cmdOut, log.WithFields(opts.OWI.Fields()))
errOut.Close()

var errmsg []byte
select {
case errmsg = <-errch:
case <-time.After(1 * time.Second):
errmsg = []byte("failed to read content initializer response")
}
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0. If it's 42, it was deliberate.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 42 {
log.WithError(err).WithField("exitCode", status.ExitStatus()).WithField("args", args).Error("content init failed")
return xerrors.Errorf("content initializer failed")
return xerrors.Errorf(string(errmsg))
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/ws-daemon/pkg/content/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ func (s *WorkspaceService) InitWorkspace(ctx context.Context, req *api.InitWorks
err = RunInitializer(ctx, workspace.Location, req.Initializer, remoteContent, opts)
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot initialize workspace")
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot initialize workspace: %s", err.Error()))
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
}

// Tell the world we're done
err = workspace.MarkInitDone(ctx)
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot initialize workspace")
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot finish workspace init: %v", err))
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot finish workspace init: %v", err))
}

return &api.InitWorkspaceResponse{}, nil
Expand Down

0 comments on commit 60939d9

Please sign in to comment.