Search in sources :

Example 1 with OpenstackComputeClient

use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.

the class OpenstackCloudContext method ensureHasPublicIp.

public Server ensureHasPublicIp(final OpenstackCloud cloud, Server server) throws OpsException {
    final OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
    List<Ip> publicIps = helpers.findPublicIps(cloud, server);
    if (!publicIps.isEmpty()) {
        return server;
    }
    final OpenstackComputeClient compute = getComputeClient(cloud);
    log.info("Creating floating IP");
    FloatingIp floatingIp = compute.root().floatingIps().create();
    // TODO: Don't abandon the IP e.g. if the attach fails
    log.info("Attching floating IP " + floatingIp.getIp() + " to " + server.getId());
    compute.root().servers().server(server.getId()).addFloatingIp(floatingIp.getIp());
    final String serverId = server.getId();
    try {
        server = TimeoutPoll.poll(TimeSpan.FIVE_MINUTES, TimeSpan.TEN_SECONDS, new PollFunction<Server>() {

            @Override
            public Server call() throws Exception {
                log.info("Waiting for floating IP attach; polling server: " + serverId);
                Server server = compute.root().servers().server(serverId).show();
                List<Ip> publicIps = helpers.findPublicIps(cloud, server);
                if (publicIps.isEmpty()) {
                    return null;
                }
                return server;
            }
        });
    } catch (TimeoutException e) {
        throw new OpsException("Timeout while waiting for attached public IP to show up", e);
    } catch (ExecutionException e) {
        throw new OpsException("Error while waiting for attached public IP to show up", e);
    }
    return server;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) PollFunction(org.platformlayer.TimeoutPoll.PollFunction) Ip(org.openstack.model.compute.Addresses.Network.Ip) FloatingIp(org.openstack.model.compute.FloatingIp) ExecutionException(java.util.concurrent.ExecutionException) FloatingIp(org.openstack.model.compute.FloatingIp) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with OpenstackComputeClient

use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.

the class OpenstackCloudContext method createInstance.

