use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method copyRecipesAndArtifacts.
@SuppressWarnings("PMD.ExceptionAsFlowControl")
private void copyRecipesAndArtifacts(Deployment deployment) throws InvalidRequestException, IOException {
try {
LocalOverrideRequest localOverrideRequest = SerializerFactory.getFailSafeJsonObjectMapper().readValue(deployment.getDeploymentDocument(), LocalOverrideRequest.class);
if (!Utils.isEmpty(localOverrideRequest.getRecipeDirectoryPath())) {
Path recipeDirectoryPath = Paths.get(localOverrideRequest.getRecipeDirectoryPath());
copyRecipesToComponentStore(recipeDirectoryPath);
}
if (!Utils.isEmpty(localOverrideRequest.getArtifactsDirectoryPath())) {
Path kernelArtifactsDirectoryPath = kernel.getNucleusPaths().componentStorePath().resolve(ComponentStore.ARTIFACT_DIRECTORY);
Path artifactsDirectoryPath = Paths.get(localOverrideRequest.getArtifactsDirectoryPath());
try {
Utils.copyFolderRecursively(artifactsDirectoryPath, kernelArtifactsDirectoryPath, (Path src, Path dst) -> {
// existing component and then do the deployment to make it work.
if (PlatformResolver.isWindows) {
try {
if (Files.exists(dst) && FileUtils.contentEquals(src.toFile(), dst.toFile())) {
return false;
}
} catch (IOException e) {
logger.atError().log("Unable to determine if files are equal", e);
return true;
}
}
return true;
}, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IOException(String.format("Unable to copy artifacts from %s due to: %s", artifactsDirectoryPath, e.getMessage()), e);
}
}
} catch (JsonProcessingException e) {
throw new InvalidRequestException("Unable to parse the deployment request - Invalid JSON", e);
}
}
use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method parseAndValidateJobDocument.
private DeploymentDocument parseAndValidateJobDocument(Deployment deployment) throws InvalidRequestException {
String jobDocumentString = deployment.getDeploymentDocument();
if (Utils.isEmpty(jobDocumentString)) {
throw new InvalidRequestException("Job document cannot be empty");
}
DeploymentDocument document;
try {
switch(deployment.getDeploymentType()) {
case LOCAL:
LocalOverrideRequest localOverrideRequest = SerializerFactory.getFailSafeJsonObjectMapper().readValue(jobDocumentString, LocalOverrideRequest.class);
Map<String, String> rootComponents = new HashMap<>();
Set<String> rootComponentsInRequestedGroup = new HashSet<>();
config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS, localOverrideRequest.getGroupName() == null ? LOCAL_DEPLOYMENT_GROUP_NAME : THING_GROUP_RESOURCE_NAME_PREFIX + localOverrideRequest.getGroupName()).forEach(t -> rootComponentsInRequestedGroup.add(t.getName()));
if (!Utils.isEmpty(rootComponentsInRequestedGroup)) {
rootComponentsInRequestedGroup.forEach(c -> {
Topics serviceTopic = kernel.findServiceTopic(c);
if (serviceTopic != null) {
String version = Coerce.toString(serviceTopic.find(VERSION_CONFIG_KEY));
rootComponents.put(c, version);
}
});
}
document = DeploymentDocumentConverter.convertFromLocalOverrideRequestAndRoot(localOverrideRequest, rootComponents);
break;
case IOT_JOBS:
case SHADOW:
// Note: This is the data contract that gets sending down from FCS::CreateDeployment
// Configuration is really a bad name choice as it is too generic but we can change it later
// since it is only a internal model
Configuration configuration = SerializerFactory.getFailSafeJsonObjectMapper().readValue(jobDocumentString, Configuration.class);
document = DeploymentDocumentConverter.convertFromDeploymentConfiguration(configuration);
break;
default:
throw new IllegalArgumentException("Invalid deployment type: " + deployment.getDeploymentType());
}
} catch (JsonProcessingException | IllegalArgumentException e) {
throw new InvalidRequestException("Unable to parse the job document", e);
}
deployment.setDeploymentDocumentObj(document);
return document;
}
use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method createNewDeployment.
private void createNewDeployment(Deployment deployment) {
logger.atInfo().kv(DEPLOYMENT_ID_LOG_KEY, deployment.getId()).kv("DeploymentType", deployment.getDeploymentType().toString()).log("Received deployment in the queue");
DeploymentTask deploymentTask;
boolean cancellable = true;
if (DEFAULT.equals(deployment.getDeploymentStage())) {
deploymentTask = createDefaultNewDeployment(deployment);
} else {
deploymentTask = createKernelUpdateDeployment(deployment);
cancellable = false;
if (DeploymentType.IOT_JOBS.equals(deployment.getDeploymentType())) {
// Keep track of IoT jobs for de-duplication
IotJobsHelper.getLatestQueuedJobs().addProcessedJob(deployment.getId());
}
}
if (deploymentTask == null) {
return;
}
deploymentStatusKeeper.persistAndPublishDeploymentStatus(deployment.getId(), deployment.getDeploymentType(), JobStatus.IN_PROGRESS.toString(), new HashMap<>());
if (DEFAULT.equals(deployment.getDeploymentStage())) {
try {
context.get(KernelAlternatives.class).cleanupLaunchDirectoryLinks();
deploymentDirectoryManager.createNewDeploymentDirectory(deployment.getDeploymentDocumentObj().getDeploymentId());
deploymentDirectoryManager.writeDeploymentMetadata(deployment);
} catch (IOException ioException) {
logger.atError().log("Unable to create deployment directory", ioException);
updateDeploymentResultAsFailed(deployment, deploymentTask, true, new DeploymentTaskFailureException(ioException));
return;
}
List<String> requiredCapabilities = deployment.getDeploymentDocumentObj().getRequiredCapabilities();
if (requiredCapabilities != null && !requiredCapabilities.isEmpty()) {
List<String> missingCapabilities = requiredCapabilities.stream().filter(reqCapabilities -> !kernel.getSupportedCapabilities().contains(reqCapabilities)).collect(Collectors.toList());
if (!missingCapabilities.isEmpty()) {
updateDeploymentResultAsFailed(deployment, deploymentTask, false, new MissingRequiredCapabilitiesException("The current nucleus version doesn't support one " + "or more capabilities that are required by this deployment: " + String.join(", ", missingCapabilities)));
return;
}
}
if (DeploymentType.LOCAL.equals(deployment.getDeploymentType())) {
try {
copyRecipesAndArtifacts(deployment);
} catch (InvalidRequestException | IOException e) {
logger.atError().log("Error copying recipes and artifacts", e);
updateDeploymentResultAsFailed(deployment, deploymentTask, false, e);
return;
}
}
}
Future<DeploymentResult> process = executorService.submit(deploymentTask);
logger.atInfo().kv("deployment", deployment.getId()).log("Started deployment execution");
currentDeploymentTaskMetadata = new DeploymentTaskMetadata(deploymentTask, process, deployment.getId(), deployment.getDeploymentType(), new AtomicInteger(1), deployment.getDeploymentDocumentObj(), cancellable);
}
Aggregations