Skip to content

Commit

Permalink
Move byon config parsing to machine location
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanayov committed Aug 4, 2017
1 parent f919a39 commit 58847c1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,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 @@ -147,68 +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");
if (machineConfig.containsKey("winrm") && !(machineConfig.get("winrm") instanceof String)) {
machineConfig.put("address", machineConfig.get("winrm"));
machineConfig.remove("winrm");
} else {
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,14 @@

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.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 +47,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 +320,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 +384,84 @@ protected void updateMachineConfig(T machine, Map<?, ?> flags) {
// For backwards compatibility, where peristed state did not have this.
origConfigs = Maps.newLinkedHashMap();
}
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"));

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

if (ssh == null && winrm == null) {
throw new IllegalArgumentException("Must specify exactly one of 'ssh' or 'winrm' for machine: "+machine);
}

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

0 comments on commit 58847c1

Please sign in to comment.