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/check central data #100

Merged
merged 6 commits into from
Aug 19, 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
7 changes: 5 additions & 2 deletions cmd/commands/datasetIngestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ For Windows you need instead to specify -user username:password on the command l
// and unless copy flag defined via command line
if !copyFlag && !nocopyFlag { // NOTE this whole copyFlag, nocopyFlag ordeal makes no sense whatsoever
if !beamlineAccount {
err := datasetIngestor.CheckDataCentrallyAvailable(user["username"], RSYNCServer, datasetSourceFolder)
if err != nil {
sshErr, otherErr := datasetIngestor.CheckDataCentrallyAvailableSsh(user["username"], RSYNCServer, datasetSourceFolder, os.Stdout)
if otherErr != nil {
log.Fatalf("CheckDataCentrallyAvailableSsh returned an error: %v\n", otherErr)
}
if sshErr != nil {
color.Set(color.FgYellow)
log.Printf("The source folder %v is not centrally available (decentral use case).\nThe data must first be copied to a rsync cache server.\n ", datasetSourceFolder)
color.Unset()
Expand Down
32 changes: 15 additions & 17 deletions datasetIngestor/checkDataCentrallyAvailable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package datasetIngestor

import (
"errors"
"log"
"os"
"fmt"
"io"
"os/exec"
"runtime"
)

// execCommand is a variable that points to exec.Command, allowing it to be replaced in tests.
var execCommand = exec.Command

// CheckDataCentrallyAvailable checks if a specific directory (sourceFolder) is available on a remote server (ARCHIVEServer)
// CheckDataCentrallyAvailableSsh checks if a specific directory (sourceFolder) is available on a remote server (ARCHIVEServer)
// using the provided username for SSH connection. It returns an error if the directory is not available or if there's an issue with the SSH connection.
func CheckDataCentrallyAvailable(username string, ARCHIVEServer string, sourceFolder string) (err error) {
// Returned values:
// - sshErr - the error returned by the ssh command
// - err - other error that prevents the ssh command from being executed
func CheckDataCentrallyAvailableSsh(username string, ARCHIVEServer string, sourceFolder string, sshOutput io.Writer) (sshErr error, otherErr error) {
// NOTE why not use crypto/ssh ???
// NOTE is this even a reliable method for people outside PSI?
// NOTE even if the folder is there, not all files might be there!
var cmd *exec.Cmd

// Check the operating system
Expand All @@ -24,26 +27,21 @@ func CheckDataCentrallyAvailable(username string, ARCHIVEServer string, sourceFo
// Check if ssh exists
_, err := exec.LookPath("ssh") // locate a program in the user's path
if err != nil {
log.Println("SSH is not installed. Please install OpenSSH client.")
return err
return nil, errors.New("no ssh implementation is available")
}

// Create a new exec.Command to run the SSH command. The command checks if the directory exists on the remote server.
// The "-q" option suppresses all warnings, "-l" specifies the login name on the remote server.
cmd = execCommand("ssh", "-q", "-l", username, ARCHIVEServer, "test", "-d", sourceFolder)
default:
log.Printf("%s is not supported.\n", os)
return errors.New("unsupported operating system")
return nil, fmt.Errorf("unsupported operating system: %s", os)
}

// Redirect the command's standard error to the process's standard error.
// This means that any error messages from the command will be displayed in the terminal.
cmd.Stderr = os.Stderr

// Log the command that is being run for debugging purposes.
log.Printf("Running %v.\n", cmd.Args)
// Redirect the command's output to sshOutput var
cmd.Stdout = sshOutput
cmd.Stderr = sshOutput

// Run the command and return any error that occurs.
err = cmd.Run()
return err
sshErr = cmd.Run()
return sshErr, nil
}
13 changes: 8 additions & 5 deletions datasetIngestor/checkDataCentrallyAvailable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ func TestCheckDataCentrallyAvailable(t *testing.T) {
defer func() { execCommand = oldExecCommand }()

t.Run(tt.name, func(t *testing.T) {
err := CheckDataCentrallyAvailable(tt.username, tt.archiveServer, tt.sourceFolder)
if (err != nil) != tt.wantErr {
t.Errorf("CheckDataCentrallyAvailable() error = %v, wantErr %v", err, tt.wantErr)
sshErr, otherErr := CheckDataCentrallyAvailableSsh(tt.username, tt.archiveServer, tt.sourceFolder, nil)
if otherErr != nil {
t.Errorf("other error encountered: %v", otherErr)
}
if err != nil && tt.wantErr && err.Error() != tt.errMsg {
t.Errorf("CheckDataCentrallyAvailable() errMsg = %v, wantErrMsg %v", err.Error(), tt.errMsg)
if (sshErr != nil) != tt.wantErr {
t.Errorf("CheckDataCentrallyAvailable() error = %v, wantErr %v", sshErr != nil, tt.wantErr)
}
if sshErr != nil && tt.wantErr && sshErr.Error() != tt.errMsg {
t.Errorf("CheckDataCentrallyAvailable() errMsg = %v, wantErrMsg %v", sshErr.Error(), tt.errMsg)
}
})
}
Expand Down