Skip to content

Commit 08cbd61

Browse files
devkimittalHarness
authored andcommitted
feat: [CI-19269]: working dir env (#55)
* c441cd feat: [CI-19269]: working dir env
1 parent de9c51e commit 08cbd61

File tree

8 files changed

+120
-16
lines changed

8 files changed

+120
-16
lines changed

handler/setup.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/harness/harness-docker-runner/engine/docker"
2121
"github.com/harness/harness-docker-runner/engine/spec"
2222
"github.com/harness/harness-docker-runner/executor"
23+
"github.com/harness/harness-docker-runner/internal/paths"
2324
"github.com/harness/harness-docker-runner/livelog"
2425
"github.com/harness/harness-docker-runner/logger"
2526
"github.com/harness/harness-docker-runner/pipeline"
@@ -156,25 +157,31 @@ func updateVolumes(r api.SetupRequest) {
156157
if v.HostPath.ID == "harness" {
157158
v.HostPath.Create = true
158159
v.HostPath.Remove = true
159-
v.HostPath.Path = v.HostPath.Path + "-" + sanitize(r.ID)
160+
v.HostPath.Path = paths.ResolveHostPath(v.HostPath.Path + "-" + sanitize(r.ID))
161+
}
162+
if v.HostPath.ID == "addon" {
163+
v.HostPath.Create = true
164+
v.HostPath.Remove = true
165+
v.HostPath.Path = paths.ResolveHostPath(v.HostPath.Path)
160166
}
161167
}
168+
162169
}
163170
}
164171

165172
func getSharedVolume() *spec.Volume {
166173
return &spec.Volume{
167174
HostPath: &spec.VolumeHostPath{
168175
Name: pipeline.SharedVolName,
169-
Path: pipeline.SharedVolPath,
176+
Path: pipeline.GetSharedVolPath(),
170177
ID: "engine",
171178
},
172179
}
173180
}
174181

