2929import java .util .concurrent .Callable ;
3030import java .util .concurrent .TimeUnit ;
3131
32+ import com .google .common .collect .ImmutableList ;
3233import org .apache .brooklyn .api .entity .EntityLocal ;
3334import org .apache .brooklyn .api .mgmt .Task ;
3435import org .apache .brooklyn .api .sensor .AttributeSensor ;
3536import org .apache .brooklyn .core .entity .BrooklynConfigKeys ;
3637import org .apache .brooklyn .core .entity .Entities ;
3738import org .apache .brooklyn .core .mgmt .BrooklynTaskTags ;
3839import org .apache .brooklyn .core .sensor .Sensors ;
39- import org .apache .brooklyn .entity .software .base .lifecycle .NativeWindowsScriptRunner ;
4040import org .apache .brooklyn .entity .software .base .lifecycle .WinRmExecuteHelper ;
41+ import org .apache .brooklyn .location .winrm .NaiveWindowsScriptRunner ;
4142import org .apache .brooklyn .location .winrm .WinRmMachineLocation ;
43+ import org .apache .brooklyn .util .collections .MutableMap ;
44+ import org .apache .brooklyn .util .core .internal .winrm .WinRmScriptTool ;
4245import org .apache .brooklyn .util .core .internal .winrm .WinRmTool ;
4346import org .apache .brooklyn .util .core .internal .winrm .WinRmToolResponse ;
4447import org .apache .brooklyn .util .core .task .Tasks ;
5154import org .slf4j .Logger ;
5255import org .slf4j .LoggerFactory ;
5356
54- import com .google .common .collect .ImmutableList ;
5557import com .google .common .collect .ImmutableMap ;
5658import com .google .common .collect .Maps ;
5759
58- public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwareProcessDriver implements NativeWindowsScriptRunner {
60+ public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwareProcessDriver implements NaiveWindowsScriptRunner {
5961 private static final Logger LOG = LoggerFactory .getLogger (AbstractSoftwareProcessWinRmDriver .class );
6062
6163 AttributeSensor <String > WINDOWS_USERNAME = Sensors .newStringSensor ("windows.username" ,
@@ -175,7 +177,7 @@ public int copyResource(Map<Object, Object> sshFlags, String source, String targ
175177 if (createParentDir ) {
176178 createDirectory (getDirectory (target ), "Creating resource directory" );
177179 }
178-
180+
179181 InputStream stream = null ;
180182 try {
181183 Tasks .setBlockingDetails ("retrieving resource " +source +" for copying across" );
@@ -204,15 +206,15 @@ protected void createDirectory(String directoryName, String summaryForLogging) {
204206 }
205207
206208 @ Override
207- public Integer executeNativeOrPsCommand (Map flags , String regularCommand , String powerShellCommand , String phase , Boolean allowNoOp ) {
209+ public Integer executeNativeOrPsCommand (Map flags , String regularCommand , final String powerShellCommand , String summaryForLogging , Boolean allowNoOp ) {
208210 if (Strings .isBlank (regularCommand ) && Strings .isBlank (powerShellCommand )) {
209211 if (allowNoOp ) {
210212 return new WinRmToolResponse ("" , "" , 0 ).getStatusCode ();
211213 } else {
212- throw new IllegalStateException (String .format ("Exactly one of cmd or psCmd must be set for %s of %s" , phase , entity ));
214+ throw new IllegalStateException (String .format ("Exactly one of cmd or psCmd must be set for %s of %s" , summaryForLogging , entity ));
213215 }
214216 } else if (!Strings .isBlank (regularCommand ) && !Strings .isBlank (powerShellCommand )) {
215- throw new IllegalStateException (String .format ("%s and %s cannot both be set for %s of %s" , regularCommand , powerShellCommand , phase , entity ));
217+ throw new IllegalStateException (String .format ("%s and %s cannot both be set for %s of %s" , regularCommand , powerShellCommand , summaryForLogging , entity ));
216218 }
217219
218220 ByteArrayOutputStream stdIn = new ByteArrayOutputStream ();
@@ -236,7 +238,7 @@ public Integer executeNativeOrPsCommand(Map flags, String regularCommand, String
236238
237239 WinRmToolResponse response ;
238240 if (Strings .isBlank (regularCommand )) {
239- response = getLocation ().executePsScript (ImmutableList . of ( powerShellCommand ) );
241+ response = getLocation ().executePsScript (powerShellCommand );
240242 } else {
241243 response = getLocation ().executeCommand (ImmutableList .of (regularCommand ));
242244 }
@@ -249,6 +251,24 @@ public Integer executeNativeOrPsCommand(Map flags, String regularCommand, String
249251 return response .getStatusCode ();
250252 }
251253
254+ public int executeNativeOrPsScript (Map flags , List <String > regularCommands , final List <String > powerShellCommands , String summaryForLogging , Boolean allowNoOp ) {
255+ if ((regularCommands == null || regularCommands .size () == 0 ) && (powerShellCommands == null || powerShellCommands .size () == 0 )) {
256+ if (allowNoOp ) {
257+ return new WinRmToolResponse ("" , "" , 0 ).getStatusCode ();
258+ } else {
259+ throw new IllegalStateException (String .format ("Exactly one of cmd or psCmd must be set for %s" , summaryForLogging ));
260+ }
261+ }
262+
263+ MutableMap <String , Object > scriptProps = MutableMap .<String , Object >of (WinRmScriptTool .PROP_SUMMARY .getName (), summaryForLogging );
264+ WinRmScriptTool scriptTool = new WinRmScriptTool (scriptProps , this );
265+ if (regularCommands == null || regularCommands .size () == 0 ) {
266+ return scriptTool .execPsScript (flags , powerShellCommands );
267+ } else {
268+ return scriptTool .execNativeScript (flags , regularCommands );
269+ }
270+ }
271+
252272 private void writeToStream (ByteArrayOutputStream stream , String string ) {
253273 try {
254274 stream .write (string .getBytes ());
@@ -257,18 +277,30 @@ private void writeToStream(ByteArrayOutputStream stream, String string) {
257277 }
258278 }
259279
260- public int execute (List < String > script ) {
280+ public int execute (String script ) {
261281 return getLocation ().executeCommand (script ).getStatusCode ();
262282 }
263283
264- public int executePsScriptNoRetry (List <String > psScript ) {
265- return getLocation ().executePsScript (ImmutableMap .of (WinRmTool .PROP_EXEC_TRIES , 1 ), psScript ).getStatusCode ();
284+ public int executePsCommandNoRetry (String psCommand ) {
285+ return getLocation ().executePsScript (ImmutableMap .of (WinRmTool .PROP_EXEC_TRIES , 1 ), ImmutableList .of (psCommand )).getStatusCode ();
286+ }
287+
288+ public int executeCommandNoRetry (String command ) {
289+ return getLocation ().executeCommand (ImmutableMap .of (WinRmTool .PROP_EXEC_TRIES , 1 ), ImmutableList .of (command )).getStatusCode ();
266290 }
267291
268292 public int executePsScript (List <String > psScript ) {
269293 return getLocation ().executePsScript (psScript ).getStatusCode ();
270294 }
271295
296+ public WinRmToolResponse executeCommand (String cmdCommand ) {
297+ return getLocation ().executeCommand (cmdCommand );
298+ }
299+
300+ public WinRmToolResponse executePsScript (String psCommand ) {
301+ return getLocation ().executePsScript (psCommand );
302+ }
303+
272304 public int copyTo (File source , String destination ) {
273305 return getLocation ().copyTo (source , destination );
274306 }
@@ -278,12 +310,7 @@ public int copyTo(InputStream source, String destination) {
278310 }
279311
280312 public void rebootAndWait () {
281- try {
282- executePsScriptNoRetry (ImmutableList .of ("Restart-Computer -Force" ));
283- } catch (Exception e ) {
284- // Restarting the computer will cause the command to fail; ignore the exception and continue
285- Exceptions .propagateIfFatal (e );
286- }
313+ executePsCommandNoRetry ("Restart-Computer -Force" );
287314 waitForWinRmStatus (false , entity .getConfig (VanillaWindowsProcess .REBOOT_BEGUN_TIMEOUT ));
288315 waitForWinRmStatus (true , entity .getConfig (VanillaWindowsProcess .REBOOT_COMPLETED_TIMEOUT )).getWithError ();
289316 }
@@ -298,7 +325,7 @@ private ReferenceWithError<Boolean> waitForWinRmStatus(final boolean requiredSta
298325 @ Override
299326 public Boolean call () throws Exception {
300327 try {
301- return (execute (ImmutableList . of ( "hostname" ) ) == 0 ) == requiredStatus ;
328+ return (execute ("hostname" ) == 0 ) == requiredStatus ;
302329 } catch (Exception e ) {
303330 return !requiredStatus ;
304331 }
0 commit comments