Skip to content

Commit 6398d81

Browse files
committed
Fixed namespace folder creation, and added fileutils
1 parent 371aa41 commit 6398d81

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

locker/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func parseAndSendMetaData(connection net.Conn, inputData *InputData) (fileInfo o
7676

7777
log.Info().
7878
Str("file name", fileInfo.Name()).
79+
Str("Namespace", fmt.Sprintf("%s/%s/%s", inputData.NameSpace, inputData.Project, inputData.JobID)).
7980
Str("size", fmt.Sprintf("%d", fileInfo.Size())).
8081
Str("hash", fmt.Sprintf("%v", inputData.FileHash)).
8182
Str("id", inputData.ID.String()).

locker/fileutils/fileutils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fileutils
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/rs/zerolog/log"
8+
)
9+
10+
func EnsurePathExists(elements ...string) error {
11+
fullPath := filepath.Join(elements...)
12+
13+
err := os.MkdirAll(fullPath, os.ModeDir)
14+
if err != nil {
15+
log.Err(err).Msg("Cannot create folder structure")
16+
}
17+
return err
18+
}
19+
20+
func CreateFileAt() {
21+
22+
}
23+
24+
func MoveFile(oldPath string, newPath string) error {
25+
err := os.Rename(oldPath, newPath)
26+
if err != nil {
27+
log.Err(err).Msg("File move failed!")
28+
}
29+
return err
30+
}

locker/fileutils/fileutils_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package fileutils
2+
3+
// Test EnsurePathexists with 0, 1 and more []string

locker/server.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"time"
15+
"vikingPingvin/locker/locker/fileutils"
1516
"vikingPingvin/locker/locker/messaging"
1617
"vikingPingvin/locker/locker/messaging/protobuf"
1718

@@ -27,9 +28,12 @@ type connectionType struct {
2728

2829
// Struct containing file data from received MetaData message
2930
type metaInfo struct {
30-
fileHash []byte
31-
fileName string
32-
ID []byte
31+
fileHash []byte
32+
fileName string
33+
namespace string
34+
project string
35+
jobID string
36+
ID []byte
3337
}
3438

3539
type Server interface {
@@ -74,7 +78,7 @@ func handleConnection(connection net.Conn) {
7478
artifactReceived := false
7579
var artifactPath *os.File
7680

77-
metaData := metaInfo{}
81+
metaData := &metaInfo{}
7882

7983
timeoutDuration := 5 * time.Second
8084
invalidCounterMax := 10
@@ -153,18 +157,33 @@ func handleConnection(connection net.Conn) {
153157
if receptionSuccesful {
154158
//Rename file
155159
baseDir := filepath.Dir(artifactPath.Name())
156-
newPath := filepath.Join(baseDir, metaData.fileName)
157-
os.Rename(artifactPath.Name(), newPath)
160+
newPath := filepath.Join(baseDir,
161+
metaData.namespace,
162+
metaData.project,
163+
metaData.jobID,
164+
metaData.fileName)
165+
err := fileutils.EnsurePathExists(filepath.Dir(newPath))
166+
if err == nil {
167+
err = fileutils.MoveFile(artifactPath.Name(), newPath)
168+
if err != nil {
169+
log.Err(err).Msg("SAD")
170+
}
171+
}
158172
log.Info().Msgf("Artifact ready: %s", newPath)
159173
}
160-
sendAckMessage(connection, &metaData, receptionSuccesful)
174+
sendAckMessage(connection, metaData, receptionSuccesful)
161175
}
162176

163-
func handleProtoMeta(metaMessage *protobuf.FileMeta) (file *os.File, metaData metaInfo) {
177+
func handleProtoMeta(metaMessage *protobuf.FileMeta) (file *os.File, _ *metaInfo) {
178+
179+
metaData := &metaInfo{}
164180

165181
xidValue, _ := xid.FromBytes(metaMessage.GetId())
166182
metaData.fileHash = metaMessage.GetHash()
167183
metaData.fileName = metaMessage.GetFilename()
184+
metaData.namespace = metaMessage.GetNamespace()
185+
metaData.project = metaMessage.GetProject()
186+
metaData.jobID = metaMessage.GetJobID()
168187
metaData.ID = metaMessage.GetId()
169188

170189
log.Info().

0 commit comments

Comments
 (0)