Skip to content

Commit d94af92

Browse files
authored
Docker capabilities (#45)
* my commit * adding some log statements * adding some log statements * adding some log statements * version update * add a container mojo * fixed the usage of a string command * some testing for running inside wsl for windows * updated ContainerMojo to bring in the parameters from the mvn command line. Handling the ability to not remove containers and/or images. * need to see the size of the parameters when we have 0 containers/images * need to see the size of the parameters when we have 0 containers/images * need to see the size of the parameters when we have 0 containers/images * working on why the fed file isn't pushed in * working on why the fed file isn't pushed in * working on why the fed file isn't pushed in * working on why the fed file isn't pushed in * updating the generate domain certificate * added the cert location * add pass file * update cert location * update cert location * changed some math * some troubleshooting * environment variables was set up wrong * environment variables was set up wrong * environment variables was set up wrong * environment variables was set up wrong * boolean parsing * removing log statements * removed unused imports * change log to debug * update docs * update docs * add certs and stuff * added admin node manager location * added admin node manager location
1 parent a3628ce commit d94af92

File tree

12 files changed

+700
-49
lines changed

12 files changed

+700
-49
lines changed

doc/getting-started/getting-started.adoc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ For manual checks via ConfigurationStudio the content of the archive is located
376376

377377
== Deployment
378378

379+
=== Using projdeploy
380+
379381
The project can be deployed to a gateway via the plugin.
380382

381383
Just invoke the `apigw:deploy` goal within the deployment project and specify the target domain and group.
@@ -389,3 +391,67 @@ The project shall be deployed to the group `test`.
389391
-Daxway.deploy.group=test \
390392
clean apigw:deploy
391393

394+
=== Creating and Deploying new Docker Images and Containers
395+
396+
This project will create new Docker images and containers. The Docker image will be created with the fed file that was
397+
created when the package command was invoked.
398+
399+
400+
401+
Run this mvn plugin by invoking `apigw:container`. You must specify the following properties:
402+
403+
* axway.deploy.group (mandatory)
404+
* axway.passphrase.deploy (optional)
405+
* axway.container.scripts (mandatory)
406+
* axway.remove.container (mandatory)
407+
* axway.container.name (optional)
408+
* axway.remove.image (optional)
409+
* axway.image.name (mandatory)
410+
* axway.image.tag (optional)
411+
* axway.parent.image.name (optional)
412+
* axway.parent.image.tag (optional)
413+
* axway.license (mandatory)
414+
* axway.merge.dir (optional)
415+
* axway.environment.variables
416+
* axway.links (optional)
417+
* axway.ports (optional)
418+
* axway.domain.cert (optional)
419+
* axway.domain.key (optional)
420+
* axway.domain.key.pass.file (optional)
421+
* axway.admin.node.manager.host (mandatory)
422+
* axway.metrics.db.url (optional)
423+
* axway.metrics.db.username (optional)
424+
* axway.metrics.db.password (optional)
425+
426+
When dealing with a domain cert and key there are 2 ways to handle this. First would be to generate your own cert, key
427+
and password file. These would then be needed to be passed in during the mvn call.
428+
429+
If no domain, key or password file is passed in maven will assume that it needs to use default cert and key. You will
430+
need to create a `cert\tmp\pass.txt` file. In this file you just add `changeme` and save this file. Maven will assume
431+
that is the location of the file.
432+
433+
In the following example, we will delete the existing container and image, then we will recreate everything using a
434+
default domain cert and key.
435+
436+
```
437+
mvn clean apigw:container \
438+
--define axway.deploy.group=emt-group \
439+
--define axway.container.scripts=/mnt/c/Axway_7.6.2/Software/dockerscripts/emt-containers-1.0.0-9 \
440+
--define axway.remove.container=true \
441+
--define axway.container.name=apimgr \
442+
--define axway.remove.image=true \
443+
--define axway.image.name=apimgt \
444+
--define axway.image.tag=7.6.2_3 \
445+
--define axway.parent.image.name=apigw-base \
446+
--define axway.parent.image.tag=7.6.2_3 \
447+
--define axway.license=/mnt/c/Axway_7.6.2/Software/API_7.6_Docker_Temp.lic \
448+
--define axway.merge.dir=/mnt/c/Axway_7.6.2/tmp/apigateway \
449+
--define axway.environment.variables="METRICS_DB_URL;jdbc:mysql://mysql:3306/metrics\?useSSL=false,METRICS_DB_USERNAME;metrics,METRICS_DB_PASS;metrics,EMT_DEPLOYMENT_ENABLED;true,EMT_ANM_HOSTS;anm:8090,CASS_HOST;cassandra" \
450+
--define axway.links="anm;anm,cassandra_2.2.12;cassandra,mysql_5.7;mysql" \
451+
--define axway.ports="8075;8075,8065;8065,8080;8080" \
452+
--define axway.admin.node.manager.host=anm:8090 \
453+
--define axway.metrics.db.url=jdbc:mysql://mysql:3306/metrics\?useSSL=false \
454+
--define axway.metrics.db.username=metrics \
455+
--define axway.metrics.db.password=metrics
456+
```
457+

src/main/java/com/axway/maven/apigw/AbstractGatewayMojo.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import java.io.File;
44
import java.text.SimpleDateFormat;
5-
import java.util.ArrayList;
6-
import java.util.Date;
7-
import java.util.HashSet;
8-
import java.util.List;
9-
import java.util.Optional;
10-
import java.util.Set;
5+
import java.util.*;
116

127
import org.apache.maven.artifact.Artifact;
138
import org.apache.maven.plugin.AbstractMojo;
@@ -119,6 +114,7 @@ public abstract class AbstractGatewayMojo extends AbstractMojo {
119114

120115
@Parameter(defaultValue = "${project}", readonly = true)
121116
protected MavenProject project;
117+
122118

123119
public MavenProject getProject() {
124120
return this.project;
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
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

Comments
 (0)