embulk-input-sftp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/README.md +126 -0
- data/build.gradle +95 -0
- data/config/checkstyle/checkstyle.xml +128 -0
- data/config/checkstyle/default.xml +108 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +160 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/input/sftp.rb +3 -0
- data/src/main/java/org/embulk/input/sftp/PluginTask.java +67 -0
- data/src/main/java/org/embulk/input/sftp/ProxyTask.java +85 -0
- data/src/main/java/org/embulk/input/sftp/SftpFileInput.java +217 -0
- data/src/main/java/org/embulk/input/sftp/SftpFileInputPlugin.java +67 -0
- data/src/main/java/org/embulk/input/sftp/SingleFileProvider.java +71 -0
- data/src/test/java/org/embulk/input/sftp/TestSftpFileInputPlugin.java +5 -0
- data/src/test/resources/id_rsa +30 -0
- data/src/test/resources/id_rsa.pub +1 -0
- metadata +95 -0
Binary file
|
data/gradlew
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
##############################################################################
|
4
|
+
##
|
5
|
+
## Gradle start up script for UN*X
|
6
|
+
##
|
7
|
+
##############################################################################
|
8
|
+
|
9
|
+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
10
|
+
DEFAULT_JVM_OPTS=""
|
11
|
+
|
12
|
+
APP_NAME="Gradle"
|
13
|
+
APP_BASE_NAME=`basename "$0"`
|
14
|
+
|
15
|
+
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
16
|
+
MAX_FD="maximum"
|
17
|
+
|
18
|
+
warn ( ) {
|
19
|
+
echo "$*"
|
20
|
+
}
|
21
|
+
|
22
|
+
die ( ) {
|
23
|
+
echo
|
24
|
+
echo "$*"
|
25
|
+
echo
|
26
|
+
exit 1
|
27
|
+
}
|
28
|
+
|
29
|
+
# OS specific support (must be 'true' or 'false').
|
30
|
+
cygwin=false
|
31
|
+
msys=false
|
32
|
+
darwin=false
|
33
|
+
case "`uname`" in
|
34
|
+
CYGWIN* )
|
35
|
+
cygwin=true
|
36
|
+
;;
|
37
|
+
Darwin* )
|
38
|
+
darwin=true
|
39
|
+
;;
|
40
|
+
MINGW* )
|
41
|
+
msys=true
|
42
|
+
;;
|
43
|
+
esac
|
44
|
+
|
45
|
+
# Attempt to set APP_HOME
|
46
|
+
# Resolve links: $0 may be a link
|
47
|
+
PRG="$0"
|
48
|
+
# Need this for relative symlinks.
|
49
|
+
while [ -h "$PRG" ] ; do
|
50
|
+
ls=`ls -ld "$PRG"`
|
51
|
+
link=`expr "$ls" : '.*-> \(.*\)$'`
|
52
|
+
if expr "$link" : '/.*' > /dev/null; then
|
53
|
+
PRG="$link"
|
54
|
+
else
|
55
|
+
PRG=`dirname "$PRG"`"/$link"
|
56
|
+
fi
|
57
|
+
done
|
58
|
+
SAVED="`pwd`"
|
59
|
+
cd "`dirname \"$PRG\"`/" >/dev/null
|
60
|
+
APP_HOME="`pwd -P`"
|
61
|
+
cd "$SAVED" >/dev/null
|
62
|
+
|
63
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
64
|
+
|
65
|
+
# Determine the Java command to use to start the JVM.
|
66
|
+
if [ -n "$JAVA_HOME" ] ; then
|
67
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
68
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
69
|
+
JAVACMD="$JAVA_HOME/jre/sh/java"
|
70
|
+
else
|
71
|
+
JAVACMD="$JAVA_HOME/bin/java"
|
72
|
+
fi
|
73
|
+
if [ ! -x "$JAVACMD" ] ; then
|
74
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
75
|
+
|
76
|
+
Please set the JAVA_HOME variable in your environment to match the
|
77
|
+
location of your Java installation."
|
78
|
+
fi
|
79
|
+
else
|
80
|
+
JAVACMD="java"
|
81
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
82
|
+
|
83
|
+
Please set the JAVA_HOME variable in your environment to match the
|
84
|
+
location of your Java installation."
|
85
|
+
fi
|
86
|
+
|
87
|
+
# Increase the maximum file descriptors if we can.
|
88
|
+
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
89
|
+
MAX_FD_LIMIT=`ulimit -H -n`
|
90
|
+
if [ $? -eq 0 ] ; then
|
91
|
+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
92
|
+
MAX_FD="$MAX_FD_LIMIT"
|
93
|
+
fi
|
94
|
+
ulimit -n $MAX_FD
|
95
|
+
if [ $? -ne 0 ] ; then
|
96
|
+
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
97
|
+
fi
|
98
|
+
else
|
99
|
+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
100
|
+
fi
|
101
|
+
fi
|
102
|
+
|
103
|
+
# For Darwin, add options to specify how the application appears in the dock
|
104
|
+
if $darwin; then
|
105
|
+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
106
|
+
fi
|
107
|
+
|
108
|
+
# For Cygwin, switch paths to Windows format before running java
|
109
|
+
if $cygwin ; then
|
110
|
+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
111
|
+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
112
|
+
JAVACMD=`cygpath --unix "$JAVACMD"`
|
113
|
+
|
114
|
+
# We build the pattern for arguments to be converted via cygpath
|
115
|
+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
116
|
+
SEP=""
|
117
|
+
for dir in $ROOTDIRSRAW ; do
|
118
|
+
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
119
|
+
SEP="|"
|
120
|
+
done
|
121
|
+
OURCYGPATTERN="(^($ROOTDIRS))"
|
122
|
+
# Add a user-defined pattern to the cygpath arguments
|
123
|
+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
124
|
+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
125
|
+
fi
|
126
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
127
|
+
i=0
|
128
|
+
for arg in "$@" ; do
|
129
|
+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
130
|
+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
131
|
+
|
132
|
+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
133
|
+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
134
|
+
else
|
135
|
+
eval `echo args$i`="\"$arg\""
|
136
|
+
fi
|
137
|
+
i=$((i+1))
|
138
|
+
done
|
139
|
+
case $i in
|
140
|
+
(0) set -- ;;
|
141
|
+
(1) set -- "$args0" ;;
|
142
|
+
(2) set -- "$args0" "$args1" ;;
|
143
|
+
(3) set -- "$args0" "$args1" "$args2" ;;
|
144
|
+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
145
|
+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
146
|
+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
147
|
+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
148
|
+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
149
|
+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
150
|
+
esac
|
151
|
+
fi
|
152
|
+
|
153
|
+
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
154
|
+
function splitJvmOpts() {
|
155
|
+
JVM_OPTS=("$@")
|
156
|
+
}
|
157
|
+
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
158
|
+
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
159
|
+
|
160
|
+
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
data/gradlew.bat
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
@if "%DEBUG%" == "" @echo off
|
2
|
+
@rem ##########################################################################
|
3
|
+
@rem
|
4
|
+
@rem Gradle startup script for Windows
|
5
|
+
@rem
|
6
|
+
@rem ##########################################################################
|
7
|
+
|
8
|
+
@rem Set local scope for the variables with windows NT shell
|
9
|
+
if "%OS%"=="Windows_NT" setlocal
|
10
|
+
|
11
|
+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
12
|
+
set DEFAULT_JVM_OPTS=
|
13
|
+
|
14
|
+
set DIRNAME=%~dp0
|
15
|
+
if "%DIRNAME%" == "" set DIRNAME=.
|
16
|
+
set APP_BASE_NAME=%~n0
|
17
|
+
set APP_HOME=%DIRNAME%
|
18
|
+
|
19
|
+
@rem Find java.exe
|
20
|
+
if defined JAVA_HOME goto findJavaFromJavaHome
|
21
|
+
|
22
|
+
set JAVA_EXE=java.exe
|
23
|
+
%JAVA_EXE% -version >NUL 2>&1
|
24
|
+
if "%ERRORLEVEL%" == "0" goto init
|
25
|
+
|
26
|
+
echo.
|
27
|
+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
28
|
+
echo.
|
29
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
30
|
+
echo location of your Java installation.
|
31
|
+
|
32
|
+
goto fail
|
33
|
+
|
34
|
+
:findJavaFromJavaHome
|
35
|
+
set JAVA_HOME=%JAVA_HOME:"=%
|
36
|
+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
37
|
+
|
38
|
+
if exist "%JAVA_EXE%" goto init
|
39
|
+
|
40
|
+
echo.
|
41
|
+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
42
|
+
echo.
|
43
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
44
|
+
echo location of your Java installation.
|
45
|
+
|
46
|
+
goto fail
|
47
|
+
|
48
|
+
:init
|
49
|
+
@rem Get command-line arguments, handling Windowz variants
|
50
|
+
|
51
|
+
if not "%OS%" == "Windows_NT" goto win9xME_args
|
52
|
+
if "%@eval[2+2]" == "4" goto 4NT_args
|
53
|
+
|
54
|
+
:win9xME_args
|
55
|
+
@rem Slurp the command line arguments.
|
56
|
+
set CMD_LINE_ARGS=
|
57
|
+
set _SKIP=2
|
58
|
+
|
59
|
+
:win9xME_args_slurp
|
60
|
+
if "x%~1" == "x" goto execute
|
61
|
+
|
62
|
+
set CMD_LINE_ARGS=%*
|
63
|
+
goto execute
|
64
|
+
|
65
|
+
:4NT_args
|
66
|
+
@rem Get arguments from the 4NT Shell from JP Software
|
67
|
+
set CMD_LINE_ARGS=%$
|
68
|
+
|
69
|
+
:execute
|
70
|
+
@rem Setup the command line
|
71
|
+
|
72
|
+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
73
|
+
|
74
|
+
@rem Execute Gradle
|
75
|
+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
76
|
+
|
77
|
+
:end
|
78
|
+
@rem End local scope for the variables with windows NT shell
|
79
|
+
if "%ERRORLEVEL%"=="0" goto mainEnd
|
80
|
+
|
81
|
+
:fail
|
82
|
+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
83
|
+
rem the _cmd.exe /c_ return code!
|
84
|
+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
85
|
+
exit /b 1
|
86
|
+
|
87
|
+
:mainEnd
|
88
|
+
if "%OS%"=="Windows_NT" endlocal
|
89
|
+
|
90
|
+
:omega
|
@@ -0,0 +1,67 @@
|
|
1
|
+
package org.embulk.input.sftp;
|
2
|
+
|
3
|
+
import com.google.common.base.Optional;
|
4
|
+
import org.embulk.config.Config;
|
5
|
+
import org.embulk.config.ConfigDefault;
|
6
|
+
import org.embulk.config.ConfigInject;
|
7
|
+
import org.embulk.config.Task;
|
8
|
+
import org.embulk.spi.BufferAllocator;
|
9
|
+
import org.embulk.spi.unit.LocalFile;
|
10
|
+
|
11
|
+
import java.util.List;
|
12
|
+
|
13
|
+
public interface PluginTask
|
14
|
+
extends Task
|
15
|
+
{
|
16
|
+
@Config("host")
|
17
|
+
String getHost();
|
18
|
+
|
19
|
+
@Config("port")
|
20
|
+
@ConfigDefault("22")
|
21
|
+
int getPort();
|
22
|
+
|
23
|
+
@Config("user")
|
24
|
+
String getUser();
|
25
|
+
|
26
|
+
@Config("password")
|
27
|
+
@ConfigDefault("null")
|
28
|
+
Optional<String> getPassword();
|
29
|
+
|
30
|
+
@Config("secret_key_file")
|
31
|
+
@ConfigDefault("null")
|
32
|
+
Optional<LocalFile> getSecretKeyFile();
|
33
|
+
void setSecretKeyFile(Optional<LocalFile> secretKeyFile);
|
34
|
+
|
35
|
+
@Config("secret_key_passphrase")
|
36
|
+
@ConfigDefault("\"\"")
|
37
|
+
String getSecretKeyPassphrase();
|
38
|
+
|
39
|
+
@Config("user_directory_is_root")
|
40
|
+
@ConfigDefault("true")
|
41
|
+
Boolean getUserDirIsRoot();
|
42
|
+
|
43
|
+
@Config("timeout")
|
44
|
+
@ConfigDefault("600") // 10 minutes
|
45
|
+
int getSftpConnectionTimeout();
|
46
|
+
|
47
|
+
@Config("max_connection_retry")
|
48
|
+
@ConfigDefault("5") // 5 times retry to connect sftp server if failed.
|
49
|
+
int getMaxConnectionRetry();
|
50
|
+
|
51
|
+
@Config("path_prefix")
|
52
|
+
String getPathPrefix();
|
53
|
+
|
54
|
+
@Config("last_path")
|
55
|
+
@ConfigDefault("null")
|
56
|
+
Optional<String> getLastPath();
|
57
|
+
|
58
|
+
@Config("proxy")
|
59
|
+
@ConfigDefault("null")
|
60
|
+
Optional<ProxyTask> getProxy();
|
61
|
+
|
62
|
+
List<String> getFiles();
|
63
|
+
void setFiles(List<String> files);
|
64
|
+
|
65
|
+
@ConfigInject
|
66
|
+
BufferAllocator getBufferAllocator();
|
67
|
+
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
package org.embulk.input.sftp;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.annotation.JsonCreator;
|
4
|
+
import com.fasterxml.jackson.annotation.JsonValue;
|
5
|
+
import com.google.common.base.Optional;
|
6
|
+
import org.apache.commons.vfs2.FileSystemOptions;
|
7
|
+
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
|
8
|
+
import org.embulk.config.Config;
|
9
|
+
import org.embulk.config.ConfigDefault;
|
10
|
+
import org.embulk.config.ConfigException;
|
11
|
+
import org.embulk.config.Task;
|
12
|
+
|
13
|
+
import java.util.Locale;
|
14
|
+
|
15
|
+
interface ProxyTask
|
16
|
+
extends Task
|
17
|
+
{
|
18
|
+
@Config("type")
|
19
|
+
ProxyType getType();
|
20
|
+
|
21
|
+
@Config("host")
|
22
|
+
Optional<String> getHost();
|
23
|
+
|
24
|
+
@Config("user")
|
25
|
+
@ConfigDefault("null")
|
26
|
+
Optional<String> getUser();
|
27
|
+
|
28
|
+
@Config("password")
|
29
|
+
@ConfigDefault("null")
|
30
|
+
Optional<String> getPassword();
|
31
|
+
|
32
|
+
@Config("port")
|
33
|
+
@ConfigDefault("22")
|
34
|
+
int getPort();
|
35
|
+
|
36
|
+
@Config("command")
|
37
|
+
@ConfigDefault("null")
|
38
|
+
Optional<String> getCommand();
|
39
|
+
|
40
|
+
enum ProxyType
|
41
|
+
{
|
42
|
+
HTTP,
|
43
|
+
SOCKS,
|
44
|
+
STREAM;
|
45
|
+
|
46
|
+
@JsonValue
|
47
|
+
@Override
|
48
|
+
public String toString()
|
49
|
+
{
|
50
|
+
return name().toLowerCase(Locale.ENGLISH);
|
51
|
+
}
|
52
|
+
|
53
|
+
@JsonCreator
|
54
|
+
public static ProxyType fromString(String value)
|
55
|
+
{
|
56
|
+
switch (value) {
|
57
|
+
case "http":
|
58
|
+
return HTTP;
|
59
|
+
case "socks":
|
60
|
+
return SOCKS;
|
61
|
+
case "stream":
|
62
|
+
return STREAM;
|
63
|
+
default:
|
64
|
+
throw new ConfigException(String.format("Unknown proxy type '%s'. Supported proxy types are http, socks, stream", value));
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
public static SftpFileSystemConfigBuilder setProxyType(SftpFileSystemConfigBuilder builder, FileSystemOptions fsOptions, ProxyTask.ProxyType type)
|
69
|
+
{
|
70
|
+
SftpFileSystemConfigBuilder.ProxyType setType = null;
|
71
|
+
switch (type) {
|
72
|
+
case HTTP:
|
73
|
+
setType = SftpFileSystemConfigBuilder.PROXY_HTTP;
|
74
|
+
break;
|
75
|
+
case SOCKS:
|
76
|
+
setType = SftpFileSystemConfigBuilder.PROXY_SOCKS5;
|
77
|
+
break;
|
78
|
+
case STREAM:
|
79
|
+
setType = SftpFileSystemConfigBuilder.PROXY_STREAM;
|
80
|
+
}
|
81
|
+
builder.setProxyType(fsOptions, setType);
|
82
|
+
return builder;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
@@ -0,0 +1,217 @@
|
|
1
|
+
package org.embulk.input.sftp;
|
2
|
+
|
3
|
+
import com.google.common.base.Function;
|
4
|
+
import com.google.common.base.Throwables;
|
5
|
+
import com.google.common.collect.ImmutableList;
|
6
|
+
import org.apache.commons.io.FilenameUtils;
|
7
|
+
import org.apache.commons.vfs2.FileObject;
|
8
|
+
import org.apache.commons.vfs2.FileSystemException;
|
9
|
+
import org.apache.commons.vfs2.FileSystemOptions;
|
10
|
+
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
|
11
|
+
import org.apache.commons.vfs2.provider.sftp.IdentityInfo;
|
12
|
+
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
|
13
|
+
import org.embulk.config.ConfigException;
|
14
|
+
import org.embulk.config.TaskReport;
|
15
|
+
import org.embulk.spi.Exec;
|
16
|
+
import org.embulk.spi.TransactionalFileInput;
|
17
|
+
import org.embulk.spi.unit.LocalFile;
|
18
|
+
import org.embulk.spi.util.InputStreamFileInput;
|
19
|
+
import org.slf4j.Logger;
|
20
|
+
|
21
|
+
import java.io.File;
|
22
|
+
import java.net.URI;
|
23
|
+
import java.net.URISyntaxException;
|
24
|
+
import java.util.List;
|
25
|
+
|
26
|
+
public class SftpFileInput
|
27
|
+
extends InputStreamFileInput
|
28
|
+
implements TransactionalFileInput
|
29
|
+
{
|
30
|
+
private static final Logger log = Exec.getLogger(SftpFileInput.class);
|
31
|
+
|
32
|
+
public SftpFileInput(PluginTask task, int taskIndex)
|
33
|
+
{
|
34
|
+
super(task.getBufferAllocator(), new SingleFileProvider(task, taskIndex, initializeStandardFileSystemManager()));
|
35
|
+
}
|
36
|
+
|
37
|
+
public void abort()
|
38
|
+
{
|
39
|
+
}
|
40
|
+
|
41
|
+
public TaskReport commit()
|
42
|
+
{
|
43
|
+
return Exec.newTaskReport();
|
44
|
+
}
|
45
|
+
|
46
|
+
@Override
|
47
|
+
public void close()
|
48
|
+
{
|
49
|
+
}
|
50
|
+
|
51
|
+
private static StandardFileSystemManager initializeStandardFileSystemManager()
|
52
|
+
{
|
53
|
+
if (!log.isDebugEnabled()) {
|
54
|
+
// TODO: change logging format: org.apache.commons.logging.Log
|
55
|
+
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
|
56
|
+
}
|
57
|
+
|
58
|
+
StandardFileSystemManager manager = new StandardFileSystemManager();
|
59
|
+
try {
|
60
|
+
manager.init();
|
61
|
+
}
|
62
|
+
catch (FileSystemException ex) {
|
63
|
+
Throwables.propagate(ex);
|
64
|
+
}
|
65
|
+
|
66
|
+
return manager;
|
67
|
+
}
|
68
|
+
|
69
|
+
private static String initializeUserInfo(PluginTask task)
|
70
|
+
{
|
71
|
+
String userInfo = task.getUser();
|
72
|
+
if (task.getPassword().isPresent()) {
|
73
|
+
userInfo += ":" + task.getPassword().get();
|
74
|
+
}
|
75
|
+
return userInfo;
|
76
|
+
}
|
77
|
+
|
78
|
+
public static FileSystemOptions initializeFsOptions(PluginTask task)
|
79
|
+
{
|
80
|
+
FileSystemOptions fsOptions = new FileSystemOptions();
|
81
|
+
|
82
|
+
try {
|
83
|
+
SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
|
84
|
+
builder.setUserDirIsRoot(fsOptions, task.getUserDirIsRoot());
|
85
|
+
builder.setTimeout(fsOptions, task.getSftpConnectionTimeout());
|
86
|
+
builder.setStrictHostKeyChecking(fsOptions, "no");
|
87
|
+
|
88
|
+
if (task.getSecretKeyFile().isPresent()) {
|
89
|
+
IdentityInfo identityInfo = new IdentityInfo(
|
90
|
+
new File((task.getSecretKeyFile().transform(localFileToPathString()).get())),
|
91
|
+
task.getSecretKeyPassphrase().getBytes()
|
92
|
+
);
|
93
|
+
builder.setIdentityInfo(fsOptions, identityInfo);
|
94
|
+
log.info("set identity: {}", task.getSecretKeyFile().get());
|
95
|
+
}
|
96
|
+
|
97
|
+
if (task.getProxy().isPresent()) {
|
98
|
+
ProxyTask proxy = task.getProxy().get();
|
99
|
+
|
100
|
+
ProxyTask.ProxyType.setProxyType(builder, fsOptions, proxy.getType());
|
101
|
+
|
102
|
+
if (proxy.getHost().isPresent()) {
|
103
|
+
builder.setProxyHost(fsOptions, proxy.getHost().get());
|
104
|
+
builder.setProxyPort(fsOptions, proxy.getPort());
|
105
|
+
}
|
106
|
+
|
107
|
+
if (proxy.getUser().isPresent()) {
|
108
|
+
builder.setProxyUser(fsOptions, proxy.getUser().get());
|
109
|
+
}
|
110
|
+
|
111
|
+
if (proxy.getPassword().isPresent()) {
|
112
|
+
builder.setProxyPassword(fsOptions, proxy.getPassword().get());
|
113
|
+
}
|
114
|
+
|
115
|
+
if (proxy.getCommand().isPresent()) {
|
116
|
+
builder.setProxyCommand(fsOptions, proxy.getCommand().get());
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
catch (FileSystemException ex) {
|
121
|
+
Throwables.propagate(ex);
|
122
|
+
}
|
123
|
+
|
124
|
+
return fsOptions;
|
125
|
+
}
|
126
|
+
|
127
|
+
public static String getSftpFileUri(PluginTask task)
|
128
|
+
{
|
129
|
+
try {
|
130
|
+
return new URI("sftp", initializeUserInfo(task), task.getHost(), task.getPort(), task.getPathPrefix(), null, null).toString();
|
131
|
+
}
|
132
|
+
catch (URISyntaxException ex) {
|
133
|
+
throw new ConfigException(ex);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
public static List<String> listFilesByPrefix(PluginTask task)
|
138
|
+
{
|
139
|
+
ImmutableList.Builder<String> builder = ImmutableList.builder();
|
140
|
+
int maxConnectionRetry = task.getMaxConnectionRetry();
|
141
|
+
|
142
|
+
StandardFileSystemManager manager = null;
|
143
|
+
int count = 0;
|
144
|
+
while (true) {
|
145
|
+
try {
|
146
|
+
manager = initializeStandardFileSystemManager();
|
147
|
+
FileObject files = manager.resolveFile(getSftpFileUri(task), initializeFsOptions(task));
|
148
|
+
String basename = FilenameUtils.getBaseName(task.getPathPrefix());
|
149
|
+
|
150
|
+
if (files.isFolder()) {
|
151
|
+
for (FileObject f : files.getChildren()) {
|
152
|
+
if (f.isFile()) {
|
153
|
+
addFileToList(builder, f.toString(), "");
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
else {
|
158
|
+
FileObject parent = files.getParent();
|
159
|
+
for (FileObject f : parent.getChildren()) {
|
160
|
+
if (f.isFile()) {
|
161
|
+
addFileToList(builder, f.toString(), basename);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
}
|
165
|
+
return builder.build();
|
166
|
+
}
|
167
|
+
catch (FileSystemException ex) {
|
168
|
+
if (++count == maxConnectionRetry) {
|
169
|
+
Throwables.propagate(ex);
|
170
|
+
}
|
171
|
+
log.warn("failed to connect sftp server: " + ex.getMessage(), ex);
|
172
|
+
|
173
|
+
try {
|
174
|
+
long sleepTime = ((long) Math.pow(2, count) * 1000);
|
175
|
+
log.warn("sleep in next connection retry: {} milliseconds", sleepTime);
|
176
|
+
Thread.sleep(sleepTime); // milliseconds
|
177
|
+
}
|
178
|
+
catch (InterruptedException ex2) {
|
179
|
+
// Ignore this exception because this exception is just about `sleep`.
|
180
|
+
log.warn(ex2.getMessage(), ex2);
|
181
|
+
}
|
182
|
+
log.warn("retrying to connect sftp server: " + count + " times");
|
183
|
+
}
|
184
|
+
finally {
|
185
|
+
if (manager != null) {
|
186
|
+
manager.close();
|
187
|
+
}
|
188
|
+
}
|
189
|
+
}
|
190
|
+
}
|
191
|
+
|
192
|
+
private static void addFileToList(ImmutableList.Builder<String> builder, String fileName, String basename)
|
193
|
+
{
|
194
|
+
if (!basename.isEmpty()) {
|
195
|
+
String remoteBasename = FilenameUtils.getBaseName(fileName);
|
196
|
+
if (remoteBasename.startsWith(basename)) {
|
197
|
+
builder.add(fileName);
|
198
|
+
log.info("add file to the request list: {}", fileName);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
else {
|
202
|
+
builder.add(fileName);
|
203
|
+
log.info("add file to the request list: {}", fileName);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
private static Function<LocalFile, String> localFileToPathString()
|
208
|
+
{
|
209
|
+
return new Function<LocalFile, String>()
|
210
|
+
{
|
211
|
+
public String apply(LocalFile file)
|
212
|
+
{
|
213
|
+
return file.getPath().toString();
|
214
|
+
}
|
215
|
+
};
|
216
|
+
}
|
217
|
+
}
|