Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External ip config for winrm byon location #690

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
import org.apache.brooklyn.entity.stock.BasicApplication;
import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,6 +42,8 @@ protected LocalManagementContext newTestManagementContext() {
BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
props.put("brooklyn.external.inPlaceSupplier1", "org.apache.brooklyn.core.config.external.InPlaceExternalConfigSupplier");
props.put("brooklyn.external.inPlaceSupplier1.byonPassword", "passw0rd");
props.put("brooklyn.external.inPlaceSupplier1.byonUser", "admin");
props.put("brooklyn.external.inPlaceSupplier1.ip", "127.0.0.1");

return LocalManagementContextForTests.builder(true)
.useProperties(props)
Expand All @@ -49,13 +52,13 @@ protected LocalManagementContext newTestManagementContext() {
}

@Test()
public void testWindowsMachinesPasswordExternalProvider() throws Exception {
public void testWindowsMachinesExternalProvider() throws Exception {
RecordingWinRmTool.constructorProps.clear();
final String yaml = Joiner.on("\n").join("location:",
" byon:",
" hosts:",
" - winrm: 127.0.0.1",
" user: admin",
" - winrm: $brooklyn:external(\"inPlaceSupplier1\", \"ip\")",
" user: $brooklyn:external(\"inPlaceSupplier1\", \"byonUser\")",
" brooklyn.winrm.config.winrmToolClass: org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool",
" password: $brooklyn:external(\"inPlaceSupplier1\", \"byonPassword\")",
" osFamily: windows",
Expand All @@ -67,17 +70,19 @@ public void testWindowsMachinesPasswordExternalProvider() throws Exception {

BasicApplication app = (BasicApplication) createAndStartApplication(yaml);
waitForApplicationTasks(app);
assertEquals(RecordingWinRmTool.constructorProps.get(0).get("host"), "127.0.0.1");
assertEquals(RecordingWinRmTool.constructorProps.get(0).get(WinRmMachineLocation.USER.getName()), "admin");
assertEquals(RecordingWinRmTool.constructorProps.get(0).get(WinRmMachineLocation.PASSWORD.getName()), "passw0rd");
}

@Test()
public void testWindowsMachinesNoPasswordExternalProvider() throws Exception {
public void testWindowsMachinesNoExternalProvider() throws Exception {
RecordingWinRmTool.constructorProps.clear();
final String yaml = Joiner.on("\n").join("location:",
" byon:",
" hosts:",
" - winrm: 127.0.0.1",
" user: admin",
" user: $brooklyn:external(\"inPlaceSupplier1\", \"byonUserEmpty\")",
" brooklyn.winrm.config.winrmToolClass: org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool",
" password: $brooklyn:external(\"inPlaceSupplier1\", \"byonPasswordddd\")",
" osFamily: windows",
Expand All @@ -89,9 +94,36 @@ public void testWindowsMachinesNoPasswordExternalProvider() throws Exception {

BasicApplication app = (BasicApplication) createAndStartApplication(yaml);
waitForApplicationTasks(app);
assertNull(RecordingWinRmTool.constructorProps.get(0).get(WinRmMachineLocation.USER.getName()));
assertNull(RecordingWinRmTool.constructorProps.get(0).get(WinRmMachineLocation.PASSWORD.getName()));
}

@Test()
public void testWindowsMachinesNoExternalIPProvider() throws Exception {
RecordingWinRmTool.constructorProps.clear();
final String yaml = Joiner.on("\n").join("location:",
" byon:",
" hosts:",
" - winrm: $brooklyn:external(\"inPlaceSupplier1\", \"ipEmpty\")",
" user: $brooklyn:external(\"inPlaceSupplier1\", \"byonUserEmpty\")",
" brooklyn.winrm.config.winrmToolClass: org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool",
" password: $brooklyn:external(\"inPlaceSupplier1\", \"byonPasswordddd\")",
" osFamily: windows",
"services:",
"- type: org.apache.brooklyn.entity.software.base.VanillaWindowsProcess",
" brooklyn.config:",
" launch.command: echo launch",
" checkRunning.command: echo running");

try {
BasicApplication app = (BasicApplication) createAndStartApplication(yaml);
waitForApplicationTasks(app);
Asserts.shouldHaveFailedPreviously();
} catch (Exception e) {
Asserts.expectedFailureContains(e, "Must specify exactly one of 'ssh' or 'winrm' for machine");
}
}

@Override
protected Logger getLogger() {
return log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.brooklyn.core.config.Sanitizer;
import org.apache.brooklyn.core.location.AbstractLocationResolver;
import org.apache.brooklyn.core.mgmt.internal.LocalLocationManager;
import org.apache.brooklyn.core.objs.BasicConfigurableObject;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.ClassLoaderUtils;
import org.apache.brooklyn.util.core.config.ConfigBag;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
if (host instanceof String) {
machineSpec = parseMachine((String)host, locationClass, defaultProps, spec);
} else if (host instanceof Map) {
machineSpec = parseMachine((Map<String, ?>)host, locationClass, defaultProps, spec);
machineSpec = parseMachine((Map<String, ?>)host, locationClass, defaultProps);
} else {
throw new IllegalArgumentException("Expected machine to be String or Map, but was "+host.getClass().getName()+" ("+host+")");
}
Expand All @@ -146,62 +147,16 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
return config;
}

protected LocationSpec<? extends MachineLocation> parseMachine(Map<String, ?> vals, Class<? extends MachineLocation> locationClass, Map<String, ?> defaults, String specForErrMsg) {
Map<String, Object> valSanitized = Sanitizer.sanitize(vals);
protected LocationSpec<? extends MachineLocation> parseMachine(Map<String, ?> vals, Class<? extends MachineLocation> locationClass, Map<String, ?> defaults) {
Map<String, Object> machineConfig = MutableMap.copyOf(vals);

String osFamily = (String) machineConfig.remove(OS_FAMILY.getName());
String ssh = (String) machineConfig.remove("ssh");
String winrm = (String) machineConfig.remove("winrm");
Map<Integer, String> tcpPortMappings = (Map<Integer, String>) machineConfig.get("tcpPortMappings");

checkArgument(ssh != null ^ winrm != null, "Must specify exactly one of 'ssh' or 'winrm' for machine: %s", valSanitized);

UserAndHostAndPort userAndHostAndPort;
String host;
int port;
if (ssh != null) {
userAndHostAndPort = parseUserAndHostAndPort(ssh, 22);
} else {
// TODO set to null and rely on the MachineLocation. If not then make a dependency to WinRmMachineLocation and use its config key name.
userAndHostAndPort = parseUserAndHostAndPort(winrm, vals.get("winrm.useHttps") != null && (Boolean)vals.get("winrm.useHttps") ? 5986 : 5985);
}

// If there is a tcpPortMapping defined for the connection-port, then use that for ssh/winrm machine
port = userAndHostAndPort.getHostAndPort().getPort();
if (tcpPortMappings != null && tcpPortMappings.containsKey(port)) {
String override = tcpPortMappings.get(port);
HostAndPort hostAndPortOverride = HostAndPort.fromString(override);
if (!hostAndPortOverride.hasPort()) {
throw new IllegalArgumentException("Invalid portMapping ('"+override+"') for port "+port+" in "+specForErrMsg);
}
port = hostAndPortOverride.getPort();
host = hostAndPortOverride.getHostText().trim();
} else {
host = userAndHostAndPort.getHostAndPort().getHostText().trim();
}

machineConfig.put("address", host);
try {
InetAddress.getByName(host);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid host '"+host+"' specified in '"+specForErrMsg+"': "+e);
}

if (userAndHostAndPort.getUser() != null) {
checkArgument(!vals.containsKey("user"), "Must not specify user twice for machine: %s", valSanitized);
machineConfig.put("user", userAndHostAndPort.getUser());
}
if (userAndHostAndPort.getHostAndPort().hasPort()) {
checkArgument(!vals.containsKey("port"), "Must not specify port twice for machine: %s", valSanitized);
machineConfig.put("port", port);
}
for (Map.Entry<String, ?> entry : defaults.entrySet()) {
if (!machineConfig.containsKey(entry.getKey())) {
machineConfig.put(entry.getKey(), entry.getValue());
}
}

Class<? extends MachineLocation> locationClassHere = locationClass;
if (osFamily != null) {
locationClassHere = getLocationClass(osFamily);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

import java.io.Closeable;
import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.net.HostAndPort;
import org.apache.brooklyn.api.location.Location;
import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.api.location.MachineLocation;
Expand All @@ -45,6 +48,7 @@
import org.apache.brooklyn.util.collections.MutableSet;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.core.flags.SetFromFlag;
import org.apache.brooklyn.util.net.UserAndHostAndPort;
import org.apache.brooklyn.util.stream.Streams;
import org.apache.brooklyn.util.text.WildcardGlobs;
import org.apache.brooklyn.util.text.WildcardGlobs.PhraseTreatment;
Expand Down Expand Up @@ -317,7 +321,7 @@ public T obtain(Map<?,?> flags) throws NoMachinesAvailableException {
T desiredMachine = (T) flags.get("desiredMachine");
ConfigBag allflags = ConfigBag.newInstanceExtending(config().getBag()).putAll(flags);
Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = allflags.get(MACHINE_CHOOSER);

synchronized (lock) {
Set<T> a = getAvailable();
if (a.isEmpty()) {
Expand Down Expand Up @@ -381,13 +385,89 @@ protected void updateMachineConfig(T machine, Map<?, ?> flags) {
// For backwards compatibility, where peristed state did not have this.
origConfigs = Maps.newLinkedHashMap();
}
if (((AbstractLocation) machine).config().getBag().getAllConfig().get("provider") != null &&
((AbstractLocation) machine).config().getBag().getAllConfig().get("provider").equals("byon")) {
parseMachineConfig((AbstractLocation) machine);
}
Map<String, Object> strFlags = ConfigBag.newInstance(flags).getAllConfig();
Map<String, Object> origConfig = ((ConfigurationSupportInternal)machine.config()).getLocalBag().getAllConfig();
origConfigs.put(machine, origConfig);
requestPersist();

((ConfigurationSupportInternal)machine.config()).putAll(strFlags);
}

private void parseMachineConfig(AbstractLocation machine) {
String ssh = machine.config().get(ConfigKeys.newStringConfigKey("ssh"));
machine.config().removeKey(ConfigKeys.newStringConfigKey("ssh"));
String winrm = machine.config().get(ConfigKeys.newStringConfigKey("winrm"));
machine.config().removeKey(ConfigKeys.newStringConfigKey("winrm"));

if (ssh ==null && winrm == null && machine.config().get(ConfigKeys.newStringConfigKey("address")) != null) {
return;
}

Preconditions.checkArgument(ssh != null || winrm != null, "Must specify exactly one of 'ssh' or 'winrm' for machine: " + machine);

Map<Integer, String> tcpPortMappings = (Map<Integer, String>) machine.config().get(ConfigKeys.newConfigKey(Map.class, "tcpPortMappings"));

UserAndHostAndPort userAndHostAndPort;
String host;
int port;
if (ssh != null) {
userAndHostAndPort = parseUserAndHostAndPort(ssh, 22);
} else {
// TODO set to null and rely on the MachineLocation. If not then make a dependency to WinRmMachineLocation and use its config key name.
Boolean useHttps = machine.config().get(ConfigKeys.newConfigKey(Boolean.class,"winrm.useHttps"));
userAndHostAndPort = parseUserAndHostAndPort(winrm, useHttps != null && useHttps ? 5986 : 5985);
}

// If there is a tcpPortMapping defined for the connection-port, then use that for ssh/winrm machine
port = userAndHostAndPort.getHostAndPort().getPort();
if (tcpPortMappings != null && tcpPortMappings.containsKey(port)) {
String override = tcpPortMappings.get(port);
HostAndPort hostAndPortOverride = HostAndPort.fromString(override);
if (!hostAndPortOverride.hasPort()) {
throw new IllegalArgumentException("Invalid portMapping ('"+override+"') for port "+port+" in machine configuration "+machine.config());
}
port = hostAndPortOverride.getPort();
host = hostAndPortOverride.getHostText().trim();
} else {
host = userAndHostAndPort.getHostAndPort().getHostText().trim();
}

machine.config().set(ConfigKeys.newStringConfigKey("address"), host);
try {
InetAddress.getByName(host);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid host '"+host+"' specified in '"+machine+"': "+e);
}

if (userAndHostAndPort.getUser() != null) {
machine.config().set(ConfigKeys.newStringConfigKey("user"), userAndHostAndPort.getUser());
}
if (userAndHostAndPort.getHostAndPort().hasPort()) {
machine.config().set(ConfigKeys.newConfigKey(Integer.class,"port"), port);
}
}

private UserAndHostAndPort parseUserAndHostAndPort(String val) {
String userPart = null;
String hostPart = val;
if (val.contains("@")) {
userPart = val.substring(0, val.indexOf("@"));
hostPart = val.substring(val.indexOf("@")+1);
}
return UserAndHostAndPort.fromParts(userPart, HostAndPort.fromString(hostPart));
}

private UserAndHostAndPort parseUserAndHostAndPort(String val, int defaultPort) {
UserAndHostAndPort result = parseUserAndHostAndPort(val);
if (!result.getHostAndPort().hasPort()) {
result = UserAndHostAndPort.fromParts(result.getUser(), result.getHostAndPort().getHostText(), defaultPort);
}
return result;
}

protected void restoreMachineConfig(MachineLocation machine) {
if (origConfigs == null) {
Expand Down