Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/sync data #97

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/commands/datasetIngestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ For Windows you need instead to specify -user username:password on the command l
log.Printf("Attachment file %v added to dataset %v\n", addAttachment, datasetId)
}
if copyFlag {
err := datasetIngestor.SyncDataToFileserver(datasetId, user, RSYNCServer, datasetSourceFolder, absFileListing)
err := datasetIngestor.SyncLocalDataToFileserver(datasetId, user, RSYNCServer, datasetSourceFolder, absFileListing, os.Stdout)
if err == nil {
// delayed enabling
archivable = true
Expand Down
30 changes: 16 additions & 14 deletions datasetIngestor/syncDataToFileserver_unix.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || nacl || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris

// very important: there must be an empty line after the build flag line .
package datasetIngestor

import (
"fmt"
"log"
"os"
"io"
"os/exec"
"regexp"
"strings"

version "github.com/mcuadros/go-version"
"regexp"
)

// functionality needed for "de-central" data
func SyncDataToFileserver(datasetId string, user map[string]string, RSYNCServer string, sourceFolder string, absFileListing string) (err error) {
// copies data from a local machine to a fileserver, uses RSync underneath
func SyncLocalDataToFileserver(datasetId string, user map[string]string, RSYNCServer string, sourceFolder string, absFileListing string, cmdOutput io.Writer) (err error) {
consolethinks marked this conversation as resolved.
Show resolved Hide resolved
username := user["username"]
shortDatasetId := strings.Split(datasetId, "/")[1]
log.Println("short dataset id:", shortDatasetId)
destFolder := "archive/" + shortDatasetId + sourceFolder
serverConnectString := fmt.Sprintf("%s@%s:%s", username, RSYNCServer, destFolder)
// append trailing slash to sourceFolder to indicate that the *contents* of the folder should be copied
// no special handling for blanks in sourceFolder needed here
fullSourceFolderPath := sourceFolder + "/"

versionNumber, err := getRsyncVersion()
if err != nil {
log.Fatal("Error getting rsync version: ", err)
return fmt.Errorf("error getting rsync version: %v", err)
}

rsyncCmd := buildRsyncCmd(versionNumber, absFileListing, fullSourceFolderPath, serverConnectString)

// Show rsync's output
rsyncCmd.Stderr = os.Stderr
log.Printf("Running: %v.\n", rsyncCmd.Args)

// Show rsync's output
rsyncCmd.Stdout = cmdOutput
rsyncCmd.Stderr = cmdOutput
fmt.Fprintf(cmdOutput, "Running: %v.\n", rsyncCmd.Args)
err = rsyncCmd.Run()
return err
}
Expand All @@ -46,15 +48,15 @@ func getRsyncVersion() (string, error) {
return "", err
}
version := string(output)

// Use a regular expression to find the version number.
// It will match the first occurrence of a string in the format "x.y.z" in the `version` string, where "x", "y", and "z" are one or more digits.
re := regexp.MustCompile(`\d+\.\d+\.\d+`)
versionNumber := re.FindString(version)
if versionNumber == "" {
return "", fmt.Errorf("could not find version number in rsync version string: %s", version)
}

return versionNumber, nil
}

Expand Down
19 changes: 10 additions & 9 deletions datasetIngestor/syncDataToFileserver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
package datasetIngestor

import (
"log"
"fmt"
"io"
"path"
"regexp"
"strings"
)

func SyncDataToFileserver(datasetId string, user map[string]string, RSYNCServer string, sourceFolder string, absFileListing string) (err error) {

// copies data from a local machine to a fileserver, uses scp underneath
func SyncLocalDataToFileserver(datasetId string, user map[string]string, RSYNCServer string, sourceFolder string, absFileListing string, commandOutput io.Writer) (err error) {
username := user["username"]
password := user["password"]
shortDatasetId := strings.Split(datasetId, "/")[1]
Expand All @@ -26,9 +27,6 @@ func SyncDataToFileserver(datasetId string, user map[string]string, RSYNCServer
destFolder := "archive/" + shortDatasetId + strings.Join(destparts[0:len(destparts)-1], "/")
destFolder2 := "archive/" + shortDatasetId + strings.Join(destparts[0:len(destparts)], "/")

// fmt.Println("Destination folder:", destFolder)
// fmt.Println("Sourcefolder:", sourceFolder)

// add port number if missing
FullRSYNCServer := RSYNCServer
if !strings.Contains(RSYNCServer, ":") {
Expand All @@ -51,16 +49,19 @@ func SyncDataToFileserver(datasetId string, user map[string]string, RSYNCServer
if absFileListing != "" {
lines, err := readLines(absFileListing)
if err != nil {
log.Fatalf("Could not read filellist, readLines: %s", err)
return fmt.Errorf("could not read filelist, readlines: %v", err)
}
for _, line := range lines {
windowsSource := re.ReplaceAllString(path.Join(sourceFolder, line), "$1:/")
// log.Printf("Copying data via scp from %s to %s\n", windowsSource, destFolder2)
fmt.Fprintf(commandOutput, "Copying data via scp from %s to %s\n", windowsSource, destFolder)
err = c.Send(destFolder2, windowsSource)
if err != nil {
return err
}
}
} else {
windowsSource := re.ReplaceAllString(sourceFolder, "$1:/")
// log.Printf("Copying data via scp from %s to %s\n", windowsSource, destFolder)
fmt.Fprintf(commandOutput, "Copying data via scp from %s to %s\n", windowsSource, destFolder)
err = c.Send(destFolder, windowsSource)
}
return err
Expand Down