|
| 1 | +package com.axway.maven.apigw; |
| 2 | + |
| 3 | +import com.axway.maven.apigw.utils.*; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import org.apache.commons.io.FileUtils; |
| 7 | +import org.apache.maven.plugin.MojoExecutionException; |
| 8 | +import org.apache.maven.plugin.MojoFailureException; |
| 9 | +import org.apache.maven.plugins.annotations.Execute; |
| 10 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 11 | +import org.apache.maven.plugins.annotations.Mojo; |
| 12 | +import org.apache.maven.plugins.annotations.Parameter; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +@Mojo(name = "container", defaultPhase = LifecyclePhase.NONE, requiresProject = true, threadSafe = false) |
| 20 | +@Execute(phase = LifecyclePhase.PACKAGE) |
| 21 | +public class ContainerMojo extends AbstractGatewayMojo { |
| 22 | + |
| 23 | + public static final String DEPLOY_DIR_NAME = "axway-deploy"; |
| 24 | + |
| 25 | + private static final String PROJECT_NAME = "gateway"; |
| 26 | + |
| 27 | + @Parameter(property = "axway.deploy.group", required = true) |
| 28 | + private String deployGroup; |
| 29 | + |
| 30 | + @Parameter(property = "axway.passphrase.deploy", required = false) |
| 31 | + private String passphraseDeploy = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * Properties to create and deploy new docker containers. |
| 35 | + */ |
| 36 | + @Parameter(property = "axway.container.scripts", required = true) |
| 37 | + protected File containerScripts; |
| 38 | + |
| 39 | + @Parameter(property = "axway.container.name", required = true) |
| 40 | + protected String containerName; |
| 41 | + |
| 42 | + @Parameter(property = "axway.remove.container", required = false) |
| 43 | + protected String axwayRemoveContainer; |
| 44 | + |
| 45 | + @Parameter(property = "axway.image.name", required = true) |
| 46 | + protected String imageName; |
| 47 | + |
| 48 | + @Parameter(property = "axway.image.tag", required = false) |
| 49 | + protected String imageTag; |
| 50 | + |
| 51 | + @Parameter(property = "axway.remove.image", required = false) |
| 52 | + protected String axwayRemoveImage; |
| 53 | + |
| 54 | + @Parameter(property = "axway.parent.image.name", required = false) |
| 55 | + protected String parentImageName; |
| 56 | + |
| 57 | + @Parameter(property = "axway.parent.image.tag", required = false) |
| 58 | + protected String parentImageTag; |
| 59 | + |
| 60 | + @Parameter(property = "axway.license", required = true) |
| 61 | + protected String license; |
| 62 | + |
| 63 | + @Parameter(property = "axway.merge.dir", required = false) |
| 64 | + protected String mergeDir; |
| 65 | + |
| 66 | + @Parameter(property = "axway.ports", required = false) |
| 67 | + private String axwayPorts; |
| 68 | + |
| 69 | + @Parameter(property = "axway.links", required = false) |
| 70 | + private String axwayLinks; |
| 71 | + |
| 72 | + @Parameter(property = "axway.environment.variables", required = false) |
| 73 | + private String axwayEnvironmentVariables; |
| 74 | + |
| 75 | + @Parameter(property = "axway.domain.cert", required = false) |
| 76 | + private String axwayDomainCert; |
| 77 | + |
| 78 | + @Parameter(property = "axway.domain.key", required = false) |
| 79 | + private String axwayDomainKey; |
| 80 | + |
| 81 | + @Parameter(property = "axway.domain.key.pass.file", required = false) |
| 82 | + private String axwayDomainKeyPassFile; |
| 83 | + |
| 84 | + @Parameter(property = "axway.admin.node.manager.host", required = true) |
| 85 | + private String adminNodeManagerHost; |
| 86 | + |
| 87 | + @Parameter(property = "axway.metrics.db.url", required = false) |
| 88 | + private String metricsDbUrl; |
| 89 | + |
| 90 | + @Parameter(property = "axway.metrics.db.username", required = false) |
| 91 | + private String metricsDbUsername; |
| 92 | + |
| 93 | + @Parameter(property = "axway.metrics.db.password", required = false) |
| 94 | + private String metricsDbPassword; |
| 95 | + |
| 96 | + private boolean removeContainer = false; |
| 97 | + private boolean removeImage = false; |
| 98 | + |
| 99 | + private Map<String, String> containerPorts; |
| 100 | + private Map<String, String> containerLinks; |
| 101 | + private Map<String, String> containerEnvironmentVariables; |
| 102 | + |
| 103 | + public boolean isRemoveContainer() { |
| 104 | + if ( !this.removeContainer ) { |
| 105 | + this.removeContainer = Boolean.parseBoolean(axwayRemoveContainer); |
| 106 | + } |
| 107 | + return this.removeContainer; |
| 108 | + } |
| 109 | + |
| 110 | + public boolean isRemoveImage() { |
| 111 | + if ( !this.removeImage ) { |
| 112 | + this.removeImage = Boolean.parseBoolean(axwayRemoveImage); |
| 113 | + } |
| 114 | + return this.removeImage; |
| 115 | + } |
| 116 | + |
| 117 | + public Map<String, String> getContainerPorts() { |
| 118 | + if ( containerPorts == null ) { |
| 119 | + containerPorts = splitString(axwayPorts); |
| 120 | + } |
| 121 | + return containerPorts; |
| 122 | + } |
| 123 | + |
| 124 | + public Map<String, String> getContainerLinks() { |
| 125 | + if ( containerLinks == null ) { |
| 126 | + containerLinks = splitString(axwayLinks); |
| 127 | + } |
| 128 | + return containerLinks; |
| 129 | + } |
| 130 | + |
| 131 | + public Map<String, String> getContainerEnvironmentVariables() { |
| 132 | + if ( containerEnvironmentVariables == null ) { |
| 133 | + containerEnvironmentVariables = splitString(axwayEnvironmentVariables); |
| 134 | + } |
| 135 | + return containerEnvironmentVariables; |
| 136 | + } |
| 137 | + |
| 138 | + private Map<String, String> splitString(String splitMe) { |
| 139 | + String[] split = splitMe.split(","); |
| 140 | + Map<String, String> map = new HashMap<String, String>(); |
| 141 | + for ( String mapping : split ) { |
| 142 | + String[] mappings = mapping.split(";"); |
| 143 | + map.put(mappings[0], mappings[1]); |
| 144 | + } |
| 145 | + return map; |
| 146 | + } |
| 147 | + |
| 148 | + public ContainerMojo() { |
| 149 | + } |
| 150 | + |
| 151 | + private File getTempDir() { |
| 152 | + return new File(getTargetDir(), DEPLOY_DIR_NAME); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 157 | + PackageType pkg = getPackageType(); |
| 158 | + |
| 159 | + switch (pkg) { |
| 160 | + case POLICY: |
| 161 | + deployPolicyProject(); |
| 162 | + break; |
| 163 | + |
| 164 | + case SERVER: |
| 165 | + deployServerProject(); |
| 166 | + break; |
| 167 | + |
| 168 | + case DEPLOYMENT: |
| 169 | + deployDeploymentProject(); |
| 170 | + break; |
| 171 | + |
| 172 | + default: |
| 173 | + throw new MojoExecutionException("Unsupported package type: " + getPackageType()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private void deployPolicyProject() throws MojoExecutionException { |
| 178 | + try { |
| 179 | + // pack test server project |
| 180 | + ProjectPack packer = new ProjectPack(this.homeAxwayGW, getLog()); |
| 181 | + packer.setPassphrasePol(this.passphrasePol); |
| 182 | + int exitCode = packer.execute(getTempDir(), PROJECT_NAME, this.testServerDirectory, null); |
| 183 | + if (exitCode != 0) { |
| 184 | + throw new MojoExecutionException("failed to build packed project"); |
| 185 | + } |
| 186 | + |
| 187 | + // configure fed |
| 188 | + File pol = new File(getTempDir(), PROJECT_NAME + ".pol"); |
| 189 | + File env = new File(getTempDir(), PROJECT_NAME + ".env"); |
| 190 | + File info = new File(getTempDir(), PROJECT_NAME + ".info.json"); |
| 191 | + |
| 192 | + ObjectMapper mapper = new ObjectMapper(); |
| 193 | + ObjectNode root = buildBasicArtifactInfo(); |
| 194 | + String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 195 | + FileUtils.writeStringToFile(info, json, "UTF-8"); |
| 196 | + |
| 197 | + File fed = configFed(pol, env, info); |
| 198 | + |
| 199 | + // deploy to server |
| 200 | + deployFed(fed); |
| 201 | + } catch (IOException e) { |
| 202 | + throw new MojoExecutionException("Error on packing project", e); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private void deployServerProject() throws MojoExecutionException { |
| 207 | + File archiveBuildDir = getArchiveBuildDir(); |
| 208 | + |
| 209 | + // configure fed |
| 210 | + File pol = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_POL); |
| 211 | + File env = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_ENV); |
| 212 | + File info = new File(archiveBuildDir, ServerArchiveMojo.FILE_GATEWAY_INFO); |
| 213 | + |
| 214 | + File fed = configFed(pol, env, info); |
| 215 | + |
| 216 | + // deploy to server |
| 217 | + deployFed(fed); |
| 218 | + } |
| 219 | + |
| 220 | + private void deployDeploymentProject() throws MojoExecutionException { |
| 221 | + File fed = new File(getArchiveBuildDir(), DeploymentArchiveMojo.FILE_FED_NAME); |
| 222 | + |
| 223 | + deployFed(fed); |
| 224 | + } |
| 225 | + |
| 226 | + private File configFed(File pol, File env, File info) throws MojoExecutionException { |
| 227 | + FedBuilder fb = new FedBuilder(this, pol, env, this.configConfigFile, info); |
| 228 | + fb.setPassphrasePol(this.passphrasePol); |
| 229 | + fb.setPassphraseFed(this.passphraseFed); |
| 230 | + |
| 231 | + fb.addPropertyFiles(getPropertyFiles()); |
| 232 | + |
| 233 | + fb.setCertificatesFile(this.configCertsFile); |
| 234 | + fb.enableVerboseMode(this.verboseCfgTools); |
| 235 | + |
| 236 | + File fed = new File(getTempDir(), PROJECT_NAME + ".fed"); |
| 237 | + |
| 238 | + int exitCode = fb.execute(fed, null); |
| 239 | + if (exitCode != 0) { |
| 240 | + throw new MojoExecutionException("failed to configure project: exitCode=" + exitCode); |
| 241 | + } |
| 242 | + |
| 243 | + return fed; |
| 244 | + } |
| 245 | + |
| 246 | + private void deployFed(File fed) throws MojoExecutionException { |
| 247 | + try { |
| 248 | + Map<String, String> polProps = new HashMap<>(); |
| 249 | + polProps.put("Name", this.project.getGroupId() + ":" + this.project.getArtifactId()); |
| 250 | + polProps.put("Version", this.project.getVersion()); |
| 251 | + polProps.put("Type", "Test Deployment"); |
| 252 | + |
| 253 | + Source source = new Source(fed, this.passphraseFed); |
| 254 | + Target target = new Target(this.deployGroup, this.passphraseDeploy); |
| 255 | + |
| 256 | + AbstractCommandExecutor deploy; |
| 257 | + |
| 258 | + // containerName is populated, so we are going to create a new container |
| 259 | + AbstractCommandExecutor dockerCommands = new DockerCommands("Docker Commands", getLog()); |
| 260 | + int exitCode = dockerCommands.execute("Remove Container", this.isRemoveContainer(), this.containerName, |
| 261 | + null, null, null, null, null, |
| 262 | + null, null, null, null); |
| 263 | + if ( exitCode != 0 ) { |
| 264 | + throw new MojoExecutionException("Failed to remove existing container: exitCode: " + exitCode); |
| 265 | + } |
| 266 | + |
| 267 | + exitCode = dockerCommands.execute("Remove Image", this.isRemoveImage(), this.containerName, |
| 268 | + this.imageName, this.imageTag, null, null, null, |
| 269 | + null, null, null, null); |
| 270 | + if ( exitCode != 0 ) { |
| 271 | + throw new MojoExecutionException("Failed to remove existing image: exitCode: " + exitCode); |
| 272 | + } |
| 273 | + |
| 274 | + deploy = new DockerImage(this.containerScripts, this.imageName, this.imageTag, this.parentImageName, |
| 275 | + this.parentImageTag, this.license, this.mergeDir, this.axwayDomainCert, this.axwayDomainKey, |
| 276 | + this.axwayDomainKeyPassFile, getLog()); |
| 277 | + exitCode = deploy.execute(source, target, polProps, null); |
| 278 | + if ( exitCode != 0 ) { |
| 279 | + throw new MojoExecutionException("Failed to create new Docker Image: exitCode: " + exitCode); |
| 280 | + } |
| 281 | + |
| 282 | + exitCode = dockerCommands.execute("Create Container", false, this.containerName, |
| 283 | + this.imageName, this.imageTag, this.getContainerPorts(), this.getContainerLinks(), |
| 284 | + this.getContainerEnvironmentVariables(), this.adminNodeManagerHost, this.metricsDbUrl, |
| 285 | + this.metricsDbUsername, this.metricsDbPassword); |
| 286 | + if ( exitCode != 0 ) { |
| 287 | + throw new MojoExecutionException("Failed to create new container: exitCode: " + exitCode); |
| 288 | + } |
| 289 | + } catch (IOException e) { |
| 290 | + throw new MojoExecutionException("Error on deploying project", e); |
| 291 | + } |
| 292 | + } |
| 293 | +} |
0 commit comments