]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshFilesDeployment.java
Improve SSH support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / SshFilesDeployment.java
1 package org.argeo.slc.jsch;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.slc.core.deploy.ResourceSet;
8 import org.springframework.core.io.Resource;
9
10 import com.jcraft.jsch.Session;
11
12 public class SshFilesDeployment extends AbstractJschTask implements Runnable {
13 private String targetBase = "";
14 private ResourceSet resourceSet;
15
16 @Override
17 void run(Session session) {
18 JschMultiTasks multiTasks = new JschMultiTasks();
19
20 List<String> subDirs = new ArrayList<String>();
21 Map<String, Resource> resources = resourceSet.listResources();
22 for (String relPath : resources.keySet()) {
23 // Create dir if necessary
24 String dir;
25 int lastIndexSubDir = relPath.lastIndexOf('/');
26 if (lastIndexSubDir > 0)
27 dir = targetBase + '/' + relPath.substring(0, lastIndexSubDir);
28 else
29 dir = targetBase;
30
31 if (!subDirs.contains(dir)) {
32 subDirs.add(dir);
33 }
34
35 // Copy resource
36 Resource resource = resources.get(relPath);
37 ScpTo scpTo = new ScpTo();
38 scpTo.setLocalResource(resource);
39 scpTo.setRemotePath(targetBase + "/" + relPath);
40 multiTasks.getTasks().add(scpTo);
41
42 // TODO: set permissions
43 }
44
45 RemoteExec remoteExec = new RemoteExec();
46 for (String dir : subDirs) {
47 remoteExec.getCommands().add("mkdir -p " + dir);
48 }
49 multiTasks.getTasks().add(0, remoteExec);
50
51 multiTasks.setSshTarget(getSshTarget());
52 multiTasks.run(session);
53 }
54
55 public void setTargetBase(String targetBase) {
56 this.targetBase = targetBase;
57 }
58
59 public void setResourceSet(ResourceSet resourceSet) {
60 this.resourceSet = resourceSet;
61 }
62 }