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

Replace Environment variables in remoteFileName during upload and dow… #6

Merged
merged 2 commits into from
Aug 19, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public boolean perform(AbstractBuild build, Launcher launcher,
if (null == fileName) {
fileName = file.getName();
}
remoteFile = Util.replaceMacro(getRemoteFile(), env);
exitStatus = sshClient.downloadFile(logger, remoteFile, localFolder, fileName);
GsshBuilderWrapper.printSplit(logger);
} catch (Exception e) {
Expand Down Expand Up @@ -173,4 +174,4 @@ public ListBoxModel doFillServerInfoItems() {
return m;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,22 @@ public boolean perform(AbstractBuild build, Launcher launcher,
int exitStatus = -1;
try {
EnvVars env = build.getEnvironment(listener);
String uploadFileName = Util.fixEmptyAndTrim(Util.replaceMacro(getFileName(), env));
String localFilePath = Util.fixEmptyAndTrim(Util.replaceMacro(getLocalFilePath(), env));
String remoteLocation = Util.fixEmptyAndTrim(Util.replaceMacro(getRemoteLocation(), env));

if (localFilePath != null && remoteLocation != null) {
FilePath path = new FilePath(new File(localFilePath));
if (path.exists() && path.isDirectory()) {
for (FilePath f : path.list()) {
exitStatus = sshClient.uploadFile(logger, f.getName(), new File(f.getRemote()), remoteLocation);
exitStatus = sshClient.uploadFile(logger, uploadFileName, new File(f.getRemote()), remoteLocation);
}
} else {
File file = new File(localFilePath);
if (null == fileName) {
fileName = file.getName();
}
exitStatus = sshClient.uploadFile(logger, fileName, file, remoteLocation);
exitStatus = sshClient.uploadFile(logger, uploadFileName, file, remoteLocation);
}
GsshBuilderWrapper.printSplit(logger);
}
Expand Down Expand Up @@ -191,4 +192,4 @@ public ListBoxModel doFillServerInfoItems() {
return m;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import java.io.FileNotFoundException;
import com.jcraft.jsch.SftpException;
import java.io.IOException;

/**
* This is Ssh handler , user for handling SSH related event and requirments
Expand Down Expand Up @@ -101,6 +104,7 @@ public int uploadFile(PrintStream logger, String fileName,
Thread.sleep(2000);
sftp = (ChannelSftp) channel;
sftp.setFilenameEncoding("UTF-8");
prepareUpload(sftp, serverLocation, false);
sftp.cd(serverLocation);
out = sftp.put(fileName, 777);
Thread.sleep(2000);
Expand Down Expand Up @@ -304,6 +308,52 @@ public void setPassword(String password) {
this.password = password;
}

public boolean prepareUpload(
ChannelSftp sftpChannel,
String path,
boolean overwrite)
throws SftpException, IOException, FileNotFoundException {

boolean result = false;

// Build romote path subfolders inclusive:
String[] folders = path.split("/");
for (String folder : folders) {
if (folder.length() > 0) {
// This is a valid folder:
try {
System.out.println("Current Folder path before cd:" + folder);
sftpChannel.cd(folder);
} catch (SftpException e) {
// No such folder yet:
System.out.println("Inside create folders: ");
sftpChannel.mkdir(folder);
sftpChannel.cd(folder);
}
}
}

// Folders ready. Remove such a file if exists:
if (sftpChannel.ls(path).size() > 0) {
if (!overwrite) {
System.out.println(
"Error - file " + path + " was not created on server. " +
"It already exists and overwriting is forbidden.");
} else {
// Delete file:
sftpChannel.ls(path); // Search file.
sftpChannel.rm(path); // Remove file.
result = true;
}
} else {
// No such file:
result = true;
}

return result;
}


@Override
public String toString() {
return "Server Info [" + this.ip + " ," + this.port + ","
Expand Down