Search in sources :

Example 1 with OperatingSystemRecipe

use of org.platformlayer.images.model.OperatingSystemRecipe 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 2 with OperatingSystemRecipe

use of org.platformlayer.images.model.OperatingSystemRecipe in project platformlayer by platformlayer.

the class DesktopController method addChildren.

@Override
protected void addChildren() throws OpsException {
    Desktop model = OpsContext.get().getInstance(Desktop.class);
    InstanceBuilder instance = InstanceBuilder.build(model.dnsName, this, model.getTags());
    instance.publicPorts.add(22);
    instance.hostPolicy.allowRunInContainer = true;
    instance.minimumMemoryMb = 4096;
    addChild(instance);
    {
        RecipeOperatingSystem os = injected(RecipeOperatingSystem.class);
        os.operatingSystem = new OperatingSystemRecipe();
        os.operatingSystem.setDistribution("debian");
        os.operatingSystem.setVersion("wheezy");
        instance.addChild(os);
    }
    // We use curl for backups
    instance.addChild(PackageDependency.build("curl"));
    {
        PublicEndpoint endpoint = injected(PublicEndpoint.class);
        // endpoint.network = null;
        endpoint.publicPort = 22;
        endpoint.backendPort = 22;
        endpoint.dnsName = model.dnsName;
        endpoint.tagItem = model.getKey();
        endpoint.parentItem = model.getKey();
        instance.addChild(endpoint);
    }
// {
// BackupDirectory backup = injected(BackupDirectory.class);
// backup.itemKey = model.getKey();
//
// File jenkinsRoot = new File("/var/lib/jenkins");
// backup.backupRoot = jenkinsRoot;
//
// String[] excludes = { "jobs/*/workspace", "jobs/*/modules", "jobs/*/builds/*/workspace.tar.gz",
// ".m2/repository" };
//
// for (String exclude : excludes) {
// backup.excludes.add(new File(jenkinsRoot, exclude));
// }
//
// instance.addChild(backup);
// }
}
Also used : Desktop(org.platformlayer.service.desktop.model.Desktop) PublicEndpoint(org.platformlayer.ops.networks.PublicEndpoint) RecipeOperatingSystem(org.platformlayer.ops.packages.RecipeOperatingSystem) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe) InstanceBuilder(org.platformlayer.ops.instances.InstanceBuilder)

Example 3 with OperatingSystemRecipe

use of org.platformlayer.images.model.OperatingSystemRecipe in project platformlayer by platformlayer.

the class GoogleComputeClient method createInstance.

public Instance createInstance(GoogleCloud cloud, MachineCreationRequest request, PublicKey sshPublicKey) throws OpsException {
    try {
        Image foundImage = null;
        {
            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 = listImages(PROJECTID_GOOGLE);
            // TODO: We need a better solution here!!
            log.warn("Hard coding image names");
            Set<String> imageNames = Sets.newHashSet("ubuntu-12-04-v20120621");
            for (Image image : images) {
                if (imageNames.contains(image.getName())) {
                    foundImage = image;
                    break;
                }
            }
            if (foundImage == null) {
                throw new IllegalArgumentException("Could not find image");
            }
        }
        // GCE requires that the name comply with RFC1035, which I think means a valid DNS
        // For now, just use a UUID, with a pl- prefix so it doesn't start with a number
        // TODO: Fix this!
        String instanceName = "pl-" + UUID.randomUUID().toString();
        Operation createServerOperation;
        {
            Instance create = new Instance();
            create.setName(instanceName);
            create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));
            {
                NetworkInterface networkInterface = new NetworkInterface();
                networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));
                AccessConfig networkAccessConfig = new AccessConfig();
                networkAccessConfig.setType("ONE_TO_ONE_NAT");
                networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));
                create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
            }
            Metadata metadata = new Metadata();
            metadata.setItems(Lists.<Items>newArrayList());
            create.setMetadata(metadata);
            if (request.tags != null) {
                for (Tag tag : request.tags) {
                    Metadata.Items meta = new Metadata.Items();
                    meta.setKey(tag.getKey());
                    meta.setValue(tag.getValue());
                    metadata.getItems().add(meta);
                }
            }
            if (request.sshPublicKey != null) {
                Metadata.Items meta = new Metadata.Items();
                meta.setKey("sshKeys");
                meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));
                metadata.getItems().add(meta);
            }
            create.setImage(foundImage.getSelfLink());
            MachineType flavor = getClosestInstanceType(request);
            if (flavor == null) {
                throw new OpsException("Cannot determine machine type for request");
            }
            create.setMachineType(flavor.getSelfLink());
            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: " + instanceName);
            try {
                createServerOperation = compute.instances().insert(projectId, create).execute();
            } catch (IOException e) {
                throw new OpsException("Error launching new instance", e);
            }
        }
        log.info("Waiting for server to be ready");
        createServerOperation = waitComplete(createServerOperation, 10, TimeUnit.MINUTES);
        Instance created;
        InstanceState state = null;
        while (true) {
            created = findInstanceByName(instanceName);
            state = InstanceState.get(created);
            log.info("Instance state: " + state);
            if (state.isRunning()) {
                break;
            }
            Thread.sleep(1000);
        }
        return created;
    } catch (InterruptedException e) {
        ExceptionUtils.handleInterrupted(e);
        throw new OpsException("Error building server", e);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for server build", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Set(java.util.Set) Instance(com.google.api.services.compute.model.Instance) Metadata(com.google.api.services.compute.model.Metadata) MachineType(com.google.api.services.compute.model.MachineType) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Operation(com.google.api.services.compute.model.Operation) Items(com.google.api.services.compute.model.Metadata.Items) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) AccessConfig(com.google.api.services.compute.model.AccessConfig) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) Items(com.google.api.services.compute.model.Metadata.Items) Tag(org.platformlayer.core.model.Tag) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with OperatingSystemRecipe

use of org.platformlayer.images.model.OperatingSystemRecipe in project platformlayer by platformlayer.

the class DiskImageController method getRequestedOperatingSystem.

protected OperatingSystem getRequestedOperatingSystem(DiskImageRecipe recipe) {
    OperatingSystemRecipe operatingSystemRecipe = recipe.operatingSystem;
    if (operatingSystemRecipe == null) {
        operatingSystemRecipe = new OperatingSystemRecipe();
    }
    if (Strings.isNullOrEmpty(operatingSystemRecipe.distribution)) {
        return OperatingSystem.DebianSqueeze;
    }
    Distribution distribution = Distribution.parse(operatingSystemRecipe.distribution);
    String version = operatingSystemRecipe.version;
    if (Strings.isNullOrEmpty(version)) {
        version = distribution.getDefaultOsVersion();
    }
    return new OperatingSystem(distribution, version);
}
Also used : OperatingSystem(org.platformlayer.service.imagefactory.OperatingSystem) Distribution(org.platformlayer.service.imagefactory.OperatingSystem.Distribution) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe)

