use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class HttpProxyHelper method chooseProxy.
private String chooseProxy(OpsTarget target, List<String> proxies) {
String bestProxy = null;
TimeSpan bestTime = null;
for (String proxy : proxies) {
// {
// // We choose the fastest proxy that gives us a 200 response
// String url = proxy + "acng-report.html";
// CurlRequest request = new CurlRequest(url);
// request.setTimeout(5);
// try {
// CurlResult result = request.executeRequest(target);
// if (result.getHttpResult() != 200) {
// log.info("Unexpected response code while testing proxy: " + proxy + ". Code=" + result.getHttpResult());
// continue;
// }
// TimeSpan timeTotal = result.getTimeTotal();
// if (bestTime == null || timeTotal.isLessThan(bestTime)) {
// bestProxy = proxy;
// bestTime = timeTotal;
// }
// } catch (ProcessExecutionException e) {
// log.info("Error while testing proxy: " + proxy, e);
// }
// }
{
// We choose the fastest proxy that gives us a 200 response
String url = "http://ftp.debian.org/debian/dists/stable/Release.gpg";
CurlRequest request = new CurlRequest(url);
request.proxy = proxy;
request.timeout = TimeSpan.FIVE_SECONDS;
try {
CurlResult result = request.executeRequest(target);
if (result.getHttpResult() != 200) {
log.info("Unexpected response code while testing proxy: " + proxy + ". Code=" + result.getHttpResult());
continue;
}
TimeSpan timeTotal = result.getTimeTotal();
if (bestTime == null || timeTotal.isLessThan(bestTime)) {
bestProxy = proxy;
bestTime = timeTotal;
}
} catch (OpsException e) {
log.info("Error while testing proxy: " + proxy, e);
}
}
}
return bestProxy;
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class HttpBackends method buildUri.
public URI buildUri(NetworkPoint src, String scheme, ItemBase model, int port) throws OpsException {
Machine machine = instances.getMachine(model);
InetAddressChooser chooser = InetAddressChooser.preferIpv6();
InetAddress address = machine.getNetworkPoint().getBestAddress(src, chooser);
String host = InetAddresses.toAddrString(address);
URI uri;
try {
uri = new URI(scheme, null, host, port, null, null, null);
} catch (URISyntaxException e) {
throw new OpsException("Error building URI", e);
}
return uri;
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class ConfigMap method write.
public static boolean write(OpsTarget target, File path, Properties properties) throws OpsException {
String contents;
try {
StringWriter sw = new StringWriter();
properties.store(sw, null);
contents = sw.toString();
} catch (IOException e) {
throw new OpsException("Error serializing properties", e);
}
// String existing = target.readTextFile(path);
// if (Objects.equal(existing, contents)) {
// return false;
// }
FileUpload.upload(target, path, contents);
return true;
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class DirectImageStore method bringToMachine.
@Override
public void bringToMachine(String imageId, OpsTarget destination, File destinationPath) throws OpsException {
Properties imageProperties = fileStore.readProperties(imageId);
if (imageProperties == null) {
throw new OpsException("Image not found: " + imageId);
}
File imageFile = getImageFile(imageId, imageProperties);
if (destination.isSameMachine(target)) {
Command copyCommand = Command.build("cp {0} {1}", imageFile, destinationPath);
destination.executeCommand(copyCommand.setTimeout(TimeSpan.FIVE_MINUTES));
} else {
PeerToPeerCopy peerToPeerCopy = Injection.getInstance(PeerToPeerCopy.class);
peerToPeerCopy.copy(target, imageFile, destination, destinationPath);
// throw new OpsException("SCPing images between machines not yet implemented");
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class PublicEndpoint method addChildren.
@Override
protected void addChildren() throws OpsException {
final OwnedEndpoint endpoint;
{
endpoint = addChild(OwnedEndpoint.class);
endpoint.publicPort = publicPort;
endpoint.publicPortCluster = publicPortCluster;
endpoint.backendPort = backendPort;
endpoint.parentItem = parentItem;
endpoint.transport = transport;
}
if (tagItem != null) {
Tagger tagger = injected(Tagger.class);
OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {
@Override
public TagChanges get() throws OpsException {
int maxAttempts = 5;
int attempt = 0;
PublicEndpointBase item = endpoint.getItem();
while (true) {
attempt++;
if (item == null) {
if (!OpsContext.isDelete()) {
throw new OpsException("Endpoint not created");
} else {
log.warn("No endpoint => no tagging to be done");
return null;
}
}
List<EndpointInfo> endpointInfos = EndpointInfo.findEndpoints(item.getTags(), publicPort);
if (!endpointInfos.isEmpty()) {
TagChanges tagChanges = new TagChanges();
for (EndpointInfo endpointInfo : endpointInfos) {
tagChanges.addTags.add(endpointInfo.toTag());
}
return tagChanges;
}
if (attempt != maxAttempts) {
log.info("Endpoint not yet found; sleeping and retrying");
TimeSpan.FIVE_SECONDS.doSafeSleep();
item = platformLayerClient.getItem(item.getKey());
continue;
} else {
throw new OpsException("Cannot find endpoint for port: " + publicPort);
}
}
}
};
tagger.platformLayerKey = tagItem;
tagger.tagChangesProvider = tagChanges;
addChild(tagger);
}
if (defaultBlocked) {
// Block on machine's firewall
log.warn("Not adding firewall block; relying on default block");
// addChild(IptablesFirewallEntry.build(FirewallRecord.buildBlockPort(protocol, backendPort)));
}
if (!Strings.isNullOrEmpty(dnsName)) {
EndpointDnsRecord dns = injected(EndpointDnsRecord.class);
dns.destinationPort = publicPort;
dns.endpointProvider = new Provider<PublicEndpointBase>() {
@Override
public PublicEndpointBase get() {
return endpoint.getItem();
}
};
dns.dnsName = dnsName;
addChild(dns);
}
}
Aggregations