|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.brooklyn.util.core.internal.winrm; |
| 20 | + |
| 21 | +import com.google.common.base.Joiner; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import org.apache.brooklyn.config.ConfigKey; |
| 24 | +import org.apache.brooklyn.util.collections.MutableMap; |
| 25 | +import org.apache.brooklyn.util.core.internal.ssh.ShellTool; |
| 26 | +import org.apache.brooklyn.util.os.Os; |
| 27 | +import org.apache.brooklyn.util.text.Identifiers; |
| 28 | +import org.apache.brooklyn.util.text.Strings; |
| 29 | +import org.apache.brooklyn.util.time.Time; |
| 30 | + |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.apache.brooklyn.core.config.ConfigKeys.newConfigKeyWithDefault; |
| 36 | +import static org.apache.brooklyn.util.core.internal.ssh.ShellAbstractTool.getOptionalVal; |
| 37 | + |
| 38 | +/** |
| 39 | + * Implemented like SshCliTool |
| 40 | + */ |
| 41 | +// TODO Use a general interface for SshTool and WinRmScriptTool with |
| 42 | +public class WinRmScriptTool { |
| 43 | + public static final ConfigKey<String> PROP_SCRIPT_DIR = newConfigKeyWithDefault(ShellTool.PROP_SCRIPT_DIR, "The directory where the script should be uploaded. It is an env variable.", "temp"); |
| 44 | + public static final ConfigKey<String> PROP_SUMMARY = ShellTool.PROP_SUMMARY; |
| 45 | + |
| 46 | + private static final String SCRIPT_TEMP_DIR_VARIABLE = "BROOKLYN_TEMP_SCRIPT_DIR"; |
| 47 | + |
| 48 | + private String scriptNameWithoutExtension; |
| 49 | + private String scriptDir; |
| 50 | + private String summary; |
| 51 | + private NaiveWindowsScriptRunner scriptRunner; |
| 52 | + |
| 53 | + public WinRmScriptTool(Map<String, ?> props, NaiveWindowsScriptRunner scriptRunner) { |
| 54 | + this.scriptDir = getOptionalVal(props, PROP_SCRIPT_DIR); |
| 55 | + |
| 56 | + String summary = getOptionalVal(props, PROP_SUMMARY); |
| 57 | + if (summary != null) { |
| 58 | + summary = Strings.makeValidFilename(summary); |
| 59 | + if (summary.length() > 30) |
| 60 | + summary = summary.substring(0, 30); |
| 61 | + } |
| 62 | + |
| 63 | + this.summary = summary; |
| 64 | + this.scriptNameWithoutExtension = "brooklyn-" + |
| 65 | + Time.makeDateStampString() + "-" + Identifiers.makeRandomId(4) + |
| 66 | + (Strings.isBlank(summary) ? "" : "-" + summary); |
| 67 | + |
| 68 | + this.scriptRunner = scriptRunner; |
| 69 | + } |
| 70 | + |
| 71 | + public static String psStringExpressionToBatchVariable(String psString, String batchVariable) { |
| 72 | + return "for /f \"delims=\" %%i in ('powershell -noprofile \\'Write-Host \""+ psString +"\"\\') do @set "+batchVariable+ "=%i"; |
| 73 | + } |
| 74 | + |
| 75 | + private String psExtension(String filename) { |
| 76 | + return filename + ".ps1"; |
| 77 | + } |
| 78 | + |
| 79 | + private String batchExtension(String filename) { |
| 80 | + return filename + ".bat"; |
| 81 | + } |
| 82 | + |
| 83 | + public int execPsScript(List<String> psCommands) { |
| 84 | + return execPsScript(ImmutableMap.<String, Object>of(), psCommands); |
| 85 | + } |
| 86 | + |
| 87 | + public int execPsScript(Map<String, Object> flags, List<String> commands) { |
| 88 | + byte[] scriptBytes = toScript(commands).getBytes(); |
| 89 | + String scriptFilename = psExtension(scriptNameWithoutExtension); |
| 90 | + if (flags.containsKey("scriptFilename")) |
| 91 | + flags.put("scriptFilename", scriptFilename); |
| 92 | + String scriptPath = Os.mergePathsWin("$env:"+scriptDir, scriptFilename); |
| 93 | + scriptRunner.copyTo(new ByteArrayInputStream(scriptBytes), scriptPath); |
| 94 | + return scriptRunner.executeNativeOrPsCommand(MutableMap.of(), null, "& " + scriptPath, summary, false); |
| 95 | + } |
| 96 | + |
| 97 | + public int execNativeScript(List<String> commands) { |
| 98 | + return execNativeScript(ImmutableMap.<String, Object>of(), commands); |
| 99 | + } |
| 100 | + |
| 101 | + public int execNativeScript(Map<String, Object> flags, List<String> commands) { |
| 102 | + byte[] scriptBytes = toScript(commands).getBytes(); |
| 103 | + String scriptFilename = batchExtension(scriptNameWithoutExtension); |
| 104 | + if (flags.containsKey("scriptFilename")) |
| 105 | + flags.put("scriptFilename", scriptFilename); |
| 106 | + scriptRunner.copyTo(new ByteArrayInputStream(scriptBytes), Os.mergePathsWin("$env:"+scriptDir, scriptFilename)); |
| 107 | + String scriptPath = Os.mergePathsWin("%"+scriptDir+"%", scriptFilename); |
| 108 | + return scriptRunner.executeNativeOrPsCommand(MutableMap.of(), scriptPath, null, summary, false); |
| 109 | + } |
| 110 | + |
| 111 | + private String toScript(List<String> commands) { |
| 112 | + return Joiner.on("\r\n").join(commands); |
| 113 | + } |
| 114 | +} |
0 commit comments