Skip to content

Commit

Permalink
fix(worker): artifact upload outside of workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and richardlt committed Jan 17, 2020
1 parent e747fa2 commit d1e0b8b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
6 changes: 2 additions & 4 deletions engine/worker/cmd_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"os"
"strconv"

"github.com/ovh/cds/engine/worker/pkg/workerruntime"

"github.com/ovh/cds/engine/worker/internal"

"github.com/spf13/cobra"

"github.com/ovh/cds/engine/worker/internal"
"github.com/ovh/cds/engine/worker/pkg/workerruntime"
"github.com/ovh/cds/sdk"
)

Expand Down
9 changes: 6 additions & 3 deletions engine/worker/cmd_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"

"github.com/ovh/cds/engine/worker/internal"
"github.com/ovh/cds/engine/worker/pkg/workerruntime"
"github.com/ovh/cds/sdk"
)

Expand Down Expand Up @@ -60,10 +61,12 @@ func uploadCmd() func(cmd *cobra.Command, args []string) {
sdk.Exit("Wrong usage: Example : worker upload --tag={{.cds.version}} filea fileb filec*")
}

cwd, _ := os.Getwd()
for _, arg := range args {
a := sdk.WorkflowNodeRunArtifact{
Name: arg,
Tag: cmdUploadTag,
a := workerruntime.UploadArtifact{
Name: arg,
Tag: cmdUploadTag,
WorkingDirectory: cwd,
}

data, errMarshal := json.Marshal(a)
Expand Down
26 changes: 14 additions & 12 deletions engine/worker/internal/action/builtin_artifact_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"path"
"path/filepath"
"strings"
"sync"
"time"
Expand All @@ -23,9 +25,9 @@ func RunArtifactUpload(ctx context.Context, wk workerruntime.Runtime, a sdk.Acti
return res, err
}

path := strings.TrimSpace(sdk.ParameterValue(a.Parameters, "path"))
if path == "" {
path = "."
artifactPath := strings.TrimSpace(sdk.ParameterValue(a.Parameters, "path"))
if artifactPath == "" {
artifactPath = "."
}

workdir, err := workerruntime.WorkingDirectory(ctx)
Expand All @@ -40,22 +42,23 @@ func RunArtifactUpload(ctx context.Context, wk workerruntime.Runtime, a sdk.Acti
abs = workdir.Name()
}

wkDirFS := afero.NewBasePathFs(afero.NewOsFs(), abs)
if !path.IsAbs(artifactPath) {
artifactPath = filepath.Join(abs, artifactPath)
}

tag := sdk.ParameterFind(a.Parameters, "tag")
if tag == nil {
return res, errors.New("tag variable is empty. aborting")
}

path = strings.TrimPrefix(path, abs)
// Global all files matching filePath
filesPath, err := afero.Glob(wkDirFS, path)
filesPath, err := afero.Glob(afero.NewOsFs(), artifactPath)
if err != nil {
return res, fmt.Errorf("cannot perform globbing of pattern '%s': %s", path, err)
return res, fmt.Errorf("cannot perform globbing of pattern '%s': %s", artifactPath, err)
}

if len(filesPath) == 0 {
return res, fmt.Errorf("pattern '%s' matched no file", path)
return res, fmt.Errorf("pattern '%s' matched no file", artifactPath)
}

var globalError = &sdk.MultiError{}
Expand All @@ -77,12 +80,11 @@ func RunArtifactUpload(ctx context.Context, wk workerruntime.Runtime, a sdk.Acti
wg.Add(len(filesPath))
for _, p := range filesPath {
go func(path string) {
absFile := abs + "/" + strings.TrimPrefix(path, abs)
log.Debug("Uploading %s projectKey:%v integrationName:%v job:%d", absFile, projectKey, integrationName, jobID)
log.Debug("Uploading %s projectKey:%v integrationName:%v job:%d", path, projectKey, integrationName, jobID)
defer wg.Done()
throughTempURL, duration, err := wk.Client().QueueArtifactUpload(ctx, projectKey, integrationName, jobID, tag.Value, absFile)
throughTempURL, duration, err := wk.Client().QueueArtifactUpload(ctx, projectKey, integrationName, jobID, tag.Value, path)
if err != nil {
chanError <- sdk.WrapError(err, "Error while uploading artifact %s", absFile)
chanError <- sdk.WrapError(err, "Error while uploading artifact %s", path)
wgErrors.Add(1)
return
}
Expand Down
12 changes: 10 additions & 2 deletions engine/worker/internal/handler_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"path"
"path/filepath"

"github.com/ovh/cds/engine/worker/internal/action"
"github.com/ovh/cds/engine/worker/pkg/workerruntime"
Expand All @@ -22,18 +24,23 @@ func uploadHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
return
}

var art sdk.WorkflowNodeRunArtifact
var art workerruntime.UploadArtifact
if err := json.Unmarshal(data, &art); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

artifactPath := art.Name
if !path.IsAbs(artifactPath) {
artifactPath = filepath.Join(art.WorkingDirectory, art.Name)
}

a := sdk.Action{
Parameters: []sdk.Parameter{
{
Name: "path",
Type: sdk.StringParameter,
Value: art.Name,
Value: artifactPath,
},
{
Name: "tag",
Expand All @@ -57,6 +64,7 @@ func uploadHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
return
}
ctx = workerruntime.SetWorkingDirectory(ctx, workingDir)

result, err := action.RunArtifactUpload(ctx, wk, a, wk.currentJob.secrets)
if err != nil {
wk.SendLog(ctx, workerruntime.LevelError, fmt.Sprintf("Artifact upload failed: %v", err))
Expand Down
6 changes: 6 additions & 0 deletions engine/worker/pkg/workerruntime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type DownloadArtifact struct {
Destination string `json:"destination"`
}

type UploadArtifact struct {
Name string `json:"name"`
Tag string `json:"tag"`
WorkingDirectory string `json:"working_directory"`
}

type FilePath struct {
Path string `json:"path"`
}
Expand Down

0 comments on commit d1e0b8b

Please sign in to comment.