public Server createInstance(OpenstackCloud cloud, String serverName, MachineCreationRequest request) throws OpsException {
    OpenstackComputeClient computeClient = getComputeClient(cloud);
    try {
        Image foundImage = null;
        CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
        if (!cloudBehaviours.canUploadImages()) {
            // For now, we presume this is the HP cloud and hard-code the name
            // if (!cloudBehaviours.isHpCloud()) {
            // throw new UnsupportedOperationException();
            // }
            DiskImageRecipe recipe = null;
            if (request.recipeId != null) {
                recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
            }
            OperatingSystemRecipe operatingSystem = null;
            if (recipe != null) {
                operatingSystem = recipe.getOperatingSystem();
            }
            log.info("Listing images to pick best image");
            Iterable<Image> images = computeClient.root().images().list();
            if (cloudBehaviours.isHpCloud()) {
                // TODO: We need a better solution here!!
                Set<String> imageNames = Sets.newHashSet("Debian Squeeze 6.0.3 Server 64-bit 20120123");
                log.warn("Hard coding image name (presuming HP cloud)");
                // TODO: Match OS
                for (Image image : images) {
                    if (imageNames.contains(image.getName())) {
                        foundImage = image;
                        break;
                    }
                }
            } else if (cloudBehaviours.isRackspaceCloud()) {
                if (operatingSystem == null) {
                    operatingSystem = new OperatingSystemRecipe();
                    operatingSystem.setDistribution("debian");
                    operatingSystem.setVersion("squeeze");
                }
                for (Image image : images) {
                    boolean matchesDistribution = false;
                    boolean matchesVersion = false;
                    for (Image.ImageMetadata.ImageMetadataItem item : image.getMetadata()) {
                        if (item.getKey().equals("os_distro")) {
                            if (operatingSystem != null && operatingSystem.getDistribution() != null) {
                                if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), item.getValue())) {
                                    matchesDistribution = true;
                                }
                            }
                        }
                        if (item.getKey().equals("os_version")) {
                            if (operatingSystem != null && operatingSystem.getVersion() != null) {
                                if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), item.getValue())) {
                                    matchesVersion = true;
                                } else if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), "debian")) {
                                    if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), "squeeze") && Comparisons.equalsIgnoreCase(item.getValue(), "6")) {
                                        matchesVersion = true;
                                    } else {
                                        matchesVersion = false;
                                    }
                                } else if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), "ubuntu")) {
                                    if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), "lucid") && Comparisons.equalsIgnoreCase(item.getValue(), "10.04LTS")) {
                                        matchesVersion = true;
                                    } else {
                                        matchesVersion = false;
                                    }
                                } else {
                                    matchesVersion = false;
                                }
                            }
                        }
                    }
                    if (matchesDistribution && matchesVersion) {
                        foundImage = image;
                        break;
                    }
                }
            } else {
                for (Image image : images) {
                    boolean isMatch = false;
                    for (Image.ImageMetadata.ImageMetadataItem item : image.getMetadata()) {
                        if (item.getKey().equals(Tag.IMAGE_OS_DISTRIBUTION)) {
                            if (operatingSystem != null && operatingSystem.getDistribution() != null) {
                                if (!Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), item.getValue())) {
                                    isMatch = false;
                                }
                            }
                        }
                        if (item.getKey().equals(Tag.IMAGE_OS_VERSION)) {
                            if (operatingSystem != null && operatingSystem.getVersion() != null) {
                                if (!Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), item.getValue())) {
                                    isMatch = false;
                                }
                            }
                        }
                    }
                    if (isMatch) {
                        foundImage = image;
                        break;
                    }
                }
            }
            if (foundImage == null) {
                throw new IllegalArgumentException("Could not find image");
            }
        } else {
            List<ImageFormat> formats = Collections.singletonList(ImageFormat.DiskQcow2);
            CloudImage image = imageFactory.getOrCreateImageId(cloud, formats, request.recipeId);
            String imageId = image.getId();
            log.info("Getting image details for image: " + imageId);
            foundImage = computeClient.root().images().image(imageId).show();
            if (foundImage == null) {
                throw new IllegalArgumentException("Could not find image: " + imageId);
            }
        }
        SecurityGroup createdSecurityGroup = null;
        if (cloudBehaviours.supportsSecurityGroups()) {
            SecurityGroup createTemplate = new SecurityGroup();
            createTemplate.setName(SECURITY_GROUP_PREFIX + serverName);
            createTemplate.setDescription("Security group for instance: " + serverName);
            try {
                log.info("Creating security group: " + createTemplate.getName());
                createdSecurityGroup = computeClient.root().securityGroups().create(createTemplate);
            } catch (OpenstackException e) {
                for (SecurityGroup candidate : computeClient.root().securityGroups().list()) {
                    if (Objects.equal(candidate.getName(), createTemplate.getName())) {
                        createdSecurityGroup = candidate;
                        break;
                    }
                }
                if (createdSecurityGroup != null) {
                    // Ignore
                    log.warn("Ignoring 'security group already exists' error: " + e.getMessage());
                } else {
                    throw new OpsException("Error creating security group", e);
                }
            }
            {
                CreateSecurityGroupRuleRequest newRule = new CreateSecurityGroupRuleRequest();
                newRule.setCidr("0.0.0.0/0");
                newRule.setFromPort(22);
                newRule.setToPort(22);
                newRule.setIpProtocol("tcp");
                newRule.setParentGroupId(createdSecurityGroup.getId());
                try {
                    log.info("Creating security group rule for port: " + newRule.getToPort());
                    SecurityGroupRule createdRule = computeClient.root().securityGroupRules().create(newRule);
                } catch (OpenstackException e) {
                    String message = e.getMessage();
                    if (message != null && message.contains("This rule already exists")) {
                        log.warn("Ignoring 'rule already exists': " + e.getMessage());
                    } else {
                        throw new OpsException("Error creating security group access", e);
                    }
                }
            }
        }
        AsyncServerOperation createServerOperation;
        {
            ServerForCreate create = new ServerForCreate();
            create.setName(serverName);
            if (request.sshPublicKey != null) {
                if (cloudBehaviours.supportsPublicKeys()) {
                    OpenstackCloudHelpers cloudHelpers = new OpenstackCloudHelpers();
                    KeyPair keyPair = cloudHelpers.ensurePublicKeyUploaded(computeClient, request.sshPublicKeyName, request.sshPublicKey);
                    create.setKeyName(keyPair.getName());
                } else if (cloudBehaviours.supportsFileInjection()) {
                    String fileContents = SshKeys.serialize(request.sshPublicKey);
                    create.addUploadFile("/root/.ssh/authorized_keys", Utf8.getBytes(fileContents));
                } else {
                    throw new OpsException("No supported SSH key mechanism on cloud");
                }
            }
            create.setImageRef(foundImage.getId());
            Flavor flavor = getClosestInstanceType(computeClient, request);
            if (flavor == null) {
                throw new OpsException("Cannot determine instance type for request");
            }
            create.setFlavorRef(flavor.getId());
            if (request.securityGroups != null) {
                // TODO: Reimplement if needed
                throw new UnsupportedOperationException();
            }
            if (createdSecurityGroup != null) {
                ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
                serverSecurityGroup.setName(createdSecurityGroup.getName());
                create.getSecurityGroups().add(serverSecurityGroup);
            }
            create.setConfigDrive(cloudBehaviours.useConfigDrive());
            log.info("Launching new server: " + create.getName());
            createServerOperation = computeClient.createServer(create);
        }
        log.info("Waiting for server to be ready");
        Server server = createServerOperation.waitComplete();
        Server instanceInfo = null;
        String stateName = null;
        while (true) {
            instanceInfo = getInstanceInfo(computeClient, server.getId());
            stateName = instanceInfo.getStatus();
            log.info("Instance state: " + stateName);
            //
            if (stateName.equals("BUILD")) {
                break;
            }
            if (stateName.equals("ACTIVE")) {
                break;
            }
            Thread.sleep(1000);
        }
        // Even if the machine is in 'error' state, we still want to associate it with us
        if (request.tags != null) {
            Server newServerInfo = new Server();
            Metadata metadata = new Metadata();
            for (Tag tag : request.tags) {
                Metadata.Item meta = new Metadata.Item();
                meta.setKey(tag.getKey());
                meta.setValue(tag.getValue());
                metadata.getItems().add(meta);
            }
            newServerInfo.setMetadata(metadata);
            log.info("Tagging server: " + server.getId());
            computeClient.root().servers().server(server.getId()).update(newServerInfo);
        }
        return server;
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new OpsException("Error building server", e);
    } catch (OpenstackException e) {
        throw new OpsException("Error building server", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Server(org.openstack.model.compute.Server) Metadata(org.openstack.model.compute.Metadata) CloudImage(org.platformlayer.ops.images.CloudImage) Image(org.openstack.model.compute.Image) CloudImage(org.platformlayer.ops.images.CloudImage) OpenstackException(org.openstack.client.OpenstackException) ImageFormat(org.platformlayer.ops.images.ImageFormat) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) CreateSecurityGroupRuleRequest(org.openstack.model.compute.CreateSecurityGroupRuleRequest) KeyPair(org.openstack.model.compute.KeyPair) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) SecurityGroupRule(org.openstack.model.compute.SecurityGroupRule) SecurityGroup(org.openstack.model.compute.SecurityGroup) Flavor(org.openstack.model.compute.Flavor) ServerForCreate(org.openstack.model.compute.ServerForCreate) Tag(org.platformlayer.core.model.Tag) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation)

