Skip to content

Commit 6e2f64b

Browse files
committed
- Use sftp with one zipped file for up and download
- Simplify some remote command executions
1 parent cbe8f87 commit 6e2f64b

File tree

4 files changed

+119
-641
lines changed

4 files changed

+119
-641
lines changed

src/main/java/org/moe/gradle/remote/AbstractServerSCPTask.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/main/java/org/moe/gradle/remote/Server.java

Lines changed: 17 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.moe.gradle.remote;
1818

19-
import com.jcraft.jsch.ChannelExec;
2019
import com.jcraft.jsch.JSch;
2120
import com.jcraft.jsch.JSchException;
2221
import com.jcraft.jsch.Session;
@@ -35,7 +34,6 @@
3534
import org.moe.gradle.remote.file.FileList;
3635
import org.moe.gradle.utils.Require;
3736

38-
import java.io.ByteArrayOutputStream;
3937
import java.io.File;
4038
import java.io.FileInputStream;
4139
import java.io.IOException;
@@ -211,45 +209,14 @@ public void connect() {
211209

212210
private void prepareServerMOE() {
213211
final MoeSDK sdk = plugin.getSDK();
214-
215-
final File gradlewZip = sdk.getGradlewZip();
216-
final FileList list = new FileList(gradlewZip.getParentFile(), getBuildDir());
217-
final String remoteGradlewZip = list.add(gradlewZip);
218-
upload("prepare - gradlew", list);
219-
220-
final String output = exec("install MOE SDK", "" +
221-
"cd " + getBuildDir().getPath() + " && " +
222-
223-
"unzip " + remoteGradlewZip + " && " +
224-
225-
"cd gradlew && " +
226-
227-
"echo 'distributionBase=GRADLE_USER_HOME' >> gradle/wrapper/gradle-wrapper.properties && " +
228-
"echo 'distributionPath=wrapper/dists' >> gradle/wrapper/gradle-wrapper.properties && " +
229-
"echo 'zipStoreBase=GRADLE_USER_HOME' >> gradle/wrapper/gradle-wrapper.properties && " +
230-
"echo 'zipStorePath=wrapper/dists' >> gradle/wrapper/gradle-wrapper.properties && " +
231-
"echo 'distributionUrl=https\\://services.gradle.org/distributions/gradle-" + plugin.getRequiredGradleVersion() + "-bin.zip' >> gradle/wrapper/gradle-wrapper.properties && " +
232-
233-
"echo 'buildscript {' >> build.gradle && " +
234-
"echo ' repositories {' >> build.gradle && " +
235-
"echo ' " + settings.getGradleRepositories() + "' >> build.gradle && " +
236-
"echo ' }' >> build.gradle && " +
237-
"echo ' dependencies {' >> build.gradle && " +
238-
"echo ' classpath group: \"org.multi-os-engine\", name: \"moe-gradle\", version: \"" + sdk.pluginVersion + "\"' >> build.gradle && " +
239-
"echo ' }' >> build.gradle && " +
240-
"echo '}' >> build.gradle && " +
241-
"echo '' >> build.gradle && " +
242-
"echo 'apply plugin: \"moe-sdk\"' >> build.gradle && " +
243-
"echo 'task printSDKRoot << { print \"" + SDK_ROOT_MARK + ":${moe.sdk.root}\" }' >> build.gradle && " +
244-
245-
"./gradlew printSDKRoot -s && " +
246-
"cd .. && rm -rf gradlew && rm -f gradlew.zip"
247-
);
248-
final int start = output.indexOf(SDK_ROOT_MARK);
249-
Require.NE(start, -1, "SDK_ROOT_MARK not found");
250-
final int start2 = start + SDK_ROOT_MARK.length() + 1;
251212
try {
252-
sdkDir = new URI("file://" + output.substring(start2, output.indexOf('\n', start2)));
213+
final FileList list = new FileList(sdk.getRoot().getParentFile(), new URI("file://" + getUserHome() + "/").resolve(".moe-remote"));
214+
final String remoteGradlewZip = list.add(sdk.getRoot());
215+
upload("upload sdk", list);
216+
// Since zip's can't hold executable info, we need to apply it afterwards. Maybe we can be more selective if we want
217+
// Or do it in ServerFileUploader, just making the files executable that also are locally
218+
exec("make executable", "chmod -R +x " + list.getTarget().getPath());
219+
sdkDir = new URI("file://" + remoteGradlewZip);
253220
} catch (URISyntaxException e) {
254221
throw new GradleException(e.getMessage(), e);
255222
}
@@ -258,77 +225,25 @@ private void prepareServerMOE() {
258225
}
259226

260227
private void setupUserHome() {
261-
final ChannelExec channel;
262-
try {
263-
channel = (ChannelExec) session.openChannel("exec");
264-
} catch (JSchException e) {
265-
throw new GradleException(e.getMessage(), e);
266-
}
267-
268-
channel.setCommand("echo $HOME");
269-
270-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
271-
channel.setOutputStream(baos);
272-
273-
try {
274-
channel.connect();
275-
} catch (JSchException e) {
276-
throw new GradleException(e.getMessage(), e);
277-
}
278-
279-
while (!channel.isClosed()) {
280-
try {
281-
Thread.sleep(100);
282-
} catch (InterruptedException e) {
283-
throw new GradleException(e.getMessage(), e);
284-
}
285-
}
286-
287-
channel.disconnect();
288-
if (channel.getExitStatus() != 0) {
289-
throw new GradleException("Failed to initialize connection with server");
290-
}
291-
userHome = baos.toString().trim();
228+
ServerCommandRunner runner = new ServerCommandRunner(this, "get home", "echo $HOME");
229+
runner.run();
230+
userHome = runner.getOutput().trim();
292231
LOG.quiet("MOE Remote Build - REMOTE_HOME=" + getUserHome());
293232
}
294233

295234
private void setupBuildDir() {
296-
final ChannelExec channel;
297-
try {
298-
channel = (ChannelExec) session.openChannel("exec");
299-
} catch (JSchException e) {
300-
throw new GradleException(e.getMessage(), e);
301-
}
302-
303-
channel.setCommand("mktemp -d");
304-
305-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
306-
channel.setOutputStream(baos);
307-
308-
try {
309-
channel.connect();
310-
} catch (JSchException e) {
311-
throw new GradleException(e.getMessage(), e);
312-
}
313-
314-
while (!channel.isClosed()) {
315-
try {
316-
Thread.sleep(100);
317-
} catch (InterruptedException e) {
318-
throw new GradleException(e.getMessage(), e);
319-
}
320-
}
235+
buildDir = getTempDir();
236+
LOG.quiet("MOE Remote Build - REMOTE_BUILD_DIR=" + buildDir.getPath());
237+
}
321238

322-
channel.disconnect();
323-
if (channel.getExitStatus() != 0) {
324-
throw new GradleException("Failed to initialize connection with server");
325-
}
239+
public URI getTempDir() {
240+
ServerCommandRunner runner = new ServerCommandRunner(this, "temp dir", "mktemp -d");
241+
runner.run();
326242
try {
327-
buildDir = new URI("file://" + baos.toString().trim());
243+
return new URI("file://" + runner.getOutput().trim());
328244
} catch (URISyntaxException e) {
329245
throw new GradleException(e.getMessage(), e);
330246
}
331-
LOG.quiet("MOE Remote Build - REMOTE_BUILD_DIR=" + buildDir.getPath());
332247
}
333248

334249
private void disconnect() {

0 commit comments

Comments
 (0)