|
| 1 | +package com.boozallen.aissemble.upgrade.migration.v1_7_0; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * aiSSEMBLE::Foundation::Upgrade |
| 6 | + * %% |
| 7 | + * Copyright (C) 2021 Booz Allen |
| 8 | + * %% |
| 9 | + * This software package is licensed under the Booz Allen Public License. All Rights Reserved. |
| 10 | + * #L% |
| 11 | + */ |
| 12 | + |
| 13 | +import com.boozallen.aissemble.upgrade.migration.AbstractAissembleMigration; |
| 14 | +import com.boozallen.aissemble.upgrade.util.pom.PomHelper; |
| 15 | +import com.boozallen.aissemble.upgrade.util.pom.PomModifications; |
| 16 | +import org.apache.maven.model.InputLocation; |
| 17 | +import org.apache.maven.model.Model; |
| 18 | +import org.apache.maven.model.PluginExecution; |
| 19 | +import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +/** |
| 27 | + * Upgrades pipeline POM files to replace the old aissemble-spark-application chart template execution with the updated |
| 28 | + * execution that uses the ghcr.io repository. These executions are used to create the SparkApplication files that can |
| 29 | + * be executed/submitted by Airflow. However, they are included in all pipelines regardless of whether Airflow is in |
| 30 | + * use. |
| 31 | + */ |
| 32 | +public class SparkAppExecMigration extends AbstractAissembleMigration { |
| 33 | + |
| 34 | + public static final String NEW_CHART = "${aissemble.helm.repo.protocol}://${aissemble.helm.repo}/aissemble-spark-application-chart"; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected boolean shouldExecuteOnFile(File file) { |
| 38 | + Model pom = PomHelper.getLocationAnnotatedModel(file); |
| 39 | + return !getSparkAppExecutions(pom).isEmpty(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected boolean performMigration(File file) { |
| 44 | + Model pom = PomHelper.getLocationAnnotatedModel(file); |
| 45 | + List<PluginExecution> execs = getSparkAppExecutions(pom); |
| 46 | + PomModifications modifications = new PomModifications(); |
| 47 | + for (PluginExecution exec : execs) { |
| 48 | + modifyExecution(exec, modifications); |
| 49 | + } |
| 50 | + return PomHelper.writeModifications(file, modifications.finalizeMods()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Modifies the exec-maven-plugin execution to use the new aissemble-spark-application chart template format. |
| 55 | + * @param exec the plugin <execution> to modify |
| 56 | + * @param modifications accumulator to which modifications are added |
| 57 | + */ |
| 58 | + private void modifyExecution(PluginExecution exec, PomModifications modifications) { |
| 59 | + Xpp3Dom[] args = getConfig(exec, "arguments").getChildren("argument"); |
| 60 | + for (Xpp3Dom arg : args) { |
| 61 | + InputLocation location = (InputLocation) arg.getInputLocation(); |
| 62 | + String value = arg.getValue(); |
| 63 | + if ("aissemble-spark-application".equals(value)) { |
| 64 | + InputLocation end = PomHelper.incrementColumn(location, "aissemble-spark-application".length()); |
| 65 | + modifications.add(new PomModifications.Replacement(location, end, NEW_CHART)); |
| 66 | + } else if ("--repo".equals(value) || "${aissemble.helm.repo}".equals(value)) { |
| 67 | + InputLocation start = PomHelper.incrementColumn(location, -"<argument>".length()); |
| 68 | + InputLocation end = PomHelper.incrementColumn(location, value.length() + "</argument>".length()); |
| 69 | + modifications.add(new PomModifications.Deletion(start, end)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the plugin <execution> blocks which use the exec-maven-plugin to run helm template on the aissemble-spark-application chart. |
| 76 | + * @param pom the POM to search |
| 77 | + * @return the executions that need to be modified |
| 78 | + */ |
| 79 | + private List<PluginExecution> getSparkAppExecutions(Model pom) { |
| 80 | + return pom.getBuild().getPlugins().stream().filter(plugin -> plugin.getArtifactId().equals("exec-maven-plugin")) |
| 81 | + .flatMap(plugin -> plugin.getExecutions().stream()) |
| 82 | + .filter(execution -> execution.getGoals().contains("exec")) |
| 83 | + .filter(this::isHelmExec) |
| 84 | + .filter(this::containsOldSparkAppArg) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + } |
| 87 | + |
| 88 | + private boolean isHelmExec(PluginExecution execution) { |
| 89 | + return getConfig(execution, "executable").getValue().equals("helm"); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Checks if the exec-maven-plugin execution contains the old aissemble-spark-application argument. |
| 94 | + * |
| 95 | + * @param execution the plugin <execution> to check |
| 96 | + * @return true if the execution contains the old argument |
| 97 | + */ |
| 98 | + private boolean containsOldSparkAppArg(PluginExecution execution) { |
| 99 | + Xpp3Dom args = ((Xpp3Dom) execution.getConfiguration()).getChild("arguments"); |
| 100 | + return Stream.of(args.getChildren("argument")) |
| 101 | + .anyMatch(arg -> "aissemble-spark-application".equals(arg.getValue())); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets the <configuration> item for a given plugin <execution> by name. |
| 106 | + * @param execution the plugin <execution> to search |
| 107 | + * @param name the name of the configuration item to find |
| 108 | + * @return the configuration item DOM |
| 109 | + */ |
| 110 | + private Xpp3Dom getConfig(PluginExecution execution, String name) { |
| 111 | + return ((Xpp3Dom) execution.getConfiguration()).getChild(name); |
| 112 | + } |
| 113 | +} |
0 commit comments