|
22 | 22 |
|
23 | 23 | import java.io.Closeable;
|
24 | 24 | import java.io.File;
|
| 25 | +import java.net.InetAddress; |
25 | 26 | import java.util.ArrayList;
|
26 | 27 | import java.util.Collection;
|
27 | 28 | import java.util.List;
|
28 | 29 | import java.util.Map;
|
29 | 30 | import java.util.Set;
|
30 | 31 |
|
| 32 | +import com.google.common.net.HostAndPort; |
31 | 33 | import org.apache.brooklyn.api.location.Location;
|
32 | 34 | import org.apache.brooklyn.api.location.LocationSpec;
|
33 | 35 | import org.apache.brooklyn.api.location.MachineLocation;
|
|
45 | 47 | import org.apache.brooklyn.util.collections.MutableSet;
|
46 | 48 | import org.apache.brooklyn.util.core.config.ConfigBag;
|
47 | 49 | import org.apache.brooklyn.util.core.flags.SetFromFlag;
|
| 50 | +import org.apache.brooklyn.util.net.UserAndHostAndPort; |
48 | 51 | import org.apache.brooklyn.util.stream.Streams;
|
49 | 52 | import org.apache.brooklyn.util.text.WildcardGlobs;
|
50 | 53 | import org.apache.brooklyn.util.text.WildcardGlobs.PhraseTreatment;
|
@@ -317,7 +320,7 @@ public T obtain(Map<?,?> flags) throws NoMachinesAvailableException {
|
317 | 320 | T desiredMachine = (T) flags.get("desiredMachine");
|
318 | 321 | ConfigBag allflags = ConfigBag.newInstanceExtending(config().getBag()).putAll(flags);
|
319 | 322 | Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = allflags.get(MACHINE_CHOOSER);
|
320 |
| - |
| 323 | + |
321 | 324 | synchronized (lock) {
|
322 | 325 | Set<T> a = getAvailable();
|
323 | 326 | if (a.isEmpty()) {
|
@@ -381,13 +384,84 @@ protected void updateMachineConfig(T machine, Map<?, ?> flags) {
|
381 | 384 | // For backwards compatibility, where peristed state did not have this.
|
382 | 385 | origConfigs = Maps.newLinkedHashMap();
|
383 | 386 | }
|
| 387 | + parseMachineConfig((AbstractLocation) machine); |
384 | 388 | Map<String, Object> strFlags = ConfigBag.newInstance(flags).getAllConfig();
|
385 | 389 | Map<String, Object> origConfig = ((ConfigurationSupportInternal)machine.config()).getLocalBag().getAllConfig();
|
386 | 390 | origConfigs.put(machine, origConfig);
|
387 | 391 | requestPersist();
|
388 | 392 |
|
389 | 393 | ((ConfigurationSupportInternal)machine.config()).putAll(strFlags);
|
390 | 394 | }
|
| 395 | + |
| 396 | + private void parseMachineConfig(AbstractLocation machine) { |
| 397 | + String ssh = machine.config().get(ConfigKeys.newStringConfigKey("ssh")); |
| 398 | + machine.config().removeKey(ConfigKeys.newStringConfigKey("ssh")); |
| 399 | + String winrm = machine.config().get(ConfigKeys.newStringConfigKey("winrm")); |
| 400 | + machine.config().removeKey(ConfigKeys.newStringConfigKey("winrm")); |
| 401 | + |
| 402 | + Map<Integer, String> tcpPortMappings = (Map<Integer, String>) machine.config().get(ConfigKeys.newConfigKey(Map.class, "tcpPortMappings")); |
| 403 | + |
| 404 | + if (ssh == null && winrm == null) { |
| 405 | + throw new IllegalArgumentException("Must specify exactly one of 'ssh' or 'winrm' for machine: "+machine); |
| 406 | + } |
| 407 | + |
| 408 | + UserAndHostAndPort userAndHostAndPort; |
| 409 | + String host; |
| 410 | + int port; |
| 411 | + if (ssh != null) { |
| 412 | + userAndHostAndPort = parseUserAndHostAndPort(ssh, 22); |
| 413 | + } else { |
| 414 | + // TODO set to null and rely on the MachineLocation. If not then make a dependency to WinRmMachineLocation and use its config key name. |
| 415 | + Boolean useHttps = machine.config().get(ConfigKeys.newConfigKey(Boolean.class,"winrm.useHttps")); |
| 416 | + userAndHostAndPort = parseUserAndHostAndPort(winrm, useHttps != null && useHttps ? 5986 : 5985); |
| 417 | + } |
| 418 | + |
| 419 | + // If there is a tcpPortMapping defined for the connection-port, then use that for ssh/winrm machine |
| 420 | + port = userAndHostAndPort.getHostAndPort().getPort(); |
| 421 | + if (tcpPortMappings != null && tcpPortMappings.containsKey(port)) { |
| 422 | + String override = tcpPortMappings.get(port); |
| 423 | + HostAndPort hostAndPortOverride = HostAndPort.fromString(override); |
| 424 | + if (!hostAndPortOverride.hasPort()) { |
| 425 | + throw new IllegalArgumentException("Invalid portMapping ('"+override+"') for port "+port+" in machine configuration "+machine.config()); |
| 426 | + } |
| 427 | + port = hostAndPortOverride.getPort(); |
| 428 | + host = hostAndPortOverride.getHostText().trim(); |
| 429 | + } else { |
| 430 | + host = userAndHostAndPort.getHostAndPort().getHostText().trim(); |
| 431 | + } |
| 432 | + |
| 433 | + machine.config().set(ConfigKeys.newStringConfigKey("address"), host); |
| 434 | + try { |
| 435 | + InetAddress.getByName(host); |
| 436 | + } catch (Exception e) { |
| 437 | + throw new IllegalArgumentException("Invalid host '"+host+"' specified in '"+machine+"': "+e); |
| 438 | + } |
| 439 | + |
| 440 | + if (userAndHostAndPort.getUser() != null) { |
| 441 | + machine.config().set(ConfigKeys.newStringConfigKey("user"), userAndHostAndPort.getUser()); |
| 442 | + } |
| 443 | + if (userAndHostAndPort.getHostAndPort().hasPort()) { |
| 444 | + machine.config().set(ConfigKeys.newConfigKey(Integer.class,"port"), port); |
| 445 | + } |
| 446 | + } |
| 447 | + |
| 448 | + private UserAndHostAndPort parseUserAndHostAndPort(String val) { |
| 449 | + String userPart = null; |
| 450 | + String hostPart = val; |
| 451 | + if (val.contains("@")) { |
| 452 | + userPart = val.substring(0, val.indexOf("@")); |
| 453 | + hostPart = val.substring(val.indexOf("@")+1); |
| 454 | + } |
| 455 | + return UserAndHostAndPort.fromParts(userPart, HostAndPort.fromString(hostPart)); |
| 456 | + } |
| 457 | + |
| 458 | + private UserAndHostAndPort parseUserAndHostAndPort(String val, int defaultPort) { |
| 459 | + UserAndHostAndPort result = parseUserAndHostAndPort(val); |
| 460 | + if (!result.getHostAndPort().hasPort()) { |
| 461 | + result = UserAndHostAndPort.fromParts(result.getUser(), result.getHostAndPort().getHostText(), defaultPort); |
| 462 | + } |
| 463 | + return result; |
| 464 | + } |
391 | 465 |
|
392 | 466 | protected void restoreMachineConfig(MachineLocation machine) {
|
393 | 467 | if (origConfigs == null) {
|
|
0 commit comments