Example 5 with OperatingSystemRecipe

use of org.platformlayer.images.model.OperatingSystemRecipe in project platformlayer by platformlayer.

the class JavaVirtualMachine method addTo.

@Override
public void addTo(DiskImageRecipe recipe) {
    if (version.equals("6")) {
        // TODO: What if it's not debian squeeze??
        Repository repository = new Repository();
        repository.setKey("debian-non-free");
        repository.getSource().add("deb http://ftp.us.debian.org/debian squeeze non-free");
        recipe.getRepository().add(repository);
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-bin", "shared/accepted-sun-dlj-v1-1", "boolean", "true");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-bin", "shared/accepted-sun-dlj-v1-1", "boolean", "true");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jdk", "shared/accepted-sun-dlj-v1-1", "boolean", "true");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jre", "shared/accepted-sun-dlj-v1-1", "boolean", "true");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jre", "sun-java6-jre/stopthread", "boolean", "true");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jre", "sun-java6-jre/jcepolicy", "note", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-bin", "shared/error-sun-dlj-v1-1", "error", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jdk", "shared/error-sun-dlj-v1-1", "error", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jre", "shared/error-sun-dlj-v1-1", "error", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-bin", "shared/present-sun-dlj-v1-1", "note", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jdk", "shared/present-sun-dlj-v1-1", "note", "");
        DiskImageRecipeBuilder.addPreconfigure(recipe, "sun-java6-jre", "shared/present-sun-dlj-v1-1", "note", "");
        recipe.getAddPackage().add("sun-java6-jre");
        if (addJdk) {
            recipe.getAddPackage().add("sun-java6-jdk");
        }
    } else if (version.equals("7")) {
        OperatingSystemRecipe operatingSystem = recipe.getOperatingSystem();
        if (operatingSystem == null) {
            operatingSystem = new OperatingSystemRecipe();
        }
        if (operatingSystem.getDistribution() == null) {
            operatingSystem.setDistribution("debian");
        }
        if (operatingSystem.getVersion() == null) {
            if (operatingSystem.getDistribution().equalsIgnoreCase("debian")) {
                operatingSystem.setVersion("wheezy");
            }
        }
        recipe.setOperatingSystem(operatingSystem);
    } else {
        throw new IllegalArgumentException("Unknown java version: " + version);
    }
}
Also used : Repository(org.platformlayer.images.model.Repository) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe)

Aggregations

OperatingSystemRecipe (org.platformlayer.images.model.OperatingSystemRecipe)5 Tag (org.platformlayer.core.model.Tag)2 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)2 OpsException (org.platformlayer.ops.OpsException)2 AccessConfig (com.google.api.services.compute.model.AccessConfig)1 Image (com.google.api.services.compute.model.Image)1 Instance (com.google.api.services.compute.model.Instance)1 MachineType (com.google.api.services.compute.model.MachineType)1 Metadata (com.google.api.services.compute.model.Metadata)1 Items (com.google.api.services.compute.model.Metadata.Items)1 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)1 Operation (com.google.api.services.compute.model.Operation)1 IOException (java.io.IOException)1 Set (java.util.Set)1 TimeoutException (java.util.concurrent.TimeoutException)1 OpenstackException (org.openstack.client.OpenstackException)1 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)1 AsyncServerOperation (org.openstack.client.compute.AsyncServerOperation)1 CreateSecurityGroupRuleRequest (org.openstack.model.compute.CreateSecurityGroupRuleRequest)1 Flavor (org.openstack.model.compute.Flavor)1