Skip to content

Commit

Permalink
run git command as gitpod user when using pvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sagor999 committed Oct 21, 2022
1 parent 91dea2e commit 773f5bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion components/content-service/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type Client struct {

// UpstreamCloneURI is the fork upstream of a repository
UpstreamRemoteURI string

// if true will run git command as gitpod user (should be executed as root that has access to sudo in this case)
RunAsGitpodUser bool
}

// Status describes the status of a Git repo/working copy akin to "git status"
Expand Down Expand Up @@ -197,7 +200,12 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman

span.LogKV("args", fullArgs)

cmd := exec.Command("git", fullArgs...)
cmdName := "git"
if c.RunAsGitpodUser {
cmdName = "sudo"
fullArgs = append([]string{"-u", "gitpod", "git"}, fullArgs...)
}
cmd := exec.Command(cmdName, fullArgs...)
cmd.Dir = c.Location
cmd.Env = env

Expand Down
17 changes: 17 additions & 0 deletions components/content-service/pkg/initializer/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ func (ws *GitInitializer) Run(ctx context.Context, mappings []archive.IDMapping)
return err
}

// make sure that folder itself is owned by gitpod user prior to doing git clone
// this is needed as otherwise git clone will fail if the folder is owned by root
if ws.RunAsGitpodUser {
args := []string{"gitpod", ws.Location}
cmd := exec.Command("chown", args...)
res, cerr := cmd.CombinedOutput()
if cerr != nil && !process.IsNotChildProcess(cerr) {
err = git.OpFailedError{
Args: args,
ExecErr: cerr,
Output: string(res),
Subcommand: "chown",
}
return err
}
}

log.WithField("stage", "init").WithField("location", ws.Location).Debug("Running git clone on workspace")
err = ws.Clone(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion components/content-service/pkg/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ func newGitInitializer(ctx context.Context, loc string, req *csapi.GitInitialize
Config: req.Config.CustomConfig,
AuthMethod: authMethod,
AuthProvider: authProvider,
RunAsGitpodUser: forceGitpodUser,
},
TargetMode: targetMode,
CloneTarget: req.CloneTaget,
Chown: forceGitpodUser,
Chown: false,
}, nil
}

Expand Down

0 comments on commit 773f5bd

Please sign in to comment.