brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [08/27] incubator-brooklyn git commit: Adds copyTo functions to WinRmMachineLocation
Date Fri, 29 May 2015 17:22:01 GMT
Adds copyTo functions to WinRmMachineLocation


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/ce2a7c2e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/ce2a7c2e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/ce2a7c2e

Branch: refs/heads/master
Commit: ce2a7c2e75fef66b532359e3e4e7b27cef76253e
Parents: fb18f53
Author: Martin Harris <github@nakomis.com>
Authored: Thu Apr 9 14:21:18 2015 +0100
Committer: Richard Downer <richard@apache.org>
Committed: Thu May 28 17:27:34 2015 +0100

----------------------------------------------------------------------
 .../location/basic/WinRmMachineLocation.java    | 43 +++++++++++++++++---
 .../AbstractSoftwareProcessWinRmDriver.java     | 10 +++++
 2 files changed, 48 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ce2a7c2e/core/src/main/java/brooklyn/location/basic/WinRmMachineLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/WinRmMachineLocation.java b/core/src/main/java/brooklyn/location/basic/WinRmMachineLocation.java
index 0c53f15..54e3829 100644
--- a/core/src/main/java/brooklyn/location/basic/WinRmMachineLocation.java
+++ b/core/src/main/java/brooklyn/location/basic/WinRmMachineLocation.java
@@ -18,22 +18,29 @@
  */
 package brooklyn.location.basic;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.net.InetAddress;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
 import javax.annotation.Nullable;
 
+import org.apache.commons.codec.binary.Base64;
+
 import brooklyn.config.ConfigKey;
 import brooklyn.entity.basic.ConfigKeys;
 import brooklyn.location.MachineDetails;
 import brooklyn.location.MachineLocation;
 import brooklyn.location.OsDetails;
+import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.flags.SetFromFlag;
 import io.cloudsoft.winrm4j.winrm.WinRmTool;
 import io.cloudsoft.winrm4j.winrm.WinRmToolResponse;
 
-import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 
 public class WinRmMachineLocation extends AbstractLocation implements MachineLocation {
@@ -44,6 +51,9 @@ public class WinRmMachineLocation extends AbstractLocation implements MachineLoc
     public static final ConfigKey<String> WINDOWS_PASSWORD = ConfigKeys.newStringConfigKey("windows.password",
             "Password to use when connecting to the remote machine");
 
+    public static final ConfigKey<Integer> COPY_FILE_CHUNK_SIZE_BYTES = ConfigKeys.newIntegerConfigKey("windows.copy.file.size.bytes",
+            "Size of file chunks (in bytes) to be used when copying a file to the remote
server", 1024);
+
     @SetFromFlag
     protected String user;
 
@@ -88,15 +98,38 @@ public class WinRmMachineLocation extends AbstractLocation implements
MachineLoc
     }
 
     public int executePsScript(List<String> psScript) {
-        // FIXME: Remove this!
-        System.out.println("Host: " + getHostname());
-        System.out.println("User: " + getUsername());
-        System.out.println("Password: " + getPassword());
         WinRmTool winRmTool = WinRmTool.connect(getHostname(), getUsername(), getPassword());
         WinRmToolResponse response = winRmTool.executePs(psScript);
         return response.getStatusCode();
     }
 
+    public int copyTo(File source, File destination) {
+        try {
+            return copyTo(new FileInputStream(source), destination);
+        } catch (FileNotFoundException e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+
+    public int copyTo(InputStream source, File destination) {
+        executePsScript(ImmutableList.of("rm -ErrorAction SilentlyContinue " + destination.getPath()));
+        try {
+            byte[] inputData = new byte[getConfig(COPY_FILE_CHUNK_SIZE_BYTES)];
+            int bytesRead = source.read(inputData);
+            while (bytesRead > 0) {
+                byte[] chunk = Arrays.copyOf(inputData, bytesRead);
+                String encoded = new String(Base64.encodeBase64(chunk));
+                executePsScript(ImmutableList.of("Add-Content -Encoding Byte -path " + destination.getPath()
+
+                        " -value ([System.Convert]::FromBase64String(\"" + encoded + "\"))"));
+                bytesRead = source.read(inputData);
+            }
+
+            return 0;
+        } catch (java.io.IOException e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+
     public String getUsername() {
         return config().get(WINDOWS_USERNAME);
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ce2a7c2e/software/base/src/main/java/brooklyn/entity/basic/AbstractSoftwareProcessWinRmDriver.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/basic/AbstractSoftwareProcessWinRmDriver.java
b/software/base/src/main/java/brooklyn/entity/basic/AbstractSoftwareProcessWinRmDriver.java
index 8d65bec..b53685b 100644
--- a/software/base/src/main/java/brooklyn/entity/basic/AbstractSoftwareProcessWinRmDriver.java
+++ b/software/base/src/main/java/brooklyn/entity/basic/AbstractSoftwareProcessWinRmDriver.java
@@ -18,6 +18,8 @@
  */
 package brooklyn.entity.basic;
 
+import java.io.File;
+import java.io.InputStream;
 import java.util.List;
 
 import brooklyn.location.basic.WinRmMachineLocation;
@@ -40,4 +42,12 @@ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwar
         return getMachine().executePsScript(psScript);
     }
 
+    public int copyTo(File source, File destination) {
+        return getMachine().copyTo(source, destination);
+    }
+
+    public int copyTo(InputStream source, File destination) {
+        return getMachine().copyTo(source, destination);
+    }
+
 }


Mime
View raw message