Example 3 with OpenstackComputeClient

use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tags instanceTags = instance.getTags();
    OpenstackCloud cloud = findCloud();
    if (cloud == null) {
        throw new OpsException("Could not find cloud");
    }
    OpenstackComputeClient computeClient = openstack.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));
            String serverName = buildServerName();
            Server created = openstack.createInstance(cloud, serverName, request);
            {
                Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getId());
                platformLayer.addTag(instance.getKey(), instanceTag);
            }
            assignedInstanceIds.add(created.getId());
        }
    }
    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
        throw new OpsException("Instance not yet assigned");
    }
    Machine 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);
        Server server = openstack.findServerById(cloud, assignedInstanceId);
        if (server == null) {
            if (OpsContext.isConfigure()) {
                throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
            }
        } else {
            server = openstack.ensureHasPublicIp(cloud, server);
            AsyncServerOperation powerOnOperation = openstack.ensurePoweredOn(cloud, server);
            if (powerOnOperation != null) {
                waitOperation(powerOnOperation);
            }
            machine = new OpenstackComputeMachine(openstack, cloud, server);
            SshKey sshKey = service.getSshKey();
            target = machine.getTarget(sshKey);
        }
    }
    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
        CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
        boolean supportsSecurityGroups = cloudBehaviours.supportsSecurityGroups();
        for (String instanceId : assignedInstanceIds) {
            Server server = openstack.findServerById(cloud, instanceId);
            if (server == null) {
                log.warn("Could not find assigned server: " + instanceId + ", ignoring");
                continue;
            }
            SecurityGroup securityGroup = null;
            if (supportsSecurityGroups) {
                securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
            }
            AsyncServerOperation terminateOperation = openstack.terminateInstance(cloud, instanceId);
            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());
                }
            }
        }
    }
    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) OpenstackCloud(org.platformlayer.service.cloud.openstack.model.OpenstackCloud) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) SecurityGroup(org.openstack.model.compute.SecurityGroup) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Machine(org.platformlayer.ops.Machine) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) CloudBehaviours(org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours) Tag(org.platformlayer.core.model.Tag) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Tags(org.platformlayer.core.model.Tags) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) Handler(org.platformlayer.ops.Handler)

