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;
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
}
}
}
}
}
}
}
Aggregations