Skip to content

Commit

Permalink
fix(worker): merge worker and context PATH (#7207)
Browse files Browse the repository at this point in the history
* fix(worker): merge worker and context PATH

Signed-off-by: richardlt <richard.le.terrier@gmail.com>

* fix(worker): clean path usage

---------

Signed-off-by: richardlt <richard.le.terrier@gmail.com>
Co-authored-by: Steven Guiheux <steven.guiheux@corp.ovh.com>
  • Loading branch information
richardlt and sguiheux authored Nov 6, 2024
1 parent 92251b7 commit 37a1cb5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
16 changes: 0 additions & 16 deletions contrib/grpcplugins/grpcplugins_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"runtime"
"strings"

"github.com/bugsnag/osext"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/actionplugin"
"github.com/pkg/errors"
Expand Down Expand Up @@ -53,21 +52,6 @@ func RunScript(ctx context.Context, actPlug *actionplugin.Common, chanRes chan *
cmd.Stderr = pw
cmd.Env = os.Environ()

workerpath, err := osext.Executable()
if err != nil {
gores.Status = sdk.StatusFail
gores.Details = fmt.Sprintf("failure due to internal error (Worker Path): %v", err)
chanRes <- gores
return err
}

for i := range cmd.Env {
if strings.HasPrefix(cmd.Env[i], "PATH") {
cmd.Env[i] = fmt.Sprintf("%s:%s", cmd.Env[i], path.Dir(workerpath))
break
}
}

reader := bufio.NewReader(pr)

outchan := make(chan bool)
Expand Down
38 changes: 33 additions & 5 deletions engine/worker/internal/plugin/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/kardianos/osext"
Expand Down Expand Up @@ -113,15 +114,42 @@ func createGRPCPluginSocket(ctx context.Context, pluginType string, pluginName s
dir = workdir.Name()
}

envs := w.Environ()
// Retrieve worker environment variables
workerEnvs := w.Environ()
mWorkerEnvs := make(map[string]string, len(workerEnvs))
for _, e := range workerEnvs {
splitted := strings.SplitN(e, "=", 2)
if len(splitted) != 2 {
continue
}
mWorkerEnvs[splitted[0]] = splitted[1]
}

// Add jobv2 env variable
if env != nil {
for k, v := range env {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
// Add env variable from execution context
for k, v := range env {
// Set all env ( do not ovveride existing var )
if _, ok := mWorkerEnvs[k]; !ok && k != "PATH" {
mWorkerEnvs[k] = v
continue
}
}

// Manage PATH
if v, has := env["PATH"]; has {
existingPath := mWorkerEnvs["PATH"]
existingPathList := filepath.SplitList(existingPath)
newPath := v
newPathList := filepath.SplitList(newPath)
newPathList = append(newPathList, existingPathList...)
newPathList = sdk.Unique(newPathList)
mWorkerEnvs["PATH"] = strings.Join(newPathList, string(filepath.ListSeparator))
}

var envs []string
for k, v := range mWorkerEnvs {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}

c := clientSocket{}

workerpath, err := osext.Executable()
Expand Down
18 changes: 5 additions & 13 deletions engine/worker/internal/runV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"strings"
"time"

"github.com/bugsnag/osext"
"github.com/pkg/errors"
"github.com/rockbears/log"
"github.com/spf13/afero"
Expand Down Expand Up @@ -928,26 +927,19 @@ func (w *CurrentWorker) GetEnvVariable(ctx context.Context, contexts sdk.Workflo
}
}

// Add in path, the current directory of the worker to be able to use worker command
workerpath, err := osext.Executable()
if err != nil {
return nil, sdk.NewErrorFrom(sdk.ErrWrongRequest, "unable to get current executable path")
}

pathList := sdk.StringSlice{}
pathListSplitted := filepath.SplitList(os.Getenv("PATH"))
pathList = append(pathList, pathListSplitted...)
pathList = append(pathList, path.Dir(workerpath))

// Retrieve path outputs from previous steps
currentStepsStatus := w.GetCurrentStepsStatus()
for _, ss := range currentStepsStatus {
pathList = append(pathList, ss.PathOutputs...)
}

// Remove duplicate paths.
pathList.Unique()
// Inject supercharged PATH environ variable.
newEnvVar["PATH"] = strings.Join(pathList, string(filepath.ListSeparator))
if len(pathList) > 0 {
newEnvVar["PATH"] = strings.Join(pathList, string(filepath.ListSeparator))
}

return newEnvVar, nil
}

Expand Down

0 comments on commit 37a1cb5

Please sign in to comment.