use of org.platformlayer.ops.SshOpsTarget in project platformlayer by platformlayer.
the class AsBlock method find.
public static AsBlock find(OpsTarget target) {
SshOpsTarget sshOpsTarget = (SshOpsTarget) target;
InetAddress host = sshOpsTarget.getHost();
AsBlock asBlock = find(host);
if (asBlock == null) {
log.warn("Could not determine AS-Block for: " + host.getHostAddress() + " (" + target + ")");
}
return asBlock;
}
use of org.platformlayer.ops.SshOpsTarget in project platformlayer by platformlayer.
the class SocatPeerToPeerCopy method findChannel.
private InetAddressPair findChannel(OpsTarget src, OpsTarget target) throws OpsException {
InetAddress srcAddress = ((SshOpsTarget) src).getHost();
InetAddress targetAddress = ((SshOpsTarget) target).getHost();
if (srcAddress instanceof Inet4Address) {
if (targetAddress instanceof Inet6Address) {
Inet6Address srcIpv6 = findIpv6(src);
if (srcIpv6 != null) {
srcAddress = srcIpv6;
} else {
throw new UnsupportedOperationException();
}
}
} else {
if (targetAddress instanceof Inet4Address) {
Inet6Address targetIpv6 = findIpv6(target);
if (targetIpv6 != null) {
targetAddress = targetIpv6;
} else {
throw new UnsupportedOperationException();
}
}
}
return new InetAddressPair(srcAddress, targetAddress);
}
use of org.platformlayer.ops.SshOpsTarget in project platformlayer by platformlayer.
the class SshAgentPeerToPeerCopy method copy.
@Override
public void copy(final OpsTarget src, final File srcFile, final OpsTarget dest, final File finalDestFile) throws OpsException {
File tempDir = dest.createTempDir();
try {
final File tempDest = new File(tempDir, finalDestFile.getName());
{
SshOpsTarget srcSshOpsTarget = (SshOpsTarget) src;
String sshSrc = srcSshOpsTarget.getUsername() + "@";
InetAddress srcHost = srcSshOpsTarget.getHost();
if (InetAddressUtils.isIpv6(srcHost)) {
sshSrc += "[" + InetAddresses.toAddrString(srcHost) + "]";
} else {
sshSrc += InetAddresses.toAddrString(srcHost);
}
sshSrc += ":" + srcFile.getAbsolutePath();
Command pullCommand = Command.build("scp -o StrictHostKeyChecking=no {0} {1}", sshSrc, tempDest);
pullCommand.setKeyPair(srcSshOpsTarget.getKeyPair());
dest.executeCommand(pullCommand.setTimeout(TimeSpan.TEN_MINUTES));
Md5Hash targetHash = dest.getFileHash(tempDest);
Md5Hash srcHash = src.getFileHash(srcFile);
if (!Objects.equal(srcHash, targetHash)) {
dest.rm(tempDest);
throw new OpsException("Files did not match after transfer");
}
}
dest.mv(tempDest, finalDestFile);
} finally {
dest.rmdir(tempDir);
}
}
use of org.platformlayer.ops.SshOpsTarget in project platformlayer by platformlayer.
the class CloudInstanceMapper method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
Tags instanceTags = instance.getTags();
GoogleCloud cloud = findCloud();
if (cloud == null) {
throw new OpsException("Could not find cloud");
}
GoogleComputeClient computeClient = googleComputeClientFactory.getComputeClient(cloud);
getRecursionState().pushChildScope(cloud);
List<String> assignedInstanceIds = instanceTags.findAll(Tag.ASSIGNED);
if (assignedInstanceIds.isEmpty()) {
if (createInstance && !OpsContext.isDelete()) {
MachineCreationRequest request = buildMachineCreationRequest();
PlatformLayerKey instanceKey = instance.getKey();
request.tags.add(Tag.buildParentTag(instanceKey));
PublicKey servicePublicKey = service.getSshKey().getKeyPair().getPublic();
Instance created = computeClient.createInstance(cloud, request, servicePublicKey);
{
Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getName());
platformLayer.addTag(instance.getKey(), instanceTag);
}
assignedInstanceIds.add(created.getName());
}
}
if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
throw new OpsException("Instance not yet assigned");
}
GoogleComputeMachine machine = null;
OpsTarget target = null;
if (!assignedInstanceIds.isEmpty()) {
if (assignedInstanceIds.size() != 1) {
log.warn("Multiple instance ids found: " + assignedInstanceIds);
}
// We just take the first instance id
String assignedInstanceId = Iterables.getFirst(assignedInstanceIds, null);
Instance server = computeClient.findInstanceByName(assignedInstanceId);
if (server == null) {
if (OpsContext.isConfigure()) {
throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
}
} else {
server = computeClient.ensureHasPublicIp(server);
machine = new GoogleComputeMachine(computeClient, cloud, server);
SshKey sshKey = service.getSshKey();
target = machine.getTarget(GoogleComputeClient.USER_NAME, sshKey.getKeyPair());
// We need to use sudo while we set up root access
((SshOpsTarget) target).setEnsureRunningAsRoot(true);
}
}
if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
for (String instanceId : assignedInstanceIds) {
Instance server = computeClient.findInstanceByName(instanceId);
if (server == null) {
log.warn("Could not find assigned server: " + instanceId + ", ignoring");
continue;
}
// TODO: Remove associated firewall rules
log.warn("Deleting firewall rules not yet implemented");
// SecurityGroup securityGroup = null;
// if (supportsSecurityGroups) {
// securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
// }
Operation terminateOperation = computeClient.terminateInstance(instanceId);
try {
computeClient.waitComplete(terminateOperation, 5, TimeUnit.MINUTES);
} catch (TimeoutException e) {
throw new OpsException("Timeout while waiting for instance termination", e);
}
// if (securityGroup != null) {
// // We need to terminate the instance before we delete the security group it uses
// if (terminateOperation != null) {
// waitOperation(terminateOperation);
// }
//
// try {
// log.info("Deleting security group: " + securityGroup.getId());
// computeClient.root().securityGroups().securityGroup(securityGroup.getId()).delete();
// } catch (OpenstackNotFoundException e) {
// log.info("Ignoring not-found error while deleting security group: " + securityGroup.getId());
// }
// }
}
if (machine != null) {
machine.refreshState();
}
}
RecursionState recursion = getRecursionState();
if (OpsContext.isDelete() && machine == null) {
recursion.setPreventRecursion(true);
} else {
recursion.pushChildScope(machine);
recursion.pushChildScope(target);
}
}
Aggregations