Search in sources :

Example 1 with SshOpsTarget

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;
}
Also used : SshOpsTarget(org.platformlayer.ops.SshOpsTarget) InetAddress(java.net.InetAddress)

Example 2 with SshOpsTarget

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);
}
Also used : Inet4Address(java.net.Inet4Address) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 3 with SshOpsTarget

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) File(java.io.File) InetAddress(java.net.InetAddress) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 4 with SshOpsTarget

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Instance(com.google.api.services.compute.model.Instance) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance) PublicKey(java.security.PublicKey) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) Operation(com.google.api.services.compute.model.Operation) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) GoogleComputeClient(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient) GoogleCloud(org.platformlayer.service.cloud.google.model.GoogleCloud) Tag(org.platformlayer.core.model.Tag) Tags(org.platformlayer.core.model.Tags) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) TimeoutException(java.util.concurrent.TimeoutException) Handler(org.platformlayer.ops.Handler)

Aggregations

SshOpsTarget (org.platformlayer.ops.SshOpsTarget)4 InetAddress (java.net.InetAddress)3 OpsException (org.platformlayer.ops.OpsException)2 Md5Hash (com.fathomdb.hash.Md5Hash)1 Instance (com.google.api.services.compute.model.Instance)1 Operation (com.google.api.services.compute.model.Operation)1 File (java.io.File)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1 PublicKey (java.security.PublicKey)1 TimeoutException (java.util.concurrent.TimeoutException)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Tag (org.platformlayer.core.model.Tag)1 Tags (org.platformlayer.core.model.Tags)1 Command (org.platformlayer.ops.Command)1 Handler (org.platformlayer.ops.Handler)1 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 SshKey (org.platformlayer.ops.helpers.SshKey)1 GoogleCloud (org.platformlayer.service.cloud.google.model.GoogleCloud)1