175182
// getTiVolume returns a volume (directory) which is used to store TI related data
176183
func getTiVolume(setupID string) *spec.Volume {
177-
tiDir := fmt.Sprintf("%s-%s", ti.VolumePath, sanitize(setupID))
184+
tiDir := fmt.Sprintf("%s-%s", ti.GetVolumePath(), sanitize(setupID))
178185
return &spec.Volume{
179186
HostPath: &spec.VolumeHostPath{
180187
Name: ti.VolumeName,

handler/step.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func convert(err error) api.PollStepResponse {
112112
func getSharedVolumeMount() *spec.VolumeMount {
113113
return &spec.VolumeMount{
114114
Name: pipeline.SharedVolName,
115-
Path: pipeline.SharedVolPath,
115+
Path: pipeline.GetSharedVolPath(),
116116
}
117117
}
118118

internal/paths/paths.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2022 Drone.IO Inc. All rights reserved.
2+
// Use of this source code is governed by the Polyform License
3+
// that can be found in the LICENSE file.
4+
5+
// Copyright 2019 Drone.IO Inc. All rights reserved.
6+
// Use of this source code is governed by the Polyform License
7+
// that can be found in the LICENSE file.
8+
9+
// Package internal contains runner internals.
10+
package paths
11+
12+
import (
13+
"os"
14+
"path/filepath"
15+
)
16+
17+
const (
18+
defaultWorkingDir = "/tmp"
19+
workingDirEnvVar = "WORKING_DIR"
20+
)
21+
22+
var workingDir string
23+
24+
func init() {
25+
workingDir = os.Getenv(workingDirEnvVar)
26+
if workingDir == "" {
27+
workingDir = defaultWorkingDir
28+
}
29+
// Ensure the path is absolute
30+
if !filepath.IsAbs(workingDir) {
31+
absPath, err := filepath.Abs(workingDir)
32+
if err == nil {
33+
workingDir = absPath
34+
}
35+
}
36+
}
37+
38+
// JoinPath joins the working directory with the provided path segments
39+
// If the path is already absolute, it returns the path as-is
40+
func JoinPath(elem ...string) string {
41+
if len(elem) == 0 {
42+
return workingDir
43+
}
44+
// If the first element is an absolute path, use it directly
45+
if filepath.IsAbs(elem[0]) {
46+
return filepath.Join(elem...)
47+
}
48+
// Otherwise, join with working directory
49+
return filepath.Join(append([]string{workingDir}, elem...)...)
50+
}
51+
52+
// GetSharedVolPath returns the path for shared volume
53+
func GetSharedVolPath() string {
54+
return JoinPath("engine")
55+
}
56+
57+
// GetTIVolPath returns the path for TI volume
58+
func GetTIVolPath() string {
59+
return JoinPath("ti")
60+
}
61+
62+
// ResolveHostPath resolves a host path to be within the working directory
63+
// System paths (like Docker socket) are returned as-is
64+
// Other absolute paths are made relative to the working directory
65+
func ResolveHostPath(path string) string {
66+
// If it's already an absolute path, strip the leading / and make it relative to working dir
67+
// This ensures paths like /harness become {WORKING_DIR}/harness
68+
if filepath.IsAbs(path) {
69+
// Remove leading separator(s)
70+
cleanPath := filepath.Clean(path)
71+
if len(cleanPath) > 0 && (cleanPath[0] == '/' || cleanPath[0] == '\\') {
72+
cleanPath = cleanPath[1:]
73+
}
74+
// Handle Windows drive letters (C:, D:, etc.)
75+
if len(cleanPath) >= 2 && cleanPath[1] == ':' {
76+
cleanPath = cleanPath[2:]
77+
if len(cleanPath) > 0 && (cleanPath[0] == '/' || cleanPath[0] == '\\') {
78+
cleanPath = cleanPath[1:]
79+
}
80+
}
81+
return JoinPath(cleanPath)
82+
}
83+
// If it's relative, join with working directory
84+
return JoinPath(path)
85+
}

pipeline/runtime/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func executeRunStep(ctx context.Context, engine *engine.Engine, r *api.StartStep
4444
var outputSecretsFile string
4545

4646
if enablePluginOutputSecrets {
47-
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.SharedVolPath, step.ID)
47+
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.GetSharedVolPath(), step.ID)
4848
step.Envs["DRONE_OUTPUT"] = outputFile
4949

50-
outputSecretsFile = fmt.Sprintf("%s/%s-output-secrets.env", pipeline.SharedVolPath, step.ID)
50+
outputSecretsFile = fmt.Sprintf("%s/%s-output-secrets.env", pipeline.GetSharedVolPath(), step.ID)
5151
step.Envs["HARNESS_OUTPUT_SECRET_FILE"] = outputSecretsFile
5252
} else {
53-
outputFile = fmt.Sprintf("%s/%s.out", pipeline.SharedVolPath, step.ID)
53+
outputFile = fmt.Sprintf("%s/%s.out", pipeline.GetSharedVolPath(), step.ID)
5454
step.Envs["DRONE_OUTPUT"] = outputFile
5555
}
5656

@@ -68,7 +68,7 @@ func executeRunStep(ctx context.Context, engine *engine.Engine, r *api.StartStep
6868
// Log the command being executed
6969
printCommand(step, out)
7070

71-
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.SharedVolPath, step.ID)
71+
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.GetSharedVolPath(), step.ID)
7272
step.Envs["PLUGIN_ARTIFACT_FILE"] = artifactFile
7373

7474
exited, err := engine.Run(ctx, step, out)

pipeline/runtime/runtest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func executeRunTestStep(ctx context.Context, engine *engine.Engine, r *api.Start
5151

5252
var outputFile string
5353
if enablePluginOutputSecrets {
54-
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.SharedVolPath, step.ID)
54+
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.GetSharedVolPath(), step.ID)
5555
} else {
56-
outputFile = fmt.Sprintf("%s/%s.out", pipeline.SharedVolPath, step.ID)
56+
outputFile = fmt.Sprintf("%s/%s.out", pipeline.GetSharedVolPath(), step.ID)
5757
}
5858

5959
if len(r.Outputs) > 0 {
@@ -62,7 +62,7 @@ func executeRunTestStep(ctx context.Context, engine *engine.Engine, r *api.Start
6262
step.Command[0] += getOutputVarCmd(step.Entrypoint, r.OutputVars, outputFile, enablePluginOutputSecrets)
6363
}
6464

65-
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.SharedVolPath, step.ID)
65+
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.GetSharedVolPath(), step.ID)
6666
step.Envs["PLUGIN_ARTIFACT_FILE"] = artifactFile
6767