Example 4 with OpenstackComputeClient

use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.

the class EnsureFirewallIngress method handler.

@Handler
public void handler(OpenstackCloud cloud, OpenstackComputeMachine machine) throws OpsException, OpenstackException {
    CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
    OpenstackComputeClient openstackComputeClient = cloudContext.getComputeClient(cloud);
    // Find the public address, although the OpenStack firewall may be blocking it
    publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
    if (cloudBehaviours.supportsSecurityGroups()) {
        Server server = machine.getServer();
        SecurityGroup securityGroup = openstackHelpers.getMachineSecurityGroup(openstackComputeClient, server);
        securityGroup = openstackComputeClient.root().securityGroups().securityGroup(securityGroup.getId()).show();
        SecurityGroupRule matchingRule = findMatchingRule(securityGroup);
        if (OpsContext.isConfigure()) {
            if (matchingRule == null) {
                CreateSecurityGroupRuleRequest rule = new CreateSecurityGroupRuleRequest();
                rule.setCidr("0.0.0.0/0");
                rule.setIpProtocol("tcp");
                rule.setFromPort(model.publicPort);
                rule.setToPort(model.publicPort);
                rule.setParentGroupId(securityGroup.getId());
                openstackComputeClient.root().securityGroupRules().create(rule);
            }
        }
        if (OpsContext.isDelete()) {
            if (matchingRule != null) {
                openstackComputeClient.root().securityGroupRules().securityGroupRule(matchingRule.id).delete();
            }
        }
    }
}
Also used : CreateSecurityGroupRuleRequest(org.openstack.model.compute.CreateSecurityGroupRuleRequest) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) CloudBehaviours(org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours) SecurityGroupRule(org.openstack.model.compute.SecurityGroupRule) SecurityGroup(org.openstack.model.compute.SecurityGroup) Handler(org.platformlayer.ops.Handler)

Example 5 with OpenstackComputeClient

use of org.openstack.client.common.OpenstackComputeClient in project platformlayer by platformlayer.

the class CloudBehaviours method getCachedInfo.

CachedInfo getCachedInfo() throws OpsException {
    // TODO: We key on endpoint, to avoid repeated calls.
    // Is it safe to assume that everyone has the same capabilities on a cloud??
    CachedInfo info = cachedInfo.get(cloud.endpoint);
    if (info == null) {
        OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
        OpenstackComputeClient compute = helpers.buildOpenstackComputeClient(cloud);
        List<Extension> extensions = Lists.newArrayList(compute.root().extensions().list());
        info = new CachedInfo(extensions);
        cachedInfo.put(cloud.endpoint, info);
    }
    return info;
}
Also used : Extension(org.openstack.model.common.Extension) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient)

Aggregations

OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)9 OpsException (org.platformlayer.ops.OpsException)6 Server (org.openstack.model.compute.Server)5 OpenstackException (org.openstack.client.OpenstackException)4 AsyncServerOperation (org.openstack.client.compute.AsyncServerOperation)4 OpenstackNotFoundException (org.openstack.client.OpenstackNotFoundException)3 SecurityGroup (org.openstack.model.compute.SecurityGroup)3 CreateSecurityGroupRuleRequest (org.openstack.model.compute.CreateSecurityGroupRuleRequest)2 SecurityGroupRule (org.openstack.model.compute.SecurityGroupRule)2 Tag (org.platformlayer.core.model.Tag)2 Handler (org.platformlayer.ops.Handler)2 CloudBehaviours (org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Extension (org.openstack.model.common.Extension)1 Ip (org.openstack.model.compute.Addresses.Network.Ip)1 Flavor (org.openstack.model.compute.Flavor)1 FloatingIp (org.openstack.model.compute.FloatingIp)1 Image (org.openstack.model.compute.Image)1 KeyPair (org.openstack.model.compute.KeyPair)1