AgentClient agentClient = agentManager.getAgentClient(subjectManager.getOverlord(), bundleTargetResourceId);
BundleAgentService bundleAgentService = agentClient.getBundleAgentService();
// The BundleResourceDeployment record must exist in the db before the agent request because the agent may try
// to add History to it during immediate deployments. So, create and persist it (requires a new trans).
BundleResourceDeployment resourceDeployment = bundleManager.createBundleResourceDeploymentInNewTrans(
subjectManager.getOverlord(), deployment.getId(), bundleTargetResourceId);
if (null != bundleTarget.getResourceType().getResourceTypeBundleConfiguration()) {
// Ask the agent to schedule the request. The agent should add history as needed.
try {
BundleScheduleRequest request = bundleManager.getScheduleRequest(subject, resourceDeployment.getId(),
isCleanDeployment, isRevert);
// add the deployment request history (in a new trans)
BundleResourceDeploymentHistory history = new BundleResourceDeploymentHistory(subject.getName(),
AUDIT_ACTION_DEPLOYMENT_REQUESTED, deployment.getName(), null,
BundleResourceDeploymentHistory.Status.SUCCESS, "Requested deployment time: "
+ request.getRequestedDeployTimeAsString(), null);
bundleManager.addBundleResourceDeploymentHistoryInNewTrans(subjectManager.getOverlord(),
resourceDeployment.getId(), history);
BundleScheduleResponse response = bundleAgentService.schedule(request);
if (!response.isSuccess()) {
// Handle Schedule Failures. This may include deployment failures for immediate deployment request
bundleManager.setBundleResourceDeploymentStatusInNewTransaction(subject,
resourceDeployment.getId(), BundleDeploymentStatus.FAILURE);
history = new BundleResourceDeploymentHistory(subject.getName(), AUDIT_ACTION_DEPLOYMENT,
deployment.getName(), null, BundleResourceDeploymentHistory.Status.FAILURE,
response.getErrorMessage(), null);
bundleManager.addBundleResourceDeploymentHistoryInNewTrans(subject, resourceDeployment.getId(),
history);
}
} catch (Throwable t) {
// fail the unlaunched resource deployment
BundleResourceDeploymentHistory failureHistory = new BundleResourceDeploymentHistory(subject.getName(),
AUDIT_ACTION_DEPLOYMENT, deployment.getName(), null,
BundleResourceDeploymentHistory.Status.FAILURE, "Failed to schedule, agent on [" + bundleTarget
+ "] may be down: " + t, null);
bundleManager.addBundleResourceDeploymentHistoryInNewTrans(subject, resourceDeployment.getId(),
failureHistory);
bundleManager.setBundleResourceDeploymentStatusInNewTransaction(subject, resourceDeployment.getId(),
BundleDeploymentStatus.FAILURE);
}
} else {
bundleManager.setBundleResourceDeploymentStatusInNewTransaction(subject, resourceDeployment.getId(),
BundleDeploymentStatus.FAILURE);
BundleResourceDeploymentHistory history = new BundleResourceDeploymentHistory(subject.getName(),
AUDIT_ACTION_DEPLOYMENT, deployment.getName(), null, BundleResourceDeploymentHistory.Status.FAILURE,
"Target resource is not of a type that can have bundles deployed to it [resource="
+ bundleTarget.getName() + "; id=" + bundleTarget.getId() + "]. Fix target group for destination ["
+ deployment.getDestination().getName() + "]", null);
bundleManager.addBundleResourceDeploymentHistoryInNewTrans(subject, resourceDeployment.getId(), history);
}
return resourceDeployment;
}