6868
// Log the command being executed

pipeline/runtime/runtestsV2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func executeRunTestsV2Step(ctx context.Context, engine *engine.Engine, r *api.St
5959
var outputSecretsFile string
6060

6161
if enablePluginOutputSecrets {
62-
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.SharedVolPath, step.ID)
62+
outputFile = fmt.Sprintf("%s/%s-output.env", pipeline.GetSharedVolPath(), step.ID)
6363
step.Envs["DRONE_OUTPUT"] = outputFile
6464

65-
outputSecretsFile = fmt.Sprintf("%s/%s-output-secrets.env", pipeline.SharedVolPath, step.ID)
65+
outputSecretsFile = fmt.Sprintf("%s/%s-output-secrets.env", pipeline.GetSharedVolPath(), step.ID)
6666
step.Envs["HARNESS_OUTPUT_SECRET_FILE"] = outputSecretsFile
6767
} else {
68-
outputFile = fmt.Sprintf("%s/%s.out", pipeline.SharedVolPath, step.ID)
68+
outputFile = fmt.Sprintf("%s/%s.out", pipeline.GetSharedVolPath(), step.ID)
6969
step.Envs["DRONE_OUTPUT"] = outputFile
7070
}
7171

@@ -77,7 +77,7 @@ func executeRunTestsV2Step(ctx context.Context, engine *engine.Engine, r *api.St
7777

7878
logrus.WithField("step_id", r.ID).WithField("stage_id", r.StageRuntimeID).Traceln("starting step run")
7979

80-
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.SharedVolPath, step.ID)
80+
artifactFile := fmt.Sprintf("%s/%s-artifact", pipeline.GetSharedVolPath(), step.ID)
8181
step.Envs["PLUGIN_ARTIFACT_FILE"] = artifactFile
8282

8383
// Log the command being executed

pipeline/state.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pipeline
77
import (
88
"github.com/harness/harness-docker-runner/api"
99
"github.com/harness/harness-docker-runner/engine/spec"
10+
"github.com/harness/harness-docker-runner/internal/paths"
1011
"github.com/harness/harness-docker-runner/logstream"
1112
"github.com/harness/harness-docker-runner/logstream/filestore"
1213
"github.com/harness/harness-docker-runner/logstream/remote"
@@ -18,6 +19,10 @@ const (
1819
SharedVolName = "_engine"
1920
)
2021

22+
func GetSharedVolPath() string {
23+
return paths.GetSharedVolPath()
24+
}
25+
2126
// State stores the pipeline state.
2227
type State struct {
2328
volumes []*spec.Volume
@@ -63,7 +68,7 @@ func (s *State) GetLogStreamClient() logstream.Client {
6368
s.logClient = remote.NewHTTPClient(s.logConfig.URL, s.logConfig.AccountID,
6469
s.logConfig.Token, s.logConfig.IndirectUpload, false)
6570
} else {
66-
s.logClient = filestore.New(SharedVolPath)
71+
s.logClient = filestore.New(GetSharedVolPath())
6772
}
6873
}
6974
return s.logClient

ti/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
package ti
66

7+
import "github.com/harness/harness-docker-runner/internal/paths"
8+
79
const (
810
VolumeName = "ti"
911
VolumePath = "/tmp/ti"
1012
)
13+
14+
// VolumePath returns the TI volume path
15+
func GetVolumePath() string {
16+
return paths.GetTIVolPath()
17+
}

0 commit comments

Comments
 (0)