Search in sources :

Example 76 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class SolrTemplateData method getCluster.

public SolrCluster getCluster() throws OpsException {
    if (cluster == null) {
        cluster = OpsContext.get().getInstance(SolrCluster.class);
        if (cluster == null) {
            SolrServer server = getServer();
            PlatformLayerKey parent = Tag.PARENT.findUnique(server);
            if (parent != null) {
                cluster = platformLayer.getItem(parent, SolrCluster.class);
            }
        }
    }
    return cluster;
}
Also used : PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) SolrServer(org.platformlayer.service.solr.model.SolrServer) SolrCluster(org.platformlayer.service.solr.model.SolrCluster)

Example 77 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey 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)

Example 78 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class SimpleOperationQueue method jobFinished.

// @Override
// public JobExecutionList listRecentExecutions(ProjectId projectId) {
// JobExecutionList ret = JobExecutionList.create();
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// ret.add(job.getJobExecution());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData jobExecution : recentJobs) {
// if (!jobExecution.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(jobExecution);
// }
// }
//
// return ret;
// }
// @Override
// public List<JobData> listRecentJobs(ProjectId projectId) {
// List<JobData> ret = Lists.newArrayList();
//
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// JobExecutionData execution = job.getJobExecution();
// ret.add(execution.getJob());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData job : recentJobs) {
// if (!job.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(job.getJob());
// }
// }
//
// return ret;
// }
// private static final int RECENT_JOB_COUNT = 100;
@Override
public void jobFinished(JobExecutionData jobExecutionData, JobState state, JobLogger logger) throws OpsException {
    PlatformLayerKey jobKey = jobExecutionData.getJobKey();
    String executionId = jobExecutionData.getExecutionId();
    Date startTime = jobExecutionData.getStartedAt();
    Date endTime = new Date();
    synchronized (activeJobs) {
        activeJobs.remove(jobKey);
    }
    try {
        String logCookie = jobLogStore.saveJobLog(jobKey, executionId, startTime, logger);
        jobRepository.recordJobEnd(jobKey, executionId, endTime, state, logCookie);
    } catch (RepositoryException e) {
        throw new OpsException("Error writing job to repository", e);
    } catch (Exception e) {
        throw new OpsException("Error writing job log", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) Date(java.util.Date) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException)

Example 79 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class JdbcJobRepository method listRecentJobs.

// @Override
// @JdbcTransaction
// public List<JobExecutionData> listRecentExecutions(JobQuery jobQuery) throws RepositoryException {
// DbHelper db = new DbHelper();
// try {
// // We use JoinedQueryResult because we have a compound PK (projectId / jobId)
// // and JPA makes this really complicated.
//
// ProjectId projectId = jobQuery.project;
// Preconditions.checkNotNull(projectId);
// int project = db.mapToValue(projectId);
//
// Long maxAge = null;
// if (jobQuery.maxAge != null) {
// maxAge = jobQuery.maxAge.getTotalSeconds();
// }
//
// Integer limit = jobQuery.limit;
// String filterTarget = jobQuery.target != null ? jobQuery.target.getUrl() : null;
//
// JoinedQueryResult results = db.queries.listRecentExecutions(project, maxAge, filterTarget, limit);
//
// List<JobExecutionData> ret = Lists.newArrayList();
// Map<String, JobData> jobs = Maps.newHashMap();
//
// for (JobEntity job : results.getAll(JobEntity.class)) {
// ManagedItemId jobId = new ManagedItemId(job.jobId);
// PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
// jobs.put(job.jobId, mapFromEntity(job, jobKey));
// }
//
// for (JobExecutionEntity execution : results.getAll(JobExecutionEntity.class)) {
// JobData jobData = jobs.get(execution.jobId);
// if (jobData == null) {
// throw new IllegalStateException();
// }
//
// ManagedItemId jobId = new ManagedItemId(execution.jobId);
// PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
// JobExecutionData run = mapFromEntity(execution, jobKey);
// run.job = jobData;
// ret.add(run);
// }
//
// sort(ret);
//
// return ret;
// } catch (SQLException e) {
// throw new RepositoryException("Error listing job executions", e);
// } finally {
// db.close();
// }
// }
@Override
@JdbcTransaction
public List<JobData> listRecentJobs(JobQuery jobQuery) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectId projectId = jobQuery.project;
        Preconditions.checkNotNull(projectId);
        int project = db.mapToValue(projectId);
        Long maxAge = null;
        if (jobQuery.maxAge != null) {
            maxAge = jobQuery.maxAge.getTotalSeconds();
        }
        Integer limit = jobQuery.limit;
        String filterTarget = jobQuery.target != null ? jobQuery.target.getUrl() : null;
        // TODO: Include currently running jobs...
        List<JobEntity> results = db.queries.listRecentJobs(project, maxAge, filterTarget, limit);
        List<JobData> ret = Lists.newArrayList();
        for (JobEntity job : results) {
            ManagedItemId jobId = new ManagedItemId(job.jobId);
            PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
            JobData jobData = mapFromEntity(job, jobKey);
            ret.add(jobData);
        }
        sortJobs(ret);
        return ret;
    } catch (SQLException e) {
        throw new RepositoryException("Error listing job executions", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) JobData(org.platformlayer.jobs.model.JobData) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 80 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class ServiceProviderBase method resolveKeys.

public void resolveKeys(Object item) throws OpsException {
    Class<? extends Object> itemClass = item.getClass();
    for (Field field : itemClass.getFields()) {
        Class<?> fieldType = field.getType();
        if (fieldType == PlatformLayerKey.class) {
            PlatformLayerKey key;
            try {
                key = (PlatformLayerKey) field.get(item);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Error getting field: " + field, e);
            }
            if (key != null) {
                PlatformLayerKey newKey = resolveKey(key);
                if (newKey != key) {
                    try {
                        field.set(item, newKey);
                    } catch (IllegalAccessException e) {
                        throw new IllegalStateException("Error setting field: " + field, e);
                    }
                }
            }
        }
        if (fieldType == List.class) {
            Type genericFieldType = field.getGenericType();
            if (genericFieldType instanceof ParameterizedType) {
                ParameterizedType aType = (ParameterizedType) genericFieldType;
                Type[] fieldArgTypes = aType.getActualTypeArguments();
                if (fieldArgTypes.length == 1) {
                    Type fieldArgType = fieldArgTypes[0];
                    if (fieldArgType instanceof Class) {
                        Class fieldArg = (Class) fieldArgType;
                        if (fieldArg.equals(PlatformLayerKey.class)) {
                            List<PlatformLayerKey> list;
                            try {
                                list = (List<PlatformLayerKey>) field.get(item);
                            } catch (IllegalAccessException e) {
                                throw new IllegalStateException("Error getting field: " + field, e);
                            }
                            if (list != null) {
                                for (int i = 0; i < list.size(); i++) {
                                    PlatformLayerKey key = list.get(i);
                                    PlatformLayerKey newKey = resolveKey(key);
                                    if (newKey != key) {
                                        list.set(i, newKey);
                                    }
                                }
                                try {
                                    field.set(item, list);
                                } catch (IllegalAccessException e) {
                                    throw new IllegalStateException("Error setting field: " + field, e);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ModelClass(org.platformlayer.xaas.services.ModelClass) AnnotatedClass(org.platformlayer.xaas.discovery.AnnotatedClass)

Aggregations

PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)86 PlatformLayerClient (org.platformlayer.PlatformLayerClient)21 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)14 ManagedItemId (org.platformlayer.ids.ManagedItemId)13 UntypedItem (org.platformlayer.common.UntypedItem)10 ProjectId (org.platformlayer.ids.ProjectId)10 Tag (org.platformlayer.core.model.Tag)8 RepositoryException (org.platformlayer.RepositoryException)7 ServiceType (org.platformlayer.ids.ServiceType)7 JobData (org.platformlayer.jobs.model.JobData)7 InstanceBase (org.platformlayer.core.model.InstanceBase)6 ItemType (org.platformlayer.ids.ItemType)6 OpsTarget (org.platformlayer.ops.OpsTarget)6 JaxbHelper (org.platformlayer.xml.JaxbHelper)6 Handler (org.platformlayer.ops.Handler)5 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)4 UntypedItemXml (org.platformlayer.UntypedItemXml)4 FederationKey (org.platformlayer.ids.FederationKey)4 Machine (org.platformlayer.ops.Machine)4